-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Alerting] Handle when an Alerting Task fails due to its Alert object being deleted mid flight #63093
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
Merged
gmmorris
merged 13 commits into
elastic:master
from
gmmorris:alerting/deleted-alert-error
Apr 15, 2020
Merged
[Alerting] Handle when an Alerting Task fails due to its Alert object being deleted mid flight #63093
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
af4fa92
swallow 404 errors from Saved Object when they happen for an in fligh…
gmmorris 2cf8744
debug instead of swallow on 404s
gmmorris 4d9dbba
avoid rescheduling alert task if it fails to find the alert
gmmorris 9353f75
Merge branch 'master' into alerting/deleted-alert-error
elasticmachine 876dea5
avoid reliance on Boom and instead relly on SO helpers
gmmorris af7f011
Merge branch 'master' into alerting/deleted-alert-error
gmmorris 2eb2639
removed unused import
gmmorris c50ec7d
only log mesage when it isnt failure to find alert object
gmmorris 6813fa9
Merge branch 'master' into alerting/deleted-alert-error
gmmorris 435b487
handle 404s when removing a task behind an alert
gmmorris 636ff99
Merge branch 'master' into alerting/deleted-alert-error
gmmorris 522bd13
fixed flaky date behaviour
gmmorris c5cdea9
removed unused import
gmmorris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
x-pack/plugins/alerting/server/lib/delete_task_if_it_exists.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import uuid from 'uuid'; | ||
| import { taskManagerMock } from '../../../task_manager/server/mocks'; | ||
| import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; | ||
| import { deleteTaskIfItExists } from './delete_task_if_it_exists'; | ||
|
|
||
| describe('deleteTaskIfItExists', () => { | ||
| test('removes the task by its ID', async () => { | ||
| const tm = taskManagerMock.createStart(); | ||
| const id = uuid.v4(); | ||
|
|
||
| expect(await deleteTaskIfItExists(tm, id)).toBe(undefined); | ||
|
|
||
| expect(tm.remove).toHaveBeenCalledWith(id); | ||
| }); | ||
|
|
||
| test('handles 404 errors caused by the task not existing', async () => { | ||
| const tm = taskManagerMock.createStart(); | ||
| const id = uuid.v4(); | ||
|
|
||
| tm.remove.mockRejectedValue(SavedObjectsErrorHelpers.createGenericNotFoundError('task', id)); | ||
|
|
||
| expect(await deleteTaskIfItExists(tm, id)).toBe(undefined); | ||
|
|
||
| expect(tm.remove).toHaveBeenCalledWith(id); | ||
| }); | ||
|
|
||
| test('throws if any other errro is caused by task removal', async () => { | ||
| const tm = taskManagerMock.createStart(); | ||
| const id = uuid.v4(); | ||
|
|
||
| const error = SavedObjectsErrorHelpers.createInvalidVersionError(uuid.v4()); | ||
| tm.remove.mockRejectedValue(error); | ||
|
|
||
| expect(deleteTaskIfItExists(tm, id)).rejects.toBe(error); | ||
|
|
||
| expect(tm.remove).toHaveBeenCalledWith(id); | ||
| }); | ||
| }); |
17 changes: 17 additions & 0 deletions
17
x-pack/plugins/alerting/server/lib/delete_task_if_it_exists.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
| import { TaskManagerStartContract } from '../../../task_manager/server'; | ||
| import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; | ||
|
|
||
| export async function deleteTaskIfItExists(taskManager: TaskManagerStartContract, taskId: string) { | ||
| try { | ||
| await taskManager.remove(taskId); | ||
| } catch (err) { | ||
| if (!SavedObjectsErrorHelpers.isNotFoundError(err)) { | ||
| throw err; | ||
| } | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
x-pack/plugins/alerting/server/lib/is_alert_not_found_error.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { isAlertSavedObjectNotFoundError } from './is_alert_not_found_error'; | ||
| import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; | ||
| import uuid from 'uuid'; | ||
|
|
||
| describe('isAlertSavedObjectNotFoundError', () => { | ||
| test('identifies SavedObjects Not Found errors', () => { | ||
| const id = uuid.v4(); | ||
| // ensure the error created by SO parses as a string with the format we expect | ||
| expect( | ||
| `${SavedObjectsErrorHelpers.createGenericNotFoundError('alert', id)}`.includes(`alert/${id}`) | ||
| ).toBe(true); | ||
|
|
||
| const errorBySavedObjectsHelper = SavedObjectsErrorHelpers.createGenericNotFoundError( | ||
| 'alert', | ||
| id | ||
| ); | ||
|
|
||
| expect(isAlertSavedObjectNotFoundError(errorBySavedObjectsHelper, id)).toBe(true); | ||
| }); | ||
|
|
||
| test('identifies generic errors', () => { | ||
| const id = uuid.v4(); | ||
| expect(isAlertSavedObjectNotFoundError(new Error(`not found`), id)).toBe(false); | ||
| }); | ||
| }); |
11 changes: 11 additions & 0 deletions
11
x-pack/plugins/alerting/server/lib/is_alert_not_found_error.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; | ||
|
|
||
| export function isAlertSavedObjectNotFoundError(err: Error, alertId: string) { | ||
| return SavedObjectsErrorHelpers.isNotFoundError(err) && `${err}`.includes(alertId); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
x-pack/plugins/task_manager/server/lib/is_task_not_found_error.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { isTaskSavedObjectNotFoundError } from './is_task_not_found_error'; | ||
| import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; | ||
| import uuid from 'uuid'; | ||
|
|
||
| describe('isTaskSavedObjectNotFoundError', () => { | ||
| test('identifies SavedObjects Not Found errors', () => { | ||
| const id = uuid.v4(); | ||
| // ensure the error created by SO parses as a string with the format we expect | ||
| expect( | ||
| `${SavedObjectsErrorHelpers.createGenericNotFoundError('task', id)}`.includes(`task/${id}`) | ||
| ).toBe(true); | ||
|
|
||
| const errorBySavedObjectsHelper = SavedObjectsErrorHelpers.createGenericNotFoundError( | ||
| 'task', | ||
| id | ||
| ); | ||
|
|
||
| expect(isTaskSavedObjectNotFoundError(errorBySavedObjectsHelper, id)).toBe(true); | ||
| }); | ||
|
|
||
| test('identifies generic errors', () => { | ||
| const id = uuid.v4(); | ||
| expect(isTaskSavedObjectNotFoundError(new Error(`not found`), id)).toBe(false); | ||
| }); | ||
| }); |
11 changes: 11 additions & 0 deletions
11
x-pack/plugins/task_manager/server/lib/is_task_not_found_error.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; | ||
|
|
||
| export function isTaskSavedObjectNotFoundError(err: Error, taskId: string) { | ||
| return SavedObjectsErrorHelpers.isNotFoundError(err) && `${err}`.includes(taskId); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels yucky enough that I feel like we should ask for a better helper - like being able to pass
alertIdinto thatisNotFoundError()function, rather than text searching the error message ourselves. ThealertIdvalues are UUIDs today, which seems safe enough to test for, but who knows what tomorrow holds - action id's can now be user-specified via pre-configured actions ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's fair, but as there seems to be pressure to get this backported to 7.7, I think we'll have to defer that.