added retry_on_conflict and report errors from bulk agent update (#142088)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Julia Bardi 2022-09-29 13:57:16 +02:00 committed by GitHub
parent 16ca2d2895
commit 13824dd0dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View file

@ -405,6 +405,7 @@ export async function bulkUpdateAgents(
{
update: {
_id: agentId,
retry_on_conflict: 3,
},
},
{

View file

@ -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);
});
});

View file

@ -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;