Skip to content

[ISSUE #11603] Refuse slave truncation below confirmOffset in auto-switch HA - #11617

Open
frotmo wants to merge 1 commit into
apache:developfrom
frotmo:fix/truncate-below-confirm-offset
Open

frotmo wants to merge 1 commit into
apache:developfrom
frotmo:fix/truncate-below-confirm-offset

Conversation

@frotmo

@frotmo frotmo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Related issue: #11603 (this PR implements the store-side guard; controller-side election/fencing remains tracked there).

Summary

In controller mode with auto-switch HA, when the master restarts with a regressed commitlog (e.g. async-flush tail loss) and is re-elected, a slave can resolve a consistent point below its own confirmOffset and truncate messages that were already acknowledged to producers, with no error or alarm. Reproduced at store level: a slave with max/confirm = 3280 was truncated to 1580 after the master recovered to 1580.

This PR adds a guard in AutoSwitchHAClient#doTruncate: refuse the truncation when truncateOffset < confirmOffset by default, log an ERROR with the epoch details, and leave the log untouched. The slave keeps retrying the handshake instead of silently dropping confirmed data.

Behavior change

  • Default: refuse to truncate below the local confirmOffset; the handshake fails with a clear ERROR (including epoch entries and offsets) and retries.
  • enableTruncateBelowConfirmOffset (default false): explicit disaster-recovery override for operators who accept the data loss.
  • Truncation to exactly confirmOffset is still allowed (only < is refused).

Tests

  • New AutoSwitchHAClientTest:
    • refuses when truncateOffset < confirmOffset and never calls truncateFiles;
    • allows truncation at exactly confirmOffset;
    • allows truncation below confirmOffset when the override is enabled.
  • Red/green: without the guard the refusal test fails with NeverWantedButInvoked: truncateFiles.
  • Related store suite (AutoSwitchHAClientTest, EpochFileCacheTest, AutoSwitchHAStateValidationTest): 14/14 pass.
  • End-to-end check (two stores in auto-switch mode, master restarted with its last commitlog file removed): the slave keeps max/confirm at 3280 and logs
    Refuse to truncate slave log to 1580, which is below the local confirmOffset 3280....
    Note: reaching this path after a same-epoch restart also needs the epoch-preserving fix from [ISSUE #11585] Fix AutoSwitchHA slave unable to rejoin after same-epoch master restart #11588 (applied locally for the e2e check); on the current develop baseline the slave stays in HANDSHAKE before reaching doTruncate. The guard itself is independent of [ISSUE #11585] Fix AutoSwitchHA slave unable to rejoin after same-epoch master restart #11588 and also protects the pre-existing truncation path for older epochs.

Relation to other work

…uto-switch HA

A slave whose master restarted with a regressed commitlog resolved a
consistent point below its own confirmOffset and truncated messages that
were already acknowledged to producers. Refuse the truncation by default
and log an error; enableTruncateBelowConfirmOffset is added as an explicit
disaster-recovery override.

Add unit tests for the refused case, the exact-confirm boundary, and the
override.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 50.71%. Comparing base (38ddb48) to head (dc66b3a).
⚠️ Report is 25 commits behind head on develop.

Additional details and impacted files
@@              Coverage Diff              @@
##             develop   #11617      +/-   ##
=============================================
+ Coverage      50.04%   50.71%   +0.67%     
- Complexity     14515    14795     +280     
=============================================
  Files           1391     1391              
  Lines         103460   103569     +109     
  Branches       13558    13577      +19     
=============================================
+ Hits           51777    52526     +749     
+ Misses         45449    44784     -665     
- Partials        6234     6259      +25     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ShannonDing ShannonDing left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The default-deny check and explicit disaster-recovery override are useful, but I am requesting changes because the default protection can stop rejecting a known-inconsistent history on a later handshake. There is also a separate validation gap in the recovery tests.

Integration baseline: this PR applied to develop@1f083ebea, including #11588. All behavioral findings below refer to that integrated result, not the older PR branch in isolation. Code links are pinned to PR head dc66b3a. This is a code-path review; I have not rerun Maven or the two-store end-to-end scenarios for this PR.

1. [P1, production blocker] The refusal is forgotten when the same-epoch master writes past the slave's confirmed offset

Location: AutoSwitchHAClient.java, lines 462-471, the new guard in doTruncate.

Minimal triggering sequence:

  1. Master and slave have epoch entry (1, 0); the slave has maxOffset = confirmOffset = 3280. Keep enableTruncateBelowConfirmOffset=false.
  2. The master loses its tail and recovers to 1580, then resumes serving in the same epoch. With #11588, its epoch start remains 0.
  3. The first handshake resolves a consistent point of 1580. The guard correctly refuses 1580 < 3280.
  4. The master writes different new messages in the same epoch and reaches 4190.
  5. The next handshake resolves min(3280, 4190) = 3280. The guard now passes, and the slave resumes replication from 3280.

The slave retains its old [1580, 3280) while the master has rewritten that interval. The replicas can end up with equal maximum offsets but different message histories. Epoch matching checks epoch metadata and offsets, not message contents, and this guard does not retain the earlier evidence of inconsistency. The failure is therefore not merely incomplete controller fencing: the new reject-then-accept path preserves an old tail and later treats the rewritten history as consistent.

The recovery override does not address this sequence, which occurs with the override disabled throughout. Enabling it after the divergence has formed also cannot force truncation back to the originally rejected 1580: the newly computed consistent point is already 3280.

Required changes:

  • Once this inconsistency is detected, do not resume replication solely because the master's offset has increased.
  • If implementing a persistent refusal on the slave, preserve it across slave restarts and define an explicit, safe clearance condition based on validated history or authorized recovery. A numeric catch-up alone is not such a condition.
  • Alternatively, prevent the regressed master from producing this history through effective fencing or correct epoch handling before it accepts replacement writes. A later epoch increment alone is not sufficient evidence that the earlier fork is resolved.
  • Add regression coverage for refusal, same-epoch writes past the original confirmed offset, and a subsequent handshake, including a slave restart between attempts. Assert that incompatible history is not accepted; equal final offsets alone are insufficient.

This does not require implementing the entire controller-side design in this PR, but the store-side safety net must remain effective after it has detected the dangerous truncation.

2. Test insufficiency, not a separate demonstrated production defect: the allowed-truncation tests do not establish successful recovery

Location: AutoSwitchHAClientTest.java, lines 46-123.

The equality and override-enabled tests call doTruncate reflectively and verify that truncateFiles was invoked, but do not assert that doTruncate returned true. The mock maximum offset does not decrease after truncation, and successful socket writes are not configured. Consequently, a truncation invocation does not demonstrate successful offset reporting or completion of the handshake.

Required validation before merge:

  • Model the post-truncation maximum and confirmed offsets, or use real stores; assert successful completion, the TRANSFER state, and any required offset report.
  • Cover an unclean-election recovery: the former master has persisted confirmOffset=2370, while the new master's epoch 2 starts at 1580. Verify that the default setting refuses truncation without changing the log; then enable the explicit recovery override on the recovering slave and verify truncation and replication complete with matching message contents. Finally, disable the override and verify the protection is active again.
  • Document which replica receives the override, what data is being relinquished, and that the override must be disabled after recovery.

The explicit override provides a recovery mechanism for this unclean-election case, so I am not reporting the default refusal there as a second production defect. The missing success assertions are a validation gap, separate from the P1 production blocker above.

@dengxiaofu

Copy link
Copy Markdown
Contributor

Hi! I have a question about the confirmOffset guard.

If the first handshake refuses truncation because 1580 < 3280, but the master later writes new messages up to 4190, the next handshake may resolve to 3280 and allow replication to resume.

How does this implementation ensure that the slave's retained message history is still consistent with the master?

Thanks!

@frotmo

frotmo commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful read — you're right, and that's exactly the gap in the current patch. The guard is evaluated per handshake and keeps no state, so once the master grows past 3280 the consistent point becomes 3280, the guard passes, and the slave resumes replication while retaining its old [1580, 3280). The replicas can end up with equal max offsets but different message histories, and nothing compares contents. This is the P1 raised in @ShannonDing's review above.

We're consolidating this work with #11619 — see #11619 (comment) and #11619 (comment). The plan is to make the refusal sticky: persist a fail-closed marker when a truncation below confirmOffset is detected, keep rejecting later handshakes (including after a slave restart) until an explicit one-shot recovery authorization is used (bound to the expected master history and truncation offset), or the replica is rebuilt. I'll close this PR once we've aligned, so the discussion can continue there.

Thanks for flagging it — this is a good question.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants