Skip to content

Commit

Permalink
added retry_on_conflict and report errors from bulk agent update (#14…
Browse files Browse the repository at this point in the history
…2088)

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
juliaElastic and kibanamachine authored Sep 29, 2022
1 parent 16ca2d2 commit 13824dd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/services/agents/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ export async function bulkUpdateAgents(
{
update: {
_id: agentId,
retry_on_conflict: 3,
},
},
{
Expand Down
25 changes: 25 additions & 0 deletions x-pack/plugins/fleet/server/services/agents/reassign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,29 @@ describe('reassignAgents (plural)', () => {
});
expect(calledWithActionResults.body?.[1] as any).toEqual(expectedObject);
});

it('should report errors from ES agent update call', async () => {
const { soClient, esClient, agentInRegularDoc, regularAgentPolicySO2 } = createClientMock();
esClient.bulk.mockResponse({
items: [
{
update: {
_id: agentInRegularDoc._id,
error: new Error('version conflict'),
},
},
],
} as any);
const idsToReassign = [agentInRegularDoc._id];
await reassignAgents(soClient, esClient, { agentIds: idsToReassign }, regularAgentPolicySO2.id);

const calledWithActionResults = esClient.bulk.mock.calls[1][0] as estypes.BulkRequest;
const expectedObject = expect.objectContaining({
'@timestamp': expect.anything(),
action_id: expect.anything(),
agent_id: agentInRegularDoc._id,
error: 'version conflict',
});
expect(calledWithActionResults.body?.[1] as any).toEqual(expectedObject);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function reassignBatch(
throw new AgentReassignmentError('No agents to reassign, already assigned or hosted agents');
}

await bulkUpdateAgents(
const res = await bulkUpdateAgents(
esClient,
agentsToUpdate.map((agent) => ({
agentId: agent.id,
Expand All @@ -83,6 +83,12 @@ export async function reassignBatch(
}))
);

res.items
.filter((item) => !item.success)
.forEach((item) => {
errors[item.id] = item.error!;
});

const actionId = options.actionId ?? uuid();
const errorCount = Object.keys(errors).length;
const total = options.total ?? agentsToUpdate.length + errorCount;
Expand Down

0 comments on commit 13824dd

Please sign in to comment.