Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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