-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
task_store.ts
626 lines (562 loc) · 18.5 KB
/
task_store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* 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.
*/
/*
* This module contains helpers for managing the task manager storage layer.
*/
import apm from 'elastic-apm-node';
import { Subject, Observable } from 'rxjs';
import { omit, difference, partition, map, defaults } from 'lodash';
import { some, none } from 'fp-ts/lib/Option';
import { SearchResponse, UpdateDocumentByQueryResponse } from 'elasticsearch';
import {
SavedObject,
SavedObjectsSerializer,
SavedObjectsRawDoc,
ISavedObjectsRepository,
SavedObjectsUpdateResponse,
ElasticsearchClient,
} from '../../../../src/core/server';
import { asOk, asErr, Result } from './lib/result_type';
import {
ConcreteTaskInstance,
TaskInstance,
TaskLifecycle,
TaskLifecycleResult,
SerializedConcreteTaskInstance,
TaskStatus,
} from './task';
import { TaskClaim, asTaskClaimEvent } from './task_events';
import {
asUpdateByQuery,
shouldBeOneOf,
mustBeAllOf,
filterDownBy,
asPinnedQuery,
matchesClauses,
SortOptions,
} from './queries/query_clauses';
import {
updateFieldsAndMarkAsFailed,
IdleTaskWithExpiredRunAt,
InactiveTasks,
RunningOrClaimingTaskWithExpiredRetryAt,
SortByRunAtAndRetryAt,
tasksClaimedByOwner,
} from './queries/mark_available_tasks_as_claimed';
import { TaskTypeDictionary } from './task_type_dictionary';
import { ESSearchResponse, ESSearchBody } from '../../apm/typings/elasticsearch';
export interface StoreOpts {
esClient: ElasticsearchClient;
index: string;
taskManagerId: string;
maxAttempts: number;
definitions: TaskTypeDictionary;
savedObjectsRepository: ISavedObjectsRepository;
serializer: SavedObjectsSerializer;
}
export interface SearchOpts {
sort?: string | object | object[];
query?: object;
size?: number;
seq_no_primary_term?: boolean;
search_after?: unknown[];
}
export type AggregationOpts = Pick<Required<ESSearchBody>, 'aggs'> &
Pick<ESSearchBody, 'query' | 'size'>;
export interface UpdateByQuerySearchOpts extends SearchOpts {
script?: object;
}
export interface UpdateByQueryOpts extends SearchOpts {
max_docs?: number;
}
export interface OwnershipClaimingOpts {
claimOwnershipUntil: Date;
claimTasksById?: string[];
size: number;
}
export interface FetchResult {
docs: ConcreteTaskInstance[];
}
export interface ClaimOwnershipResult {
claimedTasks: number;
docs: ConcreteTaskInstance[];
}
export type BulkUpdateResult = Result<
ConcreteTaskInstance,
{ entity: ConcreteTaskInstance; error: Error }
>;
export interface UpdateByQueryResult {
updated: number;
version_conflicts: number;
total: number;
}
/**
* Wraps an elasticsearch connection and provides a task manager-specific
* interface into the index.
*/
export class TaskStore {
public readonly maxAttempts: number;
public readonly index: string;
public readonly taskManagerId: string;
public readonly errors$ = new Subject<Error>();
private esClient: ElasticsearchClient;
private definitions: TaskTypeDictionary;
private savedObjectsRepository: ISavedObjectsRepository;
private serializer: SavedObjectsSerializer;
private events$: Subject<TaskClaim>;
/**
* Constructs a new TaskStore.
* @param {StoreOpts} opts
* @prop {esClient} esClient - An elasticsearch client
* @prop {string} index - The name of the task manager index
* @prop {number} maxAttempts - The maximum number of attempts before a task will be abandoned
* @prop {TaskDefinition} definition - The definition of the task being run
* @prop {serializer} - The saved object serializer
* @prop {savedObjectsRepository} - An instance to the saved objects repository
*/
constructor(opts: StoreOpts) {
this.esClient = opts.esClient;
this.index = opts.index;
this.taskManagerId = opts.taskManagerId;
this.maxAttempts = opts.maxAttempts;
this.definitions = opts.definitions;
this.serializer = opts.serializer;
this.savedObjectsRepository = opts.savedObjectsRepository;
this.events$ = new Subject<TaskClaim>();
}
public get events(): Observable<TaskClaim> {
return this.events$;
}
private emitEvents = (events: TaskClaim[]) => {
events.forEach((event) => this.events$.next(event));
};
/**
* Schedules a task.
*
* @param task - The task being scheduled.
*/
public async schedule(taskInstance: TaskInstance): Promise<ConcreteTaskInstance> {
this.definitions.ensureHas(taskInstance.taskType);
let savedObject;
try {
savedObject = await this.savedObjectsRepository.create<SerializedConcreteTaskInstance>(
'task',
taskInstanceToAttributes(taskInstance),
{ id: taskInstance.id, refresh: false }
);
} catch (e) {
this.errors$.next(e);
throw e;
}
return savedObjectToConcreteTaskInstance(savedObject);
}
/**
* Fetches a list of scheduled tasks with default sorting.
*
* @param opts - The query options used to filter tasks
*/
public async fetch({ sort = [{ 'task.runAt': 'asc' }], ...opts }: SearchOpts = {}): Promise<
FetchResult
> {
return this.search({
...opts,
sort,
});
}
/**
* Claims available tasks from the index, which are ready to be run.
* - runAt is now or past
* - is not currently claimed by any instance of Kibana
* - has a type that is in our task definitions
*
* @param {OwnershipClaimingOpts} options
* @returns {Promise<ClaimOwnershipResult>}
*/
public claimAvailableTasks = async ({
claimOwnershipUntil,
claimTasksById = [],
size,
}: OwnershipClaimingOpts): Promise<ClaimOwnershipResult> => {
const claimTasksByIdWithRawIds = claimTasksById.map((id) =>
this.serializer.generateRawId(undefined, 'task', id)
);
const numberOfTasksClaimed = await this.markAvailableTasksAsClaimed(
claimOwnershipUntil,
claimTasksByIdWithRawIds,
size
);
const docs =
numberOfTasksClaimed > 0
? await this.sweepForClaimedTasks(claimTasksByIdWithRawIds, size)
: [];
const [documentsReturnedById, documentsClaimedBySchedule] = partition(docs, (doc) =>
claimTasksById.includes(doc.id)
);
const [documentsClaimedById, documentsRequestedButNotClaimed] = partition(
documentsReturnedById,
// we filter the schduled tasks down by status is 'claiming' in the esearch,
// but we do not apply this limitation on tasks claimed by ID so that we can
// provide more detailed error messages when we fail to claim them
(doc) => doc.status === TaskStatus.Claiming
);
const documentsRequestedButNotReturned = difference(
claimTasksById,
map(documentsReturnedById, 'id')
);
this.emitEvents([
...documentsClaimedById.map((doc) => asTaskClaimEvent(doc.id, asOk(doc))),
...documentsClaimedBySchedule.map((doc) => asTaskClaimEvent(doc.id, asOk(doc))),
...documentsRequestedButNotClaimed.map((doc) => asTaskClaimEvent(doc.id, asErr(some(doc)))),
...documentsRequestedButNotReturned.map((id) => asTaskClaimEvent(id, asErr(none))),
]);
return {
claimedTasks: documentsClaimedById.length + documentsClaimedBySchedule.length,
docs: docs.filter((doc) => doc.status === TaskStatus.Claiming),
};
};
private async markAvailableTasksAsClaimed(
claimOwnershipUntil: OwnershipClaimingOpts['claimOwnershipUntil'],
claimTasksById: OwnershipClaimingOpts['claimTasksById'],
size: OwnershipClaimingOpts['size']
): Promise<number> {
const taskMaxAttempts = [...this.definitions].reduce((accumulator, [type, { maxAttempts }]) => {
return { ...accumulator, [type]: maxAttempts || this.maxAttempts };
}, {});
const queryForScheduledTasks = mustBeAllOf(
// Either a task with idle status and runAt <= now or
// status running or claiming with a retryAt <= now.
shouldBeOneOf(IdleTaskWithExpiredRunAt, RunningOrClaimingTaskWithExpiredRetryAt)
);
// The documents should be sorted by runAt/retryAt, unless there are pinned
// tasks being queried, in which case we want to sort by score first, and then
// the runAt/retryAt. That way we'll get the pinned tasks first. Note that
// the score seems to favor newer documents rather than older documents, so
// if there are not pinned tasks being queried, we do NOT want to sort by score
// at all, just by runAt/retryAt.
const sort: SortOptions = [SortByRunAtAndRetryAt];
if (claimTasksById && claimTasksById.length) {
sort.unshift('_score');
}
const apmTrans = apm.startTransaction(`taskManager markAvailableTasksAsClaimed`, 'taskManager');
const { updated } = await this.updateByQuery(
asUpdateByQuery({
query: matchesClauses(
mustBeAllOf(
claimTasksById && claimTasksById.length
? asPinnedQuery(claimTasksById, queryForScheduledTasks)
: queryForScheduledTasks
),
filterDownBy(InactiveTasks)
),
update: updateFieldsAndMarkAsFailed(
{
ownerId: this.taskManagerId,
retryAt: claimOwnershipUntil,
},
claimTasksById || [],
taskMaxAttempts
),
sort,
}),
{
max_docs: size,
}
);
if (apmTrans) apmTrans.end();
return updated;
}
/**
* Fetches tasks from the index, which are owned by the current Kibana instance
*/
private async sweepForClaimedTasks(
claimTasksById: OwnershipClaimingOpts['claimTasksById'],
size: OwnershipClaimingOpts['size']
): Promise<ConcreteTaskInstance[]> {
const claimedTasksQuery = tasksClaimedByOwner(this.taskManagerId);
const { docs } = await this.search({
query:
claimTasksById && claimTasksById.length
? asPinnedQuery(claimTasksById, claimedTasksQuery)
: claimedTasksQuery,
size,
sort: SortByRunAtAndRetryAt,
seq_no_primary_term: true,
});
return docs;
}
/**
* Updates the specified doc in the index, returning the doc
* with its version up to date.
*
* @param {TaskDoc} doc
* @returns {Promise<TaskDoc>}
*/
public async update(doc: ConcreteTaskInstance): Promise<ConcreteTaskInstance> {
const attributes = taskInstanceToAttributes(doc);
let updatedSavedObject;
try {
updatedSavedObject = await this.savedObjectsRepository.update<SerializedConcreteTaskInstance>(
'task',
doc.id,
attributes,
{
refresh: false,
version: doc.version,
}
);
} catch (e) {
this.errors$.next(e);
throw e;
}
return savedObjectToConcreteTaskInstance(
// The SavedObjects update api forces a Partial on the `attributes` on the response,
// but actually returns the whole object that is passed to it, so as we know we're
// passing in the whole object, this is safe to do.
// This is far from ideal, but unless we change the SavedObjectsClient this is the best we can do
{ ...updatedSavedObject, attributes: defaults(updatedSavedObject.attributes, attributes) }
);
}
/**
* Updates the specified docs in the index, returning the docs
* with their versions up to date.
*
* @param {Array<TaskDoc>} docs
* @returns {Promise<Array<TaskDoc>>}
*/
public async bulkUpdate(docs: ConcreteTaskInstance[]): Promise<BulkUpdateResult[]> {
const attributesByDocId = docs.reduce((attrsById, doc) => {
attrsById.set(doc.id, taskInstanceToAttributes(doc));
return attrsById;
}, new Map());
let updatedSavedObjects: Array<SavedObjectsUpdateResponse | Error>;
try {
({ saved_objects: updatedSavedObjects } = await this.savedObjectsRepository.bulkUpdate<
SerializedConcreteTaskInstance
>(
docs.map((doc) => ({
type: 'task',
id: doc.id,
options: { version: doc.version },
attributes: attributesByDocId.get(doc.id)!,
})),
{
refresh: false,
}
));
} catch (e) {
this.errors$.next(e);
throw e;
}
return updatedSavedObjects.map<BulkUpdateResult>((updatedSavedObject, index) =>
isSavedObjectsUpdateResponse(updatedSavedObject)
? asOk(
savedObjectToConcreteTaskInstance({
...updatedSavedObject,
attributes: defaults(
updatedSavedObject.attributes,
attributesByDocId.get(updatedSavedObject.id)!
),
})
)
: asErr({
// The SavedObjectsRepository maintains the order of the docs
// so we can rely on the index in the `docs` to match an error
// on the same index in the `bulkUpdate` result
entity: docs[index],
error: updatedSavedObject,
})
);
}
/**
* Removes the specified task from the index.
*
* @param {string} id
* @returns {Promise<void>}
*/
public async remove(id: string): Promise<void> {
try {
await this.savedObjectsRepository.delete('task', id);
} catch (e) {
this.errors$.next(e);
throw e;
}
}
/**
* Gets a task by id
*
* @param {string} id
* @returns {Promise<void>}
*/
public async get(id: string): Promise<ConcreteTaskInstance> {
let result;
try {
result = await this.savedObjectsRepository.get<SerializedConcreteTaskInstance>('task', id);
} catch (e) {
this.errors$.next(e);
throw e;
}
return savedObjectToConcreteTaskInstance(result);
}
/**
* Gets task lifecycle step by id
*
* @param {string} id
* @returns {Promise<void>}
*/
public async getLifecycle(id: string): Promise<TaskLifecycle> {
try {
const task = await this.get(id);
return task.status;
} catch (err) {
if (err.output && err.output.statusCode === 404) {
return TaskLifecycleResult.NotFound;
}
throw err;
}
}
private async search(opts: SearchOpts = {}): Promise<FetchResult> {
const { query } = ensureQueryOnlyReturnsTaskObjects(opts);
try {
const {
body: {
hits: { hits: tasks },
},
} = await this.esClient.search<SearchResponse<SavedObjectsRawDoc['_source']>>({
index: this.index,
ignore_unavailable: true,
body: {
...opts,
query,
},
});
return {
docs: tasks
.filter((doc) => this.serializer.isRawSavedObject(doc))
.map((doc) => this.serializer.rawToSavedObject(doc))
.map((doc) => omit(doc, 'namespace') as SavedObject<SerializedConcreteTaskInstance>)
.map(savedObjectToConcreteTaskInstance),
};
} catch (e) {
this.errors$.next(e);
throw e;
}
}
public async aggregate<TSearchRequest extends AggregationOpts>({
aggs,
query,
size = 0,
}: TSearchRequest): Promise<ESSearchResponse<ConcreteTaskInstance, { body: TSearchRequest }>> {
const { body } = await this.esClient.search<
ESSearchResponse<ConcreteTaskInstance, { body: TSearchRequest }>
>({
index: this.index,
ignore_unavailable: true,
body: ensureAggregationOnlyReturnsTaskObjects({
query,
aggs,
size,
}),
});
return body;
}
private async updateByQuery(
opts: UpdateByQuerySearchOpts = {},
// eslint-disable-next-line @typescript-eslint/naming-convention
{ max_docs }: UpdateByQueryOpts = {}
): Promise<UpdateByQueryResult> {
const { query } = ensureQueryOnlyReturnsTaskObjects(opts);
try {
const {
// eslint-disable-next-line @typescript-eslint/naming-convention
body: { total, updated, version_conflicts },
} = await this.esClient.updateByQuery<UpdateDocumentByQueryResponse>({
index: this.index,
ignore_unavailable: true,
refresh: true,
max_docs,
conflicts: 'proceed',
body: {
...opts,
query,
},
});
return {
total,
updated,
version_conflicts,
};
} catch (e) {
this.errors$.next(e);
throw e;
}
}
}
function taskInstanceToAttributes(doc: TaskInstance): SerializedConcreteTaskInstance {
return {
...omit(doc, 'id', 'version'),
params: JSON.stringify(doc.params || {}),
state: JSON.stringify(doc.state || {}),
attempts: (doc as ConcreteTaskInstance).attempts || 0,
scheduledAt: (doc.scheduledAt || new Date()).toISOString(),
startedAt: (doc.startedAt && doc.startedAt.toISOString()) || null,
retryAt: (doc.retryAt && doc.retryAt.toISOString()) || null,
runAt: (doc.runAt || new Date()).toISOString(),
status: (doc as ConcreteTaskInstance).status || 'idle',
} as SerializedConcreteTaskInstance;
}
export function savedObjectToConcreteTaskInstance(
savedObject: Omit<SavedObject<SerializedConcreteTaskInstance>, 'references'>
): ConcreteTaskInstance {
return {
...savedObject.attributes,
id: savedObject.id,
version: savedObject.version,
scheduledAt: new Date(savedObject.attributes.scheduledAt),
runAt: new Date(savedObject.attributes.runAt),
startedAt: savedObject.attributes.startedAt ? new Date(savedObject.attributes.startedAt) : null,
retryAt: savedObject.attributes.retryAt ? new Date(savedObject.attributes.retryAt) : null,
state: parseJSONField(savedObject.attributes.state, 'state', savedObject.id),
params: parseJSONField(savedObject.attributes.params, 'params', savedObject.id),
};
}
function parseJSONField(json: string, fieldName: string, id: string) {
try {
return json ? JSON.parse(json) : {};
} catch (error) {
throw new Error(`Task "${id}"'s ${fieldName} field has invalid JSON: ${json}`);
}
}
function ensureQueryOnlyReturnsTaskObjects(opts: SearchOpts): SearchOpts {
const originalQuery = opts.query;
const queryOnlyTasks = { term: { type: 'task' } };
const query = originalQuery
? { bool: { must: [queryOnlyTasks, originalQuery] } }
: queryOnlyTasks;
return {
...opts,
query,
};
}
function ensureAggregationOnlyReturnsTaskObjects(opts: AggregationOpts): AggregationOpts {
const originalQuery = opts.query;
const filterToOnlyTasks = {
bool: {
filter: [{ term: { type: 'task' } }],
},
};
const query = originalQuery
? { bool: { must: [filterToOnlyTasks, originalQuery] } }
: filterToOnlyTasks;
return {
...opts,
query,
};
}
function isSavedObjectsUpdateResponse(
result: SavedObjectsUpdateResponse | Error
): result is SavedObjectsUpdateResponse {
return result && typeof (result as SavedObjectsUpdateResponse).id === 'string';
}