Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions x-pack/plugins/task_manager/server/lib/fill_pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { fillPool } from './fill_pool';
import { TaskPoolRunResult } from '../task_pool';

describe('fillPool', () => {
test('stops filling when there are no more tasks in the store', async () => {
test('stops filling when pool runs all claimed tasks, even if there is more capacity', async () => {
const tasks = [
[1, 2, 3],
[4, 5],
Expand All @@ -22,7 +22,7 @@ describe('fillPool', () => {

await fillPool(fetchAvailableTasks, converter, run);

expect(_.flattenDeep(run.args)).toEqual([1, 2, 3, 4, 5]);
expect(_.flattenDeep(run.args)).toEqual([1, 2, 3]);
});

test('stops filling when the pool has no more capacity', async () => {
Expand Down
49 changes: 22 additions & 27 deletions x-pack/plugins/task_manager/server/lib/fill_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/

import { performance } from 'perf_hooks';
import { after } from 'lodash';
import { TaskPoolRunResult } from '../task_pool';

export enum FillPoolResult {
NoTasksClaimed = 'NoTasksClaimed',
RanOutOfCapacity = 'RanOutOfCapacity',
PoolFilled = 'PoolFilled',
}

type BatchRun<T> = (tasks: T[]) => Promise<TaskPoolRunResult>;
Expand All @@ -35,33 +35,28 @@ export async function fillPool<TRecord, TRunner>(
run: BatchRun<TRunner>
): Promise<FillPoolResult> {
performance.mark('fillPool.start');
const markClaimedTasksOnRerunCycle = after(2, () =>
performance.mark('fillPool.claimedOnRerunCycle')
);
while (true) {
const instances = await fetchAvailableTasks();
const instances = await fetchAvailableTasks();

if (!instances.length) {
performance.mark('fillPool.bailNoTasks');
performance.measure(
'fillPool.activityDurationUntilNoTasks',
'fillPool.start',
'fillPool.bailNoTasks'
);
return FillPoolResult.NoTasksClaimed;
}
markClaimedTasksOnRerunCycle();
const tasks = instances.map(converter);
if (!instances.length) {
performance.mark('fillPool.bailNoTasks');
performance.measure(
'fillPool.activityDurationUntilNoTasks',
'fillPool.start',
'fillPool.bailNoTasks'
);
return FillPoolResult.NoTasksClaimed;
}
const tasks = instances.map(converter);

if ((await run(tasks)) === TaskPoolRunResult.RanOutOfCapacity) {
performance.mark('fillPool.bailExhaustedCapacity');
performance.measure(
'fillPool.activityDurationUntilExhaustedCapacity',
'fillPool.start',
'fillPool.bailExhaustedCapacity'
);
return FillPoolResult.RanOutOfCapacity;
}
performance.mark('fillPool.cycle');
if ((await run(tasks)) === TaskPoolRunResult.RanOutOfCapacity) {
performance.mark('fillPool.bailExhaustedCapacity');
performance.measure(
'fillPool.activityDurationUntilExhaustedCapacity',
'fillPool.start',
'fillPool.bailExhaustedCapacity'
);
return FillPoolResult.RanOutOfCapacity;
}
performance.mark('fillPool.cycle');
return FillPoolResult.PoolFilled;
}