Skip to content

Commit

Permalink
allows Alerts to recover gracefully from Executor errors (#53688)
Browse files Browse the repository at this point in the history
Prevents errors in Alert Executors from forcing their underlying tasks into a zombie state.
  • Loading branch information
gmmorris authored Jan 6, 2020
1 parent 6ce2818 commit 785b916
Show file tree
Hide file tree
Showing 31 changed files with 982 additions and 575 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { State, Context } from '../types';
import { parseDuration } from './parse_duration';
import { parseDuration } from '../lib';

interface Meta {
lastScheduledActions?: {
Expand Down
8 changes: 8 additions & 0 deletions x-pack/legacy/plugins/alerting/server/alert_instance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export { AlertInstance } from './alert_instance';
export { createAlertInstanceFactory } from './create_alert_instance_factory';
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { TaskRunnerFactory } from './lib';
import { TaskRunnerFactory } from './task_runner';
import { AlertTypeRegistry } from './alert_type_registry';
import { taskManagerMock } from '../../task_manager/server/task_manager.mock';

Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/alerting/server/alert_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import Boom from 'boom';
import { i18n } from '@kbn/i18n';
import { TaskRunnerFactory } from './lib';
import { RunContext } from '../../task_manager/server';
import { TaskRunnerFactory } from './task_runner';
import { RunContext } from '../../task_manager';
import { TaskManagerSetupContract } from './shim';
import { AlertType } from './types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import { Request } from 'hapi';
import { AlertsClientFactory, ConstructorOpts } from './alerts_client_factory';
import { alertTypeRegistryMock } from '../alert_type_registry.mock';
import { taskManagerMock } from '../../../task_manager/server/task_manager.mock';
import { KibanaRequest } from '../../../../../../src/core/server';
import { loggingServiceMock } from '../../../../../../src/core/server/mocks';
import { encryptedSavedObjectsMock } from '../../../../../plugins/encrypted_saved_objects/server/mocks';
import { alertTypeRegistryMock } from './alert_type_registry.mock';
import { taskManagerMock } from '../../task_manager/server/task_manager.mock';
import { KibanaRequest } from '../../../../../src/core/server';
import { loggingServiceMock } from '../../../../../src/core/server/mocks';
import { encryptedSavedObjectsMock } from '../../../../plugins/encrypted_saved_objects/server/mocks';

jest.mock('../alerts_client');
jest.mock('./alerts_client');

const savedObjectsClient = jest.fn();
const securityPluginSetup = {
Expand Down Expand Up @@ -55,7 +55,7 @@ test('creates an alerts client with proper constructor arguments', async () => {
const factory = new AlertsClientFactory(alertsClientFactoryParams);
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);

expect(jest.requireMock('../alerts_client').AlertsClient).toHaveBeenCalledWith({
expect(jest.requireMock('./alerts_client').AlertsClient).toHaveBeenCalledWith({
savedObjectsClient,
logger: alertsClientFactoryParams.logger,
taskManager: alertsClientFactoryParams.taskManager,
Expand All @@ -72,7 +72,7 @@ test('creates an alerts client with proper constructor arguments', async () => {
test('getUserName() returns null when security is disabled', async () => {
const factory = new AlertsClientFactory(alertsClientFactoryParams);
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

const userNameResult = await constructorCall.getUserName();
expect(userNameResult).toEqual(null);
Expand All @@ -84,7 +84,7 @@ test('getUserName() returns a name when security is enabled', async () => {
securityPluginSetup: securityPluginSetup as any,
});
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.getCurrentUser.mockResolvedValueOnce({ username: 'bob' });
const userNameResult = await constructorCall.getUserName();
Expand All @@ -94,7 +94,7 @@ test('getUserName() returns a name when security is enabled', async () => {
test('createAPIKey() returns { apiKeysEnabled: false } when security is disabled', async () => {
const factory = new AlertsClientFactory(alertsClientFactoryParams);
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

const createAPIKeyResult = await constructorCall.createAPIKey();
expect(createAPIKeyResult).toEqual({ apiKeysEnabled: false });
Expand All @@ -103,7 +103,7 @@ test('createAPIKey() returns { apiKeysEnabled: false } when security is disabled
test('createAPIKey() returns { apiKeysEnabled: false } when security is enabled but ES security is disabled', async () => {
const factory = new AlertsClientFactory(alertsClientFactoryParams);
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.createAPIKey.mockResolvedValueOnce(null);
const createAPIKeyResult = await constructorCall.createAPIKey();
Expand All @@ -116,7 +116,7 @@ test('createAPIKey() returns an API key when security is enabled', async () => {
securityPluginSetup: securityPluginSetup as any,
});
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.createAPIKey.mockResolvedValueOnce({ api_key: '123', id: 'abc' });
const createAPIKeyResult = await constructorCall.createAPIKey();
Expand All @@ -132,7 +132,7 @@ test('createAPIKey() throws when security plugin createAPIKey throws an error',
securityPluginSetup: securityPluginSetup as any,
});
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.createAPIKey.mockRejectedValueOnce(new Error('TLS disabled'));
await expect(constructorCall.createAPIKey()).rejects.toThrowErrorMatchingInlineSnapshot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import Hapi from 'hapi';
import uuid from 'uuid';
import { AlertsClient } from '../alerts_client';
import { AlertTypeRegistry, SpaceIdToNamespaceFunction } from '../types';
import { SecurityPluginStartContract, TaskManagerStartContract } from '../shim';
import { KibanaRequest, Logger } from '../../../../../../src/core/server';
import { InvalidateAPIKeyParams } from '../../../../../plugins/security/server';
import { PluginStartContract as EncryptedSavedObjectsStartContract } from '../../../../../plugins/encrypted_saved_objects/server';
import { AlertsClient } from './alerts_client';
import { AlertTypeRegistry, SpaceIdToNamespaceFunction } from './types';
import { SecurityPluginStartContract, TaskManagerStartContract } from './shim';
import { KibanaRequest, Logger } from '../../../../../src/core/server';
import { InvalidateAPIKeyParams } from '../../../../plugins/security/server';
import { PluginStartContract as EncryptedSavedObjectsStartContract } from '../../../../plugins/encrypted_saved_objects/server';

export interface ConstructorOpts {
logger: Logger;
Expand Down
6 changes: 2 additions & 4 deletions x-pack/legacy/plugins/alerting/server/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { AlertInstance } from './alert_instance';
export { validateAlertTypeParams } from './validate_alert_type_params';
export { parseDuration, getDurationSchema } from './parse_duration';
export { AlertsClientFactory } from './alerts_client_factory';
export { TaskRunnerFactory } from './task_runner_factory';
export { LicenseState } from './license_state';
export { validateAlertTypeParams } from './validate_alert_type_params';
54 changes: 54 additions & 0 deletions x-pack/legacy/plugins/alerting/server/lib/result_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.
*/

export interface Ok<T> {
tag: 'ok';
value: T;
}

export interface Err<E> {
tag: 'err';
error: E;
}
export type Result<T, E> = Ok<T> | Err<E>;

export function asOk<T>(value: T): Ok<T> {
return {
tag: 'ok',
value,
};
}

export function asErr<T>(error: T): Err<T> {
return {
tag: 'err',
error,
};
}

export function isOk<T, E>(result: Result<T, E>): result is Ok<T> {
return result.tag === 'ok';
}

export function isErr<T, E>(result: Result<T, E>): result is Err<E> {
return !isOk(result);
}

export async function promiseResult<T, E>(future: Promise<T>): Promise<Result<T, E>> {
try {
return asOk(await future);
} catch (e) {
return asErr(e);
}
}

export function map<T, E, Resolution>(
result: Result<T, E>,
onOk: (value: T) => Resolution,
onErr: (error: E) => Resolution
): Resolution {
return isOk(result) ? onOk(result.value) : onErr(result.error);
}
Loading

0 comments on commit 785b916

Please sign in to comment.