From 270f9b483c0595be276aec8bb77dbc7bf2175c27 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Fri, 25 Jun 2021 11:50:49 +0200 Subject: [PATCH] add unknown types to log message --- .../actions/check_for_unknown_docs.ts | 13 ++++++++++--- .../migrationsv2/actions/index.ts | 1 + .../migrationsv2/model/extract_errors.test.ts | 18 +++++++++++++++--- .../migrationsv2/model/extract_errors.ts | 7 ++++--- .../saved_objects/migrationsv2/model/model.ts | 6 +++--- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/core/server/saved_objects/migrationsv2/actions/check_for_unknown_docs.ts b/src/core/server/saved_objects/migrationsv2/actions/check_for_unknown_docs.ts index 6fb6e3c3e8457..e38a820b89846 100644 --- a/src/core/server/saved_objects/migrationsv2/actions/check_for_unknown_docs.ts +++ b/src/core/server/saved_objects/migrationsv2/actions/check_for_unknown_docs.ts @@ -9,6 +9,7 @@ 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, @@ -23,9 +24,15 @@ export interface CheckForUnknownDocsParams { knownTypes: string[]; } +/** @internal */ +export interface CheckForUnknownDocsResponseDoc { + id: string; + type: string; +} + /** @internal */ export interface CheckForUnknownDocsResponse { - unknownDocIds: string[]; + unknownDocs: CheckForUnknownDocsResponseDoc[]; } export const checkForUnknownDocs = ({ @@ -40,7 +47,7 @@ export const checkForUnknownDocs = ({ const query = createUnknownDocQuery(unusedTypesQuery, knownTypes); return client - .search({ + .search({ index: indexName, body: { query, @@ -49,7 +56,7 @@ export const checkForUnknownDocs = ({ .then((response) => { const { hits } = response.body.hits; return Either.right({ - unknownDocIds: hits.map((hit) => hit._id), + unknownDocs: hits.map((hit) => ({ id: hit._id, type: hit._source?.type ?? 'undefined' })), }); }) .catch(catchRetryableEsClientErrors); diff --git a/src/core/server/saved_objects/migrationsv2/actions/index.ts b/src/core/server/saved_objects/migrationsv2/actions/index.ts index e0a154dce8068..a2ddfebbda262 100644 --- a/src/core/server/saved_objects/migrationsv2/actions/index.ts +++ b/src/core/server/saved_objects/migrationsv2/actions/index.ts @@ -83,6 +83,7 @@ export { updateAndPickupMappings } from './update_and_pickup_mappings'; export type { CheckForUnknownDocsParams, CheckForUnknownDocsResponse, + CheckForUnknownDocsResponseDoc, } from './check_for_unknown_docs'; export { checkForUnknownDocs } from './check_for_unknown_docs'; diff --git a/src/core/server/saved_objects/migrationsv2/model/extract_errors.test.ts b/src/core/server/saved_objects/migrationsv2/model/extract_errors.test.ts index c09883aeec911..52847c4cc7f69 100644 --- a/src/core/server/saved_objects/migrationsv2/model/extract_errors.test.ts +++ b/src/core/server/saved_objects/migrationsv2/model/extract_errors.test.ts @@ -11,12 +11,24 @@ import { extractUnknownDocFailureReason } from './extract_errors'; describe('extractUnknownDocFailureReason', () => { it('generates the correct error message', () => { expect( - extractUnknownDocFailureReason(['unknownType:12', 'anotherUnknownType:42'], '.kibana_15') + extractUnknownDocFailureReason( + [ + { + id: 'unknownType:12', + type: 'unknownType', + }, + { + id: 'anotherUnknownType:42', + type: 'anotherUnknownType', + }, + ], + '.kibana_15' + ) ).toMatchInlineSnapshot(` "Migration failed because documents from unknown types were found. To proceed with the migration, please delete these documents from the \\".kibana_15\\" index. The unknown documents were: - - \\"unknownType:12\\" - - \\"anotherUnknownType:42\\" + - \\"unknownType:12\\" (type: \\"unknownType\\") + - \\"anotherUnknownType:42\\" (type: \\"anotherUnknownType\\") You can delete them using the following command: curl -X POST \\"{elasticsearch}/.kibana_15/_bulk?pretty\\" -H 'Content-Type: application/json' -d' { \\"delete\\" : { \\"_id\\" : \\"unknownType:12\\" } } diff --git a/src/core/server/saved_objects/migrationsv2/model/extract_errors.ts b/src/core/server/saved_objects/migrationsv2/model/extract_errors.ts index e8ba1dc0b8d7d..6b64f260e4ab4 100644 --- a/src/core/server/saved_objects/migrationsv2/model/extract_errors.ts +++ b/src/core/server/saved_objects/migrationsv2/model/extract_errors.ts @@ -7,6 +7,7 @@ */ import { TransformErrorObjects } from '../../migrations/core'; +import { CheckForUnknownDocsResponseDoc } from '../actions'; /** * Constructs migration failure message strings from corrupt document ids and document transformation errors @@ -36,17 +37,17 @@ export function extractTransformFailuresReason( } export function extractUnknownDocFailureReason( - unknownDocIds: string[], + unknownDocs: CheckForUnknownDocsResponseDoc[], sourceIndex: string ): string { return ( `Migration failed because documents from unknown types were found. ` + `To proceed with the migration, please delete these documents from the "${sourceIndex}" index.\n` + `The unknown documents were:\n` + - unknownDocIds.map((docId) => `- "${docId}"\n`).join('') + + unknownDocs.map((doc) => `- "${doc.id}" (type: "${doc.type}")\n`).join('') + `You can delete them using the following command:\n` + `curl -X POST "{elasticsearch}/${sourceIndex}/_bulk?pretty" -H 'Content-Type: application/json' -d'\n` + - unknownDocIds.map((docId) => `{ "delete" : { "_id" : "${docId}" } }\n`).join('') + + unknownDocs.map((doc) => `{ "delete" : { "_id" : "${doc.id}" } }\n`).join('') + `'` ); } diff --git a/src/core/server/saved_objects/migrationsv2/model/model.ts b/src/core/server/saved_objects/migrationsv2/model/model.ts index b7c0b90c18914..8577ec6489e92 100644 --- a/src/core/server/saved_objects/migrationsv2/model/model.ts +++ b/src/core/server/saved_objects/migrationsv2/model/model.ts @@ -319,12 +319,12 @@ export const model = (currentState: State, resW: ResponseType): } else if (stateP.controlState === 'CHECK_UNKNOWN_DOCUMENTS') { const res = resW as ExcludeRetryableEsError>; if (Either.isRight(res)) { - const { unknownDocIds } = res.right; - if (unknownDocIds.length) { + const { unknownDocs } = res.right; + if (unknownDocs.length) { return { ...stateP, controlState: 'FATAL', - reason: extractUnknownDocFailureReason(unknownDocIds, stateP.sourceIndex.value), + reason: extractUnknownDocFailureReason(unknownDocs, stateP.sourceIndex.value), }; } else { const source = stateP.sourceIndex;