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

Skip rule execution when it has a newer typeVersion than Kibana instance #172161

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/common/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export interface Rule<Params extends RuleTypeParams = never> {
revision: number;
running?: boolean | null;
viewInAppRelativeUrl?: string;
typeVersion?: number;
}

export interface SanitizedAlertsFilter extends AlertsFilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ describe('create()', () => {
ownerId: null,
});
rulesClientParams.getActionsClient.mockResolvedValue(actionsClient);
ruleTypeRegistry.getLatestRuleVersion.mockReturnValue(1);
});

describe('authorization', () => {
Expand Down Expand Up @@ -499,6 +500,7 @@ describe('create()', () => {
"foo",
],
"throttle": null,
"typeVersion": 1,
"updatedAt": "2019-02-12T21:01:22.479Z",
"updatedBy": "elastic",
}
Expand Down Expand Up @@ -727,6 +729,7 @@ describe('create()', () => {
"foo",
],
"throttle": null,
"typeVersion": 1,
"updatedAt": "2019-02-12T21:01:22.479Z",
"updatedBy": "elastic",
}
Expand Down Expand Up @@ -1167,6 +1170,7 @@ describe('create()', () => {
schedule: { interval: '1m' },
tags: ['foo'],
throttle: null,
typeVersion: 1,
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
Expand Down Expand Up @@ -1426,6 +1430,7 @@ describe('create()', () => {
schedule: { interval: '1m' },
tags: ['foo'],
throttle: null,
typeVersion: 1,
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
Expand Down Expand Up @@ -1648,6 +1653,7 @@ describe('create()', () => {
schedule: { interval: '1m' },
tags: ['foo'],
throttle: null,
typeVersion: 1,
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
Expand Down Expand Up @@ -1837,6 +1843,7 @@ describe('create()', () => {
schedule: { interval: '1m' },
tags: ['foo'],
throttle: null,
typeVersion: 1,
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
Expand Down Expand Up @@ -2004,6 +2011,7 @@ describe('create()', () => {
},
schedule: { interval: '1m' },
throttle: '10m',
typeVersion: 1,
notifyWhen: 'onActionGroupChange',
muteAll: false,
snoozeSchedule: [],
Expand Down Expand Up @@ -2145,6 +2153,7 @@ describe('create()', () => {
},
schedule: { interval: '1m' },
throttle: '10m',
typeVersion: 1,
notifyWhen: 'onThrottleInterval',
muteAll: false,
snoozeSchedule: [],
Expand Down Expand Up @@ -2286,6 +2295,7 @@ describe('create()', () => {
},
schedule: { interval: '1m' },
throttle: null,
typeVersion: 1,
notifyWhen: null,
muteAll: false,
snoozeSchedule: [],
Expand Down Expand Up @@ -2479,6 +2489,7 @@ describe('create()', () => {
},
revision: 0,
running: false,
typeVersion: 1,
},
{
references: [
Expand Down Expand Up @@ -2836,6 +2847,7 @@ describe('create()', () => {
},
schedule: { interval: '1m' },
throttle: null,
typeVersion: 1,
notifyWhen: null,
muteAll: false,
snoozeSchedule: [],
Expand Down Expand Up @@ -2942,6 +2954,7 @@ describe('create()', () => {
},
schedule: { interval: '1m' },
throttle: null,
typeVersion: 1,
notifyWhen: null,
muteAll: false,
snoozeSchedule: [],
Expand Down Expand Up @@ -3821,6 +3834,7 @@ describe('create()', () => {
},
schedule: { interval: '1m' },
throttle: null,
typeVersion: 1,
notifyWhen: null,
muteAll: false,
snoozeSchedule: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export async function createRule<Params extends RuleParams = never>(
monitoring: getDefaultMonitoringRuleDomainProperties(lastRunTimestamp.toISOString()),
revision: 0,
running: false,
typeVersion: context.ruleTypeRegistry.getLatestRuleVersion(),
},
{
legacyId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export const ruleDomainSchema = schema.object({
revision: schema.number(),
running: schema.maybe(schema.nullable(schema.boolean())),
viewInAppRelativeUrl: schema.maybe(schema.nullable(schema.string())),
typeVersion: schema.maybe(schema.number()),
});

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ export const transformRuleDomainToRuleAttributes = (
...(rule.nextRun !== undefined ? { nextRun: rule.nextRun?.toISOString() || null } : {}),
revision: rule.revision,
...(rule.running !== undefined ? { running: rule.running } : {}),
typeVersion: rule.typeVersion,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,5 @@ export interface RuleDomain<Params extends RuleParams = never> {
revision: RuleDomainSchemaType['revision'];
running?: RuleDomainSchemaType['running'];
viewInAppRelativeUrl?: RuleDomainSchemaType['viewInAppRelativeUrl'];
typeVersion?: RuleDomainSchemaType['typeVersion'];
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,5 @@ export interface RuleAttributes {
nextRun?: string | null;
revision: number;
running?: boolean | null;
typeVersion?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export function getPartialRuleFromRaw<Params extends RuleTypeParams>(
actions,
snoozeSchedule,
lastRun,
typeVersion,
...partialRawRule
}: Partial<RawRule>,
references: SavedObjectReference[] | undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ async function updateAlert<Params extends RuleTypeParams>(
revision,
updatedBy: username,
updatedAt: new Date().toISOString(),
typeVersion: context.ruleTypeRegistry.getLatestRuleVersion(),
});

const mappedParams = getMappedParams(updatedParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const fieldsToExcludeFromPublicApi: Array<keyof SanitizedRule> = [
'mapped_params',
'snoozeSchedule',
'activeSnoozes',
'typeVersion',
];

export const fieldsToExcludeFromRevisionUpdates: ReadonlySet<keyof RuleTypeParams> = new Set([
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/alerting/server/rules_client/tests/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe('update()', () => {
},
validLegacyConsumers: [],
});
ruleTypeRegistry.getLatestRuleVersion.mockReturnValue(1);
(migrateLegacyActions as jest.Mock).mockResolvedValue({
hasLegacyActions: false,
resultedActions: [],
Expand Down Expand Up @@ -444,6 +445,7 @@ describe('update()', () => {
"foo",
],
"throttle": null,
"typeVersion": 1,
"updatedAt": "2019-02-12T21:01:22.479Z",
"updatedBy": "elastic",
}
Expand Down Expand Up @@ -674,6 +676,7 @@ describe('update()', () => {
scheduledTaskId: 'task-123',
tags: ['foo'],
throttle: null,
typeVersion: 1,
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
Expand Down Expand Up @@ -914,6 +917,7 @@ describe('update()', () => {
scheduledTaskId: 'task-123',
tags: ['foo'],
throttle: null,
typeVersion: 1,
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
Expand Down Expand Up @@ -1103,6 +1107,7 @@ describe('update()', () => {
scheduledTaskId: 'task-123',
tags: ['foo'],
throttle: null,
typeVersion: 1,
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
Expand Down Expand Up @@ -1291,6 +1296,7 @@ describe('update()', () => {
"foo",
],
"throttle": "5m",
"typeVersion": 1,
"updatedAt": "2019-02-12T21:01:22.479Z",
"updatedBy": "elastic",
}
Expand Down Expand Up @@ -1444,6 +1450,7 @@ describe('update()', () => {
"foo",
],
"throttle": "5m",
"typeVersion": 1,
"updatedAt": "2019-02-12T21:01:22.479Z",
"updatedBy": "elastic",
}
Expand Down Expand Up @@ -2464,6 +2471,7 @@ describe('update()', () => {
scheduledTaskId: 'task-123',
tags: ['foo'],
throttle: null,
typeVersion: 1,
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
Expand Down Expand Up @@ -3017,6 +3025,7 @@ describe('update()', () => {
tags: ['foo'],
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
typeVersion: 1,
},
{
id: '1',
Expand Down Expand Up @@ -3227,6 +3236,7 @@ describe('update()', () => {
"foo",
],
"throttle": "5m",
"typeVersion": 1,
"updatedAt": "2019-02-12T21:01:22.479Z",
"updatedBy": "elastic",
}
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/alerting/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const AlertAttributesExcludedFromAAD = [
'nextRun',
'revision',
'running',
'typeVersion',
];

// useful for Pick<RawAlert, AlertAttributesExcludedFromAADType> which is a
Expand All @@ -69,7 +70,8 @@ export type AlertAttributesExcludedFromAADType =
| 'lastRun'
| 'nextRun'
| 'revision'
| 'running';
| 'running'
| 'typeVersion';

export function setupSavedObjects(
savedObjects: SavedObjectsServiceSetup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { schema } from '@kbn/config-schema';
import { RawRule } from '../types';

describe('rule model versions', () => {
const ruleModelVersions: CustomSavedObjectsModelVersionMap = {
const modelVersions: CustomSavedObjectsModelVersionMap = {
'1': {
changes: [],
schemas: {
Expand Down Expand Up @@ -58,14 +58,22 @@ describe('rule model versions', () => {

describe('getMinimumCompatibleVersion', () => {
it('should return the minimum compatible version for the matching rawRule', () => {
expect(getMinimumCompatibleVersion(ruleModelVersions, 1, rawRule)).toBe(1);
expect(getMinimumCompatibleVersion(ruleModelVersions, 2, rawRule)).toBe(2);
expect(getMinimumCompatibleVersion(ruleModelVersions, 3, rawRule)).toBe(2);
expect(getMinimumCompatibleVersion(ruleModelVersions, 4, rawRule)).toBe(2);
expect(getMinimumCompatibleVersion({ modelVersions, version: 1, rawRule })).toBe(1);
expect(getMinimumCompatibleVersion({ modelVersions, version: 2, rawRule })).toBe(2);
expect(getMinimumCompatibleVersion({ modelVersions, version: 3, rawRule })).toBe(2);
expect(getMinimumCompatibleVersion({ modelVersions, version: 4, rawRule })).toBe(2);
});
it('should return the minimum compatible version for the mismatching rawRule', () => {
expect(getMinimumCompatibleVersion(ruleModelVersions, 3, mismatchingRawRule)).toBe(3);
expect(getMinimumCompatibleVersion(ruleModelVersions, 4, mismatchingRawRule)).toBe(4);
expect(
getMinimumCompatibleVersion({ modelVersions, version: 3, rawRule: mismatchingRawRule })
).toBe(3);
expect(
getMinimumCompatibleVersion({ modelVersions, version: 4, rawRule: mismatchingRawRule })
).toBe(4);
});

it('should return the given version number if the number does not exist in ruleModelVersions', () => {
expect(getMinimumCompatibleVersion({ version: 999, rawRule: mismatchingRawRule })).toBe(999);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,25 @@ export const ruleModelVersions: CustomSavedObjectsModelVersionMap = {

export const getLatestRuleVersion = () => Math.max(...Object.keys(ruleModelVersions).map(Number));

export function getMinimumCompatibleVersion(
modelVersions: CustomSavedObjectsModelVersionMap,
version: number,
rawRule: RawRule
): number {
export function getMinimumCompatibleVersion({
modelVersions = ruleModelVersions,
version = getLatestRuleVersion(),
rawRule,
}: {
modelVersions?: CustomSavedObjectsModelVersionMap;
version?: number;
rawRule: RawRule;
}): number {
if (version === 1) {
return 1;
}

if (modelVersions[version].isCompatibleWithPreviousVersion(rawRule)) {
return getMinimumCompatibleVersion(modelVersions, version - 1, rawRule);
if (modelVersions[version] && modelVersions[version].isCompatibleWithPreviousVersion(rawRule)) {
return getMinimumCompatibleVersion({
modelVersions,
version: version - 1,
rawRule,
});
}

return version;
Expand Down
21 changes: 1 addition & 20 deletions x-pack/plugins/alerting/server/task_runner/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export const generateSavedObjectParams = ({
},
nextRun,
running: false,
typeVersion: 1,
},
{ refresh: false, namespace: undefined },
];
Expand Down Expand Up @@ -358,26 +359,6 @@ export const generateRunnerResult = ({
taskRunError,
}: GeneratorParams = {}) => {
return {
monitoring: {
run: {
calculated_metrics: {
success_ratio: successRatio,
},
// @ts-ignore
history: history.map((success) => ({ success, timestamp: 0 })),
last_run: {
metrics: {
duration: 0,
gap_duration_s: null,
total_alerts_created: null,
total_alerts_detected: null,
total_indexing_duration_ms: null,
total_search_duration_ms: null,
},
timestamp: '1970-01-01T00:00:00.000Z',
},
},
},
schedule: {
interval,
},
Expand Down
Loading