Skip to content

Commit

Permalink
[esArchiver] Update aliases after creating the indices (elastic#160584)
Browse files Browse the repository at this point in the history
Tackles elastic#158918

Updates `esArchiver` so that SO indices are created in two separate
steps:

`indices.create()` and `indices.updateAliases()`

This way, any Kibana requests that target SO indices (through their
aliases) will either find that the indices exist, or that they do not.

This is a less invasive approach than
elastic#159397, as it does not modify the
`esArchiver.load` flow (we don't delete the `mappings.json` files here).
  • Loading branch information
gsoldevila authored Jun 29, 2023
1 parent 679de55 commit 295b4d4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ describe('esArchiver: createCreateIndexStream()', () => {
body: {
settings: undefined,
mappings: undefined,
aliases: { foo: {} },
},
});

sinon.assert.calledWith(client.indices.updateAliases as sinon.SinonSpy, {
body: {
actions: [{ add: { alias: 'foo', index: 'index' } }],
},
});
});
Expand Down Expand Up @@ -299,6 +304,7 @@ describe('esArchiver: createCreateIndexStream()', () => {
]);

sinon.assert.notCalled(client.indices.create as sinon.SinonSpy);
sinon.assert.notCalled(client.indices.updateAliases as sinon.SinonSpy);
expect(output).toEqual([createStubDocRecord('index', 1)]);
});
});
Expand All @@ -310,8 +316,8 @@ describe('esArchiver: createCreateIndexStream()', () => {

await createPromiseFromStreams([
createListStream([
createStubIndexRecord('new-index'),
createStubIndexRecord('existing-index'),
createStubIndexRecord('new-index', { 'new-index-alias': {} }),
createStubIndexRecord('existing-index', { 'existing-index-alias': {} }),
]),
createCreateIndexStream({
client,
Expand All @@ -331,6 +337,12 @@ describe('esArchiver: createCreateIndexStream()', () => {
'index',
'new-index'
);

// only update aliases for the 'new-index'
sinon.assert.callCount(client.indices.updateAliases as sinon.SinonSpy, 1);
expect((client.indices.updateAliases as sinon.SinonSpy).args[0][0]).toHaveProperty('body', {
actions: [{ add: { alias: 'new-index-alias', index: 'new-index' } }],
});
});

it('filters documents for skipped indices', async () => {
Expand Down
17 changes: 16 additions & 1 deletion packages/kbn-es-archiver/src/lib/indices/create_index_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,35 @@ export function createCreateIndexStream({
log.debug(`Deleted saved object index [${index}]`);
}

// create the index without the aliases
await client.indices.create(
{
index,
body: {
settings,
mappings,
aliases,
},
},
{
headers: ES_CLIENT_HEADERS,
}
);

// create the aliases on a separate step (see https://github.com/elastic/kibana/issues/158918)
const actions: estypes.IndicesUpdateAliasesAction[] = Object.keys(aliases ?? {}).map(
(alias) => ({
add: {
index,
alias,
...aliases![alias],
},
})
);

if (actions.length) {
await client.indices.updateAliases({ body: { actions } });
}

stats.createdIndex(index, { settings });
} catch (err) {
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ export default function ({ getService }: FtrProviderContext) {
return tests.flat();
};

// FLAKY: https://github.com/elastic/kibana/issues/158586
// Also, https://github.com/elastic/kibana/issues/158918
// esArchiver fails with no_shard_available_action_exception after deleting indexes
describe.skip('_import', () => {
describe('_import', () => {
getTestScenarios([
[false, false],
[false, true],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ export default function ({ getService }: FtrProviderContext) {
return createTestDefinitions(testCases, false, { spaceId });
};

// FLAKY: https://github.com/elastic/kibana/issues/156998, https://github.com/elastic/kibana/issues/156922, https://github.com/elastic/kibana/issues/156921
// Also, https://github.com/elastic/kibana/issues/158918
// esArchiver fails with no_shard_available_action_exception after deleting indexes
describe.skip('_resolve', () => {
describe('_resolve', () => {
getTestScenarios().spaces.forEach(({ spaceId }) => {
const tests = createTests(spaceId);
addTests(`within the ${spaceId} space`, { spaceId, tests });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ export default function ({ getService }: FtrProviderContext) {
return createTestDefinitions(testCases, false, { overwrite, spaceId, singleRequest });
};

// FLAKY: https://github.com/elastic/kibana/issues/155846, https://github.com/elastic/kibana/issues/156045, https://github.com/elastic/kibana/issues/156041
// Also, https://github.com/elastic/kibana/issues/158918
// esArchiver fails with no_shard_available_action_exception after deleting indexes
describe.skip('_resolve_import_errors', () => {
describe('_resolve_import_errors', () => {
getTestScenarios([
[false, false],
[false, true],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ export default function ({ getService }: FtrProviderContext) {
return createTestDefinitions(testCases, false);
};

// FLAKY: https://github.com/elastic/kibana/issues/157452
describe.skip('_update', () => {
describe('_update', () => {
getTestScenarios().spaces.forEach(({ spaceId }) => {
const tests = createTests(spaceId);
addTests(`within the ${spaceId} space`, { spaceId, tests });
Expand Down

0 comments on commit 295b4d4

Please sign in to comment.