Skip to content

Commit

Permalink
removes task_manager's sorting of documents by _id
Browse files Browse the repository at this point in the history
resolves elastic#52517

Not sure if these fixes are completely kosher, specifically if there are any
down-stream effects.
  • Loading branch information
pmuellr committed Jan 9, 2020
1 parent dfac5d8 commit 28d5d3a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,9 @@ if (doc['task.runAt'].size()!=0) {
},
};

const SORT_VALUE_TO_BE_FIRST = 0;
// function changed to stop sorting by _id: https://github.com/elastic/kibana/issues/52517
export const sortByIdsThenByScheduling = (claimTasksById: string[]): SortClause => {
const {
_script: {
script: { source },
},
} = SortByRunAtAndRetryAt;
return defaultsDeep(
{
_script: {
script: {
source: `
if(params.ids.contains(doc['_id'].value)){
return ${SORT_VALUE_TO_BE_FIRST};
}
${source}
`,
params: { ids: claimTasksById },
},
},
},
SortByRunAtAndRetryAt
);
return SortByRunAtAndRetryAt;
};

export const updateFields = (fieldUpdates: {
Expand Down
17 changes: 3 additions & 14 deletions x-pack/legacy/plugins/task_manager/server/task_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('TaskStore', () => {
expect(args).toMatchObject({
index: 'tasky',
body: {
sort: [{ 'task.runAt': 'asc' }, { _id: 'desc' }],
sort: [{ 'task.runAt': 'asc' }],
query: { term: { type: 'task' } },
},
});
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('TaskStore', () => {

expect(args).toMatchObject({
body: {
sort: [{ 'task.taskType': 'desc' }, { _id: 'desc' }],
sort: [{ 'task.taskType': 'desc' }],
},
});
});
Expand Down Expand Up @@ -669,24 +669,13 @@ describe('TaskStore', () => {
script: {
lang: 'painless',
source: `
if(params.ids.contains(doc['_id'].value)){
return 0;
}
if (doc['task.retryAt'].size()!=0) {
return doc['task.retryAt'].value.toInstant().toEpochMilli();
}
if (doc['task.runAt'].size()!=0) {
return doc['task.runAt'].value.toInstant().toEpochMilli();
}
`,
params: {
ids: [
'task:33c6977a-ed6d-43bd-98d9-3f827f7b7cd8',
'task:a208b22c-14ec-4fb4-995f-d2ff7a3b03b8',
],
},
`,
},
},
});
Expand Down
11 changes: 3 additions & 8 deletions x-pack/legacy/plugins/task_manager/server/task_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,18 +427,13 @@ export class TaskStore {
}
}

// function changed to stop sorting by _id: https://github.com/elastic/kibana/issues/52517
function paginatableSort(sort: any[] = []) {
const sortById = { _id: 'desc' };

if (!sort.length) {
return [{ 'task.runAt': 'asc' }, sortById];
}

if (sort.find(({ _id }) => !!_id)) {
return sort;
return [{ 'task.runAt': 'asc' }];
}

return [...sort, sortById];
return sort;
}

function taskInstanceToAttributes(doc: TaskInstance): SavedObjectAttributes {
Expand Down

0 comments on commit 28d5d3a

Please sign in to comment.