frotmo opened a new pull request, #11588:
URL: https://github.com/apache/rocketmq/pull/11588

   Fixes #11585
   
   ## Summary
   
   In controller mode, when the original master restarts and the controller 
re-elects it with the same `MasterEpoch`, the broker rewrote the start offset 
of the existing epoch entry to its current max physical offset (e.g. `1-0` -> 
`1-5802`). The slave still held `1-0`, and `EpochFileCache#findConsistentPoint` 
requires both the epoch number and the start offset to match, so the slave 
failed with `Failed to find a consistent point` forever and never rejoined the 
SyncStateSet. The master kept accepting writes with an effective replication 
factor of 1.
   
   ## Root cause
   
   1. The controller keeps the epoch unchanged: 
`ReplicasInfoManager#electMaster` returns the current `masterEpoch` when the 
old master is elected again, and `registerBroker` does the same while the 
master is considered alive.
   2. On broker restart, `ReplicasManager` starts with `masterEpoch = 0` / 
`masterBrokerId = null`, so it takes the full `changeToMaster(masterEpoch)` 
path, where the recovered epoch entry with the same epoch number gets truncated 
and re-appended at `maxPhyOffset`.
   3. During the slave handshake, `findConsistentPoint` compares `(epoch, 
startOffset)` pairs, so `1-5802` and `1-0` never match.
   
   ## Fix
   
   Keep the existing epoch entry when the incoming `masterEpoch` already exists 
locally; a new epoch is still appended at `maxPhyOffset` as before. The epoch 
start offset is an immutable fact: a restarted master continues the same epoch, 
so its start offset must not move. `changeToMaster` and 
`changeToMasterWhenLastRoleIsMaster` now share this logic.
   
   ## Tests
   
   - New `AutoSwitchHAServiceTest` (3 cases, runs in CI):
     - same-epoch restart keeps the original start offset;
     - a new epoch is still appended at the max physical offset;
     - `changeToMasterWhenLastRoleIsMaster` behaves the same way.
   - New integration test in `AutoSwitchHATest` (the class is `@Ignore`d due to 
HA port flakiness; can be run manually): the master restarts with the same 
epoch, the existing slave reconnects, is added back to the SyncStateSet and 
continues replicating.
   - Red/green verification: without the fix the unit test fails with 
`expected:<0> but was:<5802>` and the integration test fails with `expected:<0> 
but was:<1580>`; with the fix all related tests pass:
     `mvn -pl store 
-Dtest='AutoSwitchHAServiceTest,EpochFileCacheTest,AutoSwitchHAStateValidationTest'
 test` (14/14).
   
   ## Notes
   
   - Clusters already in the broken state keep the rewritten epoch file after 
upgrading and still need a manual resync (or full rebuild) of the affected 
slave; this fix prevents new occurrences.
   - No controller/protocol change is included; only the broker-side epoch 
handling is corrected.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to