From f1cff831a932e30485a40c02dc0ebc2585ecdf62 Mon Sep 17 00:00:00 2001 From: Bamieh Date: Sun, 4 Jun 2023 22:33:06 +0300 Subject: [PATCH 1/6] first pass on code --- .../src/actions/update_and_pickup_mappings.ts | 1 + .../src/kibana_migrator.ts | 9 ++- .../src/next.ts | 10 ++- .../src/run_resilient_migrator.ts | 5 +- .../group3/dot_kibana_split.test.ts | 72 +++++++++++++++++++ .../migrations/kibana_migrator_test_kit.ts | 72 ++++++++++++++++++- 6 files changed, 164 insertions(+), 5 deletions(-) diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/actions/update_and_pickup_mappings.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/actions/update_and_pickup_mappings.ts index 58fd65c9718d0..46fa24d6a1017 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/actions/update_and_pickup_mappings.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/actions/update_and_pickup_mappings.ts @@ -18,6 +18,7 @@ import { import { pickupUpdatedMappings } from './pickup_updated_mappings'; import { DEFAULT_TIMEOUT } from './constants'; + /** @internal */ export interface UpdateAndPickupMappingsResponse { taskId: string; diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/kibana_migrator.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/kibana_migrator.ts index 828a873648d70..624477da51464 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/kibana_migrator.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/kibana_migrator.ts @@ -16,6 +16,7 @@ import Semver from 'semver'; import type { NodeRoles } from '@kbn/core-node-server'; import type { Logger } from '@kbn/logging'; import type { DocLinksServiceStart } from '@kbn/core-doc-links-server'; +import type { State } from './state'; import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; import { MAIN_SAVED_OBJECT_INDEX, @@ -79,6 +80,7 @@ export class KibanaMigrator implements IKibanaMigrator { private readonly defaultIndexTypesMap: IndexTypesMap; private readonly nodeRoles: NodeRoles; public readonly kibanaVersion: string; + public readonly stateStatus$: BehaviorSubject; /** * Creates an instance of KibanaMigrator. @@ -116,6 +118,7 @@ export class KibanaMigrator implements IKibanaMigrator { this.activeMappings = buildActiveMappings(this.mappingProperties); this.docLinks = docLinks; this.defaultIndexTypesMap = defaultIndexTypesMap; + this.stateStatus$ = new BehaviorSubject(null); } public runMigrations({ rerun = false }: { rerun?: boolean } = {}): Promise { @@ -145,6 +148,10 @@ export class KibanaMigrator implements IKibanaMigrator { return this.status$.asObservable(); } + public getStateStatus$() { + return this.stateStatus$.asObservable(); + } + private async runMigrationsInternal(): Promise { const migrationAlgorithm = this.soMigrationsConfig.algorithm; if (migrationAlgorithm === 'zdt') { @@ -225,7 +232,6 @@ export class KibanaMigrator implements IKibanaMigrator { const doneReindexing = doneReindexingDefers[indexName]; // check if this migrator's index is involved in some document redistribution const mustRelocateDocuments = !!readyToReindex; - return runResilientMigrator({ client: this.client, kibanaVersion: this.kibanaVersion, @@ -255,6 +261,7 @@ export class KibanaMigrator implements IKibanaMigrator { migrationsConfig: this.soMigrationsConfig, typeRegistry: this.typeRegistry, docLinks: this.docLinks, + stateStatus$: this.stateStatus$, }); }, }; diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts index 1b5a9fe99fe3a..3f35deb48741f 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts @@ -7,6 +7,7 @@ */ import * as Option from 'fp-ts/lib/Option'; +import * as Rx from 'rxjs'; import { omit } from 'lodash'; import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; import type { Defer } from './kibana_migrator_utils'; @@ -75,6 +76,7 @@ export const nextActionMap = ( readyToReindex: Defer, doneReindexing: Defer ) => { + return { INIT: (state: InitState) => Actions.initAction({ client, indices: [state.currentAlias, state.versionAlias] }), @@ -240,7 +242,7 @@ export const nextActionMap = ( */ refresh: false, }), - MARK_VERSION_INDEX_READY: (state: MarkVersionIndexReady) => + MARK_VERSION_INDEX_READY: (state: MarkVersionIndexReady) => Actions.updateAliases({ client, aliasActions: state.versionIndexReadyActions.value }), MARK_VERSION_INDEX_READY_CONFLICT: (state: MarkVersionIndexReadyConflict) => Actions.fetchIndices({ client, indices: [state.currentAlias, state.versionAlias] }), @@ -273,7 +275,8 @@ export const next = ( client: ElasticsearchClient, transformRawDocs: TransformRawDocs, readyToReindex: Defer, - doneReindexing: Defer + doneReindexing: Defer, + stateStatus$?: Rx.BehaviorSubject, ) => { const map = nextActionMap(client, transformRawDocs, readyToReindex, doneReindexing); return (state: State) => { @@ -281,6 +284,7 @@ export const next = ( if (state.controlState === 'DONE' || state.controlState === 'FATAL') { // Return null if we're in one of the terminating states + stateStatus$?.complete(); return null; } else { // Otherwise return the delayed action @@ -290,6 +294,8 @@ export const next = ( const nextAction = map[state.controlState] as ( state: State ) => ReturnType; + stateStatus$?.next(state); + return delay(nextAction(state)); } }; diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_resilient_migrator.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_resilient_migrator.ts index 4438847890b54..a015b4c69b93c 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_resilient_migrator.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_resilient_migrator.ts @@ -8,6 +8,7 @@ import type { Logger } from '@kbn/logging'; import type { DocLinksServiceStart } from '@kbn/core-doc-links-server'; +import * as Rx from 'rxjs'; import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; import type { SavedObjectsMigrationVersion } from '@kbn/core-saved-objects-common'; import type { ISavedObjectTypeRegistry } from '@kbn/core-saved-objects-server'; @@ -63,6 +64,7 @@ export async function runResilientMigrator({ migrationsConfig, typeRegistry, docLinks, + stateStatus$, }: { client: ElasticsearchClient; kibanaVersion: string; @@ -81,6 +83,7 @@ export async function runResilientMigrator({ migrationsConfig: SavedObjectsMigrationConfigType; typeRegistry: ISavedObjectTypeRegistry; docLinks: DocLinksServiceStart; + stateStatus$?: Rx.BehaviorSubject, }): Promise { const initialState = createInitialState({ kibanaVersion, @@ -101,7 +104,7 @@ export async function runResilientMigrator({ return migrationStateActionMachine({ initialState, logger, - next: next(migrationClient, transformRawDocs, readyToReindex, doneReindexing), + next: next(migrationClient, transformRawDocs, readyToReindex, doneReindexing, stateStatus$), model, abort: async (state?: State) => { // At this point, we could reject this migrator's defers and unblock other migrators diff --git a/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts b/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts index a5a10cd05e574..4517b24991316 100644 --- a/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts +++ b/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts @@ -54,6 +54,72 @@ describe('split .kibana index into multiple system indices', () => { await clearLog(logFilePath); }); + describe('failure cases', () => { + it.only('successfully performs the migrations even if a migrator fails', async () => { + esServer = await startElasticsearch({ + dataArchive: Path.join(__dirname, '..', 'archives', '7.7.2_xpack_100k_obj.zip'), + }); + + const updatedTypeRegistry = overrideTypeRegistry( + typeRegistry, + (type: SavedObjectsType) => { + return { + ...type, + indexPattern: RELOCATE_TYPES[type.name] ?? MAIN_SAVED_OBJECT_INDEX, + }; + } + ); + + const migratorTestKitFactory = ({ failAfterStep }: { failAfterStep?: string }) => + getKibanaMigratorTestKit({ + types: updatedTypeRegistry.getAllTypes(), + kibanaIndex: '.kibana', + logFilePath, + failAfterStep, + }); + + + let firstRunFail = false; + let secondRunFail = false; + + try { + const { runMigrations } = await migratorTestKitFactory({ + failAfterStep: 'TRANSFORMED_DOCUMENTS_BULK_INDEX', + }); + await runMigrations(); + } catch(err) { + console.log('err::', err); + firstRunFail = true; + } + + expect(firstRunFail).toBe(true); + + try { + const { runMigrations } = await migratorTestKitFactory({}); + const results = await runMigrations(); + console.log('DONE!!@#!@'); + expect( + results + .flat() + .every((result) => result.status === 'migrated' || result.status === 'patched') + ).toEqual(true); + + } catch(err) { + console.log('err BADDDDD::', err); + secondRunFail = true; + } + expect(secondRunFail).toBe(false); + console.log('COMPLETE RUN!'); + + }); + + afterAll(async () => { + await esServer?.stop(); + await delay(2); + }); + }) + + describe('when migrating from a legacy version', () => { let migratorTestKitFactory: () => Promise; @@ -455,3 +521,9 @@ describe('split .kibana index into multiple system indices', () => { }); }); }); + + +// fail es at alias change (final step) +// fail at the update target mappings (modifying operations, firs 2 ops) +// fail at any step in the clone target mappings + // update target mappings properties diff --git a/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts b/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts index 43d5cc746a7e3..7e0c78cb4cc60 100644 --- a/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts +++ b/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts @@ -49,6 +49,7 @@ import type { DocLinksServiceStart } from '@kbn/core-doc-links-server'; import type { NodeRoles } from '@kbn/core-node-server'; import { baselineDocuments, baselineTypes } from './kibana_migrator_test_kit.fixtures'; import { delay } from './test_utils'; +import { Client } from '@elastic/elasticsearch'; export const defaultLogFilePath = Path.join(__dirname, 'kibana_migrator_test_kit.log'); @@ -74,6 +75,8 @@ export interface KibanaMigratorTestKitParams { settings?: Record; types?: Array>; logFilePath?: string; + esClientProxy?: (client: Client) => Client; + failAfterStep?: string; } export interface KibanaMigratorTestKit { @@ -123,6 +126,7 @@ export const getEsClient = async ({ return await getElasticsearchClient(configService, loggerFactory, kibanaVersion); }; + export const getKibanaMigratorTestKit = async ({ settings = {}, kibanaIndex = defaultKibanaIndex, @@ -131,8 +135,12 @@ export const getKibanaMigratorTestKit = async ({ types = [], logFilePath = defaultLogFilePath, nodeRoles = defaultNodeRoles, + failAfterStep, }: KibanaMigratorTestKitParams = {}): Promise => { let hasRun = false; + let hasComplete = false; + let reachedTargetFailureStep = false + let controlState: string | undefined; const loggingSystem = new LoggingSystem(); const loggerFactory = loggingSystem.asLoggerFactory(); @@ -142,7 +150,57 @@ export const getKibanaMigratorTestKit = async ({ const loggingConf = await firstValueFrom(configService.atPath('logging')); loggingSystem.upgrade(loggingConf); - const client = await getElasticsearchClient(configService, loggerFactory, kibanaVersion); + console.log('creating ES client with failAfterStep: ', failAfterStep); + const proxyClient = (rawClient: Client): Client => { + return new Proxy(rawClient, { + get(target, prop, receiver) { + if (!failAfterStep || !hasRun || hasComplete) { + return Reflect.get(...arguments); + } + if (reachedTargetFailureStep) { + throw new Error('SIMULATING ERROR'); + } + + console.log('prop::', prop); + console.log('stateStatus::', controlState); + + switch (prop) { + case 'child': { + return new Proxy(Reflect.get(...arguments), { + apply(target, thisArg, argumentsList) { + const childClient = rawClient.child(argumentsList[0]); + console.log('reflected proxy child'); + return proxyClient(childClient); + }, + }); + } + case 'indices': { + return new Proxy(Reflect.get(...arguments), { + get(target, prop, receiver) { + console.log('INSIDE CHILD!', hasComplete, prop); + if (!hasRun || hasComplete) { + return Reflect.get(...arguments); + } + + if (prop === 'putMapping') { + console.log('putMapping called'); + return Reflect.get(...arguments); + } + + return Reflect.get(...arguments); + } + }) + } + default: { + return Reflect.get(...arguments); + } + } + }, + }) + } + + const rawClient = await getElasticsearchClient(configService, loggerFactory, kibanaVersion); + const client = proxyClient(rawClient); const typeRegistry = new SavedObjectTypeRegistry(); @@ -159,6 +217,17 @@ export const getKibanaMigratorTestKit = async ({ kibanaBranch, nodeRoles ); + + if (failAfterStep) { + const stateStatus = migrator.getStateStatus$(); + stateStatus.subscribe((result) => { + controlState = result?.controlState; + if (controlState === failAfterStep) { + console.log(`>>>> reachedTargetFailureStep true at ${failAfterStep} <<<<`); + reachedTargetFailureStep = true; + } + }) + } const runMigrations = async () => { if (hasRun) { @@ -169,6 +238,7 @@ export const getKibanaMigratorTestKit = async ({ try { return await migrator.runMigrations(); } finally { + hasComplete = true; await loggingSystem.stop(); } }; From ae4cd4ceed90a67a8cd005663bc3035f7c2a56f5 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Sun, 4 Jun 2023 20:08:13 +0000 Subject: [PATCH 2/6] [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' --- .../src/actions/update_and_pickup_mappings.ts | 1 - .../src/kibana_migrator.ts | 2 +- .../src/next.ts | 5 ++-- .../src/run_resilient_migrator.ts | 2 +- .../group3/dot_kibana_split.test.ts | 19 +++++-------- .../migrations/kibana_migrator_test_kit.ts | 27 +++++++++---------- 6 files changed, 24 insertions(+), 32 deletions(-) diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/actions/update_and_pickup_mappings.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/actions/update_and_pickup_mappings.ts index 46fa24d6a1017..58fd65c9718d0 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/actions/update_and_pickup_mappings.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/actions/update_and_pickup_mappings.ts @@ -18,7 +18,6 @@ import { import { pickupUpdatedMappings } from './pickup_updated_mappings'; import { DEFAULT_TIMEOUT } from './constants'; - /** @internal */ export interface UpdateAndPickupMappingsResponse { taskId: string; diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/kibana_migrator.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/kibana_migrator.ts index 624477da51464..a58729c383c1a 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/kibana_migrator.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/kibana_migrator.ts @@ -16,7 +16,6 @@ import Semver from 'semver'; import type { NodeRoles } from '@kbn/core-node-server'; import type { Logger } from '@kbn/logging'; import type { DocLinksServiceStart } from '@kbn/core-doc-links-server'; -import type { State } from './state'; import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; import { MAIN_SAVED_OBJECT_INDEX, @@ -35,6 +34,7 @@ import { type MigrationResult, type IndexTypesMap, } from '@kbn/core-saved-objects-base-server-internal'; +import type { State } from './state'; import { getIndicesInvolvedInRelocation } from './kibana_migrator_utils'; import { buildActiveMappings, buildTypesMappings } from './core'; import { DocumentMigrator } from './document_migrator'; diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts index 3f35deb48741f..090060f4a8b88 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts @@ -76,7 +76,6 @@ export const nextActionMap = ( readyToReindex: Defer, doneReindexing: Defer ) => { - return { INIT: (state: InitState) => Actions.initAction({ client, indices: [state.currentAlias, state.versionAlias] }), @@ -242,7 +241,7 @@ export const nextActionMap = ( */ refresh: false, }), - MARK_VERSION_INDEX_READY: (state: MarkVersionIndexReady) => + MARK_VERSION_INDEX_READY: (state: MarkVersionIndexReady) => Actions.updateAliases({ client, aliasActions: state.versionIndexReadyActions.value }), MARK_VERSION_INDEX_READY_CONFLICT: (state: MarkVersionIndexReadyConflict) => Actions.fetchIndices({ client, indices: [state.currentAlias, state.versionAlias] }), @@ -276,7 +275,7 @@ export const next = ( transformRawDocs: TransformRawDocs, readyToReindex: Defer, doneReindexing: Defer, - stateStatus$?: Rx.BehaviorSubject, + stateStatus$?: Rx.BehaviorSubject ) => { const map = nextActionMap(client, transformRawDocs, readyToReindex, doneReindexing); return (state: State) => { diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_resilient_migrator.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_resilient_migrator.ts index a015b4c69b93c..6973ec9c253f7 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_resilient_migrator.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_resilient_migrator.ts @@ -83,7 +83,7 @@ export async function runResilientMigrator({ migrationsConfig: SavedObjectsMigrationConfigType; typeRegistry: ISavedObjectTypeRegistry; docLinks: DocLinksServiceStart; - stateStatus$?: Rx.BehaviorSubject, + stateStatus$?: Rx.BehaviorSubject; }): Promise { const initialState = createInitialState({ kibanaVersion, diff --git a/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts b/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts index 4517b24991316..9ee6ea82deb91 100644 --- a/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts +++ b/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts @@ -59,7 +59,7 @@ describe('split .kibana index into multiple system indices', () => { esServer = await startElasticsearch({ dataArchive: Path.join(__dirname, '..', 'archives', '7.7.2_xpack_100k_obj.zip'), }); - + const updatedTypeRegistry = overrideTypeRegistry( typeRegistry, (type: SavedObjectsType) => { @@ -69,7 +69,7 @@ describe('split .kibana index into multiple system indices', () => { }; } ); - + const migratorTestKitFactory = ({ failAfterStep }: { failAfterStep?: string }) => getKibanaMigratorTestKit({ types: updatedTypeRegistry.getAllTypes(), @@ -77,8 +77,7 @@ describe('split .kibana index into multiple system indices', () => { logFilePath, failAfterStep, }); - - + let firstRunFail = false; let secondRunFail = false; @@ -87,7 +86,7 @@ describe('split .kibana index into multiple system indices', () => { failAfterStep: 'TRANSFORMED_DOCUMENTS_BULK_INDEX', }); await runMigrations(); - } catch(err) { + } catch (err) { console.log('err::', err); firstRunFail = true; } @@ -103,22 +102,19 @@ describe('split .kibana index into multiple system indices', () => { .flat() .every((result) => result.status === 'migrated' || result.status === 'patched') ).toEqual(true); - - } catch(err) { + } catch (err) { console.log('err BADDDDD::', err); secondRunFail = true; } expect(secondRunFail).toBe(false); console.log('COMPLETE RUN!'); - }); afterAll(async () => { await esServer?.stop(); await delay(2); }); - }) - + }); describe('when migrating from a legacy version', () => { let migratorTestKitFactory: () => Promise; @@ -522,8 +518,7 @@ describe('split .kibana index into multiple system indices', () => { }); }); - // fail es at alias change (final step) // fail at the update target mappings (modifying operations, firs 2 ops) // fail at any step in the clone target mappings - // update target mappings properties +// update target mappings properties diff --git a/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts b/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts index 7e0c78cb4cc60..f4b1852f79124 100644 --- a/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts +++ b/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts @@ -47,9 +47,9 @@ import type { ISavedObjectsRepository } from '@kbn/core-saved-objects-api-server import { getDocLinks, getDocLinksMeta } from '@kbn/doc-links'; import type { DocLinksServiceStart } from '@kbn/core-doc-links-server'; import type { NodeRoles } from '@kbn/core-node-server'; +import { Client } from '@elastic/elasticsearch'; import { baselineDocuments, baselineTypes } from './kibana_migrator_test_kit.fixtures'; import { delay } from './test_utils'; -import { Client } from '@elastic/elasticsearch'; export const defaultLogFilePath = Path.join(__dirname, 'kibana_migrator_test_kit.log'); @@ -126,7 +126,6 @@ export const getEsClient = async ({ return await getElasticsearchClient(configService, loggerFactory, kibanaVersion); }; - export const getKibanaMigratorTestKit = async ({ settings = {}, kibanaIndex = defaultKibanaIndex, @@ -139,7 +138,7 @@ export const getKibanaMigratorTestKit = async ({ }: KibanaMigratorTestKitParams = {}): Promise => { let hasRun = false; let hasComplete = false; - let reachedTargetFailureStep = false + let reachedTargetFailureStep = false; let controlState: string | undefined; const loggingSystem = new LoggingSystem(); const loggerFactory = loggingSystem.asLoggerFactory(); @@ -160,7 +159,7 @@ export const getKibanaMigratorTestKit = async ({ if (reachedTargetFailureStep) { throw new Error('SIMULATING ERROR'); } - + console.log('prop::', prop); console.log('stateStatus::', controlState); @@ -181,23 +180,23 @@ export const getKibanaMigratorTestKit = async ({ if (!hasRun || hasComplete) { return Reflect.get(...arguments); } - + if (prop === 'putMapping') { console.log('putMapping called'); return Reflect.get(...arguments); } - + return Reflect.get(...arguments); - } - }) + }, + }); } default: { return Reflect.get(...arguments); } - } - }, - }) - } + } + }, + }); + }; const rawClient = await getElasticsearchClient(configService, loggerFactory, kibanaVersion); const client = proxyClient(rawClient); @@ -217,7 +216,7 @@ export const getKibanaMigratorTestKit = async ({ kibanaBranch, nodeRoles ); - + if (failAfterStep) { const stateStatus = migrator.getStateStatus$(); stateStatus.subscribe((result) => { @@ -226,7 +225,7 @@ export const getKibanaMigratorTestKit = async ({ console.log(`>>>> reachedTargetFailureStep true at ${failAfterStep} <<<<`); reachedTargetFailureStep = true; } - }) + }); } const runMigrations = async () => { From c889987d2ad4c41275f60bda2b16cbde3ecca13c Mon Sep 17 00:00:00 2001 From: Bamieh Date: Mon, 5 Jun 2023 01:10:58 +0300 Subject: [PATCH 3/6] tidy up code --- .../src/next.ts | 1 - .../src/zdt/context/create_context.ts | 2 ++ .../src/zdt/context/types.ts | 4 ++++ .../src/zdt/migrate_index.ts | 5 ++++- .../src/zdt/next.ts | 3 +++ .../src/zdt/run_zdt_migration.ts | 5 +++++ .../migrations/group3/dot_kibana_split.test.ts | 9 --------- 7 files changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts index 05ed9b0cc744a..004aab0780daf 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/next.ts @@ -295,7 +295,6 @@ export const next = ( state: State ) => ReturnType; stateStatus$?.next(state); - return delay(nextAction(state)); } }; diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/create_context.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/create_context.ts index 407966a7ba6e8..b3c986fc59a7f 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/create_context.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/create_context.ts @@ -27,6 +27,7 @@ export const createContext = ({ typeRegistry, serializer, nodeRoles, + stateStatus$, }: CreateContextOps): MigratorContext => { return { migrationConfig, @@ -44,5 +45,6 @@ export const createContext = ({ batchSize: migrationConfig.batchSize, discardCorruptObjects: Boolean(migrationConfig.discardCorruptObjects), nodeRoles, + stateStatus$, }; }; diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/types.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/types.ts index 35edbb464ee17..dfc5356840e5f 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/types.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/types.ts @@ -18,6 +18,8 @@ import type { } from '@kbn/core-saved-objects-base-server-internal'; import type { DocLinks } from '@kbn/doc-links'; import { VersionedTransformer } from '../../document_migrator'; +import type { BehaviorSubject } from 'rxjs'; +import type { State } from '../state'; /** * The set of static, precomputed values and services used by the ZDT migration @@ -53,4 +55,6 @@ export interface MigratorContext { readonly discardCorruptObjects: boolean; /** The node roles of the Kibana instance */ readonly nodeRoles: NodeRoles; + /** Subject that emits the current active step in the migrator state machine */ + stateStatus$?: BehaviorSubject; } diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/migrate_index.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/migrate_index.ts index 33f094c765d3b..cf0e34999f44e 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/migrate_index.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/migrate_index.ts @@ -17,13 +17,14 @@ import type { } from '@kbn/core-saved-objects-server'; import type { Logger } from '@kbn/logging'; import type { DocLinksServiceStart } from '@kbn/core-doc-links-server'; +import type { BehaviorSubject } from 'rxjs'; import { NodeRoles } from '@kbn/core-node-server'; import { migrationStateActionMachine } from './migration_state_action_machine'; import type { VersionedTransformer } from '../document_migrator'; import { createContext } from './context'; import { next } from './next'; import { model } from './model'; -import { createInitialState } from './state'; +import { createInitialState, State } from './state'; export interface MigrateIndexOptions { kibanaVersion: string; @@ -45,6 +46,8 @@ export interface MigrateIndexOptions { elasticsearchClient: ElasticsearchClient; /** The node roles of the Kibana instance */ readonly nodeRoles: NodeRoles; + /** Subject that emits the current active step in the migrator state machine */ + stateStatus$?: BehaviorSubject; } export const migrateIndex = async ({ diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/next.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/next.ts index 17749104d18fe..b761c57163681 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/next.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/next.ts @@ -193,6 +193,8 @@ export const next = (context: MigratorContext) => { if (state.controlState === 'DONE' || state.controlState === 'FATAL') { // Return null if we're in one of the terminating states + context.stateStatus$?.next(state); + context.stateStatus$?.complete()) return null; } else { // Otherwise return the delayed action @@ -202,6 +204,7 @@ export const next = (context: MigratorContext) => { const nextAction = map[state.controlState] as ( state: State ) => ReturnType; + context.stateStatus$?.next(state); return delay(nextAction(state)); } }; diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/run_zdt_migration.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/run_zdt_migration.ts index 8af4649711dbf..bbcdbd64dfcdb 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/run_zdt_migration.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/run_zdt_migration.ts @@ -10,6 +10,9 @@ import type { Logger } from '@kbn/logging'; import type { DocLinksServiceStart } from '@kbn/core-doc-links-server'; import type { NodeRoles } from '@kbn/core-node-server'; import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; +import type { BehaviorSubject } from 'rxjs'; +import type { State } from './state'; + import type { ISavedObjectTypeRegistry, ISavedObjectsSerializer, @@ -43,6 +46,8 @@ export interface RunZeroDowntimeMigrationOpts { elasticsearchClient: ElasticsearchClient; /** The node roles of the Kibana instance */ nodeRoles: NodeRoles; + /** Subject that emits the current active step in the migrator state machine */ + stateStatus$?: BehaviorSubject; } export const runZeroDowntimeMigration = async ( diff --git a/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts b/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts index 3fc916bd244f7..f2eb920115845 100644 --- a/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts +++ b/src/core/server/integration_tests/saved_objects/migrations/group3/dot_kibana_split.test.ts @@ -88,7 +88,6 @@ describe('split .kibana index into multiple system indices', () => { }); await runMigrations(); } catch (err) { - console.log('err::', err); firstRunFail = true; } @@ -97,18 +96,15 @@ describe('split .kibana index into multiple system indices', () => { try { const { runMigrations } = await migratorTestKitFactory({}); const results = await runMigrations(); - console.log('DONE!!@#!@'); expect( results .flat() .every((result) => result.status === 'migrated' || result.status === 'patched') ).toEqual(true); } catch (err) { - console.log('err BADDDDD::', err); secondRunFail = true; } expect(secondRunFail).toBe(false); - console.log('COMPLETE RUN!'); }); afterAll(async () => { @@ -520,8 +516,3 @@ describe('split .kibana index into multiple system indices', () => { }); }); }); - -// fail es at alias change (final step) -// fail at the update target mappings (modifying operations, firs 2 ops) -// fail at any step in the clone target mappings -// update target mappings properties From ad2c0c49099fcc5b741471c64a40cdeb7fcd62c2 Mon Sep 17 00:00:00 2001 From: Bamieh Date: Mon, 5 Jun 2023 01:12:51 +0300 Subject: [PATCH 4/6] remove extra code while testing --- .../src/zdt/next.ts | 2 +- .../migrations/kibana_migrator_test_kit.ts | 23 ------------------- 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/next.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/next.ts index b761c57163681..7edf3796049ff 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/next.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/next.ts @@ -194,7 +194,7 @@ export const next = (context: MigratorContext) => { if (state.controlState === 'DONE' || state.controlState === 'FATAL') { // Return null if we're in one of the terminating states context.stateStatus$?.next(state); - context.stateStatus$?.complete()) + context.stateStatus$?.complete(); return null; } else { // Otherwise return the delayed action diff --git a/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts b/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts index e9136c16060d2..23569a11ec9d6 100644 --- a/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts +++ b/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts @@ -152,7 +152,6 @@ export const getKibanaMigratorTestKit = async ({ const loggingConf = await firstValueFrom(configService.atPath('logging')); loggingSystem.upgrade(loggingConf); - console.log('creating ES client with failAfterStep: ', failAfterStep); const proxyClient = (rawClient: Client): Client => { return new Proxy(rawClient, { get(target, prop, receiver) { @@ -163,36 +162,15 @@ export const getKibanaMigratorTestKit = async ({ throw new Error('SIMULATING ERROR'); } - console.log('prop::', prop); - console.log('stateStatus::', controlState); - switch (prop) { case 'child': { return new Proxy(Reflect.get(...arguments), { apply(target, thisArg, argumentsList) { const childClient = rawClient.child(argumentsList[0]); - console.log('reflected proxy child'); return proxyClient(childClient); }, }); } - case 'indices': { - return new Proxy(Reflect.get(...arguments), { - get(target, prop, receiver) { - console.log('INSIDE CHILD!', hasComplete, prop); - if (!hasRun || hasComplete) { - return Reflect.get(...arguments); - } - - if (prop === 'putMapping') { - console.log('putMapping called'); - return Reflect.get(...arguments); - } - - return Reflect.get(...arguments); - }, - }); - } default: { return Reflect.get(...arguments); } @@ -226,7 +204,6 @@ export const getKibanaMigratorTestKit = async ({ stateStatus.subscribe((result) => { controlState = result?.controlState; if (controlState === failAfterStep) { - console.log(`>>>> reachedTargetFailureStep true at ${failAfterStep} <<<<`); reachedTargetFailureStep = true; } }); From 719b351588c27fcaad40ff4977829ad49c784d4f Mon Sep 17 00:00:00 2001 From: Bamieh Date: Mon, 5 Jun 2023 01:25:44 +0300 Subject: [PATCH 5/6] remove argumnets spread --- .../migrations/kibana_migrator_test_kit.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts b/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts index 23569a11ec9d6..1cdf2cbf63222 100644 --- a/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts +++ b/src/core/server/integration_tests/saved_objects/migrations/kibana_migrator_test_kit.ts @@ -156,23 +156,23 @@ export const getKibanaMigratorTestKit = async ({ return new Proxy(rawClient, { get(target, prop, receiver) { if (!failAfterStep || !hasRun || hasComplete) { - return Reflect.get(...arguments); + return Reflect.get(target, prop, receiver); } if (reachedTargetFailureStep) { - throw new Error('SIMULATING ERROR'); + throw new Error('SIMULATING ES CONNECTION ERROR'); } switch (prop) { case 'child': { - return new Proxy(Reflect.get(...arguments), { - apply(target, thisArg, argumentsList) { + return new Proxy(Reflect.get(target, prop, receiver), { + apply(_target, _thisArg, argumentsList) { const childClient = rawClient.child(argumentsList[0]); return proxyClient(childClient); }, }); } default: { - return Reflect.get(...arguments); + return Reflect.get(target, prop, receiver); } } }, From 92b92a3063250f637b5a4275eb228b8cfb692477 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Sun, 4 Jun 2023 22:56:22 +0000 Subject: [PATCH 6/6] [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' --- .../src/run_v2_migration.ts | 3 +-- .../src/zdt/context/types.ts | 2 +- .../src/zdt/run_zdt_migration.ts | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_v2_migration.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_v2_migration.ts index 13895b4b1edfb..172f14f2c9b4e 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_v2_migration.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/run_v2_migration.ts @@ -8,7 +8,6 @@ import type { Logger } from '@kbn/logging'; import { BehaviorSubject } from 'rxjs'; -import type { State } from './state'; import type { DocLinksServiceStart } from '@kbn/core-doc-links-server'; import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; import type { @@ -23,6 +22,7 @@ import type { SavedObjectsTypeMappingDefinitions, } from '@kbn/core-saved-objects-base-server-internal'; import Semver from 'semver'; +import type { State } from './state'; import type { DocumentMigrator } from './document_migrator'; import { buildActiveMappings, createIndexMap } from './core'; import { @@ -33,7 +33,6 @@ import { import { runResilientMigrator } from './run_resilient_migrator'; import { migrateRawDocsSafely } from './core/migrate_raw_docs'; - export interface RunV2MigrationOpts { /** The current Kibana version */ kibanaVersion: string; diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/types.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/types.ts index dfc5356840e5f..280cd83fdd023 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/types.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/context/types.ts @@ -17,8 +17,8 @@ import type { SavedObjectsMigrationConfigType, } from '@kbn/core-saved-objects-base-server-internal'; import type { DocLinks } from '@kbn/doc-links'; -import { VersionedTransformer } from '../../document_migrator'; import type { BehaviorSubject } from 'rxjs'; +import { VersionedTransformer } from '../../document_migrator'; import type { State } from '../state'; /** diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/run_zdt_migration.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/run_zdt_migration.ts index bbcdbd64dfcdb..2be504d2fdbcd 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/run_zdt_migration.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/zdt/run_zdt_migration.ts @@ -11,7 +11,6 @@ import type { DocLinksServiceStart } from '@kbn/core-doc-links-server'; import type { NodeRoles } from '@kbn/core-node-server'; import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; import type { BehaviorSubject } from 'rxjs'; -import type { State } from './state'; import type { ISavedObjectTypeRegistry, @@ -21,6 +20,7 @@ import { type SavedObjectsMigrationConfigType, type MigrationResult, } from '@kbn/core-saved-objects-base-server-internal'; +import type { State } from './state'; import type { VersionedTransformer } from '../document_migrator'; import { buildMigratorConfigs } from './utils'; import { migrateIndex } from './migrate_index';