Skip to content

Commit

Permalink
initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jun 24, 2021
1 parent 2368e63 commit 927f8ec
Show file tree
Hide file tree
Showing 12 changed files with 478 additions and 274 deletions.
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,72 @@
/*
* 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 { 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 CheckForUnknownDocsResponse {
unknownDocIds: string[];
}

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

return client
.search({
index: indexName,
body: {
query,
},
})
.then((response) => {
const { hits } = response.body.hits;
return Either.right({
unknownDocIds: hits.map((hit) => hit._id),
});
})
.catch(catchRetryableEsClientErrors);
};

const createUnknownDocQuery = (
unusedTypesQuery: estypes.QueryDslQueryContainer,
knownTypes: string[]
): estypes.QueryDslQueryContainer => {
return {
bool: {
must: unusedTypesQuery,
must_not: knownTypes.map((type) => ({
term: {
type,
},
})),
},
};
};
11 changes: 11 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,12 @@ export type {
} from './update_and_pickup_mappings';
export { updateAndPickupMappings } from './update_and_pickup_mappings';

export type {
CheckForUnknownDocsParams,
CheckForUnknownDocsResponse,
} 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 +104,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 +118,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

0 comments on commit 927f8ec

Please sign in to comment.