Skip to content

[9.5](backport #7712) feat: add synchronous enrollment write strategy to prevent ghost agents - #7720

Open
mergify[bot] wants to merge 4 commits into
9.5from
mergify/bp/9.5/pr-7712
Open

[9.5](backport #7712) feat: add synchronous enrollment write strategy to prevent ghost agents#7720
mergify[bot] wants to merge 4 commits into
9.5from
mergify/bp/9.5/pr-7712

Conversation

@mergify

@mergify mergify Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

What is the problem this PR solves?

When an enrollment write is committed to fleet-server's in-memory bulk queue but the HTTP response is lost before it reaches the agent (network drop, EOF), the agent retries enrollment. If the retry lands on a different pod, that pod's FindAgent search finds nothing — the write hasn't been flushed to Elasticsearch yet — and creates a second agent document. These duplicate documents ("ghost agents") accumulate and do not resolve on their own.

The batched pre-refresh dedup approach in #7662 cannot prevent this: cross-pod retries are never in the same flush batch, so in-batch dedup never fires. Evidence from the 30k serverless checkin scale test: 35 ghost agents with enrolled_at timestamps clustered in a 21-second window (11:53:42–11:54:03 UTC), zero ErrEnrollDuplicate 429s — confirming in-batch dedup never triggered for any of the 35 retries.

How does this PR solve the problem?

A new feature flag, inputs[].server.feature_flags._sync_enrollment_write (default: false), selects the enrollment write strategy.

When false (default), the existing async bulk queue path is unchanged.

When true, createFleetAgent writes the agent document directly to Elasticsearch using op_type=create&refresh=wait_for. ES blocks until the document is committed and visible to search on all shards before returning. The enrollment response is not sent until this write completes, so any retry on any pod finds the existing document immediately. A 409 from op_type=create is treated as success.

The _ prefix on the flag name signals that it is internal and temporary. The intent is to enable it in Staging via serverless-gitops, validate with the 30k checkin scale test, and then remove the flag entirely — making the sync path the permanent enrollment write path.

How to test this PR locally

Enable the flag and run the 30k Fleet Server checkin scale test against Staging. Confirm ghost agent count is 0 after enrollment completes.

Design Checklist

  • I have ensured my design is stateless and will work when multiple fleet-server instances are behind a load balancer.
  • I have or intend to scale test my changes, ensuring it will work reliably with 100K+ agents connected.
  • I have included fail safe mechanisms to limit the load on fleet-server: rate limiting, circuit breakers, caching, load shedding, etc.

Checklist

  • I have added an entry in ./changelog/fragments using the changelog tool

Related issues

…ts (#7712)

* feat: add synchronous enrollment write strategy to prevent ghost agents

Introduce inputs[].server.feature_flags.sync_enrollment_write (default
false). When true, createFleetAgent writes the agent document directly
with op_type=create&refresh=wait_for so the document is committed and
visible to search before the enrollment response is sent. Any retry on
any pod finds the existing document immediately, eliminating ghost agents
caused by the async bulk queue / EOF race. The existing async path is
unchanged when the flag is false.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor: rename feature flag to _sync_enrollment_write to signal temporary status

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: fix createFleetAgent call to pass syncWrite argument

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: add unit tests for sync enrollment write path

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: spell out full config key in SyncEnrollmentWrite comment

The config tag uses an underscore prefix (_sync_enrollment_write) but
the doc comment didn't make the exact key explicit, causing inconsistency
with the PR description. Spell out the full key in the comment.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* chore: remove changelog fragment

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* chore: fix spelling behaviour -> behavior in comment

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: assert op_type=create and refresh=wait_for in sync enrollment tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
(cherry picked from commit d6324c7)

# Conflicts:
#	internal/pkg/api/handleEnroll_test.go
@mergify mergify Bot added backport conflicts There is a conflict in the backported pull request labels Aug 29, 2026
@mergify
mergify Bot requested a review from a team as a code owner August 29, 2026 00:43
@mergify
mergify Bot requested review from macdewee and swiatekm and removed request for a team August 29, 2026 00:43
@mergify mergify Bot added the backport label Aug 29, 2026
@mergify mergify Bot added the conflicts There is a conflict in the backported pull request label Aug 29, 2026
@mergify

mergify Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

Cherry-pick of d6324c7 has failed:

On branch mergify/bp/9.5/pr-7712
Your branch is up to date with 'origin/9.5'.

You are currently cherry-picking commit d6324c7.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   internal/pkg/api/handleEnroll.go
	modified:   internal/pkg/config/input.go

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   internal/pkg/api/handleEnroll_test.go

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@github-actions github-actions Bot added the Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team label Aug 29, 2026
@ycombinator
ycombinator enabled auto-merge (squash) August 29, 2026 00:48
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions github-actions Bot mentioned this pull request Aug 29, 2026
ycombinator
ycombinator previously approved these changes Aug 29, 2026
…9.5 backport

The 9.5 branch already had this test with the old createFleetAgent signature;
the conflict resolution kept the incoming copy too, causing a redeclaration.
Remove the stale copy and keep the new one with the syncWrite parameter.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ycombinator
ycombinator previously approved these changes Aug 29, 2026
… backport

Conflict resolution left two consecutive blank lines before
TestValidateEnrollRequest; mage check:fix requires a single blank line.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@ebeahan

ebeahan commented Aug 31, 2026

Copy link
Copy Markdown
Member

@ycombinator if we want this in the next 9.5.x patch, we need to merge this backport ASAP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport conflicts There is a conflict in the backported pull request Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants