-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Alerting] Handle when an Alerting Task fails due to its Alert object…
… being deleted mid flight (#63093) (#63569) Detects if a task run failed due to the task SO being deleted mid flight and if so writes debug logs instead of warnings. Detects if an Alerting task run failed due to the alert SO being deleted mid flight of the task and if so ensures the task doesn't reschedule itself (as it usually would with other types of tasks). Ensures that the operation of deleting or disabling an Alert won't fail if it fails to delete an already deleted task (a task might preemptively self delete if its underlying alert object was deleted, even if the overall delete operation wasn't deleted). Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
b9822b1
commit 3cdf259
Showing
12 changed files
with
240 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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