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

   After the broker's wall clock steps back, the broker writes no commit or 
rollback record of any transaction until the clock is past its last write 
again, so the transaction check asks the producer about transactions that 
already ended, and stores a committed one again at every check.
   
   - **What happens:** after a step back of 60 s, the transaction `u17`, which 
the check had committed, was stored 4 times, at offsets 1000004 to 1000007, 
about 10 s apart. The copies stay in the topic after the clock is right again, 
and every consumer group receives each of them.
   - **The cause in one line:** `batchSendOpMessage` writes a queue's operation 
records only when `startTime - lastWriteTimestamp >= 
transactionOpBatchInterval` (3 s), both read from `System.currentTimeMillis()`, 
and after a step back that difference is negative.
   - **The smallest step:** with a check every 10 s, a step back of 45 s stored 
a committed transaction twice and 60 s four times; steps of 5 s and 30 s 
passed. A clock that ran a minute ahead and is then stepped back by chrony's 
`makestep` or by `ntpd -g` at a restart does this.
   - **The other outcome:** when a transaction ended in the 3 s before the 
step, its record waits too, and the check stops at its half message at every 
round (`getStoreTimestamp() >= startTime`). So no transaction is checked for 
the length of the step, and one whose producer answered "unknown" is delivered 
that much later.
   - **Related:** #10199 had the same duplicate commit from records written 6 s 
late, and #10200 fixed that cause in 5.5.0. Here the records are written as 
late as the step is long.
   - **Versions:** seen on 5.5.0 (`apache/rocketmq:5.5.0`), and `develop` 
(`19352a246`) has the same comparison.
   
   **How it was found**
   Found autonomously by [hydo](https://github.com/kakoc/hydo), an open-source 
tool that runs the whole system in a VM where every run repeats exactly. It 
looks for bugs on its own and can replay anything it finds.
   - a NameServer with the controller embedded and one replica group of three 
brokers in controller mode (`allAckInSyncStateSet`, `SYNC_FLUSH`) in one VM, 
each node's store on a file system of its own, with `transactionCheckInterval` 
10 s and `transactionTimeOut` 6 s, so that checks come within a run
   - a Go client on rocketmq-client-go v2.1.2 sends transactional messages that 
commit, roll back, or answer "unknown" so that the check commits them; its 
producers answer every check from a decision file that is synced before the 
half message is sent; after the faults it reads every message of the topic, and 
a check fails when a message is stored twice
   - a blind exploration reached it with steps back of the clock; the schedule 
below has one fault, a step back of the VM's wall clock before step 10 of 30
   - the client's log of the checks: in a run without the step the broker 
checks `u17`, `u20` and `u44` once each; after the step it checks each of them 
and the rolled-back `r18`, `r24` and `r45` 4 times, and stores `u17` 4 times 
and `u20` 3 times
   - to reproduce it (a Linux machine with an Intel CPU, where the installer 
builds and loads hydo's kernel module with sudo; `docs/INSTALL.md` in the hydo 
repository has the other ways to install; and Go 1.27.3 for the client):
     ```sh
     curl -LO 
https://github.com/kakoc/hydo/releases/download/v0.8.0/hydo-linux-0.8.0.tar.gz
     curl -LO https://github.com/kakoc/hydo/releases/download/v0.8.0/SHA256SUMS
     sha256sum -c --ignore-missing SHA256SUMS
     tar xzf hydo-linux-0.8.0.tar.gz && ./hydo-linux-0.8.0/install.sh
     export PATH="$HOME/.hydo/bin:$PATH"
     git clone https://github.com/kakoc/hydo && cd hydo && git checkout v0.8.0
     cd hydo/onboard/examples/rocketmq && sh build.sh
     cd hydo && sh fetch-images.sh && hydo build hydo.yaml
     hydo run "disk=none | run T=10:-60"
     # -> VIOLATION [jobs.duplicates] u17 is stored twice: it carries the 
system ids 1000004 and 1000005, fp=96fde9a7f25fc758
     hydo run "disk=none | run"
     # -> OK, fp=b40afc5cabb53082
     hydo run "disk=none | run T=2:-2592000"   # the other outcome, with a step 
back of 30 days
     # -> VIOLATION [jobs.no-loss] u17 was acknowledged to process 17 at 46.316 
s and never delivered in group w of channel j, which was drained, 
fp=e4f10ed629b3d8be
     ```
     the same fingerprints on every run
   
   A producer that checks its own transaction log answers "commit" for a 
transaction it committed, so a check in the window can store one more copy of a 
message that the producer sent once.
   
   **Why it fails** (line numbers of 5.5.0; `develop` has the same code a few 
lines lower)
   1. A commit or rollback queues the half message's offset in its queue's 
`MessageQueueOpContext` (`deletePrepareMessage`, 
`broker/src/main/java/org/apache/rocketmq/broker/transaction/queue/TransactionalMessageServiceImpl.java:597`).
 `TransactionalOpBatchService` writes the queued offsets as one record of 
`RMQ_SYS_TRANS_OP_HALF_TOPIC` from `batchSendOpMessage` (line 698).
   2. `batchSendOpMessage` skips a queue while `startTime - 
mqContext.getLastWriteTimestamp() < interval` (line 712), with `startTime = 
System.currentTimeMillis()` (line 699) and the last write's time from the same 
clock (line 694). It returns `firstTimestamp + interval` as the next wake-up 
(line 744), and `TransactionalOpBatchService.run` waits `wakeupTimestamp - 
System.currentTimeMillis()` (`TransactionalOpBatchService.java:51` and `:56`). 
After a step back that is the step's length plus 3 s; only a batch of more than 
`transactionOpMsgMaxSize` (4,096 bytes) wakes it earlier.
   3. `check` (line 162) asks the producer about a half message that has no 
operation record (`isNeedCheck`, line 295). It also asks at once, without 
waiting for `transactionTimeOut`, when the last record it read was born more 
than `transactionTimeOut` after the check began (line 296), which holds for the 
records written before a step back of more than 6 s.
   4. A transaction that ended after the step has its record waiting in memory, 
so the check puts its half message back (`putBackHalfMsgQueue`, line 301) and 
asks again. The producer answers "commit", and `EndTransactionProcessor` stores 
the message again 
(`broker/src/main/java/org/apache/rocketmq/broker/processor/EndTransactionProcessor.java`,
 lines 131-150).
   5. When a transaction ended in the 3 s before the step, its half message was 
stored "after" the check began, so the check stops there (`if 
(msgExt.getStoreTimestamp() >= startTime) break;`, line 269) at every round 
until the clock passes it.
   


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