Skip to content

[Store] Sync HA applied sequence metric with recovered standby baseline - #4402

Merged
ykwd merged 5 commits into
kvcache-ai:mainfrom
pjdurden:fix/4399
Nov 4, 2026
Merged

ykwd merged 5 commits into
kvcache-ai:mainfrom
pjdurden:fix/4399

Conversation

@pjdurden

@pjdurden pjdurden commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Issue #4399: HA snapshot recovery does not update applied sequence metric

1. Root cause

The ha_oplog_applied_sequence_id gauge is only written in
OpLogApplier::ApplyOpLogEntry(). When a standby installs a recovered baseline
(batch OpLog snapshot bootstrap, warm start from local state, an empty
baseline, the preserved promotion cursor on restart, or a runtime
re-bootstrap), the applier position is set with OpLogApplier::Recover() and
HotStandbyService stores the baseline in applied_seq_id_/primary_seq_id_.
Neither step touches the gauge. If no OpLog entries follow the baseline, the
gauge keeps its initial or stale value while GetSyncStatus().applied_seq_id
reports the real position.

2. Fix

HotStandbyService now sets the gauge wherever it installs a recovered
baseline as the live standby state:

  • PrepareBootstrapBaselineLocked, preserved promotion cursor restart
  • PrepareBootstrapBaselineLocked, shared path (warm start from local state,
    or the zero baseline from LoadSnapshotBaselineLocked)
  • LoadBatchOpLogSnapshotBaselineLocked (batch OpLog snapshot bootstrap)
  • RebootstrapBatchOpLog, after swapping in the candidate state

The write is not in OpLogApplier::Recover(). The batch snapshot provider
calls Recover() on temporary candidate appliers, including Recover(0) after
a failed attempt, so a write there would move the gauge while the old standby
state is still serving. The other applied_seq_id_ stores (promotion, the
replication loop) follow entries the applier already published.

3. Files changed

  • mooncake-store/src/hot_standby_service.cpp: gauge writes at the four
    baseline installation sites above.
  • mooncake-store/tests/ha/snapshot/batch_oplog/rebootstrap_test.cpp:
    • SnapshotBootstrapWithoutSuffixSetsAppliedMetric: batch snapshot at seq 1
      with no OpLog tail. The gauge and the snapshot applied_batch are reset
      before start; after start the gauge reads 1, applied_batch is 1, and
      GetSyncStatus() reports applied_seq_id == 1 and lag_entries == 0.
    • RestartResumesPreservedPromotionCursor: gauge reset to 0 before restart
      and must read 50 after.
    • ExpectRecovered: gauge reads 4 after re-bootstrap plus suffix replay.
  • mooncake-store/tests/ha/standby/hot_standby_service_test.cpp:
    • TestWarmStart_WithLocalState: gauge reset after Stop(); the restart
      has no new entries, so only the recovered baseline can bring it back to 1.
    • TestWarmStart_WithoutLocalState: gauge seeded with a stale 42 before
      Start(); both the gauge and GetSyncStatus().applied_seq_id must read 0.

4. Risk

  • Low: only gauge writes next to existing applied_seq_id_ stores, no control
    flow change.
  • HAMetricManager is a process-wide singleton. Temporary candidate appliers
    still update the gauge through ApplyOpLogEntry while a re-bootstrap replays
    a suffix. That is existing behavior and is left for a follow-up.
  • The exported lag gauge is out of scope here.

5. Verification

Signed-off-by: pjdurden <prajjwalchittori1@gmail.com>

@he-yufeng he-yufeng left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maintainer pass on this since it sits in the same HA metrics correctness area as my #3806.

I walked the coverage question the fix has to answer: which applied_seq_id_.store sites can leave the gauge stale. On current main there are six. The four you patched (both branches of PrepareBootstrapBaselineLocked, LoadBatchOpLogSnapshotBaselineLocked, RebootstrapBatchOpLog) are exactly the baseline-installation points where no ApplyOpLogEntry runs afterward, so the gauge would otherwise keep its initial value. The two you did not touch check out as not needing it: PreparePromotionLocked only runs after final catch-up, and final catch-up applies through the applier, which already moves the gauge per entry; the two stores in the poll loop mirror oplog_applier_->GetExpectedSequenceId() after the applier has already published each entry to the gauge. The provider-level Recover() calls in batch_oplog_snapshot_provider.cpp are always followed by one of your patched service-level baseline stores, so covering the service layer covers them too. Coverage looks complete for the issue's scope.

The tests do the two things that matter here. SnapshotBootstrapWithoutSuffixSetsAppliedMetric is the issue's exact repro (snapshot with an empty incremental tail), and the snapshot-only case that presets the gauge to 42 and expects 0 pins overwrite semantics rather than "only sets when previously zero", which is the failure mode that would regress silently if someone later guarded the setter.

One scope note for the record: this pins the gauge to the standby's own recovery position. Anything consuming the gauge as a lag signal during promotion still reads it through the promotion path's own timing, which is unchanged here.

Verification on my side was static mechanism review of the patch against current main plus your green CI (CTest passes the HA suites); I did not rebuild locally. Commenting rather than approving so another maintainer can take the formal pass.

@he-yufeng

Copy link
Copy Markdown
Collaborator

