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

[Task Manager] Prevents Task Manager from trying to claim invalid tasks #76891

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
113 changes: 105 additions & 8 deletions x-pack/plugins/task_manager/server/task_store.test.ts
Expand Up @@ -633,7 +633,7 @@ if (doc['task.runAt'].size()!=0) {
const runAt = new Date();
const tasks = [
{
_id: 'aaa',
_id: 'task:aaa',
_source: {
type: 'task',
task: {
Expand All @@ -654,7 +654,104 @@ if (doc['task.runAt'].size()!=0) {
sort: ['a', 1],
},
{
// this is invalid as it doesn't have the `type` prefix
_id: 'bbb',
_source: {
type: 'task',
task: {
runAt,
taskType: 'bar',
schedule: { interval: '5m' },
attempts: 2,
status: 'claiming',
params: '{ "shazm": 1 }',
state: '{ "henry": "The 8th" }',
user: 'dabo',
scope: ['reporting', 'ceo'],
ownerId: taskManagerId,
},
},
_seq_no: 3,
_primary_term: 4,
sort: ['b', 2],
},
];
const {
result: { docs },
args: {
search: {
body: { query },
},
},
} = await testClaimAvailableTasks({
opts: {
taskManagerId,
},
claimingOpts: {
claimOwnershipUntil,
size: 10,
},
hits: tasks,
});

expect(query.bool.must).toContainEqual({
bool: {
must: [
{
term: {
'task.ownerId': taskManagerId,
},
},
{ term: { 'task.status': 'claiming' } },
],
},
});

expect(docs).toMatchObject([
{
attempts: 0,
id: 'aaa',
schedule: undefined,
params: { hello: 'world' },
runAt,
scope: ['reporting'],
state: { baby: 'Henhen' },
status: 'claiming',
taskType: 'foo',
user: 'jimbo',
ownerId: taskManagerId,
},
]);
});

test('it filters out invalid tasks that arent SavedObjects', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

It's not immediately clear to me that these tasks aren't valid - isn't the change just to make sure the id has a task: prefix and has a type field? So, either the test name isn't quite right, or else there should probably be a comment explaining more. Here's the new code being called to check the validity:

public isRawSavedObject(rawDoc: SavedObjectsRawDoc) {
const { type, namespace } = rawDoc._source;
const namespacePrefix =
namespace && this.registry.isSingleNamespace(type) ? `${namespace}:` : '';
return Boolean(
type &&
rawDoc._id.startsWith(`${namespacePrefix}${type}:`) &&
rawDoc._source.hasOwnProperty(type)
);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The way I see it is that the type: is a SavedObjects internal implementation detail.
We're using the SavedObject's validator to validate - the actual distinction seems less relevant to Task Manager as far as I can tell. 🤔

Does that make sense?

const taskManagerId = uuid.v1();
const claimOwnershipUntil = new Date(Date.now());
const runAt = new Date();
const tasks = [
{
_id: 'task:aaa',
_source: {
type: 'task',
task: {
runAt,
taskType: 'foo',
schedule: undefined,
attempts: 0,
status: 'claiming',
params: '{ "hello": "world" }',
state: '{ "baby": "Henhen" }',
user: 'jimbo',
scope: ['reporting'],
ownerId: taskManagerId,
},
},
_seq_no: 1,
_primary_term: 2,
sort: ['a', 1],
},
{
_id: 'task:bbb',
_source: {
type: 'task',
task: {
Expand Down Expand Up @@ -729,7 +826,7 @@ if (doc['task.runAt'].size()!=0) {
const runAt = new Date();
const tasks = [
{
_id: 'aaa',
_id: 'task:aaa',
_source: {
type: 'task',
task: {
Expand All @@ -750,7 +847,7 @@ if (doc['task.runAt'].size()!=0) {
sort: ['a', 1],
},
{
_id: 'bbb',
_id: 'task:bbb',
_source: {
type: 'task',
task: {
Expand Down Expand Up @@ -1069,7 +1166,7 @@ if (doc['task.runAt'].size()!=0) {
const runAt = new Date();
const tasks = [
{
_id: 'claimed-by-id',
_id: 'task:claimed-by-id',
_source: {
type: 'task',
task: {
Expand All @@ -1093,7 +1190,7 @@ if (doc['task.runAt'].size()!=0) {
sort: ['a', 1],
},
{
_id: 'claimed-by-schedule',
_id: 'task:claimed-by-schedule',
_source: {
type: 'task',
task: {
Expand All @@ -1117,7 +1214,7 @@ if (doc['task.runAt'].size()!=0) {
sort: ['b', 2],
},
{
_id: 'already-running',
_id: 'task:already-running',
_source: {
type: 'task',
task: {
Expand Down Expand Up @@ -1378,8 +1475,8 @@ if (doc['task.runAt'].size()!=0) {
});

function generateFakeTasks(count: number = 1) {
return _.times(count, () => ({
_id: 'aaa',
return _.times(count, (index) => ({
_id: `task:id-${index}`,
_source: {
type: 'task',
task: {},
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/task_manager/server/task_store.ts
Expand Up @@ -451,6 +451,7 @@ export class TaskStore {

return {
docs: (rawDocs as SavedObjectsRawDoc[])
.filter((doc) => this.serializer.isRawSavedObject(doc))
.map((doc) => this.serializer.rawToSavedObject(doc))
.map((doc) => omit(doc, 'namespace') as SavedObject<SerializedConcreteTaskInstance>)
.map(savedObjectToConcreteTaskInstance),
Expand Down