ShannonDing commented on code in PR #11588:
URL: https://github.com/apache/rocketmq/pull/11588#discussion_r4680528525
##########
store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java:
##########
@@ -209,18 +205,34 @@ public boolean changeToMasterWhenLastRoleIsMaster(int
masterEpoch) {
return false;
}
// Append new epoch to epochFile
- final EpochEntry newEpochEntry = new EpochEntry(masterEpoch,
this.defaultMessageStore.getMaxPhyOffset());
- if (this.epochCache.lastEpoch() >= masterEpoch) {
- this.epochCache.truncateSuffixByEpoch(masterEpoch);
- }
- this.epochCache.appendEntry(newEpochEntry);
+ final EpochEntry newEpochEntry = appendEpochEntry(masterEpoch);
this.defaultMessageStore.setStateMachineVersion(masterEpoch);
LOGGER.info("Change ha to master success, last role is master,
newMasterEpoch:{}, startOffset:{}",
masterEpoch, newEpochEntry.getStartOffset());
return true;
}
+ /**
+ * Append the master epoch to the epoch file. If the epoch already exists
locally, keep the original entry
+ * instead of rewriting its start offset with the current max physical
offset. A broker that restarts and is
+ * elected as the master again with the same epoch would otherwise move
the start offset of the epoch, and the
+ * slave holding the original start offset could never find a consistent
point, so it would never rejoin the
+ * sync state set.
+ */
+ private EpochEntry appendEpochEntry(final int masterEpoch) {
+ final int lastEpoch = this.epochCache.lastEpoch();
+ if (lastEpoch == masterEpoch) {
+ final EpochEntry existingEntry = this.epochCache.lastEntry();
+ LOGGER.info("Epoch entry already exists locally, keep the original
startOffset. epoch:{}, startOffset:{}",
+ existingEntry.getEpoch(), existingEntry.getStartOffset());
+ return existingEntry;
Review Comment:
This reuses the entry even if its start offset is beyond the local data.
Since the epoch file is not trimmed against the recovered CommitLog on startup,
`(epoch, start)` can be greater than `maxPhyOffset` after an unclean shutdown,
and the slave handshake would then treat its own tail as consistent (see the
review summary). Suggest:
```java
if (lastEpoch == masterEpoch) {
final EpochEntry existingEntry = this.epochCache.lastEntry();
if (existingEntry.getStartOffset() <=
this.defaultMessageStore.getMaxPhyOffset()) {
LOGGER.info(...);
return existingEntry;
}
LOGGER.warn("Epoch {} startOffset {} exceeds maxPhyOffset {}, rewrite
it", ...);
this.epochCache.truncateSuffixByEpoch(masterEpoch);
}
```
Could you also add a case in `AutoSwitchHAServiceTest` where
`getMaxPhyOffset()` returns a value smaller than the existing epoch's start
offset, and assert that the entry is rewritten instead of reused?
--
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]