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

[Alerting] retry internal OCC calls within alertsClient #77838

Merged
merged 4 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions x-pack/plugins/alerts/server/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1696,14 +1696,22 @@ describe('muteAll()', () => {
muteAll: false,
},
references: [],
version: '123',
});

await alertsClient.muteAll({ id: '1' });
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith('alert', '1', {
muteAll: true,
mutedInstanceIds: [],
updatedBy: 'elastic',
});
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith(
'alert',
'1',
{
muteAll: true,
mutedInstanceIds: [],
updatedBy: 'elastic',
},
{
version: '123',
}
);
});

describe('authorization', () => {
Expand Down Expand Up @@ -1785,11 +1793,18 @@ describe('unmuteAll()', () => {
});

await alertsClient.unmuteAll({ id: '1' });
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith('alert', '1', {
muteAll: false,
mutedInstanceIds: [],
updatedBy: 'elastic',
});
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith(
'alert',
'1',
{
muteAll: false,
mutedInstanceIds: [],
updatedBy: 'elastic',
},
{
version: '123',
}
);
});

describe('authorization', () => {
Expand Down
126 changes: 103 additions & 23 deletions x-pack/plugins/alerts/server/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { parseIsoOrRelativeDate } from './lib/iso_or_relative_date';
import { alertInstanceSummaryFromEventLog } from './lib/alert_instance_summary_from_event_log';
import { IEvent } from '../../event_log/server';
import { parseDuration } from '../common/parse_duration';
import { retryIfConflicts } from './lib/retry_if_conflicts';
import { partiallyUpdateAlert } from './saved_objects';

export interface RegistryAlertTypeWithAuth extends RegistryAlertType {
authorizedConsumers: string[];
Expand Down Expand Up @@ -421,6 +423,14 @@ export class AlertsClient {
}

public async update({ id, data }: UpdateOptions): Promise<PartialAlert> {
return await retryIfConflicts(
this.logger,
`alertsClient.update('${id}')`,
async () => await this.updateWithOCC({ id, data })
mikecote marked this conversation as resolved.
Show resolved Hide resolved
);
}

private async updateWithOCC({ id, data }: UpdateOptions): Promise<PartialAlert> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seeing as AlertsClient is already a huge file, is there a way we can add the retry into this without adding an extra method for each operation? 🤔
Perhaps by using some kind of decorator that wraps these methods or something along those lines?
This will also become quite a bit more complicated when we introduce optional version to these methods, if we can find a way to reduce this into a single extension, it will help with maintenance down the line.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do we support decorators? I'm not sure inlining these is going to save all that space, is it? My thinking was that once we add versions to these methods, that basically just means to set retries to 0, so will likely be a straight-forward change to each of the sites.

The reason I separated these out was to try to keep the actual methods a little cleaner - wrapping like this obviously doesn't scale real well, beyond the first time to you do it (now!). I can go either way on inlining, but I think the current shape is a little more readable than inlined.

Copy link
Member Author

Choose a reason for hiding this comment

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

After conversation with Gidi, we decided to think about some alternatives to this approach, but couldn't come up with anything that would require even more changes to the alerts client.

let alertSavedObject: SavedObject<RawAlert>;

try {
Expand Down Expand Up @@ -529,7 +539,15 @@ export class AlertsClient {
};
}

public async updateApiKey({ id }: { id: string }) {
public async updateApiKey({ id }: { id: string }): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.updateApiKey('${id}')`,
async () => await this.updateApiKeyWithOCC({ id })
);
}

private async updateApiKeyWithOCC({ id }: { id: string }) {
let apiKeyToInvalidate: string | null = null;
let attributes: RawAlert;
let version: string | undefined;
Expand Down Expand Up @@ -597,7 +615,15 @@ export class AlertsClient {
}
}

public async enable({ id }: { id: string }) {
public async enable({ id }: { id: string }): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.enable('${id}')`,
async () => await this.enableWithOCC({ id })
);
}

private async enableWithOCC({ id }: { id: string }) {
let apiKeyToInvalidate: string | null = null;
let attributes: RawAlert;
let version: string | undefined;
Expand Down Expand Up @@ -658,7 +684,15 @@ export class AlertsClient {
}
}

public async disable({ id }: { id: string }) {
public async disable({ id }: { id: string }): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.disable('${id}')`,
async () => await this.disableWithOCC({ id })
);
}

private async disableWithOCC({ id }: { id: string }) {
let apiKeyToInvalidate: string | null = null;
let attributes: RawAlert;
let version: string | undefined;
Expand Down Expand Up @@ -711,8 +745,19 @@ export class AlertsClient {
}
}

public async muteAll({ id }: { id: string }) {
const { attributes } = await this.unsecuredSavedObjectsClient.get<RawAlert>('alert', id);
public async muteAll({ id }: { id: string }): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.muteAll('${id}')`,
async () => await this.muteAllWithOCC({ id })
);
}

private async muteAllWithOCC({ id }: { id: string }) {
const { attributes, version } = await this.unsecuredSavedObjectsClient.get<RawAlert>(
'alert',
id
);
await this.authorization.ensureAuthorized(
attributes.alertTypeId,
attributes.consumer,
Expand All @@ -723,19 +768,34 @@ export class AlertsClient {
await this.actionsAuthorization.ensureAuthorized('execute');
}

await this.unsecuredSavedObjectsClient.update(
'alert',
const updateAttributes = this.updateMeta({
muteAll: true,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
});
const updateOptions = { version };

await partiallyUpdateAlert(
this.unsecuredSavedObjectsClient,
id,
this.updateMeta({
muteAll: true,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
})
updateAttributes,
updateOptions
);
}

public async unmuteAll({ id }: { id: string }): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.unmuteAll('${id}')`,
async () => await this.unmuteAllWithOCC({ id })
);
}

public async unmuteAll({ id }: { id: string }) {
const { attributes } = await this.unsecuredSavedObjectsClient.get<RawAlert>('alert', id);
private async unmuteAllWithOCC({ id }: { id: string }) {
const { attributes, version } = await this.unsecuredSavedObjectsClient.get<RawAlert>(
'alert',
id
);
await this.authorization.ensureAuthorized(
attributes.alertTypeId,
attributes.consumer,
Expand All @@ -746,18 +806,30 @@ export class AlertsClient {
await this.actionsAuthorization.ensureAuthorized('execute');
}

await this.unsecuredSavedObjectsClient.update(
'alert',
const updateAttributes = this.updateMeta({
muteAll: false,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
});
const updateOptions = { version };

await partiallyUpdateAlert(
this.unsecuredSavedObjectsClient,
id,
this.updateMeta({
muteAll: false,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
})
updateAttributes,
updateOptions
);
}

public async muteInstance({ alertId, alertInstanceId }: MuteOptions): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.muteInstance('${alertId}')`,
async () => await this.muteInstanceWithOCC({ alertId, alertInstanceId })
);
}

public async muteInstance({ alertId, alertInstanceId }: MuteOptions) {
private async muteInstanceWithOCC({ alertId, alertInstanceId }: MuteOptions) {
const { attributes, version } = await this.unsecuredSavedObjectsClient.get<Alert>(
'alert',
alertId
Expand Down Expand Up @@ -788,7 +860,15 @@ export class AlertsClient {
}
}

public async unmuteInstance({
public async unmuteInstance({ alertId, alertInstanceId }: MuteOptions): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.unmuteInstance('${alertId}')`,
async () => await this.unmuteInstanceWithOCC({ alertId, alertInstanceId })
);
}

private async unmuteInstanceWithOCC({
alertId,
alertInstanceId,
}: {
Expand Down
Loading