[Upgrade Assistant] Avoid creating the new index if it already exists when reindexing (#123817) (#123865)

This commit is contained in:
Alison Goryachev 2022-01-27 08:16:35 -05:00 committed by GitHub
parent c5fa8e2be4
commit 481fb8f536
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 8 deletions

View file

@ -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}`);
}

View file

@ -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');