Skip to content

Commit

Permalink
[8.x] [Fleet] Retry on conflict when updating agent space (#205964) (#…
Browse files Browse the repository at this point in the history
…206302)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Fleet] Retry on conflict when updating agent space
(#205964)](#205964)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nicolas
Chaulet","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-01-10T15:44:33Z","message":"[Fleet]
Retry on conflict when updating agent space
(#205964)","sha":"eddbbb1895920c36e7687854a10d598f913e206c","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v9.0.0","backport:prev-minor"],"title":"[Fleet]
Retry on conflict when updating agent
space","number":205964,"url":"https://github.com/elastic/kibana/pull/205964","mergeCommit":{"message":"[Fleet]
Retry on conflict when updating agent space
(#205964)","sha":"eddbbb1895920c36e7687854a10d598f913e206c"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/205964","number":205964,"mergeCommit":{"message":"[Fleet]
Retry on conflict when updating agent space
(#205964)","sha":"eddbbb1895920c36e7687854a10d598f913e206c"}}]}]
BACKPORT-->

Co-authored-by: Nicolas Chaulet <[email protected]>
  • Loading branch information
kibanamachine and nchaulet authored Jan 10, 2025
1 parent 918a42c commit 7cc634c
Showing 1 changed file with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,6 @@ export async function updateAgentPolicySpaces({
ignore_unavailable: true,
refresh: true,
});
await esClient.updateByQuery({
index: AGENTS_INDEX,
query: {
bool: {
must: {
terms: {
policy_id: [agentPolicyId],
},
},
},
},
script: `ctx._source.namespaces = [${newSpaceIds.map((spaceId) => `"${spaceId}"`).join(',')}]`,
ignore_unavailable: true,
refresh: true,
});

const agentIndexExists = await esClient.indices.exists({
index: AGENTS_INDEX,
Expand All @@ -195,6 +180,21 @@ export async function updateAgentPolicySpaces({
break;
}

const agentBulkRes = await esClient.bulk({
operations: agents.flatMap(({ id }) => [
{ update: { _id: id, _index: AGENTS_INDEX, retry_on_conflict: 5 } },
{ doc: { namespaces: newSpaceIds } },
]),
refresh: 'wait_for',
index: AGENTS_INDEX,
});

for (const item of agentBulkRes.items) {
if (item.update?.error) {
throw item.update?.error;
}
}

const lastAgent = agents[agents.length - 1];
searchAfter = lastAgent.sort;

Expand Down

0 comments on commit 7cc634c

Please sign in to comment.