From ef94118492e09e1563c3a6105511599b5b0e9944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20C=C3=B4t=C3=A9?= Date: Thu, 25 Jul 2019 19:21:56 -0400 Subject: [PATCH] Only provide obsoleteIndexTemplatePattern to the default index migrator to avoid race conditions (#42016) * Only provide obsoleteIndexTemplatePattern to the default index migrator to avoid race conditions * Add test * Apply PR feedback --- .../migrations/kibana/kibana_migrator.test.ts | 24 +++++++++++++++++++ .../migrations/kibana/kibana_migrator.ts | 7 ++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts index 7237d62dca6e2..9fc8afd356043 100644 --- a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts @@ -84,6 +84,30 @@ describe('KibanaMigrator', () => { const migrationResults = await new KibanaMigrator({ kbnServer }).awaitMigration(); expect(migrationResults.length).toEqual(2); }); + + it('only handles and deletes index templates once', async () => { + const { kbnServer } = mockKbnServer(); + const clusterStub = jest.fn(() => ({ status: 404 })); + const waitUntilReady = jest.fn(async () => undefined); + + kbnServer.server.plugins.elasticsearch = { + waitUntilReady, + getCluster() { + return { + callWithInternalUser: clusterStub, + }; + }, + }; + + await new KibanaMigrator({ kbnServer }).awaitMigration(); + + // callCluster with "cat.templates" is called by "deleteIndexTemplates" function + // and should only be done once + const callClusterCommands = clusterStub.mock.calls + .map(([callClusterPath]) => callClusterPath) + .filter(callClusterPath => callClusterPath === 'cat.templates'); + expect(callClusterCommands.length).toBe(1); + }); }); }); diff --git a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts index f51c15f882a4e..0c2cb768fc011 100644 --- a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts +++ b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts @@ -93,8 +93,9 @@ export class KibanaMigrator { await server.plugins.elasticsearch.waitUntilReady(); const config = server.config(); + const kibanaIndexName = config.get('kibana.index'); const indexMap = createIndexMap( - config.get('kibana.index'), + kibanaIndexName, this.kbnServer.uiExports.savedObjectSchemas, this.mappingProperties ); @@ -110,7 +111,9 @@ export class KibanaMigrator { pollInterval: config.get('migrations.pollInterval'), scrollDuration: config.get('migrations.scrollDuration'), serializer: this.serializer, - obsoleteIndexTemplatePattern: 'kibana_index_template*', + // Only necessary for the migrator of the kibana index. + obsoleteIndexTemplatePattern: + index === kibanaIndexName ? 'kibana_index_template*' : undefined, convertToAliasScript: indexMap[index].script, }); });