-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Task Manager] Batches the update operations in Task Manager #71470
Merged
gmmorris
merged 10 commits into
elastic:master
from
gmmorris:task-manager/batch-update-delete
Jul 21, 2020
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
19246f5
make updates buffered in task manager
gmmorris dcc2312
added missing unit tests
gmmorris b51cefe
Merge branch 'master' into task-manager/batch-update-delete
gmmorris c2d8b5f
cleaned up buffered store
gmmorris 6a7b5d8
corrected type signature
gmmorris edb3f1e
Merge branch 'master' into task-manager/batch-update-delete
gmmorris 7dada08
allow longer duration of buffering and max bound for the buffer
gmmorris 3f56067
configure buffered task store based on Task Manager config
gmmorris a2d7943
refactores buffer flushing to be a single clean stream
gmmorris d9a1b62
Merge branch 'master' into task-manager/batch-update-delete
gmmorris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
x-pack/plugins/task_manager/server/buffered_task_store.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,82 @@ | ||
/* | ||
* 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 uuid from 'uuid'; | ||
import { taskStoreMock } from './task_store.mock'; | ||
import { BufferedTaskStore } from './buffered_task_store'; | ||
import { asErr, asOk } from './lib/result_type'; | ||
import { TaskStatus } from './task'; | ||
|
||
describe('Buffered Task Store', () => { | ||
test('proxies the TaskStore for `maxAttempts` and `remove`', async () => { | ||
const taskStore = taskStoreMock.create({ maxAttempts: 10 }); | ||
taskStore.bulkUpdate.mockResolvedValue([]); | ||
const bufferedStore = new BufferedTaskStore(taskStore, {}); | ||
|
||
expect(bufferedStore.maxAttempts).toEqual(10); | ||
|
||
bufferedStore.remove('1'); | ||
expect(taskStore.remove).toHaveBeenCalledWith('1'); | ||
}); | ||
|
||
describe('update', () => { | ||
test("proxies the TaskStore's `bulkUpdate`", async () => { | ||
const taskStore = taskStoreMock.create({ maxAttempts: 10 }); | ||
const bufferedStore = new BufferedTaskStore(taskStore, {}); | ||
|
||
const task = mockTask(); | ||
|
||
taskStore.bulkUpdate.mockResolvedValue([asOk(task)]); | ||
|
||
expect(await bufferedStore.update(task)).toMatchObject(task); | ||
expect(taskStore.bulkUpdate).toHaveBeenCalledWith([task]); | ||
}); | ||
|
||
test('handles partially successfull bulkUpdates resolving each call appropriately', async () => { | ||
const taskStore = taskStoreMock.create({ maxAttempts: 10 }); | ||
const bufferedStore = new BufferedTaskStore(taskStore, {}); | ||
|
||
const tasks = [mockTask(), mockTask(), mockTask()]; | ||
|
||
taskStore.bulkUpdate.mockResolvedValueOnce([ | ||
asOk(tasks[0]), | ||
asErr({ entity: tasks[1], error: new Error('Oh no, something went terribly wrong') }), | ||
asOk(tasks[2]), | ||
]); | ||
|
||
const results = [ | ||
bufferedStore.update(tasks[0]), | ||
bufferedStore.update(tasks[1]), | ||
bufferedStore.update(tasks[2]), | ||
]; | ||
expect(await results[0]).toMatchObject(tasks[0]); | ||
expect(results[1]).rejects.toMatchInlineSnapshot( | ||
`[Error: Oh no, something went terribly wrong]` | ||
); | ||
expect(await results[2]).toMatchObject(tasks[2]); | ||
}); | ||
}); | ||
}); | ||
|
||
function mockTask() { | ||
return { | ||
id: `task_${uuid.v4()}`, | ||
attempts: 0, | ||
schedule: undefined, | ||
params: { hello: 'world' }, | ||
retryAt: null, | ||
runAt: new Date(), | ||
scheduledAt: new Date(), | ||
scope: undefined, | ||
startedAt: null, | ||
state: { foo: 'bar' }, | ||
status: TaskStatus.Idle, | ||
taskType: 'report', | ||
user: undefined, | ||
version: '123', | ||
ownerId: '123', | ||
}; | ||
} |
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,39 @@ | ||
/* | ||
* 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 { TaskStore } from './task_store'; | ||
import { ConcreteTaskInstance } from './task'; | ||
import { Updatable } from './task_runner'; | ||
import { createBuffer, Operation, BufferOptions } from './lib/bulk_operation_buffer'; | ||
import { unwrapPromise } from './lib/result_type'; | ||
|
||
// by default allow updates to be buffered for up to 50ms | ||
const DEFAULT_BUFFER_MAX_DURATION = 50; | ||
|
||
export class BufferedTaskStore implements Updatable { | ||
private bufferedUpdate: Operation<ConcreteTaskInstance, Error>; | ||
constructor(private readonly taskStore: TaskStore, options: BufferOptions) { | ||
this.bufferedUpdate = createBuffer<ConcreteTaskInstance, Error>( | ||
(docs) => taskStore.bulkUpdate(docs), | ||
{ | ||
bufferMaxDuration: DEFAULT_BUFFER_MAX_DURATION, | ||
...options, | ||
} | ||
); | ||
} | ||
|
||
public get maxAttempts(): number { | ||
return this.taskStore.maxAttempts; | ||
} | ||
|
||
public async update(doc: ConcreteTaskInstance): Promise<ConcreteTaskInstance> { | ||
return unwrapPromise(this.bufferedUpdate(doc)); | ||
} | ||
|
||
public async remove(id: string): Promise<void> { | ||
return this.taskStore.remove(id); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think there would be value to have this as a kibana.yml config with a default of that number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered it, but it feel very internal and I think for now we can keep it as a constant.
We can expose it later if we feel it's worth it, but I think the poll_interval and max_workers is enough cognitive load for our users. :)