From 13824dd0ddbb88bcbd8995fab85ee325ffa6ae4f Mon Sep 17 00:00:00 2001 From: Julia Bardi <90178898+juliaElastic@users.noreply.github.com> Date: Thu, 29 Sep 2022 13:57:16 +0200 Subject: [PATCH] added retry_on_conflict and report errors from bulk agent update (#142088) Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../fleet/server/services/agents/crud.ts | 1 + .../server/services/agents/reassign.test.ts | 25 +++++++++++++++++++ .../services/agents/reassign_action_runner.ts | 8 +++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/fleet/server/services/agents/crud.ts b/x-pack/plugins/fleet/server/services/agents/crud.ts index 305bf2b6bacb4..55a244664238b 100644 --- a/x-pack/plugins/fleet/server/services/agents/crud.ts +++ b/x-pack/plugins/fleet/server/services/agents/crud.ts @@ -405,6 +405,7 @@ export async function bulkUpdateAgents( { update: { _id: agentId, + retry_on_conflict: 3, }, }, { diff --git a/x-pack/plugins/fleet/server/services/agents/reassign.test.ts b/x-pack/plugins/fleet/server/services/agents/reassign.test.ts index a54c2cb56c944..3fb9d2ee1f33b 100644 --- a/x-pack/plugins/fleet/server/services/agents/reassign.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/reassign.test.ts @@ -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); + }); }); diff --git a/x-pack/plugins/fleet/server/services/agents/reassign_action_runner.ts b/x-pack/plugins/fleet/server/services/agents/reassign_action_runner.ts index 55c0e00728d16..96405e464b358 100644 --- a/x-pack/plugins/fleet/server/services/agents/reassign_action_runner.ts +++ b/x-pack/plugins/fleet/server/services/agents/reassign_action_runner.ts @@ -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, @@ -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;