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

[SoMigV2] Fail fast if unknown document types are present in the source index #103341

Merged
Merged
Show file tree
Hide file tree
Changes from 11 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 @@ -192,6 +192,7 @@ export class KibanaMigrator {
migrationVersionPerType: this.documentMigrator.migrationVersion,
indexPrefix: index,
migrationsConfig: this.soMigrationsConfig,
typeRegistry: this.typeRegistry,
});
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import * as Either from 'fp-ts/lib/Either';
import * as TaskEither from 'fp-ts/lib/TaskEither';
import { estypes } from '@elastic/elasticsearch';
import type { SavedObjectsRawDocSource } from '../../serialization';
import { ElasticsearchClient } from '../../../elasticsearch';
import {
catchRetryableEsClientErrors,
RetryableEsClientError,
} from './catch_retryable_es_client_errors';

/** @internal */
export interface CheckForUnknownDocsParams {
client: ElasticsearchClient;
indexName: string;
unusedTypesQuery: estypes.QueryDslQueryContainer;
knownTypes: string[];
}

/** @internal */
export interface CheckForUnknownDocsResponseDoc {
id: string;
type: string;
}

/** @internal */
export interface CheckForUnknownDocsResponse {
unknownDocs: CheckForUnknownDocsResponseDoc[];
}

export const checkForUnknownDocs = ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this file get an explicit unit test?

client,
indexName,
unusedTypesQuery,
knownTypes,
}: CheckForUnknownDocsParams): TaskEither.TaskEither<
RetryableEsClientError,
CheckForUnknownDocsResponse
> => () => {
const query = createUnknownDocQuery(unusedTypesQuery, knownTypes);

return client
.search<SavedObjectsRawDocSource>({
index: indexName,
body: {
query,
},
})
.then((response) => {
const { hits } = response.body.hits;
return Either.right({
unknownDocs: hits.map((hit) => ({ id: hit._id, type: hit._source?.type ?? 'undefined' })),
});
})
.catch(catchRetryableEsClientErrors);
};

const createUnknownDocQuery = (
unusedTypesQuery: estypes.QueryDslQueryContainer,
knownTypes: string[]
): estypes.QueryDslQueryContainer => {
return {
bool: {
must: unusedTypesQuery,
must_not: knownTypes.map((type) => ({
term: {
type,
},
})),
},
};
Comment on lines +75 to +84
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included the unusedTypesQuery in the unknown doc queries, are we're not migrating them to the temp index, and as it would allow to 'remove' old types by unregistering them while adding them to the unused query.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean that users must remove them to continue the migration? If so, it might be considered as a breaking change even though we just fixed a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I mean the opposite actually: I'm excluding the objects matching the unusedTypesQuery, to avoid failing if any is encountered given than we don't migrate them anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, must usage is a bit misleading here because unusedTypesQuery contains another must_not inside.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, the whole unusedTypesQuery naming is misleading because it's actually an excludeUnusedTypesQuery

};
12 changes: 12 additions & 0 deletions src/core/server/saved_objects/migrationsv2/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ export type { ReindexResponse, ReindexParams } from './reindex';
export { reindex } from './reindex';

import type { IncompatibleMappingException } from './wait_for_reindex_task';

export { waitForReindexTask } from './wait_for_reindex_task';

export type { VerifyReindexParams } from './verify_reindex';
export { verifyReindex } from './verify_reindex';

import type { AliasNotFound, RemoveIndexNotAConcreteIndex } from './update_aliases';

export type { AliasAction, UpdateAliasesParams } from './update_aliases';
export { updateAliases } from './update_aliases';

Expand All @@ -78,6 +80,13 @@ export type {
} from './update_and_pickup_mappings';
export { updateAndPickupMappings } from './update_and_pickup_mappings';

export type {
CheckForUnknownDocsParams,
CheckForUnknownDocsResponse,
CheckForUnknownDocsResponseDoc,
} from './check_for_unknown_docs';
export { checkForUnknownDocs } from './check_for_unknown_docs';

export { waitForPickupUpdatedMappingsTask } from './wait_for_pickup_updated_mappings_task';

export type {
Expand All @@ -96,9 +105,11 @@ export interface IndexNotFound {
type: 'index_not_found_exception';
index: string;
}

export interface WaitForReindexTaskFailure {
readonly cause: { type: string; reason: string };
}

export interface TargetIndexHadWriteBlock {
type: 'target_index_had_write_block';
}
Expand All @@ -108,6 +119,7 @@ export interface AcknowledgeResponse {
acknowledged: boolean;
shardsAcknowledged: boolean;
}

// Map of left response 'type' string -> response interface
export interface ActionErrorTypeMap {
wait_for_task_completion_timeout: WaitForTaskCompletionTimeout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import * as Either from 'fp-ts/lib/Either';
import * as TaskEither from 'fp-ts/lib/TaskEither';
import { ElasticsearchClient } from '../../../elasticsearch';
Expand Down
7 changes: 6 additions & 1 deletion src/core/server/saved_objects/migrationsv2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import type { SavedObjectsMigrationVersion } from '../types';
import type { TransformRawDocs } from './types';
import { MigrationResult } from '../migrations/core';
import { next } from './next';
import { createInitialState, model } from './model';
import { model } from './model';
import { createInitialState } from './initial_state';
import { migrationStateActionMachine } from './migrations_state_action_machine';
import { SavedObjectsMigrationConfigType } from '../saved_objects_config';
import type { ISavedObjectTypeRegistry } from '../saved_objects_type_registry';

/**
* Migrates the provided indexPrefix index using a resilient algorithm that is
Expand All @@ -32,6 +34,7 @@ export async function runResilientMigrator({
migrationVersionPerType,
indexPrefix,
migrationsConfig,
typeRegistry,
}: {
client: ElasticsearchClient;
kibanaVersion: string;
Expand All @@ -42,6 +45,7 @@ export async function runResilientMigrator({
migrationVersionPerType: SavedObjectsMigrationVersion;
indexPrefix: string;
migrationsConfig: SavedObjectsMigrationConfigType;
typeRegistry: ISavedObjectTypeRegistry;
}): Promise<MigrationResult> {
const initialState = createInitialState({
kibanaVersion,
Expand All @@ -50,6 +54,7 @@ export async function runResilientMigrator({
migrationVersionPerType,
indexPrefix,
migrationsConfig,
typeRegistry,
});
return migrationStateActionMachine({
initialState,
Expand Down
Loading