-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Task Manager] time out work when it overruns in poller (#74980)
If the work performed by the poller hangs, meaning the promise fails to resolve/reject, then the poller can get stuck in a mode where it just waits for ever and no longer polls for fresh work. This PR introduces a timeout after which the poller will automatically reject the work, freeing the poller to restart pulling fresh work.
- Loading branch information
Showing
10 changed files
with
140 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
x-pack/plugins/task_manager/server/lib/timeout_promise_after.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { timeoutPromiseAfter } from './timeout_promise_after'; | ||
|
||
const delay = (ms: number, result: unknown) => | ||
new Promise((resolve) => setTimeout(() => resolve(result), ms)); | ||
|
||
const delayRejection = (ms: number, result: unknown) => | ||
new Promise((resolve, reject) => setTimeout(() => reject(result), ms)); | ||
|
||
describe('Promise Timeout', () => { | ||
test('resolves when wrapped promise resolves', async () => { | ||
return expect( | ||
timeoutPromiseAfter(delay(100, 'OK'), 1000, () => 'TIMEOUT ERR') | ||
).resolves.toMatchInlineSnapshot(`"OK"`); | ||
}); | ||
|
||
test('reject when wrapped promise rejects', async () => { | ||
return expect( | ||
timeoutPromiseAfter(delayRejection(100, 'ERR'), 1000, () => 'TIMEOUT ERR') | ||
).rejects.toMatchInlineSnapshot(`"ERR"`); | ||
}); | ||
|
||
test('reject it the timeout elapses', async () => { | ||
return expect( | ||
timeoutPromiseAfter(delay(1000, 'OK'), 100, () => 'TIMEOUT ERR') | ||
).rejects.toMatchInlineSnapshot(`"TIMEOUT ERR"`); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/task_manager/server/lib/timeout_promise_after.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export function timeoutPromiseAfter<T, G>( | ||
future: Promise<T>, | ||
ms: number, | ||
onTimeout: () => G | ||
): Promise<T> { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => reject(onTimeout()), ms); | ||
future.then(resolve).catch(reject); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters