Skip to content

Record UPGRADE results from check-in upgrade_details - #8319

Open
ycombinator wants to merge 18 commits into
elastic:mainfrom
ycombinator:upgrade-results-from-checkin
Open

ycombinator wants to merge 18 commits into
elastic:mainfrom
ycombinator:upgrade-results-from-checkin

Conversation

@ycombinator

@ycombinator ycombinator commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What is the problem this PR solves?

Each UPGRADE ack makes Fleet Server do two refreshed Elasticsearch writes: the result in .fleet-actions-results and the update in .fleet-agents. When Elasticsearch is slow, either write can time out, so the ack returns a 500, the agent retries it up to 5 times, and every retry repeats both writes against the already loaded cluster.

Failed and rolled-back upgrades are also acked without an error, so Kibana's Agent activity counts them as successes. See #8274 for the full design discussion.

How does this PR solve the problem?

The check-in upgrade_details becomes the source of truth for UPGRADE action results, as was always planned in elastic/elastic-agent#3119, and the .fleet-actions-results doc is created once, when the outcome is known:

  • UPG_FAILED records error_msg once the failure is final: a download failure can be retried until metadata.retry_until, so it is recorded only after that deadline has passed (plus a one minute clock-skew grace). Failures with no retry_until, such as extract or replace failures, are recorded immediately.
  • UPG_ROLLBACK records an error built from metadata.reason, unless the UPGRADE action requested the rollback (data.rollback), in which case it records a success.
  • A check-in without upgrade_details records a success when the stored upgrade shows evidence of completion: the agent's version changed, or the stored state is UPG_WATCHING. A stored UPG_FAILED or UPG_ROLLBACK whose upgrade_started_at is still set has no result yet (creating it failed), so it is recorded with a generic error. Other stored states, for example a canceled UPG_SCHEDULED, record nothing.

An ack whose action matches the stored upgrade_details.action_id and carries no error returns 200 without writing; otherwise it writes as before, but without refreshing the result create.

Known limitation: for upgrades to SNAPSHOT versions, the Agent clears upgrade_details.metadata.retry_until after resolving the snapshot build ID, even though the artifact download keeps retrying. A transient download failure after that point is recorded as a final failure even if a later retry succeeds. Upgrades to released versions are not affected.

Scale tests

Ran /perf-tests (10k agents, serverless QA) on this PR and compared it with a baseline run on main.

main (#60) This PR (#59)
Upgrade action → delivered to agent on check-in, p95 95.6 s 96.5 s
Upgrade delivered → ack, p95 601 s 380 s
Agents that sent an UPGRADE ack 10,000 4,413
Fleet Server HTTP 5xx 296 261
Fleet Server HTTP 429 61.2k 55.4k
Elasticsearch circuit breaker errors / rejections 0 / 0 0 / 0
unable to record the upgrade result, retrying on a later check-in 0 454
Upgrade step completed in 11m37s hit the 16 min poll limit (9,992 of 10,000 acked)
  • Check-in delivery latency is unchanged.
  • Elasticsearch load during the upgrade stalls on both: slow small writes to the single-shard .fleet-agents-7. This happened on main too, so it isn't specific to this change. Run-to-run ES sizing and autoscaling varied a lot.
  • The retry message only appears with this PR: slow .fleet-agents writes cancel the request context and the result is retried on a later check-in.

How to test this PR locally

Run go test ./internal/pkg/api/ ./internal/pkg/dl/ for the unit tests. The integration tests are in internal/pkg/server/upgrade_results_integration_test.go and need the docker compose environment: TEST_RUN=Test_Upgrade_Results mage test:integration.

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 commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in ./changelog/fragments using the changelog tool

Related issues

🤖 Generated with Claude Code

Create the UPGRADE action result from the check-in upgrade_details once the
outcome is known (UPG_FAILED, UPG_ROLLBACK, or the check-in without
upgrade_details that ends the watch period) instead of on every UPGRADE ack.
An ack that matches the stored upgrade_details.action_id and carries no error
returns 200 without writing; otherwise it writes as before, but without
refreshing the result create.

Failed and rolled-back upgrades are now recorded with an error, so they count
as failures in Kibana's Agent activity.

Closes elastic#8274

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@ycombinator ycombinator added the enhancement New feature or request label Nov 20, 2026
Copilot AI balanced review requested due to automatic review settings November 20, 2026 21:04
@mergify

mergify Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

This pull request does not have a backport label. Could you fix it @ycombinator? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-./d./d is the label to automatically backport to the 8./d branch. /d is the digit
  • backport-active-all is the label that automatically backports to all active branches.
  • backport-active-8 is the label that automatically backports to all active minor branches for the 8 major.
  • backport-active-9 is the label that automatically backports to all active minor branches for the 9 major.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Retryable upgrade failures are prematurely recorded as immutable final failures, while incorrect mock expectations cause the new upgrade tests to fail before validating the behavior.

4 open issues
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file
What changed in this PR

This PR moves UPGRADE result recording to check-in upgrade_details, reducing repeated Elasticsearch writes from acknowledgments and reporting failed or rolled-back upgrades in Agent activity.

Changes:

  • Record results when check-ins report failure, rollback, or completion of a stored upgrade.
  • Skip matching, error-free UPGRADE acknowledgments and remove refreshes from UPGRADE result creates.
  • Extend the stored upgrade schema and add unit tests, integration tests, and a changelog entry.
File Description
model/​schema.json Define stored upgrade action, state, and error fields.
internal/​pkg/​server/​upgrade_results_integration_test.go Test upgrade outcomes and acknowledgment behavior through HTTP and Elasticsearch.
internal/​pkg/​model/​schema.go Add generated upgrade-detail fields.
internal/​pkg/​dl/​action_results.go Make result-create refresh behavior caller-controlled.
internal/​pkg/​api/​handleCheckin.go Create upgrade results from check-in outcomes.
internal/​pkg/​api/​handleCheckin_test.go Cover result creation, duplicate suppression, and write failures.
internal/​pkg/​api/​handleAck.go Skip matching upgrade acknowledgments and avoid result refreshes.
internal/​pkg/​api/​handleAck_test.go Test acknowledgment suppression and fallback writes.
changelog/​fragments/​1795207179-record-upgrade-results-from-checkin-upgrade-details.yaml Document the upgrade-result behavior change.

Review effort: Balanced

Comment thread internal/pkg/api/handleCheckin.go Resolved
Comment thread internal/pkg/api/handleCheckin_test.go Resolved
Comment thread internal/pkg/api/handleCheckin.go Resolved

id := acr.ActionID + ":" + acr.AgentID
_, err = bulker.Create(ctx, index, id, body, bulk.WithRefresh())
_, err = bulker.Create(ctx, index, id, body, opts...)

@ycombinator ycombinator Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed via e76a0a07ce6

@ycombinator ycombinator added backport-skip Skip notification from the automated backport with mergify Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team labels Nov 20, 2026
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 18:53
ycombinator and others added 2 commits November 23, 2026 10:56
The action cache only retains the action ID and type, so a cache hit
produced an UPGRADE result with empty namespaces.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved upgrade-state handling can skip missing failure results, discard failure metadata, and incorrectly record canceled upgrades as successful.

5 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Cached actions lose namespaces needed for result visibility

internal/​pkg/​api/​handleCheckin.go:940

GetAction returns only the action ID and type, not namespaces (internal/pkg/cache/cache.go:112–150). Progress check-ins populate this cache, so completion can create a result without the original namespaces, breaking namespace-scoped activity visibility. Fetch the full action here, or extend the cache to preserve namespaces.

1 resolved since last review
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file

Review effort: Balanced

Comment thread internal/pkg/api/handleCheckin.go Resolved
Comment thread internal/pkg/api/handleCheckin.go Resolved
Copilot AI balanced review requested due to automatic review settings November 23, 2026 18:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Critical check-in logic can permanently suppress missing upgrade results and irreversibly record retryable download failures or successful user-requested rollbacks as terminal errors.

4 open issues
3 previously missed issues

In code that hasn't changed since last review

High severity User-requested rollbacks are incorrectly recorded as failures

internal/​pkg/​api/​handleCheckin.go:788

UPG_ROLLBACK also represents a successful user-requested rollback: the Agent's default-branch manual rollback path sets this state with a manual-rollback reason. When those details precede the ack for a rollback action, this branch records an immutable error result and the matching ack is skipped, counting a successful requested rollback as a failure. Inspect the action's rollback flag and distinguish requested rollbacks from watchdog-triggered reverts before selecting the result error.

High severity Failure details are cleared before the result is recoverable

internal/​pkg/​api/​handleCheckin.go:877

If an older Fleet Server stored UPG_FAILED or UPG_ROLLBACK but its ack never created a result, the first check-in without details skips that result here and then erases the stored action ID and error. The failure becomes unrecoverable from later check-ins, and a delayed error-free ack can record success instead. Ensure the stored terminal result exists before clearing the details, preserving enough metadata to reconstruct failure and rollback errors.

Medium severity Stale failure reports can clear a newer upgrade marker

internal/​pkg/​api/​handleCheckin.go:820

After Kibana dispatches another upgrade, a check-in can still report the previous failure—the Agent's default-branch upgrade-wait helper explicitly accounts for stale UPG_FAILED details. This unconditional write clears the new upgrade_started_at even when recorded is true, and subsequent progress check-ins do not restore it, so the new upgrade loses its Updating marker. Preserve the marker on repeated terminal reports and associate any clearing with the upgrade that actually ended.

1 resolved since last review
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file

Review effort: Balanced

The agent reports UPG_FAILED for each failed download attempt while it
keeps retrying until metadata.retry_until. Treat a UPG_FAILED check-in
as final only when retry_until is missing or has passed (plus a clock
skew grace), so a retried download that succeeds is not recorded as a
failure. The decision uses the check-in alone, so any Fleet Server
instance reaches the same outcome.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 19:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical check-in bugs can lose final retry outcomes and falsely record cancelled scheduled upgrades as successful.

4 open issues
1 resolved since last review
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file

Review effort: Balanced

Comment thread internal/pkg/api/handleCheckin.go Resolved
A check-in without upgrade_details recorded a success for any stored
upgrade, including a scheduled upgrade the agent canceled. Record a
success only for a changed version or a stored UPG_WATCHING, and settle a
stored UPG_FAILED or UPG_ROLLBACK that has not been settled yet
(upgrade_started_at still set) with an idempotent create from the stored
state. Other stored states record nothing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 20:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved check-in handling issues can record permanent failures for retryable downloads, misclassify rollbacks as successes, and discard stored rollback reasons.

5 open issues
2 resolved since last review
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file

Review effort: Balanced

Comment thread internal/pkg/api/handleCheckin.go Resolved
Comment thread internal/pkg/api/handleCheckin.go Resolved
Comment thread internal/pkg/api/handleCheckin.go Outdated
}
return "upgrade failed", true
case ud.State == string(UpgradeDetailsStateUPGROLLBACK) && agent.UpgradeStartedAt != "":
return rollbackErrorMessage(nil), true

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed via 899e1cf.

