Skip to content

Commit

Permalink
Handle "other indices not found" conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
gsoldevila committed Jun 4, 2023
1 parent fa263ee commit 3c50588
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import type { AliasAction, FetchIndexResponse } from '../actions';
import type { BulkIndexOperationTuple } from './create_batches';
import { OutdatedDocumentsSearchRead, ReindexSourceToTempRead } from '../state';

/** @internal */
export const REINDEX_TEMP_SUFFIX = '_reindex_temp';

/** @internal */
export type Aliases = Partial<Record<string, string>>;

Expand Down Expand Up @@ -309,7 +312,7 @@ export function getMigrationType({
* @returns A temporary index name to reindex documents
*/
export const getTempIndexName = (indexPrefix: string, kibanaVersion: string): string =>
`${indexPrefix}_${kibanaVersion}_reindex_temp`;
`${indexPrefix}_${kibanaVersion}${REINDEX_TEMP_SUFFIX}`;

/** Increase batchSize by 20% until a maximum of maxBatchSize */
export const increaseBatchSize = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
increaseBatchSize,
hasLaterVersionAlias,
aliasVersion,
REINDEX_TEMP_SUFFIX,
} from './helpers';
import { buildTempIndexMap, createBatches } from './create_batches';
import type { MigrationLog } from '../types';
Expand Down Expand Up @@ -1541,7 +1542,7 @@ export const model = (currentState: State, resW: ResponseType<AllActionStates>):
// migration from the same source.
return { ...stateP, controlState: 'MARK_VERSION_INDEX_READY_CONFLICT' };
} else if (isTypeof(left, 'index_not_found_exception')) {
if (left.index === stateP.tempIndex) {
if (left.index.endsWith(REINDEX_TEMP_SUFFIX)) {
// another instance has already completed the migration and deleted
// the temporary index
return { ...stateP, controlState: 'MARK_VERSION_INDEX_READY_CONFLICT' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
type ISavedObjectTypeRegistry,
type SavedObjectsType,
MAIN_SAVED_OBJECT_INDEX,
ALL_SAVED_OBJECT_INDICES,
} from '@kbn/core-saved-objects-server';
import { DEFAULT_INDEX_TYPES_MAP } from '@kbn/core-saved-objects-base-server-internal';
import {
Expand Down Expand Up @@ -406,6 +407,7 @@ describe('split .kibana index into multiple system indices', () => {
});

// FLAKY: https://github.com/elastic/kibana/issues/157510
// This test takes too long. Can be manually executed to verify the correct behavior.
describe.skip('when multiple Kibana migrators run in parallel', () => {
it('correctly migrates 7.7.2_xpack_100k_obj.zip archive', async () => {
esServer = await startElasticsearch({
Expand All @@ -414,15 +416,29 @@ describe('split .kibana index into multiple system indices', () => {
const esClient = await getEsClient();

const breakdownBefore = await getAggregatedTypesCountAllIndices(esClient);
expect(breakdownBefore).toMatchSnapshot('before migration');
expect(breakdownBefore).toEqual({
'.kibana': {
'apm-telemetry': 1,
config: 1,
dashboard: 52994,
'index-pattern': 1,
search: 1,
space: 1,
'ui-metric': 5,
visualization: 53004,
},
'.kibana_task_manager': {
task: 5,
},
});

for (let i = 0; i < PARALLEL_MIGRATORS; ++i) {
await clearLog(Path.join(__dirname, `dot_kibana_split_instance_${i}.log`));
}

const testKits = await Promise.all(
new Array(PARALLEL_MIGRATORS)
.fill({
new Array(PARALLEL_MIGRATORS).fill(true).map((_, index) =>
getKibanaMigratorTestKit({
settings: {
migrations: {
discardUnknownObjects: currentVersion,
Expand All @@ -431,13 +447,10 @@ describe('split .kibana index into multiple system indices', () => {
},
kibanaIndex: MAIN_SAVED_OBJECT_INDEX,
types: typeRegistry.getAllTypes(),
defaultIndexTypesMap: DEFAULT_INDEX_TYPES_MAP,
logFilePath: Path.join(__dirname, `dot_kibana_split_instance_${index}.log`),
})
.map((config, index) =>
getKibanaMigratorTestKit({
...config,
logFilePath: Path.join(__dirname, `dot_kibana_split_instance_${index}.log`),
})
)
)
);

const results = await Promise.all(testKits.map((testKit) => testKit.runMigrations()));
Expand All @@ -447,9 +460,30 @@ describe('split .kibana index into multiple system indices', () => {
.every((result) => result.status === 'migrated' || result.status === 'patched')
).toEqual(true);

await esClient.indices.refresh({ index: ALL_SAVED_OBJECT_INDICES });

const breakdownAfter = await getAggregatedTypesCountAllIndices(esClient);
expect(breakdownAfter).toMatchSnapshot('after migration');
});
expect(breakdownAfter).toEqual({
'.kibana': {
'apm-telemetry': 1,
config: 1,
space: 1,
'ui-metric': 5,
},
'.kibana_alerting_cases': {},
'.kibana_analytics': {
dashboard: 52994,
'index-pattern': 1,
search: 1,
visualization: 53004,
},
'.kibana_ingest': {},
'.kibana_security_solution': {},
'.kibana_task_manager': {
task: 5,
},
});
}, 1200000);

afterEach(async () => {
await esServer?.stop();
Expand Down

0 comments on commit 3c50588

Please sign in to comment.