Skip to content

[ISSUE #11603]Reject AutoSwitch HA truncation below the confirmed offset - #11610

Open
wozhenbang2004 wants to merge 4 commits into
apache:developfrom
wozhenbang2004:11603-confirmed-truncation-guard
Open

wozhenbang2004 wants to merge 4 commits into
apache:developfrom
wozhenbang2004:11603-confirmed-truncation-guard

Conversation

@wozhenbang2004

@wozhenbang2004 wozhenbang2004 commented Nov 25, 2026 •

Copy link
Copy Markdown

Related to #11603.

A replica now remembers a refused truncation across reconnects and restarts. A restarting broker also waits for the Controller’s election result before opening for writes; registration alone no longer makes it writable.

For recovery, the runbook keeps the original store offline and rebuilds a replica under a new identity from a source the operator has verified. The replacement copies the selected history. It does not merge messages from the offline store.

I built the Broker and Controller modules with JDK 8 and ran local checks for planned failover, a graceful DLedger Controller restart, and planned failover with a single-member JRaft Controller. I also ran a planned replica replacement with DLedger: the replacement matched the source’s maximum and confirmed offsets and message contents, and the retired store stayed unchanged. That run did not exercise recovery after an unclean election.

This change requires all Controllers to be upgraded before any upgraded Broker starts. Is rebuilding under a new identity an acceptable recovery path here, or should the original replica be able to rejoin in place?

@codecov-commenter

codecov-commenter commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.47904% with 198 lines in your changes missing coverage. Please review.
✅ Project coverage is 51.45%. Comparing base (1f083eb) to head (cf0f0f5).
⚠️ Report is 3 commits behind head on develop.

Files with missing lines Patch % Lines
...g/apache/rocketmq/common/utils/CheckpointFile.java 0.00% 58 Missing ⚠️
...he/rocketmq/broker/controller/ReplicasManager.java 38.46% 32 Missing and 8 partials ⚠️
...cketmq/store/ha/autoswitch/AutoSwitchHAClient.java 49.18% 21 Missing and 10 partials ⚠️
...etmq/store/ha/autoswitch/ConfirmedOffsetGuard.java 76.81% 12 Missing and 4 partials ⚠️
...ketmq/store/ha/autoswitch/AutoSwitchHAService.java 76.31% 6 Missing and 3 partials ⚠️
...e/rocketmq/store/ha/autoswitch/EpochFileCache.java 86.88% 5 Missing and 3 partials ⚠️
...tmq/controller/impl/manager/BrokerReplicaInfo.java 36.36% 3 Missing and 4 partials ⚠️
...q/controller/impl/manager/ReplicasInfoManager.java 84.78% 3 Missing and 4 partials ⚠️
...ister/RegisterBrokerToControllerRequestHeader.java 0.00% 6 Missing ⚠️
...ster/RegisterBrokerToControllerResponseHeader.java 0.00% 6 Missing ⚠️
... and 4 more
Additional details and impacted files
@@              Coverage Diff              @@
##             develop   #11610      +/-   ##
=============================================
+ Coverage      50.68%   51.45%   +0.76%     
- Complexity     14782    15091     +309     
=============================================
  Files           1391     1394       +3     
  Lines         103571   104176     +605     
  Branches       13575    13690     +115     
=============================================
+ Hits           52494    53601    +1107     
+ Misses         44819    44225     -594     
- Partials        6258     6350      +92     

☔ 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.

Thanks for picking up #11603. Refusing to truncate confirmed data is the right direction, but with the current check I found two problems, both reproduced end to end at the store level (two DefaultMessageStores driven through AutoSwitchHAService, no controller, on this PR merged with the latest develop, which includes #11588), and compared against develop without this change.

1. The guard is evaluated per handshake, so it only delays the problem and then ends in a silent divergence

Scenario A (the #11603 scenario):

  1. Master (epoch 1) and slave reach maxPhyOffset = 3280; slave confirmOffset = 3280.
  2. The master drops its last commitlog file and restarts: maxPhyOffset = 1580, changeToMaster(1) keeps the epoch entry (1, 0) (#11588).
  3. The slave reconnects: the consistent point is 1580 < 3280, so the handshake is refused, as intended.
  4. The master keeps serving writes (15 messages, maxPhyOffset = 4190).
  5. The next handshake resolves min(3280, 4190) = 3280, which is no longer below confirmOffset, so it is accepted and the slave continues appending from 3280.
develop (no guard) this PR
While the master is behind slave truncates to 1580, confirmOffset becomes 1580 Refusing to truncate slave log below confirmOffset: ... truncateOffset=1580, confirmOffset=3280 (twice)
After the master passes 3280 caught up (4190 / 4190), same message at offset 1700 caught up (4190 / 4190), but the message at offset 1700 differs: master store timestamp ...324770 vs slave ...303546

So the slave ends up keeping the old [1580, 3280) while the master has written different messages at the same offsets in the same epoch, and nothing reports it after the first few seconds. Before this change the outcome was "confirmed data lost, but replicas consistent"; with it the outcome is "replicas silently forked", which is harder to detect and recover from (consume queue offsets on the slave point to different messages, and the slave may later be elected). The window is short: the master only needs to write as much as it lost.

The root cause is that once an epoch is reused after a tail loss, offsets alone cannot tell the two logs apart, so a per-handshake offset comparison cannot stay correct. Some possible directions:

  • Make the refusal sticky: once refused for a given master epoch / epoch start offset, keep refusing while the master stays on that epoch, and persist this so it survives a slave restart (on restart the slave's confirmOffset is still 3280 and the same race applies).
  • Or address it at the source (#11603): do not re-elect a regressed master with the same epoch, or bump the epoch when a master recovers with data loss. Then the epoch entries themselves differ, and the consistent point stays below the slave's confirmOffset, so the refusal becomes stable.

Either way, please add a test along the lines of scenario A that asserts the handshake is still refused after the master has written past the slave's confirmOffset; the current tests only exercise a single truncateSlaveLog call with mocks.

2. With enableElectUncleanMaster=true, the old master can never rejoin

Scenario B:

  1. Master and slave at epoch 1 replicate 10 messages (1580).
  2. The slave goes away; the master writes 5 more messages and the syncStateSet shrinks to {master}. setSyncStateSet sets the master's confirmOffset to its maxPhyOffset (2370), which is persisted in the checkpoint.
  3. The master crashes; the lagging broker is elected with epoch 2 (unclean election, starting at 1580) and writes 10 messages.
  4. The old master restarts as a slave of the new master: it loads confirmOffset = 2370, the consistent point is 1580.
develop (no guard) this PR
Old master rejoining truncates its unreplicated tail, catches up (3160 / 3160) Refusing ... truncateOffset=1580, confirmOffset=2370 every 5-7 seconds; never catches up

Unclean election accepts data loss by design, so the replica is now stuck until an operator wipes its store. Clean elections are not affected (the master's confirmOffset is the minimum ack of the syncStateSet, so it never exceeds what the newly elected in-sync replica holds). Please decide how this case should behave, e.g. a switch, or letting the controller tell the slave the election was unclean. At minimum, please document the behavior change and the manual recovery procedure in the PR description.

@ShannonDing ShannonDing changed the title Reject AutoSwitch HA truncation below the confirmed offset [ISSUE #11603]Reject AutoSwitch HA truncation below the confirmed offset Nov 25, 2026

@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.

Follow-up to my existing change request, with a correction to the test assessment and additional validation requirements. I am not adding a new production finding or repeating the two existing blockers and their reproduction scenarios.

Integration baseline: this PR applied to develop@1f083ebea, including #11588. The production behavior under review is the integrated result, not the older PR branch in isolation. The links below are pinned to PR head 5910be5 for navigation. This follow-up is based on code inspection; it does not claim any additional end-to-end test runs.

Correction: the equality-boundary test does exercise handshake parsing

My earlier statement that the new tests only exercise a single truncateSlaveLog call with mocks was too broad. In AutoSwitchHAStateValidationTest.java, lines 100-151, the rejection test calls the helper directly, but the equality-boundary test explicitly enters HANDSHAKE, feeds a handshake frame through the reader, and checks the transition to TRANSFER. That is useful coverage and should be retained.

Additional validation needed for the rejection path

The remaining test gap is that rejection is not exercised through that same handshake entry point. The production check is in AutoSwitchHAClient.java, lines 481-492.

Please add a reader-level rejection test: start the client in HANDSHAKE, provide epoch entries that resolve to an offset below the local confirmOffset, and process the frame through the reader. Assert that the handshake fails without calling truncateFiles, entering TRANSFER, or reporting a new replication start offset. Keep the existing successful equality-boundary test as the control.

For the unclean-election recovery mechanism selected to address the existing second blocker, please also add an automated recovery-path test. It should verify the default refusal, then verify that the explicitly authorized recovery path updates both the maximum and confirmed offsets and completes replication with matching message contents. Verifying only that truncation was invoked is not sufficient to establish successful recovery.

These are test/validation requirements, not separate demonstrated production defects. The production blockers and their required behavioral changes remain in the linked review.

@wozhenbang2004

Copy link
Copy Markdown
Author

Thanks for the review, @ShannonDing. I pushed f4cb2ad and a small follow-up in cf0f0f5. The refusal survives reconnects and restarts. The follow-up keeps the write fence and role changes under the same lock, and checks the recorded refusal before the follower does local HA cleanup.

I ran a planned DLedger replica replacement and then restarted the new replica. It kept its new ID, the message contents and offsets still matched the source, and it picked up the next write. The retired store stayed unchanged. This was a planned replacement, not recovery after an unclean election.

Would rebuilding under a new identity and keeping the original store offline satisfy the recovery requirement, or do you want the original replica to rejoin in place?

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.

3 participants