…esults integration tests

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 20:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The success condition incorrectly treats a version change as proof of upgrade success even when a stored rollback is pending, permanently recording the rollback as a success in the append-only results.

5 open issues
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file

Review effort: Balanced

…keep its reason

A rollback returns the agent to its previous version, so a changed
version does not imply a successful upgrade. Store metadata.reason in
the upgrade details model so an unsettled rollback recovered from the
stored state keeps its reason.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 23:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Discarding retryable upgrade failures can leave the stored state scheduled/requested, allowing a no-error acknowledgment to record immutable success for an upgrade that ultimately fails.

4 open issues
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file

Review effort: Balanced

Comment thread internal/pkg/api/handleCheckin.go Resolved
@ycombinator
ycombinator requested a review from samuelvl November 25, 2026 14:11
@ycombinator
ycombinator marked this pull request as ready for review November 25, 2026 14:11
@ycombinator
ycombinator requested a review from a team as a code owner November 25, 2026 14:11
@mergify

mergify Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

LocalMetadata: json.RawMessage(`{"elastic":{"agent":{"uninstallable":true}}}`),
Outputs: map[string]*model.PolicyOutput{
"default": {
APIKey: "es-output-key",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm addressing the lint failures on lines that are not touched by this PR in #8370 so that this PR here does not get bloated with unrelated changes.

Ignoring a retryable UPG_FAILED left the stored upgrade at UPG_SCHEDULED
or UPG_REQUESTED, so the ack the agent sends for a failed upgrade (without
an error) was taken for a skipped upgrade and recorded a success. Store it
as UPG_DOWNLOADING with the failure as retry_error_msg instead, which also
shows the upgrade as progressed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Comment thread internal/pkg/api/handleCheckin.go Resolved
Comment on lines +138 to +144
// Always read the action from Elasticsearch: the action cache only retains the action ID and
// type, so a cache hit would not provide the namespaces the result must carry. This runs once,
// when the outcome is known, so the extra read is cheap.
actions, err := dl.FindAction(ctx, ct.bulker, actionID)
if err != nil {
return fmt.Errorf("unable to find upgrade action %q: %w", actionID, err)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you had a chance to run the 100k-agent tests with these changes? It would be good to compare check-in latency and Elasticsearch load with main.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chatted in Slack. Agreed to do a 10k test and compare with main for baseline.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated PR description with comparison. See "Scale tests" section.

Comment thread internal/pkg/api/handleAck_test.go Resolved
Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:40
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

A failed result create with an empty upgrade timestamp leaves no pending marker, causing subsequent check-ins and acknowledgments to skip retries and permanently lose the result even after Elasticsearch recovers.

2 open issues
2 resolved since last review
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:50
A failed action lookup or result create is logged and retried by a later
check-in, instead of failing the check-in: the agent must still report its
health and receive actions. The stored upgrade_details are kept so the
retry still has what it needs.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

A failed result creation when upgrade_started_at is already empty permanently loses the action result because subsequent check-ins and acknowledgments skip recovery.

2 open issues
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A failed result creation after a version-inferred success loses its retry path, allowing the next unchanged-version, no-details check-in to clear the action ID without recording the result.

3 open issues
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file

Review effort: Balanced

// Do not fail the check-in (the agent must still report its health and receive
// actions), and keep the stored upgrade_details so the next check-in tries again.
zerolog.Ctx(ctx).Error().Err(err).Str(ecs.ActionID, ud.ActionID).Str(ecs.AgentID, agent.Agent.ID).Msg("unable to record the upgrade result, retrying on a later check-in")
return nil
@ycombinator

Copy link
Copy Markdown
Contributor Author

/perf-tests

1 similar comment
@ycombinator

Copy link
Copy Markdown
Contributor Author

/perf-tests

Copilot AI balanced review requested due to automatic review settings November 25, 2026 23:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved check-in handling can clear a newly scheduled upgrade’s timestamp and lose terminal results after transient result-creation failures.

4 open issues
1 file not reviewed
  • internal/pkg/model/schema.go: Generated file

Review effort: Balanced

Comment on lines +837 to +838
if resultErr == nil {
doc[dl.FieldUpgradeStartedAt] = nil
@ycombinator
ycombinator requested a review from samuelvl November 26, 2026 11:13

This branch has not been deployed

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

Labels

backport-skip Skip notification from the automated backport with mergify enhancement New feature or 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.

Retried UPGRADE acks overload Elasticsearch; record upgrade results from check-in upgrade_details

3 participants