-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
model.ts
791 lines (770 loc) · 31.3 KB
/
model.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { gt, valid } from 'semver';
import * as Either from 'fp-ts/lib/Either';
import * as Option from 'fp-ts/lib/Option';
import { cloneDeep } from 'lodash';
import { AliasAction, FetchIndexResponse, isLeftTypeof, RetryableEsClientError } from './actions';
import { AllActionStates, InitState, State } from './types';
import { IndexMapping } from '../mappings';
import { ResponseType } from './next';
import { SavedObjectsMigrationVersion } from '../types';
import { disableUnknownTypeMappingFields } from '../migrations/core/migration_context';
import { SavedObjectsMigrationConfigType } from '../saved_objects_config';
/**
* A helper function/type for ensuring that all control state's are handled.
*/
function throwBadControlState(p: never): never;
function throwBadControlState(controlState: any) {
throw new Error('Unexpected control state: ' + controlState);
}
/**
* A helper function/type for ensuring that all response types are handled.
*/
function throwBadResponse(state: State, p: never): never;
function throwBadResponse(state: State, res: any): never {
throw new Error(
`${state.controlState} received unexpected action response: ` + JSON.stringify(res)
);
}
/**
* Merge the _meta.migrationMappingPropertyHashes mappings of an index with
* the given target mappings.
*
* @remarks When another instance already completed a migration, the existing
* target index might contain documents and mappings created by a plugin that
* is disabled in the current Kibana instance performing this migration.
* Mapping updates are commutative (deeply merged) by Elasticsearch, except
* for the `_meta` key. By merging the `_meta.migrationMappingPropertyHashes`
* mappings from the existing target index index into the targetMappings we
* ensure that any `migrationPropertyHashes` for disabled plugins aren't lost.
*
* Right now we don't use these `migrationPropertyHashes` but it could be used
* in the future to detect if mappings were changed. If mappings weren't
* changed we don't need to reindex but can clone the index to save disk space.
*
* @param targetMappings
* @param indexMappings
*/
function mergeMigrationMappingPropertyHashes(
targetMappings: IndexMapping,
indexMappings: IndexMapping
) {
return {
...targetMappings,
_meta: {
migrationMappingPropertyHashes: {
...indexMappings._meta?.migrationMappingPropertyHashes,
...targetMappings._meta?.migrationMappingPropertyHashes,
},
},
};
}
function indexBelongsToLaterVersion(indexName: string, kibanaVersion: string): boolean {
const version = valid(indexVersion(indexName));
return version != null ? gt(version, kibanaVersion) : false;
}
/**
* Extracts the version number from a >= 7.11 index
* @param indexName A >= v7.11 index name
*/
function indexVersion(indexName?: string): string | undefined {
return (indexName?.match(/.+_(\d+\.\d+\.\d+)_\d+/) || [])[1];
}
/**
* Creates a record of alias -> index name pairs
*/
function getAliases(indices: FetchIndexResponse) {
return Object.keys(indices).reduce((acc, index) => {
Object.keys(indices[index].aliases || {}).forEach((alias) => {
// TODO throw if multiple .kibana aliases point to the same index?
acc[alias] = index;
});
return acc;
}, {} as Record<string, string>);
}
const delayRetryState = <S extends State>(
state: S,
errorMessage: string,
/** How many times to retry a step that fails */
maxRetryAttempts: number
): S => {
if (state.retryCount >= maxRetryAttempts) {
return {
...state,
controlState: 'FATAL',
reason: `Unable to complete the ${state.controlState} step after ${maxRetryAttempts} attempts, terminating.`,
};
} else {
const retryCount = state.retryCount + 1;
const retryDelay = 1000 * Math.min(Math.pow(2, retryCount), 64); // 2s, 4s, 8s, 16s, 32s, 64s, 64s, 64s ...
return {
...state,
retryCount,
retryDelay,
logs: [
...state.logs,
{
level: 'error',
message: `Action failed with '${errorMessage}'. Retrying attempt ${retryCount} in ${
retryDelay / 1000
} seconds.`,
},
],
};
}
};
const resetRetryState = <S extends State>(state: S): S => {
return { ...state, ...{ retryCount: 0, retryDelay: 0 } };
};
export type ExcludeRetryableEsError<Response> = Exclude<
| Exclude<
Response,
Either.Either<Response extends Either.Left<unknown> ? Response['left'] : never, never>
>
| Either.Either<
Exclude<
Response extends Either.Left<unknown> ? Response['left'] : never,
RetryableEsClientError
>,
Response extends Either.Right<unknown> ? Response['right'] : never
>,
Either.Left<never>
>;
export const model = (currentState: State, resW: ResponseType<AllActionStates>): State => {
// The action response `resW` is weakly typed, the type includes all action
// responses. Each control state only triggers one action so each control
// state only has one action response type. This allows us to narrow the
// response type to only the action response associated with a specific
// control state using:
// `const res = resW as ResponseType<typeof stateP.controlState>;`
let stateP: State = cloneDeep(currentState);
// Handle retryable_es_client_errors. Other left values need to be handled
// by the control state specific code below.
if (
Either.isLeft<unknown, unknown>(resW) &&
isLeftTypeof(resW.left, 'retryable_es_client_error')
) {
// Retry the same step after an exponentially increasing delay.
return delayRetryState(stateP, resW.left.message, stateP.retryAttempts);
} else {
// If the action didn't fail with a retryable_es_client_error, reset the
// retry counter and retryDelay state
stateP = resetRetryState(stateP);
}
if (stateP.controlState === 'INIT') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
const indices = res.right;
const aliases = getAliases(indices);
if (
// `.kibana` and the version specific aliases both exists and
// are pointing to the same index. This version's migration has already
// been completed.
aliases[stateP.currentAlias] != null &&
aliases[stateP.versionAlias] != null &&
aliases[stateP.currentAlias] === aliases[stateP.versionAlias]
) {
return {
...stateP,
// Skip to 'OUTDATED_DOCUMENTS_SEARCH' so that if a new plugin was
// installed / enabled we can transform any old documents and update
// the mappings for this plugin's types.
controlState: 'OUTDATED_DOCUMENTS_SEARCH',
// Source is a none because we didn't do any migration from a source
// index
sourceIndex: Option.none,
targetIndex: `${stateP.indexPrefix}_${stateP.kibanaVersion}_001`,
targetIndexMappings: mergeMigrationMappingPropertyHashes(
stateP.targetIndexMappings,
indices[aliases[stateP.currentAlias]].mappings
),
versionIndexReadyActions: Option.none,
};
} else if (
// `.kibana` is pointing to an index that belongs to a later
// version of Kibana .e.g. a 7.11.0 instance found the `.kibana` alias
// pointing to `.kibana_7.12.0_001`
indexBelongsToLaterVersion(aliases[stateP.currentAlias], stateP.kibanaVersion)
) {
return {
...stateP,
controlState: 'FATAL',
reason: `The ${
stateP.currentAlias
} alias is pointing to a newer version of Kibana: v${indexVersion(
aliases[stateP.currentAlias]
)}`,
};
} else if (
// If the `.kibana` alias exists
aliases[stateP.currentAlias] != null
) {
// The source index is the index the `.kibana` alias points to
const source = aliases[stateP.currentAlias];
const target = stateP.versionIndex;
return {
...stateP,
controlState: 'SET_SOURCE_WRITE_BLOCK',
sourceIndex: Option.some(source) as Option.Some<string>,
targetIndex: target,
targetIndexMappings: disableUnknownTypeMappingFields(
stateP.targetIndexMappings,
indices[source].mappings
),
versionIndexReadyActions: Option.some<AliasAction[]>([
{ remove: { index: source, alias: stateP.currentAlias, must_exist: true } },
{ add: { index: target, alias: stateP.currentAlias } },
{ add: { index: target, alias: stateP.versionAlias } },
{ remove_index: { index: stateP.tempIndex } },
]),
};
} else if (indices[stateP.legacyIndex] != null) {
// Migrate from a legacy index
// If the user used default index names we can narrow the version
// number we use when creating a backup index. This is purely to help
// users more easily identify how "old" and index is so that they can
// decide if it's safe to delete these rollback backups. Because
// backups are kept for rollback, a version number is more useful than
// a date.
let legacyVersion = '';
if (stateP.indexPrefix === '.kibana') {
legacyVersion = 'pre6.5.0';
} else if (stateP.indexPrefix === '.kibana_task_manager') {
legacyVersion = 'pre7.4.0';
} else {
legacyVersion = 'pre' + stateP.kibanaVersion;
}
const legacyReindexTarget = `${stateP.indexPrefix}_${legacyVersion}_001`;
const target = stateP.versionIndex;
return {
...stateP,
controlState: 'LEGACY_SET_WRITE_BLOCK',
sourceIndex: Option.some(legacyReindexTarget) as Option.Some<string>,
targetIndex: target,
targetIndexMappings: disableUnknownTypeMappingFields(
stateP.targetIndexMappings,
indices[stateP.legacyIndex].mappings
),
legacyReindexTargetMappings: indices[stateP.legacyIndex].mappings,
legacyPreMigrationDoneActions: [
{ remove_index: { index: stateP.legacyIndex } },
{
add: {
index: legacyReindexTarget,
alias: stateP.currentAlias,
},
},
],
versionIndexReadyActions: Option.some<AliasAction[]>([
{
remove: {
index: legacyReindexTarget,
alias: stateP.currentAlias,
must_exist: true,
},
},
{ add: { index: target, alias: stateP.currentAlias } },
{ add: { index: target, alias: stateP.versionAlias } },
{ remove_index: { index: stateP.tempIndex } },
]),
};
} else {
// This cluster doesn't have an existing Saved Object index, create a
// new version specific index.
const target = stateP.versionIndex;
return {
...stateP,
controlState: 'CREATE_NEW_TARGET',
sourceIndex: Option.none as Option.None,
targetIndex: target,
versionIndexReadyActions: Option.some([
{ add: { index: target, alias: stateP.currentAlias } },
{ add: { index: target, alias: stateP.versionAlias } },
]) as Option.Some<AliasAction[]>,
};
}
} else {
return throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'LEGACY_SET_WRITE_BLOCK') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
// If the write block is sucessfully in place
if (Either.isRight(res)) {
return { ...stateP, controlState: 'LEGACY_CREATE_REINDEX_TARGET' };
} else if (Either.isLeft(res)) {
// If the write block failed because the index doesn't exist, it means
// another instance already completed the legacy pre-migration. Proceed
// to the next step.
if (isLeftTypeof(res.left, 'index_not_found_exception')) {
return { ...stateP, controlState: 'LEGACY_CREATE_REINDEX_TARGET' };
} else {
// @ts-expect-error TS doesn't correctly narrow this type to never
return throwBadResponse(stateP, res);
}
} else {
return throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'LEGACY_CREATE_REINDEX_TARGET') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return {
...stateP,
controlState: 'LEGACY_REINDEX',
};
} else {
// If the createIndex action receives an 'resource_already_exists_exception'
// it will wait until the index status turns green so we don't have any
// left responses to handle here.
throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'LEGACY_REINDEX') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return {
...stateP,
controlState: 'LEGACY_REINDEX_WAIT_FOR_TASK',
legacyReindexTaskId: res.right.taskId,
};
} else {
throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'LEGACY_REINDEX_WAIT_FOR_TASK') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return {
...stateP,
controlState: 'LEGACY_DELETE',
};
} else {
const left = res.left;
if (
(isLeftTypeof(left, 'index_not_found_exception') && left.index === stateP.legacyIndex) ||
isLeftTypeof(left, 'target_index_had_write_block')
) {
// index_not_found_exception for the LEGACY_REINDEX source index:
// another instance already complete the LEGACY_DELETE step.
//
// target_index_had_write_block: another instance already completed the
// SET_SOURCE_WRITE_BLOCK step.
//
// If we detect that another instance has already completed a step, we
// can technically skip ahead in the process until after the completed
// step. However, by not skipping ahead we limit branches in the
// control state progression and simplify the implementation.
return { ...stateP, controlState: 'LEGACY_DELETE' };
} else if (isLeftTypeof(left, 'wait_for_task_completion_timeout')) {
// After waiting for the specificed timeout, the task has not yet
// completed. Retry this step to see if the task has completed after an
// exponential delay. We will basically keep polling forever until the
// Elasticeasrch task succeeds or fails.
return delayRetryState(stateP, left.message, Number.MAX_SAFE_INTEGER);
} else if (
isLeftTypeof(left, 'index_not_found_exception') ||
isLeftTypeof(left, 'incompatible_mapping_exception')
) {
// We don't handle the following errors as the algorithm will never
// run into these during the LEGACY_REINDEX_WAIT_FOR_TASK step:
// - index_not_found_exception for the LEGACY_REINDEX target index
// - incompatible_mapping_exception
throwBadResponse(stateP, left as never);
} else {
throwBadResponse(stateP, left);
}
}
} else if (stateP.controlState === 'LEGACY_DELETE') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return { ...stateP, controlState: 'SET_SOURCE_WRITE_BLOCK' };
} else if (Either.isLeft(res)) {
const left = res.left;
if (
isLeftTypeof(left, 'remove_index_not_a_concrete_index') ||
(isLeftTypeof(left, 'index_not_found_exception') && left.index === stateP.legacyIndex)
) {
// index_not_found_exception, another Kibana instance already
// deleted the legacy index
//
// remove_index_not_a_concrete_index, another Kibana instance already
// deleted the legacy index and created a .kibana alias
//
// If we detect that another instance has already completed a step, we
// can technically skip ahead in the process until after the completed
// step. However, by not skipping ahead we limit branches in the
// control state progression and simplify the implementation.
return { ...stateP, controlState: 'SET_SOURCE_WRITE_BLOCK' };
} else if (
isLeftTypeof(left, 'index_not_found_exception') ||
isLeftTypeof(left, 'alias_not_found_exception')
) {
// We don't handle the following errors as the migration algorithm
// will never cause them to occur:
// - alias_not_found_exception we're not using must_exist
// - index_not_found_exception for source index into which we reindex
// the legacy index
throwBadResponse(stateP, left as never);
} else {
throwBadResponse(stateP, left);
}
} else {
throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'SET_SOURCE_WRITE_BLOCK') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
// If the write block is successfully in place, proceed to the next step.
return {
...stateP,
controlState: 'CREATE_REINDEX_TEMP',
};
} else if (isLeftTypeof(res.left, 'index_not_found_exception')) {
// We don't handle the following errors as the migration algorithm
// will never cause them to occur:
// - index_not_found_exception
return throwBadResponse(stateP, res.left as never);
} else {
return throwBadResponse(stateP, res.left);
}
} else if (stateP.controlState === 'CREATE_REINDEX_TEMP') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return { ...stateP, controlState: 'REINDEX_SOURCE_TO_TEMP' };
} else {
// If the createIndex action receives an 'resource_already_exists_exception'
// it will wait until the index status turns green so we don't have any
// left responses to handle here.
throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'REINDEX_SOURCE_TO_TEMP') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return {
...stateP,
controlState: 'REINDEX_SOURCE_TO_TEMP_WAIT_FOR_TASK',
reindexSourceToTargetTaskId: res.right.taskId,
};
} else {
// Since this is a background task, the request should always succeed,
// errors only show up in the returned task.
throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'REINDEX_SOURCE_TO_TEMP_WAIT_FOR_TASK') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return {
...stateP,
controlState: 'SET_TEMP_WRITE_BLOCK',
};
} else {
const left = res.left;
if (
isLeftTypeof(left, 'target_index_had_write_block') ||
(isLeftTypeof(left, 'index_not_found_exception') && left.index === stateP.tempIndex)
) {
// index_not_found_exception:
// another instance completed the MARK_VERSION_INDEX_READY and
// removed the temp index.
// target_index_had_write_block
// another instance completed the SET_TEMP_WRITE_BLOCK step adding a
// write block to the temp index.
//
// For simplicity we continue linearly through the next steps even if
// we know another instance already completed these.
return {
...stateP,
controlState: 'SET_TEMP_WRITE_BLOCK',
};
} else if (isLeftTypeof(left, 'wait_for_task_completion_timeout')) {
// After waiting for the specificed timeout, the task has not yet
// completed. Retry this step to see if the task has completed after an
// exponential delay. We will basically keep polling forever until the
// Elasticeasrch task succeeds or fails.
return delayRetryState(stateP, left.message, Number.MAX_SAFE_INTEGER);
} else if (
isLeftTypeof(left, 'index_not_found_exception') ||
isLeftTypeof(left, 'incompatible_mapping_exception')
) {
// Don't handle the following errors as the migration algorithm should
// never cause them to occur:
// - incompatible_mapping_exception the temp index has `dynamic: false`
// mappings
// - index_not_found_exception for the source index, we will never
// delete the source index
throwBadResponse(stateP, left as never);
} else {
throwBadResponse(stateP, left);
}
}
} else if (stateP.controlState === 'SET_TEMP_WRITE_BLOCK') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return {
...stateP,
controlState: 'CLONE_TEMP_TO_TARGET',
};
} else {
const left = res.left;
if (isLeftTypeof(left, 'index_not_found_exception')) {
// index_not_found_exception:
// another instance completed the MARK_VERSION_INDEX_READY and
// removed the temp index.
//
// For simplicity we continue linearly through the next steps even if
// we know another instance already completed these.
return {
...stateP,
controlState: 'CLONE_TEMP_TO_TARGET',
};
} else {
throwBadResponse(stateP, left);
}
}
} else if (stateP.controlState === 'CLONE_TEMP_TO_TARGET') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return {
...stateP,
controlState: 'OUTDATED_DOCUMENTS_SEARCH',
};
} else {
const left = res.left;
if (isLeftTypeof(left, 'index_not_found_exception')) {
// index_not_found_exception means another instance alread completed
// the MARK_VERSION_INDEX_READY step and removed the temp index
// We still perform the OUTDATED_DOCUMENTS_* and
// UPDATE_TARGET_MAPPINGS steps since we might have plugins enabled
// which the other instances don't.
return {
...stateP,
controlState: 'OUTDATED_DOCUMENTS_SEARCH',
};
} else {
throwBadResponse(stateP, left);
}
}
} else if (stateP.controlState === 'OUTDATED_DOCUMENTS_SEARCH') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
// If outdated documents were found, transform them
if (res.right.outdatedDocuments.length > 0) {
return {
...stateP,
controlState: 'OUTDATED_DOCUMENTS_TRANSFORM',
outdatedDocuments: res.right.outdatedDocuments,
};
} else {
// If there are no more results we have transformed all outdated
// documents and can proceed to the next step
return {
...stateP,
controlState: 'UPDATE_TARGET_MAPPINGS',
};
}
} else {
throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'OUTDATED_DOCUMENTS_TRANSFORM') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return {
...stateP,
controlState: 'OUTDATED_DOCUMENTS_SEARCH',
};
} else {
throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'UPDATE_TARGET_MAPPINGS') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return {
...stateP,
controlState: 'UPDATE_TARGET_MAPPINGS_WAIT_FOR_TASK',
updateTargetMappingsTaskId: res.right.taskId,
};
} else {
throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'UPDATE_TARGET_MAPPINGS_WAIT_FOR_TASK') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
if (Option.isSome(stateP.versionIndexReadyActions)) {
// If there are some versionIndexReadyActions we performed a full
// migration and need to point the aliases to our newly migrated
// index.
return {
...stateP,
controlState: 'MARK_VERSION_INDEX_READY',
versionIndexReadyActions: stateP.versionIndexReadyActions,
};
} else {
// If there are none versionIndexReadyActions another instance
// already completed this migration and we only transformed outdated
// documents and updated the mappings for incase a new plugin was
// enabled.
return {
...stateP,
controlState: 'DONE',
};
}
} else {
const left = res.left;
if (isLeftTypeof(left, 'wait_for_task_completion_timeout')) {
// After waiting for the specificed timeout, the task has not yet
// completed. Retry this step to see if the task has completed after an
// exponential delay. We will basically keep polling forever until the
// Elasticeasrch task succeeds or fails.
return delayRetryState(stateP, res.left.message, Number.MAX_SAFE_INTEGER);
} else {
throwBadResponse(stateP, left);
}
}
} else if (stateP.controlState === 'CREATE_NEW_TARGET') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return {
...stateP,
controlState: 'MARK_VERSION_INDEX_READY',
};
} else {
// If the createIndex action receives an 'resource_already_exists_exception'
// it will wait until the index status turns green so we don't have any
// left responses to handle here.
throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'MARK_VERSION_INDEX_READY') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
return { ...stateP, controlState: 'DONE' };
} else {
const left = res.left;
if (isLeftTypeof(left, 'alias_not_found_exception')) {
// the versionIndexReadyActions checks that the currentAlias is still
// pointing to the source index. If this fails with an
// alias_not_found_exception another instance has completed a
// migration from the same source.
return { ...stateP, controlState: 'MARK_VERSION_INDEX_READY_CONFLICT' };
} else if (isLeftTypeof(left, 'index_not_found_exception')) {
if (left.index === stateP.tempIndex) {
// another instance has already completed the migration and deleted
// the temporary index
return { ...stateP, controlState: 'MARK_VERSION_INDEX_READY_CONFLICT' };
} else {
// The migration algorithm will never cause a
// index_not_found_exception for an index other than the temporary
// index handled above.
throwBadResponse(stateP, left as never);
}
} else if (isLeftTypeof(left, 'remove_index_not_a_concrete_index')) {
// We don't handle this error as the migration algorithm will never
// cause it to occur (this error is only relevant to the LEGACY_DELETE
// step).
throwBadResponse(stateP, left as never);
} else {
throwBadResponse(stateP, left);
}
}
} else if (stateP.controlState === 'MARK_VERSION_INDEX_READY_CONFLICT') {
// If another instance completed a migration from the same source we need
// to check that the completed migration was performed by a Kibana that's
// on the same version as this instance.
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
if (Either.isRight(res)) {
const indices = res.right;
const aliases = getAliases(indices);
if (
aliases[stateP.currentAlias] != null &&
aliases[stateP.versionAlias] != null &&
aliases[stateP.currentAlias] === aliases[stateP.versionAlias]
) {
// If the current and version aliases are pointing to the same index
// the migration was completed by another instance on the same version
// and it's safe to start serving traffic.
return { ...stateP, controlState: 'DONE' };
} else {
// Fail the migration, the instance that completed the migration is
// running a different version of Kibana. This avoids a situation where
// we loose acknowledged writes because two versions are both
// accepting writes, but are writing into difference indices.
const conflictingKibanaVersion =
indexVersion(aliases[stateP.currentAlias]) ?? aliases[stateP.currentAlias];
return {
...stateP,
controlState: 'FATAL',
reason: `Multiple versions of Kibana are attempting a migration in parallel. Another Kibana instance on version ${conflictingKibanaVersion} completed this migration (this instance is running ${stateP.kibanaVersion}). Ensure that all Kibana instances are running on same version and try again.`,
};
}
} else {
throwBadResponse(stateP, res);
}
} else if (stateP.controlState === 'DONE' || stateP.controlState === 'FATAL') {
// The state-action machine will never call the model in the terminating states
throwBadControlState(stateP as never);
} else {
return throwBadControlState(stateP);
}
};
/**
* Construct the initial state for the model
*/
export const createInitialState = ({
kibanaVersion,
targetMappings,
preMigrationScript,
migrationVersionPerType,
indexPrefix,
migrationsConfig,
}: {
kibanaVersion: string;
targetMappings: IndexMapping;
preMigrationScript?: string;
migrationVersionPerType: SavedObjectsMigrationVersion;
indexPrefix: string;
migrationsConfig: SavedObjectsMigrationConfigType;
}): InitState => {
const outdatedDocumentsQuery = {
bool: {
should: Object.entries(migrationVersionPerType).map(([type, latestVersion]) => ({
bool: {
must: { term: { type } },
must_not: { term: { [`migrationVersion.${type}`]: latestVersion } },
},
})),
},
};
const reindexTargetMappings: IndexMapping = {
dynamic: false,
properties: {
type: { type: 'keyword' },
migrationVersion: {
// @ts-expect-error we don't allow plugins to set `dynamic`
dynamic: 'true',
type: 'object',
},
},
};
const initialState: InitState = {
controlState: 'INIT',
indexPrefix,
legacyIndex: indexPrefix,
currentAlias: indexPrefix,
versionAlias: `${indexPrefix}_${kibanaVersion}`,
versionIndex: `${indexPrefix}_${kibanaVersion}_001`,
tempIndex: `${indexPrefix}_${kibanaVersion}_reindex_temp`,
kibanaVersion,
preMigrationScript: Option.fromNullable(preMigrationScript),
targetIndexMappings: targetMappings,
tempIndexMappings: reindexTargetMappings,
outdatedDocumentsQuery,
retryCount: 0,
retryDelay: 0,
retryAttempts: migrationsConfig.retryAttempts,
batchSize: migrationsConfig.batchSize,
logs: [],
};
return initialState;
};