leizhiyuan opened a new issue, #11658:
URL: https://github.com/apache/rocketmq/issues/11658

   ### Before Creating the Bug Report
   
   - [x] I found a bug, not just asking a question, which should be created in 
[GitHub Discussions](https://github.com/apache/rocketmq/discussions).
   - [x] I have searched the [GitHub 
Issues](https://github.com/apache/rocketmq/issues) and [GitHub 
Discussions](https://github.com/apache/rocketmq/discussions)  of this 
repository and believe that this is not a duplicate.
   - [x] I have confirmed that this bug belongs to the current repository, not 
other repositories of RocketMQ.
   
   ### Runtime platform environment
   
   Linux (any)
   
   ### RocketMQ version
   
   develop, commit 19352a246
   
   ### JDK Version
   
   1.8
   
   ### Describe the Bug
   
   Cleaning a whole broker-set (`CLEAN_BROKER_DATA` without 
`brokerControllerIdsToClean`) removes both `BrokerReplicaInfo` and 
`SyncStateInfo` of that broker-set. When brokers of that broker-set register 
again, both are created from scratch:
   
   - `nextAssignBrokerId` restarts at `FIRST_BROKER_CONTROLLER_ID` (1), so 
brokerIds are handed out again;
   - `masterEpoch` and `syncStateSetEpoch` restart at 0.
   
   Both counters are relied upon to be monotonic by the brokers:
   
   - a broker keeps its brokerId in its local metadata file and, when the file 
exists, only calls `registerBroker` with that id (no new `applyBrokerId`). 
`registerBroker` accepts any existing id and just overwrites its address;
   - `ReplicasManager#changeToMaster/changeToSlave` only act on `newMasterEpoch 
> this.masterEpoch`, and role is decided by `masterBrokerId == 
brokerControllerId`.
   
   `cleanLivingBroker = false` only requires `masterBrokerId == null` for a 
full clean. That is also the state after the master becomes unreachable from 
the controller and no in-sync replica is available, while the old master is 
still running and still believes it is master.
   
   ### Steps to Reproduce
   
   Against `ReplicasInfoManager` (default config):
   
   ```
   X(10.0.0.1) got id 1, elected                   master=1    
addr=10.0.0.1:10911 masterEpoch=1
   X unreachable, controller elect -> no master    master=null addr=null        
   masterEpoch=2
   cleanBrokerData(all) code=0
   Y(10.0.0.2) got id 1, elected                   master=1    
addr=10.0.0.2:10911 masterEpoch=1
   X registerBroker(id=1) code=0 masterId=1 masterEpoch=1
   after X re-registers                            master=1    
addr=10.0.0.1:10911 masterEpoch=1
   ```
   
   X never saw epoch 2 (it was unreachable), so its local state is still 
"master, epoch 1". After the clean, a new broker Y with an empty store gets the 
**same brokerId 1** and is elected at the **same epoch 1**. When X comes back 
it registers with id 1, the controller overwrites the address of id 1 with X's 
address and answers `masterBrokerId = 1, masterEpoch = 1`. X and Y both 
consider themselves master of `broker-a` at epoch 1; neither can be fenced by 
epoch, and the address of id 1 flips between them on each `registerBroker`.
   
   ### What Did You Expect to See?
   
   Removing the metadata of a broker-set should not make 
`masterEpoch`/`syncStateSetEpoch` go backwards or reuse brokerIds that may 
still be held by a running broker.
   
   ### What Did You See Instead?
   
   Same brokerId and same master epoch issued to two different brokers, see 
above.
   
   ### Additional Context
   
   Found while model checking the controller metadata state machine with 
TLA+/TLC (tracked in the umbrella issue). With admin commands disabled, the 
same invariants (`masterEpoch` never decreases, a brokerId is owned by one 
broker, no two masters at the same epoch) hold on 18.9M reachable states; with 
`CLEAN_BROKER_DATA` enabled TLC finds counterexamples within 7–11 steps.
   
   Possible directions, I would like maintainers' opinion before sending a PR 
since both touch the persisted state machine:
   
   1. Keep a per-brokerName tombstone (`lastMasterEpoch`, 
`lastSyncStateSetEpoch`, `nextAssignBrokerId`) when a broker-set is fully 
cleaned, and seed the new `SyncStateInfo`/`BrokerReplicaInfo` from it. This 
needs a snapshot format extension and replacing `isFirstTimeForElect()` 
(`masterEpoch == 0`) with an explicit flag.
   2. Make `registerBroker` verify that the id is still owned by the same 
broker (e.g. carry `registerCheckCode`), so a broker whose id was reissued must 
re-apply for a new id. This needs a protocol change.
   


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