Skip to content

Commit

Permalink
add unknown types to log message
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jun 25, 2021
1 parent c367717 commit 270f9b4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = ({
Expand All @@ -40,7 +47,7 @@ export const checkForUnknownDocs = ({
const query = createUnknownDocQuery(unusedTypesQuery, knownTypes);

return client
.search({
.search<SavedObjectsRawDocSource>({
index: indexName,
body: {
query,
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\\" } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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('') +
`'`
);
}
6 changes: 3 additions & 3 deletions src/core/server/saved_objects/migrationsv2/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ export const model = (currentState: State, resW: ResponseType<AllActionStates>):
} else if (stateP.controlState === 'CHECK_UNKNOWN_DOCUMENTS') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;
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;
Expand Down

0 comments on commit 270f9b4

Please sign in to comment.