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

   ### 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
   
   With the JRaft controller (`controllerType = jRaft`), broker liveness inside 
the state machine is decided by `RaftReplicasInfoManager#isBrokerActive`:
   
   ```java
   return (last + timeoutMillis) >= invokeTime;
   ```
   
   - `last` (`BrokerLiveInfo#lastUpdateTimestamp`) is taken from the 
**controller leader's** clock in `RaftBrokerHeartBeatManager#onBrokerHeartbeat`.
   - `invokeTime` is a field of the request header (`ElectMasterRequestHeader`, 
`AlterSyncStateSetRequestHeader`, `RegisterBrokerToControllerRequestHeader`, 
`CleanControllerBrokerDataRequestHeader`). It is initialized with 
`System.currentTimeMillis()` **on the sender** (broker / admin tool), 
serialized into `extFields`, and the controller keeps the sender's value when 
it decodes the header and appends it to the raft log.
   
   So the two sides of the comparison come from different machines, and every 
liveness decision made on behalf of a broker request is shifted by the 
broker-to-controller clock skew:
   
   - broker clock behind the leader by more than the heartbeat timeout: dead 
brokers are treated as alive. `electMaster` answers 
`CONTROLLER_MASTER_STILL_EXIST` for a dead master, or picks a dead replica as 
the new master; `registerBroker` hands out a dead master.
   - broker clock ahead of the leader: live brokers are treated as dead. 
`alterSyncStateSet` refuses to add live replicas back into the syncStateSet, 
`registerBroker` does not return the live master, `electMaster` fails.
   
   The DLedger controller is not affected: 
`DefaultBrokerHeartbeatManager#isBrokerActive` uses the local clock on both 
sides.
   
   ### Steps to Reproduce
   
   Single-node JRaft controller:
   
   1. apply brokerId 1 and 2 for `broker-a`, send heartbeats, elect broker 1 as 
master;
   2. send heartbeats for both brokers with `lastUpdateTimestamp = now - 10s` 
and `heartbeatTimeoutMillis = 1s` (both are dead from the leader's point of 
view);
   3. `electMaster(ofBrokerTrigger(..., 2L))` with `invokeTime = now - 20s` 
(broker 2's clock is 20s behind).
   
   Result: `CONTROLLER_MASTER_STILL_EXIST` (2011) — the dead master 1 is 
reported as alive.
   
   ### What Did You Expect to See?
   
   Liveness is judged with a single clock. The leader should stamp `invokeTime` 
with its own clock before appending the request to the raft log; the value is 
still replicated in the log, so all replicas stay deterministic. In the 
scenario above the election should fail with `CONTROLLER_ELECT_MASTER_FAILED` 
(2012).
   
   ### What Did You See Instead?
   
   `CONTROLLER_MASTER_STILL_EXIST` (2011).
   
   ### Additional Context
   
   Found while model checking the controller metadata state machine with 
TLA+/TLC (tracked in the umbrella issue): with a skew parameter of 0 the 
invariant "never elect a broker whose heartbeat has expired" holds; with a skew 
of 2 ticks TLC finds a counterexample.
   


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