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

   ### Which Issue(s) This PR Fixes
   
   - Fixes #11658
   - Part of #11659
   
   ### Brief Description
   
   Cleaning a broker-set from the controller dropped its `BrokerReplicaInfo` 
and `SyncStateInfo`, so when the broker-set registered again 
`nextAssignBrokerId` restarted at 1 and `masterEpoch` / `syncStateSetEpoch` 
restarted at 0. Brokers keep their brokerId in local metadata and only accept a 
strictly larger master epoch, so a broker that was unreachable during the clean 
could end up with the same brokerId and the same master epoch as a newly joined 
broker (two masters at epoch 1, see the issue).
   
   This PR keeps the counters of a cleaned broker-set and continues from them:
   
   - `ReplicasInfoManager` records `(masterEpoch, syncStateSetEpoch, 
nextAssignBrokerId)` in `cleanedBrokerSetTable` whenever a 
`CleanBrokerDataEvent` removes the replica table or the syncStateSet table of a 
broker-set (full clean, or a partial clean that empties them).
   - When the broker-set is registered again, `getNextBrokerId` / 
`applyBrokerId` continue from the recorded `nextAssignBrokerId`, and the new 
`SyncStateInfo` / `BrokerReplicaInfo` are seeded with the recorded epochs and 
next id.
   - "First election" (elect the requesting broker without liveness check) is 
now "master epoch is still the initial one of this broker-set" instead of 
`masterEpoch == 0`. `SyncStateInfo#isFirstTimeForElect` is removed because it 
can no longer answer that by itself.
   - The table is written as a third section of the `ReplicasInfoManager` 
snapshot.
   
   Result for the scenario in #11658: the new broker gets brokerId 2 and is 
elected at epoch 3; the old master still holding brokerId 1 gets 
`CONTROLLER_BROKER_NEED_TO_BE_REGISTERED` from `registerBroker`, and its 
periodic `getReplicaInfo` sees epoch 3 > 1 and switches it to slave. No broker 
or protocol change is needed.
   
   **Compatibility**
   
   - Snapshots: no hessian class changes (the new counters are written as plain 
ints/longs, `SyncStateInfo` / `BrokerReplicaInfo` have no new fields). The new 
section is appended after the two existing tables, and is read only if there 
are bytes left, so:
     - new code reads snapshots written by the current release — verified;
     - the current release reads snapshots written by this change and ignores 
the new section (its `deserializeFrom` stops after the two tables) — verified 
by compiling the `develop` versions of `ReplicasInfoManager` / `SyncStateInfo` 
/ `BrokerReplicaInfo` and loading a snapshot written with this change.
   - Behaviour change: after a clean, brokerIds and epochs of that broker-set 
continue from where they were instead of restarting from 1 / 0.
   - As with any change to how events are applied, controllers on old and new 
versions handle a `CleanBrokerDataEvent` differently, so `cleanBrokerData` 
should not be used while a controller group is being upgraded. JRaft entries 
replayed after the last snapshot that re-registered a cleaned broker-set with 
brokerId 1 would now be rejected; taking a snapshot before upgrading avoids 
that.
   
   ### How Did You Test This Change?
   
   New tests in `ReplicasInfoManagerTest`, all failing on `develop`:
   
   - `testCleanWholeBrokerSetShouldNotReuseBrokerIdOrMasterEpoch`: the #11658 
scenario. Re-applying brokerId 1 after the clean is rejected, the new broker 
gets brokerId 2 and epoch 3, and the old broker's `registerBroker` with 
brokerId 1 gets `CONTROLLER_BROKER_NEED_TO_BE_REGISTERED`.
   - `testCleanedBrokerSetCountersSurviveSnapshot`: the counters survive 
`serialize` / `deserializeFrom`.
   - `testDeserializeSnapshotWithoutCleanedBrokerSets`: a snapshot without the 
new section can still be read.
   
   `mvn -pl controller test` (JDK 8): 92 tests, 0 failures, checkstyle passed.
   


-- 
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]

  • [PR] [ISSUE #11658] fix(control... leizhiyuan (via GitHub) via commits

Reply via email to