-
Notifications
You must be signed in to change notification settings - Fork 8k
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
[Response Ops][Task Manager] Integration test for switching between task claim strategies #186419
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
52e2222
Add integration test for restarting kibana with different task claime…
ymao1 f95ab04
Adding test for switching task claimers on still running tasks
ymao1 731299a
Merge branch 'main' into tm-mget-switch
kibanamachine b7ab428
Merge branch 'main' into tm-mget-switch
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
214 changes: 214 additions & 0 deletions
214
...k/plugins/task_manager/server/integration_tests/task_manager_switch_task_claimers.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,214 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { v4 as uuidV4 } from 'uuid'; | ||
import { schema } from '@kbn/config-schema'; | ||
import { TaskStatus } from '../task'; | ||
import type { TaskClaimingOpts } from '../queries/task_claiming'; | ||
import { injectTask, setupTestServers, retry } from './lib'; | ||
import { setupKibanaServer } from './lib/setup_test_servers'; | ||
|
||
const mockTaskTypeRunFn = jest.fn(); | ||
const mockCreateTaskRunner = jest.fn(); | ||
const mockTaskType = { | ||
title: '', | ||
description: '', | ||
stateSchemaByVersion: { | ||
1: { | ||
up: (state: Record<string, unknown>) => ({ ...state, baz: state.baz || '' }), | ||
schema: schema.object({ | ||
foo: schema.string(), | ||
bar: schema.string(), | ||
baz: schema.string(), | ||
}), | ||
}, | ||
}, | ||
createTaskRunner: mockCreateTaskRunner.mockImplementation(() => ({ | ||
run: mockTaskTypeRunFn, | ||
})), | ||
}; | ||
const { TaskClaiming: TaskClaimingMock } = jest.requireMock('../queries/task_claiming'); | ||
jest.mock('../queries/task_claiming', () => { | ||
const actual = jest.requireActual('../queries/task_claiming'); | ||
return { | ||
...actual, | ||
TaskClaiming: jest.fn().mockImplementation((opts: TaskClaimingOpts) => { | ||
// We need to register here because once the class is instantiated, adding | ||
// definitions won't get claimed because of "partitionIntoClaimingBatches". | ||
opts.definitions.registerTaskDefinitions({ | ||
fooType: mockTaskType, | ||
}); | ||
return new actual.TaskClaiming(opts); | ||
}), | ||
}; | ||
}); | ||
|
||
describe('switch task claiming strategies', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should switch from default to mget and still claim tasks', async () => { | ||
const setupResultDefault = await setupTestServers(); | ||
const esServer = setupResultDefault.esServer; | ||
let kibanaServer = setupResultDefault.kibanaServer; | ||
let taskClaimingOpts: TaskClaimingOpts = TaskClaimingMock.mock.calls[0][0]; | ||
|
||
expect(taskClaimingOpts.strategy).toBe('default'); | ||
|
||
mockTaskTypeRunFn.mockImplementation(() => { | ||
return { state: {} }; | ||
}); | ||
|
||
// inject a task to run and ensure it is claimed and run | ||
const id1 = uuidV4(); | ||
await injectTask(kibanaServer.coreStart.elasticsearch.client.asInternalUser, { | ||
id: id1, | ||
taskType: 'fooType', | ||
params: { foo: true }, | ||
state: { foo: 'test', bar: 'test', baz: 'test' }, | ||
stateVersion: 4, | ||
runAt: new Date(), | ||
enabled: true, | ||
scheduledAt: new Date(), | ||
attempts: 0, | ||
status: TaskStatus.Idle, | ||
startedAt: null, | ||
retryAt: null, | ||
ownerId: null, | ||
}); | ||
|
||
await retry(async () => { | ||
expect(mockTaskTypeRunFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
if (kibanaServer) { | ||
await kibanaServer.stop(); | ||
} | ||
|
||
const setupResultMget = await setupKibanaServer({ | ||
xpack: { | ||
task_manager: { | ||
claim_strategy: 'unsafe_mget', | ||
}, | ||
}, | ||
}); | ||
kibanaServer = setupResultMget.kibanaServer; | ||
|
||
taskClaimingOpts = TaskClaimingMock.mock.calls[1][0]; | ||
expect(taskClaimingOpts.strategy).toBe('unsafe_mget'); | ||
|
||
// inject a task to run and ensure it is claimed and run | ||
const id2 = uuidV4(); | ||
await injectTask(kibanaServer.coreStart.elasticsearch.client.asInternalUser, { | ||
id: id2, | ||
taskType: 'fooType', | ||
params: { foo: true }, | ||
state: { foo: 'test', bar: 'test', baz: 'test' }, | ||
stateVersion: 4, | ||
runAt: new Date(), | ||
enabled: true, | ||
scheduledAt: new Date(), | ||
attempts: 0, | ||
status: TaskStatus.Idle, | ||
startedAt: null, | ||
retryAt: null, | ||
ownerId: null, | ||
}); | ||
|
||
await retry(async () => { | ||
expect(mockTaskTypeRunFn).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
if (kibanaServer) { | ||
await kibanaServer.stop(); | ||
} | ||
if (esServer) { | ||
await esServer.stop(); | ||
} | ||
}); | ||
|
||
it('should switch from mget to default and still claim tasks', async () => { | ||
const setupResultMget = await setupTestServers({ | ||
xpack: { | ||
task_manager: { | ||
claim_strategy: 'unsafe_mget', | ||
}, | ||
}, | ||
}); | ||
const esServer = setupResultMget.esServer; | ||
let kibanaServer = setupResultMget.kibanaServer; | ||
let taskClaimingOpts: TaskClaimingOpts = TaskClaimingMock.mock.calls[0][0]; | ||
|
||
expect(taskClaimingOpts.strategy).toBe('unsafe_mget'); | ||
|
||
mockTaskTypeRunFn.mockImplementation(() => { | ||
return { state: {} }; | ||
}); | ||
|
||
// inject a task to run and ensure it is claimed and run | ||
const id1 = uuidV4(); | ||
await injectTask(kibanaServer.coreStart.elasticsearch.client.asInternalUser, { | ||
id: id1, | ||
taskType: 'fooType', | ||
params: { foo: true }, | ||
state: { foo: 'test', bar: 'test', baz: 'test' }, | ||
stateVersion: 4, | ||
runAt: new Date(), | ||
enabled: true, | ||
scheduledAt: new Date(), | ||
attempts: 0, | ||
status: TaskStatus.Idle, | ||
startedAt: null, | ||
retryAt: null, | ||
ownerId: null, | ||
}); | ||
|
||
await retry(async () => { | ||
expect(mockTaskTypeRunFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
if (kibanaServer) { | ||
await kibanaServer.stop(); | ||
} | ||
|
||
const setupResultDefault = await setupKibanaServer(); | ||
kibanaServer = setupResultDefault.kibanaServer; | ||
|
||
taskClaimingOpts = TaskClaimingMock.mock.calls[1][0]; | ||
expect(taskClaimingOpts.strategy).toBe('default'); | ||
|
||
// inject a task to run and ensure it is claimed and run | ||
const id2 = uuidV4(); | ||
await injectTask(kibanaServer.coreStart.elasticsearch.client.asInternalUser, { | ||
id: id2, | ||
taskType: 'fooType', | ||
params: { foo: true }, | ||
state: { foo: 'test', bar: 'test', baz: 'test' }, | ||
stateVersion: 4, | ||
runAt: new Date(), | ||
enabled: true, | ||
scheduledAt: new Date(), | ||
attempts: 0, | ||
status: TaskStatus.Idle, | ||
startedAt: null, | ||
retryAt: null, | ||
ownerId: null, | ||
}); | ||
|
||
await retry(async () => { | ||
expect(mockTaskTypeRunFn).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
if (kibanaServer) { | ||
await kibanaServer.stop(); | ||
} | ||
if (esServer) { | ||
await esServer.stop(); | ||
} | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to make the tests a little less noisy