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

Migrate the pre-7.11 tasks that has no schedule field. #124304

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,45 @@ describe('successful migrations', () => {
});
});
});

describe('8.2.0', () => {
test('resets attempts and status of the failed alerting tasks without schedule interval', () => {
ersin-erdal marked this conversation as resolved.
Show resolved Hide resolved
const migration820 = getMigrations()['8.2.0'];
const taskInstance = getMockData({
taskType: 'alerting:123',
status: 'failed',
schedule: undefined,
});

expect(migration820(taskInstance, migrationContext)).toEqual({
...taskInstance,
attributes: {
...taskInstance.attributes,
attempts: 0,
status: 'idle',
},
});
});

test('does not update the tasks that are not "failed"', () => {
const migration820 = getMigrations()['8.2.0'];
const taskInstance = getMockData({
taskType: 'alerting:123',
status: 'idle',
attempts: 3,
schedule: undefined,
});

expect(migration820(taskInstance, migrationContext)).toEqual({
ersin-erdal marked this conversation as resolved.
Show resolved Hide resolved
...taskInstance,
attributes: {
...taskInstance.attributes,
attempts: 3,
status: 'idle',
},
});
});
});
ersin-erdal marked this conversation as resolved.
Show resolved Hide resolved
});

describe('handles errors during migrations', () => {
Expand Down
50 changes: 35 additions & 15 deletions x-pack/plugins/task_manager/server/saved_objects/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import {
SavedObjectsUtils,
SavedObjectUnsanitizedDoc,
} from '../../../../../src/core/server';
import { TaskInstance, TaskInstanceWithDeprecatedFields } from '../task';
import { ConcreteTaskInstance, TaskStatus } from '../task';

interface TaskInstanceLogMeta extends LogMeta {
migrations: { taskInstanceDocument: SavedObjectUnsanitizedDoc<TaskInstanceWithDeprecatedFields> };
migrations: { taskInstanceDocument: SavedObjectUnsanitizedDoc<ConcreteTaskInstance> };
}

type TaskInstanceMigration = (
doc: SavedObjectUnsanitizedDoc<TaskInstanceWithDeprecatedFields>
) => SavedObjectUnsanitizedDoc<TaskInstanceWithDeprecatedFields>;
doc: SavedObjectUnsanitizedDoc<ConcreteTaskInstance>
) => SavedObjectUnsanitizedDoc<ConcreteTaskInstance>;

export function getMigrations(): SavedObjectMigrationMap {
return {
Expand All @@ -37,18 +37,19 @@ export function getMigrations(): SavedObjectMigrationMap {
pipeMigrations(alertingTaskLegacyIdToSavedObjectIds, actionsTasksLegacyIdToSavedObjectIds),
'8.0.0'
),
'8.2.0': executeMigrationWithErrorHandling(
pipeMigrations(resetAttemptsAndStatusForTheTasksWithoutSchedule),
'8.2.0'
),
};
}

function executeMigrationWithErrorHandling(
migrationFunc: SavedObjectMigrationFn<
TaskInstanceWithDeprecatedFields,
TaskInstanceWithDeprecatedFields
>,
migrationFunc: SavedObjectMigrationFn<ConcreteTaskInstance, ConcreteTaskInstance>,
version: string
) {
return (
doc: SavedObjectUnsanitizedDoc<TaskInstanceWithDeprecatedFields>,
doc: SavedObjectUnsanitizedDoc<ConcreteTaskInstance>,
context: SavedObjectMigrationContext
) => {
try {
Expand All @@ -68,8 +69,8 @@ function executeMigrationWithErrorHandling(
}

function alertingTaskLegacyIdToSavedObjectIds(
doc: SavedObjectUnsanitizedDoc<TaskInstanceWithDeprecatedFields>
): SavedObjectUnsanitizedDoc<TaskInstanceWithDeprecatedFields> {
doc: SavedObjectUnsanitizedDoc<ConcreteTaskInstance>
): SavedObjectUnsanitizedDoc<ConcreteTaskInstance> {
if (doc.attributes.taskType.startsWith('alerting:')) {
let params: { spaceId?: string; alertId?: string } = {};
params = JSON.parse(doc.attributes.params as unknown as string);
Expand All @@ -94,8 +95,8 @@ function alertingTaskLegacyIdToSavedObjectIds(
}

function actionsTasksLegacyIdToSavedObjectIds(
doc: SavedObjectUnsanitizedDoc<TaskInstanceWithDeprecatedFields>
): SavedObjectUnsanitizedDoc<TaskInstanceWithDeprecatedFields> {
doc: SavedObjectUnsanitizedDoc<ConcreteTaskInstance>
): SavedObjectUnsanitizedDoc<ConcreteTaskInstance> {
if (doc.attributes.taskType.startsWith('actions:')) {
let params: { spaceId?: string; actionTaskParamsId?: string } = {};
params = JSON.parse(doc.attributes.params as unknown as string);
Expand Down Expand Up @@ -126,7 +127,7 @@ function actionsTasksLegacyIdToSavedObjectIds(
function moveIntervalIntoSchedule({
attributes: { interval, ...attributes },
...doc
}: SavedObjectUnsanitizedDoc<TaskInstanceWithDeprecatedFields>): SavedObjectUnsanitizedDoc<TaskInstance> {
}: SavedObjectUnsanitizedDoc<ConcreteTaskInstance>): SavedObjectUnsanitizedDoc<ConcreteTaskInstance> {
return {
...doc,
attributes: {
Expand All @@ -143,6 +144,25 @@ function moveIntervalIntoSchedule({
}

function pipeMigrations(...migrations: TaskInstanceMigration[]): TaskInstanceMigration {
return (doc: SavedObjectUnsanitizedDoc<TaskInstanceWithDeprecatedFields>) =>
return (doc: SavedObjectUnsanitizedDoc<ConcreteTaskInstance>) =>
migrations.reduce((migratedDoc, nextMigration) => nextMigration(migratedDoc), doc);
}

function resetAttemptsAndStatusForTheTasksWithoutSchedule(
doc: SavedObjectUnsanitizedDoc<ConcreteTaskInstance>
): SavedObjectUnsanitizedDoc<ConcreteTaskInstance> {
if (doc.attributes.taskType.startsWith('alerting:')) {
if (!doc.attributes.schedule?.interval && doc.attributes.status === TaskStatus.Failed) {
ersin-erdal marked this conversation as resolved.
Show resolved Hide resolved
return {
...doc,
attributes: {
...doc.attributes,
attempts: 0,
status: TaskStatus.Idle,
},
};
}
}

return doc;
}
7 changes: 6 additions & 1 deletion x-pack/plugins/task_manager/server/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,12 @@ export interface ConcreteTaskInstance extends TaskInstance {
id: string;

/**
* The saved object version from the Elaticsearch document.
* @deprecated This field has been moved under schedule (deprecated) with version 7.6.0
*/
interval?: string;

/**
* The saved object version from the Elasticsearch document.
*/
version?: string;

Expand Down
33 changes: 32 additions & 1 deletion x-pack/test/functional/es_archives/task_manager_tasks/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,35 @@
"updated_at": "2020-11-30T15:43:08.277Z"
}
}
}
}

{
"type": "doc",
"value": {
"id": "task:d33d7590-8377-11ec-8c11-2dfe94229b95",
"index": ".kibana_task_manager_1",
"source": {
"migrationVersion": {
"task": "7.6.0"
},
"task": {
"taskType": "alerting:xpack.uptime.alerts.monitorStatus",
"retryAt": null,
"runAt": "2022-02-01T15:59:42.908Z",
"scope": [
"alerting"
],
"startedAt": null,
"state": "{}",
"params": "{\"alertId\":\"c9a10a61-7dcc-4297-991a-6c52c10eb7d2\",\"spaceId\":\"default\"}",
"ownerId": null,
"scheduledAt": "2022-02-01T15:58:41.000Z",
"attempts": 3,
"status": "failed"
},
"references": [],
"updated_at": "2022-02-01T15:58:44.109Z",
"type": "task"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import expect from '@kbn/expect';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { TransportResult } from '@elastic/elasticsearch';
import { TaskInstanceWithDeprecatedFields } from '../../../../plugins/task_manager/server/task';
import {
ConcreteTaskInstance,
TaskInstanceWithDeprecatedFields,
TaskStatus,
} from '../../../../plugins/task_manager/server/task';
import { FtrProviderContext } from '../../../common/ftr_provider_context';
import { SavedObjectsUtils } from '../../../../../src/core/server/saved_objects';

Expand Down Expand Up @@ -76,5 +80,29 @@ export default function createGetTests({ getService }: FtrProviderContext) {
)}"}`
);
});

it('8.2.0 migrates alerting tasks that has no schedule.interval', async () => {
const searchResult: TransportResult<
estypes.SearchResponse<{ task: ConcreteTaskInstance }>,
unknown
> = await es.search(
{
index: '.kibana_task_manager',
body: {
query: {
term: {
_id: 'task:d33d7590-8377-11ec-8c11-2dfe94229b95',
},
},
},
},
{ meta: true }
);
expect(searchResult.statusCode).to.equal(200);
expect((searchResult.body.hits.total as estypes.SearchTotalHits).value).to.equal(1);
const hit = searchResult.body.hits.hits[0];
expect(hit!._source!.task.attempts).to.be(0);
expect(hit!._source!.task.status).to.be(TaskStatus.Idle);
ersin-erdal marked this conversation as resolved.
Show resolved Hide resolved
});
});
}