-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathtask_store.ts
616 lines (563 loc) · 18.9 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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
/*
* This module contains helpers for managing the task manager storage layer.
*/
import { Subject } from 'rxjs';
import { omit, defaults, get } from 'lodash';
import { SavedObjectError } from '@kbn/core-saved-objects-common';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { SavedObjectsBulkDeleteResponse, Logger } from '@kbn/core/server';
import {
SavedObject,
ISavedObjectsSerializer,
SavedObjectsRawDoc,
ISavedObjectsRepository,
SavedObjectsUpdateResponse,
ElasticsearchClient,
} from '@kbn/core/server';
import { RequestTimeoutsConfig } from './config';
import { asOk, asErr, Result } from './lib/result_type';
import {
ConcreteTaskInstance,
TaskInstance,
TaskLifecycle,
TaskLifecycleResult,
SerializedConcreteTaskInstance,
} from './task';
import { TaskTypeDictionary } from './task_type_dictionary';
import { AdHocTaskCounter } from './lib/adhoc_task_counter';
import { TaskValidator } from './task_validator';
export interface StoreOpts {
esClient: ElasticsearchClient;
index: string;
taskManagerId: string;
definitions: TaskTypeDictionary;
savedObjectsRepository: ISavedObjectsRepository;
serializer: ISavedObjectsSerializer;
adHocTaskCounter: AdHocTaskCounter;
allowReadingInvalidState: boolean;
logger: Logger;
requestTimeouts: RequestTimeoutsConfig;
}
export interface SearchOpts {
search_after?: Array<number | string>;
size?: number;
sort?: estypes.Sort;
query?: estypes.QueryDslQueryContainer;
seq_no_primary_term?: boolean;
}
export interface AggregationOpts {
aggs: Record<string, estypes.AggregationsAggregationContainer>;
query?: estypes.QueryDslQueryContainer;
runtime_mappings?: estypes.MappingRuntimeFields;
size?: number;
}
export interface UpdateByQuerySearchOpts extends SearchOpts {
script?: estypes.Script;
}
export interface UpdateByQueryOpts extends SearchOpts {
max_docs?: number;
}
export interface FetchResult {
docs: ConcreteTaskInstance[];
}
export type BulkUpdateResult = Result<
ConcreteTaskInstance,
{ type: string; id: string; error: SavedObjectError }
>;
export type BulkGetResult = Array<
Result<ConcreteTaskInstance, { type: string; id: string; error: SavedObjectError }>
>;
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 index: string;
public readonly taskManagerId: string;
public readonly errors$ = new Subject<Error>();
public readonly taskValidator: TaskValidator;
private esClient: ElasticsearchClient;
private esClientWithoutRetries: ElasticsearchClient;
private definitions: TaskTypeDictionary;
private savedObjectsRepository: ISavedObjectsRepository;
private serializer: ISavedObjectsSerializer;
private adHocTaskCounter: AdHocTaskCounter;
private requestTimeouts: RequestTimeoutsConfig;
/**
* Constructs a new TaskStore.
* @param {StoreOpts} opts
* @prop {esClient} esClient - An elasticsearch client
* @prop {string} index - The name of the task manager index
* @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.definitions = opts.definitions;
this.serializer = opts.serializer;
this.savedObjectsRepository = opts.savedObjectsRepository;
this.adHocTaskCounter = opts.adHocTaskCounter;
this.taskValidator = new TaskValidator({
logger: opts.logger,
definitions: opts.definitions,
allowReadingInvalidState: opts.allowReadingInvalidState,
});
this.esClientWithoutRetries = opts.esClient.child({
// Timeouts are retried and make requests timeout after (requestTimeout * (1 + maxRetries))
// The poller doesn't need retry logic because it will try again at the next polling cycle
maxRetries: 0,
});
this.requestTimeouts = opts.requestTimeouts;
}
/**
* Convert ConcreteTaskInstance Ids to match their SavedObject format as serialized
* in Elasticsearch
* @param tasks - The task being scheduled.
*/
public convertToSavedObjectIds(
taskIds: Array<ConcreteTaskInstance['id']>
): Array<ConcreteTaskInstance['id']> {
return taskIds.map((id) => this.serializer.generateRawId(undefined, 'task', id));
}
/**
* Schedules a task.
*
* @param task - The task being scheduled.
*/
public async schedule(taskInstance: TaskInstance): Promise<ConcreteTaskInstance> {
this.definitions.ensureHas(taskInstance.taskType);
let savedObject;
try {
const validatedTaskInstance =
this.taskValidator.getValidatedTaskInstanceForUpdating(taskInstance);
savedObject = await this.savedObjectsRepository.create<SerializedConcreteTaskInstance>(
'task',
taskInstanceToAttributes(validatedTaskInstance),
{ id: taskInstance.id, refresh: false }
);
if (get(taskInstance, 'schedule.interval', null) == null) {
this.adHocTaskCounter.increment();
}
} catch (e) {
this.errors$.next(e);
throw e;
}
const result = savedObjectToConcreteTaskInstance(savedObject);
return this.taskValidator.getValidatedTaskInstanceFromReading(result);
}
/**
* Bulk schedules a task.
*
* @param tasks - The tasks being scheduled.
*/
public async bulkSchedule(taskInstances: TaskInstance[]): Promise<ConcreteTaskInstance[]> {
const objects = taskInstances.map((taskInstance) => {
this.definitions.ensureHas(taskInstance.taskType);
const validatedTaskInstance =
this.taskValidator.getValidatedTaskInstanceForUpdating(taskInstance);
return {
type: 'task',
attributes: taskInstanceToAttributes(validatedTaskInstance),
id: taskInstance.id,
};
});
let savedObjects;
try {
savedObjects = await this.savedObjectsRepository.bulkCreate<SerializedConcreteTaskInstance>(
objects,
{ refresh: false }
);
this.adHocTaskCounter.increment(
taskInstances.filter((task) => {
return get(task, 'schedule.interval', null) == null;
}).length
);
} catch (e) {
this.errors$.next(e);
throw e;
}
return savedObjects.saved_objects.map((so) => {
const taskInstance = savedObjectToConcreteTaskInstance(so);
return this.taskValidator.getValidatedTaskInstanceFromReading(taskInstance);
});
}
/**
* 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,
});
}
/**
* 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,
options: { validate: boolean }
): Promise<ConcreteTaskInstance> {
const taskInstance = this.taskValidator.getValidatedTaskInstanceForUpdating(doc, {
validate: options.validate,
});
const attributes = taskInstanceToAttributes(taskInstance);
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;
}
const result = 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) }
);
return this.taskValidator.getValidatedTaskInstanceFromReading(result, {
validate: options.validate,
});
}
/**
* 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[],
options: { validate: boolean }
): Promise<BulkUpdateResult[]> {
const attributesByDocId = docs.reduce((attrsById, doc) => {
const taskInstance = this.taskValidator.getValidatedTaskInstanceForUpdating(doc, {
validate: options.validate,
});
attrsById.set(doc.id, taskInstanceToAttributes(taskInstance));
return attrsById;
}, new Map());
let updatedSavedObjects: Array<SavedObjectsUpdateResponse<SerializedConcreteTaskInstance>>;
try {
({ saved_objects: updatedSavedObjects } =
await this.savedObjectsRepository.bulkUpdate<SerializedConcreteTaskInstance>(
docs.map((doc) => ({
type: 'task',
id: doc.id,
version: doc.version,
attributes: attributesByDocId.get(doc.id)!,
})),
{
refresh: false,
}
));
} catch (e) {
this.errors$.next(e);
throw e;
}
return updatedSavedObjects.map((updatedSavedObject) => {
if (updatedSavedObject.error !== undefined) {
return asErr({
type: 'task',
id: updatedSavedObject.id,
error: updatedSavedObject.error,
});
}
const taskInstance = savedObjectToConcreteTaskInstance({
...updatedSavedObject,
attributes: defaults(
updatedSavedObject.attributes,
attributesByDocId.get(updatedSavedObject.id)!
),
});
const result = this.taskValidator.getValidatedTaskInstanceFromReading(taskInstance, {
validate: options.validate,
});
return asOk(result);
});
}
/**
* 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, { refresh: false });
} catch (e) {
this.errors$.next(e);
throw e;
}
}
/**
* Bulk removes the specified tasks from the index.
*
* @param {SavedObjectsBulkDeleteObject[]} savedObjectsToDelete
* @returns {Promise<SavedObjectsBulkDeleteResponse>}
*/
public async bulkRemove(taskIds: string[]): Promise<SavedObjectsBulkDeleteResponse> {
try {
const savedObjectsToDelete = taskIds.map((taskId) => ({ id: taskId, type: 'task' }));
return await this.savedObjectsRepository.bulkDelete(savedObjectsToDelete, { refresh: false });
} 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;
}
const taskInstance = savedObjectToConcreteTaskInstance(result);
return taskInstance;
}
/**
* Gets tasks by ids
*
* @param {Array<string>} ids
* @returns {Promise<ConcreteTaskInstance[]>}
*/
public async bulkGet(ids: string[]): Promise<BulkGetResult> {
let result;
try {
result = await this.savedObjectsRepository.bulkGet<SerializedConcreteTaskInstance>(
ids.map((id) => ({ type: 'task', id }))
);
} catch (e) {
this.errors$.next(e);
throw e;
}
return result.saved_objects.map((task) => {
if (task.error) {
return asErr({ id: task.id, type: task.type, error: task.error });
}
const taskInstance = savedObjectToConcreteTaskInstance(task);
return asOk(taskInstance);
});
}
/**
* 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 {
hits: { hits: tasks },
} = await this.esClientWithoutRetries.search<SavedObjectsRawDoc['_source']>({
index: this.index,
ignore_unavailable: true,
body: {
...opts,
query,
},
});
return {
docs: tasks
// @ts-expect-error @elastic/elasticsearch _source is optional
.filter((doc) => this.serializer.isRawSavedObject(doc))
// @ts-expect-error @elastic/elasticsearch _source is optional
.map((doc) => this.serializer.rawToSavedObject(doc))
.map((doc) => omit(doc, 'namespace') as SavedObject<SerializedConcreteTaskInstance>)
.map((doc) => savedObjectToConcreteTaskInstance(doc))
.filter((doc): doc is ConcreteTaskInstance => !!doc),
};
} catch (e) {
this.errors$.next(e);
throw e;
}
}
public async aggregate<TSearchRequest extends AggregationOpts>({
aggs,
query,
// eslint-disable-next-line @typescript-eslint/naming-convention
runtime_mappings,
size = 0,
}: TSearchRequest): Promise<estypes.SearchResponse<ConcreteTaskInstance>> {
const body = await this.esClient.search<
ConcreteTaskInstance,
Record<string, estypes.AggregationsAggregate>
>({
index: this.index,
ignore_unavailable: true,
track_total_hits: true,
body: ensureAggregationOnlyReturnsEnabledTaskObjects({
query,
aggs,
runtime_mappings,
size,
}),
});
return body;
}
public async updateByQuery(
opts: UpdateByQuerySearchOpts = {},
// eslint-disable-next-line @typescript-eslint/naming-convention
{ max_docs: max_docs }: UpdateByQueryOpts = {}
): Promise<UpdateByQueryResult> {
const { query } = ensureQueryOnlyReturnsTaskObjects(opts);
try {
const // eslint-disable-next-line @typescript-eslint/naming-convention
{ total, updated, version_conflicts } = await this.esClientWithoutRetries.updateByQuery(
{
index: this.index,
ignore_unavailable: true,
refresh: true,
conflicts: 'proceed',
body: {
...opts,
max_docs,
query,
},
},
{ requestTimeout: this.requestTimeouts.update_by_query }
);
const conflictsCorrectedForContinuation = correctVersionConflictsForContinuation(
updated,
version_conflicts,
max_docs
);
return {
total: total || 0,
updated: updated || 0,
version_conflicts: conflictsCorrectedForContinuation,
};
} catch (e) {
this.errors$.next(e);
throw e;
}
}
}
/**
* When we run updateByQuery with conflicts='proceed', it's possible for the `version_conflicts`
* to count against the specified `max_docs`, as per https://github.com/elastic/elasticsearch/issues/63671
* In order to correct for that happening, we only count `version_conflicts` if we haven't updated as
* many docs as we could have.
* This is still no more than an estimation, as there might have been less docuemnt to update that the
* `max_docs`, but we bias in favour of over zealous `version_conflicts` as that's the best indicator we
* have for an unhealthy cluster distribution of Task Manager polling intervals
*/
export function correctVersionConflictsForContinuation(
updated: estypes.ReindexResponse['updated'],
versionConflicts: estypes.ReindexResponse['version_conflicts'],
maxDocs?: number
): number {
// @ts-expect-error estypes.ReindexResponse['updated'] and estypes.ReindexResponse['version_conflicts'] can be undefined
return maxDocs && versionConflicts + updated > maxDocs ? maxDocs - updated : versionConflicts;
}
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 ensureAggregationOnlyReturnsEnabledTaskObjects(opts: AggregationOpts): AggregationOpts {
const originalQuery = opts.query;
const filterToOnlyTasks = {
bool: {
filter: [{ term: { type: 'task' } }, { term: { 'task.enabled': true } }],
},
};
const query = originalQuery
? { bool: { must: [filterToOnlyTasks, originalQuery] } }
: filterToOnlyTasks;
return {
...opts,
query,
};
}