Skip to content

[ISSUE #11585] Fix AutoSwitchHA slave unable to rejoin after same-epoch master restart - #11588

Open
frotmo wants to merge 2 commits into
apache:developfrom
frotmo:fix/autoswitch-same-epoch-master-restart
Open

frotmo wants to merge 2 commits into
apache:developfrom
frotmo:fix/autoswitch-same-epoch-master-restart

Conversation

@frotmo

@frotmo frotmo commented Nov 23, 2026

Copy link
Copy Markdown

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

…me-epoch master restart

The controller keeps the master epoch unchanged when the original master
restarts. AutoSwitchHAService.changeToMaster then rewrote the existing
epoch entry's start offset to the current max physical offset, while the
slave still held the original start offset. The two epoch entries could
never match in findConsistentPoint, so the slave kept failing with
"Failed to find a consistent point" and never rejoined the sync state set.

Keep the original epoch entry when the master epoch already exists locally.
A new epoch is still appended at the max physical offset as before.

Add a unit test for the same-epoch restart case and an AutoSwitchHA
integration test covering slave rejoin and ongoing replication.
@codecov-commenter

codecov-commenter commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@              Coverage Diff              @@
##             develop   #11588      +/-   ##
=============================================
+ Coverage      50.04%   50.29%   +0.25%     
- Complexity     14515    14613      +98     
=============================================
  Files           1391     1391              
  Lines         103460   103466       +6     
  Branches       13558    13556       -2     
=============================================
+ Hits           51777    52042     +265     
+ Misses         45449    45174     -275     
- Partials        6234     6250      +16     

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

…hHAService

Both callers reject masterEpoch < lastEpoch before appending, and the
same-epoch case returns early in appendEpochEntry, so the truncation
branch for lastEpoch > masterEpoch could never be executed. Remove it.
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.

[Bug] AutoSwitchHA slave cannot rejoin after same-epoch master restart

2 participants