Skip to content

Commit 95fcbd6

Browse files
Aias00claude
andcommitted
[ISSUE #10580] Clarify static topic epoch comparison intent
Address review feedback on PR #10581: explain why the epoch comparison in ClientMetadata.topicRouteData2EndpointsForStaticTopic changed from >= to >. The change is intentional — total queue count is now scoped to the latest epoch so a stale mapping cannot inflate maxTotalNums, with same-epoch ties broken by the largest totalQueues (covered by the UsesMaxTotalQueuesInSameEpoch test). Also document why the sort uses an overflow-safe comparator. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 20696af commit 95fcbd6

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

remoting/src/main/java/org/apache/rocketmq/remoting/rpc/ClientMetadata.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,18 @@ public static ConcurrentMap<MessageQueue, String> topicRouteData2EndpointsForSta
120120
Map<String, TopicQueueMappingInfo> topicQueueMappingInfoMap = mapEntry.getValue();
121121
ConcurrentMap<MessageQueue, TopicQueueMappingInfo> mqEndPoints = new ConcurrentHashMap<>();
122122
List<Map.Entry<String, TopicQueueMappingInfo>> mappingInfos = new ArrayList<>(topicQueueMappingInfoMap.entrySet());
123+
// Sort mappings by epoch descending, so the newest epoch is visited first.
124+
// Subtracting long epochs and casting to int overflows when the epoch gap exceeds
125+
// Integer.MAX_VALUE and reverses the ordering, so use an overflow-safe comparison.
123126
mappingInfos.sort(Comparator.comparingLong(
124127
(Map.Entry<String, TopicQueueMappingInfo> entry) -> entry.getValue().getEpoch()).reversed());
125128
int maxTotalNums = 0;
126129
long maxTotalNumOfEpoch = -1;
127130
for (Map.Entry<String, TopicQueueMappingInfo> entry : mappingInfos) {
128131
TopicQueueMappingInfo info = entry.getValue();
132+
// Scope total queue count to the latest epoch only: a stale mapping must not
133+
// inflate maxTotalNums beyond what the newest epoch advertises. Among mappings
134+
// sharing that latest epoch, keep the largest total queue count.
129135
if (info.getEpoch() > maxTotalNumOfEpoch) {
130136
maxTotalNumOfEpoch = info.getEpoch();
131137
maxTotalNums = info.getTotalQueues();

0 commit comments

Comments
 (0)