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

improve migration transform failure message and stacktrace #102478

Conversation

pgayvallet
Copy link
Contributor

@pgayvallet pgayvallet commented Jun 17, 2021

Summary

Fix #102336

  • Improve the overall display of the migration failure messages when either corrupted documents or transform errors were encountered
  • In case of transform error specifically:
    • log the version of the transform that caused the failure
    • include the original error's stacktrace in the thrown error.

Example of new stacktrace:

Unable to complete saved object migrations for the [.kibana] index: Migrations failed. Reason: 2 transformation errors were encountered:
     - foo:3: Error: Migration function for version 7.14.0 threw an error
        at transform ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:685:13)
        at migrateProp ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:761:22)
        at applyMigrations ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:498:20)
        at DocumentMigrator.transformDoc ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:436:22)
        at transform ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:246:53)
        at Immediate._onImmediate ([...]/kibana/src/core/server/saved_objects/migrations/core/migrate_raw_docs.ts:152:19)
        at processImmediate (internal/timers.js:462:21)
    Caused by:
    Error: "number" attribute should be present
        at migrationFn ([...]/kibana/src/core/server/saved_objects/migrationsv2/integration_tests/type_migration_failure.test.ts:75:19)
        at transform ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:674:22)
        at migrateProp ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:761:22)
        at applyMigrations ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:498:20)
        at DocumentMigrator.transformDoc ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:436:22)
        at transform ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:246:53)
        at Immediate._onImmediate ([...]/kibana/src/core/server/saved_objects/migrations/core/migrate_raw_docs.ts:152:19)
        at processImmediate (internal/timers.js:462:21)
    - foo:4: Error: Migration function for version 7.14.0 threw an error
        at transform ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:685:13)
        at migrateProp ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:761:22)
        at applyMigrations ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:498:20)
        at DocumentMigrator.transformDoc ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:436:22)
        at transform ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:246:53)
        at Immediate._onImmediate ([...]/kibana/src/core/server/saved_objects/migrations/core/migrate_raw_docs.ts:152:19)
        at processImmediate (internal/timers.js:462:21)
    Caused by:
    Error: "number" attribute should be present
        at migrationFn ([...]/kibana/src/core/server/saved_objects/migrationsv2/integration_tests/type_migration_failure.test.ts:75:19)
        at transform ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:674:22)
        at migrateProp ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:761:22)
        at applyMigrations ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:498:20)
        at DocumentMigrator.transformDoc ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:436:22)
        at transform ([...]/kibana/src/core/server/saved_objects/migrations/core/document_migrator.ts:246:53)
        at Immediate._onImmediate ([...]/kibana/src/core/server/saved_objects/migrations/core/migrate_raw_docs.ts:152:19)
        at processImmediate (internal/timers.js:462:21)
To allow migrations to proceed, please delete or fix these documents.

Checklist

@pgayvallet pgayvallet added Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc project:ResilientSavedObjectMigrations Reduce Kibana upgrade failures by making saved object migrations more resilient release_note:skip Skip the PR/issue when compiling release notes v7.14.0 labels Jun 17, 2021
Comment on lines +30 to +32
expect(stackLines[0]).toEqual(`Error: Migration function for version 8.0.0 threw an error`);
expect(stackLines[stackLength - 2]).toEqual(`Caused by:`);
expect(stackLines[stackLength - 1]).toEqual(`some stack trace`);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Asserting against a stack trace is always fun.

Comment on lines +20 to +22
const appendCauseStack = (error: Error, cause: Error) => {
error.stack = (error.stack ?? '') + `\nCaused by:\n${cause.stack ?? cause.message}`;
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, I choose to append the stack trace to the transform error instead of just replacing the top error's stack with the cause's stack. It felt like this made the most sense, as that way we still have the info about both errors, in case of.

(Also this is how languages properly handling nested errors are usually doing it, at least regarding the output)

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense 👍

Comment on lines +180 to +190
const expectMatchOrder = (lines: string[], patterns: FindInOrderPattern[]) => {
let lineIdx = 0;
let patternIdx = 0;

while (lineIdx < lines.length && patternIdx < patterns.length) {
const line = lines[lineIdx];
const pattern = patterns[patternIdx];
if (lineMatch(line, pattern)) {
patternIdx++;
}
lineIdx++;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A new interview exercice.

Comment on lines +100 to +108
expectMatchOrder(errorLines, [
{
mode: 'equal',
value: '- foo:3: Error: Migration function for version 7.14.0 threw an error',
},
{
mode: 'contain',
value: 'at transform',
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Best okay-ish approach I found to check the content of the error stack.

Comment on lines +867 to +868
"Migrations failed. Reason: 1 corrupt saved object documents were found: a:b
To allow migrations to proceed, please delete or fix these documents."
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, I did not handle singular/plural in the message, as it didn't really feel that important, but if we really want it, I could do it.

@pgayvallet pgayvallet marked this pull request as ready for review June 17, 2021 14:54
@pgayvallet pgayvallet requested a review from a team as a code owner June 17, 2021 14:54
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-core (Team:Core)

Copy link
Member

@Bamieh Bamieh left a comment

Choose a reason for hiding this comment

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

LGTM with a small nit in the comments

}
}

const appendCauseStack = (error: Error, cause: Error) => {
error.stack = (error.stack ?? '') + `\nCaused by:\n${cause.stack ?? cause.message}`;
Copy link
Member

Choose a reason for hiding this comment

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

if there is no error.stack we'd be starting with an empty new line \nCaused by:

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, OTOH until this function is extracted from this file, I think this is alright, as the only call to appendCauseStack is performed inside the constructor after the call to super, so we shouldn't really encounter a situation where the stack is empty (and we got unit tests to assert that)

Comment on lines +20 to +22
const appendCauseStack = (error: Error, cause: Error) => {
error.stack = (error.stack ?? '') + `\nCaused by:\n${cause.stack ?? cause.message}`;
};
Copy link
Member

Choose a reason for hiding this comment

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

Makes sense 👍

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@pgayvallet pgayvallet merged commit 0561648 into elastic:master Jun 21, 2021
pgayvallet added a commit to pgayvallet/kibana that referenced this pull request Jun 21, 2021
…02478)

* improve migration failure message and stacktrace

* remove console.log

* fix unit tests

* fix integration tests

* fix more unit tests
pgayvallet added a commit that referenced this pull request Jun 21, 2021
…2478) (#102702)

* improve migration transform failure message and stacktrace (#102478)

* improve migration failure message and stacktrace

* remove console.log

* fix unit tests

* fix integration tests

* fix more unit tests

* skip 8.0 test in `7.x`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
project:ResilientSavedObjectMigrations Reduce Kibana upgrade failures by making saved object migrations more resilient release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v7.14.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SO mig v2] log the root stack trace in case of document transform failures
4 participants