Skip to content

Commit

Permalink
Make CHECK_UNKONWN_DOCUMENTS step log warning instead of failing
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Jul 12, 2021
1 parent b2b57a2 commit 4aa3f3f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const logStateTransition = (
switch (level) {
case 'error':
return logger.error(logMessagePrefix + message);
case 'warning':
return logger.warn(logMessagePrefix + message);
case 'info':
return logger.info(logMessagePrefix + message);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('extractUnknownDocFailureReason', () => {
'.kibana_15'
)
).toMatchInlineSnapshot(`
"Migration failed because documents were found for unknown saved object types. To proceed with the migration, please delete these documents from the \\".kibana_15\\" index.
"Upgrades will fail for 8.0+ because documents were found for unknown saved object types. To ensure that upgrades will succeed in the future, either re-enable plugins or delete these documents from the \\".kibana_15\\" index after the current upgrade completes.
The documents with unknown types are:
- \\"unknownType:12\\" (type: \\"unknownType\\")
- \\"anotherUnknownType:42\\" (type: \\"anotherUnknownType\\")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ export function extractTransformFailuresReason(

export function extractUnknownDocFailureReason(
unknownDocs: CheckForUnknownDocsFoundDoc[],
sourceIndex: string
targetIndex: string
): string {
return (
`Migration failed because documents were found for unknown saved object types. ` +
`To proceed with the migration, please delete these documents from the "${sourceIndex}" index.\n` +
`Upgrades will fail for 8.0+ because documents were found for unknown saved object types. ` +
`To ensure that upgrades will succeed in the future, either re-enable plugins or delete these documents from the ` +
`"${targetIndex}" index after the current upgrade completes.\n` +
`The documents with unknown types are:\n` +
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` +
`curl -X POST "{elasticsearch}/${targetIndex}/_bulk?pretty" -H 'Content-Type: application/json' -d'\n` +
unknownDocs.map((doc) => `{ "delete" : { "_id" : "${doc.id}" } }\n`).join('') +
`'`
);
Expand Down
42 changes: 37 additions & 5 deletions src/core/server/saved_objects/migrationsv2/model/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ describe('migrations v2 model', () => {
`);
});

test('CHECK_UNKNOWN_DOCUMENTS -> FATAL if action fails and unknown docs were found', () => {
test('CHECK_UNKNOWN_DOCUMENTS -> SET_SOURCE_WRITE_BLOCK and adds logs if action fails and unknown docs were found', () => {
const checkUnknownDocumentsSourceState: CheckUnknownDocumentsState = {
...baseState,
controlState: 'CHECK_UNKNOWN_DOCUMENTS',
Expand All @@ -776,12 +776,44 @@ describe('migrations v2 model', () => {
],
});
const newState = model(checkUnknownDocumentsSourceState, res);
expect(newState.controlState).toEqual('FATAL');
expect(newState.controlState).toEqual('SET_SOURCE_WRITE_BLOCK');

expect(newState).toMatchObject({
controlState: 'FATAL',
reason: expect.stringContaining(
'Migration failed because documents were found for unknown saved object types'
controlState: 'SET_SOURCE_WRITE_BLOCK',
sourceIndex: Option.some('.kibana_3'),
targetIndex: '.kibana_7.11.0_001',
});

// This snapshot asserts that we disable the unknown saved object
// type. Because it's mappings are disabled, we also don't copy the
// `_meta.migrationMappingPropertyHashes` for the disabled type.
expect(newState.targetIndexMappings).toMatchInlineSnapshot(`
Object {
"_meta": Object {
"migrationMappingPropertyHashes": Object {
"new_saved_object_type": "4a11183eee21e6fbad864f7a30b39ad0",
},
},
"properties": Object {
"disabled_saved_object_type": Object {
"dynamic": false,
"properties": Object {},
},
"new_saved_object_type": Object {
"properties": Object {
"value": Object {
"type": "text",
},
},
},
},
}
`);

expect(newState.logs[0]).toMatchObject({
level: 'warning',
message: expect.stringContaining(
'Upgrades will fail for 8.0+ because documents were found for unknown saved object types'
),
});
});
Expand Down
53 changes: 32 additions & 21 deletions src/core/server/saved_objects/migrationsv2/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,31 +318,42 @@ export const model = (currentState: State, resW: ResponseType<AllActionStates>):
}
} else if (stateP.controlState === 'CHECK_UNKNOWN_DOCUMENTS') {
const res = resW as ExcludeRetryableEsError<ResponseType<typeof stateP.controlState>>;

const source = stateP.sourceIndex;
const target = stateP.versionIndex;
const nextState: State = {
...stateP,
controlState: 'SET_SOURCE_WRITE_BLOCK',
sourceIndex: source,
targetIndex: target,
targetIndexMappings: disableUnknownTypeMappingFields(
stateP.targetIndexMappings,
stateP.sourceIndexMappings
),
versionIndexReadyActions: Option.some<AliasAction[]>([
{ remove: { index: source.value, alias: stateP.currentAlias, must_exist: true } },
{ add: { index: target, alias: stateP.currentAlias } },
{ add: { index: target, alias: stateP.versionAlias } },
{ remove_index: { index: stateP.tempIndex } },
]),
};

if (Either.isRight(res)) {
const source = stateP.sourceIndex;
const target = stateP.versionIndex;
return {
...stateP,
controlState: 'SET_SOURCE_WRITE_BLOCK',
sourceIndex: source,
targetIndex: target,
targetIndexMappings: disableUnknownTypeMappingFields(
stateP.targetIndexMappings,
stateP.sourceIndexMappings
),
versionIndexReadyActions: Option.some<AliasAction[]>([
{ remove: { index: source.value, alias: stateP.currentAlias, must_exist: true } },
{ add: { index: target, alias: stateP.currentAlias } },
{ add: { index: target, alias: stateP.versionAlias } },
{ remove_index: { index: stateP.tempIndex } },
]),
};
return nextState;
} else {
if (isLeftTypeof(res.left, 'unknown_docs_found')) {
return {
...stateP,
controlState: 'FATAL',
reason: extractUnknownDocFailureReason(res.left.unknownDocs, stateP.sourceIndex.value),
...nextState,
logs: [
...nextState.logs,
{
level: 'warning',
message: `CHECK_UNKNOWN_DOCUMENTS ${extractUnknownDocFailureReason(
res.left.unknownDocs,
target
)}`,
},
],
};
} else {
return throwBadResponse(stateP, res.left);
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/migrationsv2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
DocumentsTransformSuccess,
} from '../migrations/core/migrate_raw_docs';

export type MigrationLogLevel = 'error' | 'info';
export type MigrationLogLevel = 'error' | 'info' | 'warning';

export interface MigrationLog {
level: MigrationLogLevel;
Expand Down

0 comments on commit 4aa3f3f

Please sign in to comment.