Skip to content

Commit

Permalink
[Upgrade Assistant] Avoid creating the new index if it already exists…
Browse files Browse the repository at this point in the history
… when reindexing (#123817)
  • Loading branch information
sabarasaba authored and alisonelizabeth committed Jan 26, 2022
1 parent 4d3a792 commit f072fc6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit f072fc6

Please sign in to comment.