Checked the coverage claim against the tree, and it holds: the four sites here are exactly the baseline commits that bypass the applier. The other applied_seq_id_.store callers do not need the gauge write:

  • PreparePromotionLocked and the two replication-loop stores all derive from the applier's expected sequence, which only advances through ApplyOpLogEntry, and that already writes the gauge.
  • The placement at the service level rather than in OpLogApplier::Recover() is the right call: rebootstrap runs Recover on throwaway candidate appliers, including Recover(0) after a failed attempt, so a gauge write there would flap the exported value while the old standby state is still live.

The snapshot-only test resetting a stale 42 down to 0 is the right pin: the gauge should track the committed baseline, not preserve history.

One adjacent thing surfaced while checking the writers: set_oplog_last_sequence_id has no production caller anywhere, only ha_metric_manager_test exercises it, so ha_oplog_last_sequence_id exports 0 forever in a real deployment. Not this PR's problem, but if that gauge is superseded by set_batch_record_durable_sequence we should delete it, and if it is not, it has the same staleness bug this PR fixes. Worth a follow-up issue either way.

Drop the snapshot-only bootstrap hunk and its test, removed upstream by
kvcache-ai#4406. Also set the applied-sequence gauge on the preserved promotion
cursor restart path added by kvcache-ai#4559, which installs a baseline without
going through the applier.
Copilot AI balanced review requested due to automatic review settings October 16, 2026 16:34

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@pjdurden

Copy link
Copy Markdown
Contributor Author

Merged main in (4d7c2e9). #4406 removed the snapshot-only bootstrap path, so that hunk and its test are gone. #4559 added a fifth baseline install that skips the applier, the preserved promotion cursor restart in PrepareBootstrapBaselineLocked, so it now sets the gauge too and RestartResumesPreservedPromotionCursor checks it. CTest will stay red until #4729 lands, main currently fails to build scenario.cpp after #4314.

@Icedcoco

Copy link
Copy Markdown
Collaborator

Thanks for updating this after the bootstrap changes on main. Keeping the new gauge writes at the points where the service installs a recovered baseline makes sense, especially since Recover() also runs on temporary candidates. The preserved-promotion restart coverage is helpful too.

Could you please address a few remaining items?

  1. Extend the existing warm-start tests to cover the metric.
    The no-suffix snapshot and preserved-promotion tests both take early-return branches, so they do not exercise the shared baseline-installation path in PrepareBootstrapBaselineLocked. In TestWarmStart_WithLocalState, resetting the gauge after Stop() and checking that restart restores it to the recovered sequence, with no new entries, would cover that path. In TestWarmStart_WithoutLocalState, seeding a stale nonzero value before Start() and checking that both the gauge and sync status report zero would restore the zero-baseline regression coverage after the legacy tests were removed.

  2. Refresh from main and rerun CI.
    [Store] Fix BatchProbeKey call in master_service DSL scenario (CI failure hot fix) #4729 has now merged, so the BatchProbeKey compilation issue mentioned in the latest update should be resolved after updating the branch. A fresh run of the relevant HA tests and required checks would help confirm the current patch.

  3. Update the PR description and test list.
    The body still references the removed snapshot-only path and hot_standby_snapshot_bootstrap_test.cpp. Please update those sections to describe the current baseline-installation sites, the preserved-promotion restart test, and the latest validation results.

As an optional improvement, the no-suffix test could also check the recovered batch cursor and GetSyncStatus().lag_entries alongside the applied sequence. This would strengthen the internal consistency coverage requested in #4399; the exported lag gauge can remain with the separate metrics work.

The temporary-applier metric publication issue already documented in the description seems reasonable to track in a separate follow-up. The requests above focus on completing the coverage and validation for this baseline fix.

Signed-off-by: pjdurden <prajjwalchittori1@gmail.com>
Signed-off-by: pjdurden <prajjwalchittori1@gmail.com>
@pjdurden

Copy link
Copy Markdown
Contributor Author

Merged main in (b994803, includes #4729) and extended the warm-start tests in 5c7c314. TestWarmStart_WithLocalState resets the gauge after Stop() and expects 1 after the restart with no new entries. TestWarmStart_WithoutLocalState seeds 42 and expects both the gauge and GetSyncStatus().applied_seq_id to be 0. I also took the optional one: the no-suffix test now checks the recovered applied_batch and lag_entries == 0. The description is updated to list the current baseline sites and tests. I could not build locally, so CI on this push is the validation.

@github-actions github-actions Bot added the size/S 0-99 changed lines (additions + deletions) label Oct 28, 2026

@Icedcoco Icedcoco left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. One non-blocking test gap: ExpectRecovered() checks the gauge after replaying a suffix, but ApplyOpLogEntry() already updates it. A successful rebootstrap with no suffix would directly cover the explicit baseline metric update.

…ffix

Signed-off-by: pjdurden <prajjwalchittori1@gmail.com>
@pjdurden

pjdurden commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

added RebootstrapWithoutSuffixSetsAppliedMetric for the no-suffix case. it publishes a snapshot covering batch 4, zeroes the gauge, then runs Compact() to trigger the re-bootstrap, so the swap in RebootstrapBatchOpLog installs the candidate without ApplyOpLogEntry ever running. it checks suffix_batches == 0, that the gauge and GetSyncStatus().applied_seq_id both read 4, and that the handoff carries the new snapshot. I could not build it locally, so CI on this push is the check. a7c41fa

@ykwd
ykwd merged commit 7a130f4 into kvcache-ai:main Nov 4, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci size/S 0-99 changed lines (additions + deletions) Store

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants