From 2d12a9dbebe6640d50485dc5ac55925c6eff1907 Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Thu, 27 Jan 2022 09:42:47 -0500 Subject: [PATCH] [Upgrade Assistant] Avoid creating the new index if it already exists when reindexing (#123817) (#123865) (#123922) (cherry picked from commit 481fb8f536880e09a73c5e35253b7bec8eacb22f) --- .../server/lib/reindexing/reindex_service.ts | 27 +++++++++++++------ .../upgrade_assistant/reindexing.js | 26 ++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts index db427161f50d36..724b282a877478 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts @@ -191,15 +191,26 @@ export const reindexServiceFactory = ( const { settings, mappings } = transformFlatSettings(flatSettings); - const { body: createIndex } = await esClient.indices.create({ - index: newIndexName, - body: { - settings, - mappings, - }, - }); + let createIndex; + try { + createIndex = await esClient.indices.create({ + index: newIndexName, + body: { + settings, + mappings, + }, + }); + } catch (err) { + // If for any reason the new index name generated by the `generateNewIndexName` already + // exists (this could happen if kibana is restarted during reindexing), we can just go + // ahead with the process without needing to create the index again. + // See: https://github.com/elastic/kibana/issues/123816 + if (err?.body?.error?.type !== 'resource_already_exists_exception') { + throw err; + } + } - if (!createIndex.acknowledged) { + if (createIndex && !createIndex?.body?.acknowledged) { throw error.cannotCreateIndex(`Index could not be created: ${newIndexName}`); } diff --git a/x-pack/test/upgrade_assistant_integration/upgrade_assistant/reindexing.js b/x-pack/test/upgrade_assistant_integration/upgrade_assistant/reindexing.js index 6a326840bc5510..635cb8e288ae12 100644 --- a/x-pack/test/upgrade_assistant_integration/upgrade_assistant/reindexing.js +++ b/x-pack/test/upgrade_assistant_integration/upgrade_assistant/reindexing.js @@ -83,6 +83,32 @@ export default function ({ getService }) { }); }); + it('can resume after reindexing was stopped right after creating the new index', async () => { + await esArchiver.load('x-pack/test/functional/es_archives/upgrade_assistant/reindex'); + + // This new index is the new soon to be created reindexed index. We create it + // upfront to simulate a situation in which the user restarted kibana half + // way through the reindex process and ended up with an extra index. + await es.indices.create({ index: 'reindexed-v7-dummydata' }); + + const { body } = await supertest + .post(`/api/upgrade_assistant/reindex/dummydata`) + .set('kbn-xsrf', 'xxx') + .expect(200); + + expect(body.indexName).to.equal('dummydata'); + expect(body.status).to.equal(ReindexStatus.inProgress); + + const lastState = await waitForReindexToComplete('dummydata'); + expect(lastState.errorMessage).to.equal(null); + expect(lastState.status).to.equal(ReindexStatus.completed); + + // Cleanup newly created index + await es.indices.delete({ + index: lastState.newIndexName, + }); + }); + it('should update any aliases', async () => { await esArchiver.load('x-pack/test/functional/es_archives/upgrade_assistant/reindex');