leizhiyuan opened a new pull request, #11538:
URL: https://github.com/apache/rocketmq/pull/11538
<!-- Please make sure the target branch is right. In most case, the target
branch should be `develop`. -->
### Which Issue(s) This PR Fixes
- Fixes #11537
### Brief Description
This PR fixes the correctness and scalability problems of the auto-batching
`ProduceAccumulator` described in #11537. The background, how to reproduce each
problem on `develop`, and the benchmark numbers are in the issue.
**Correctness**
- Reserve hold size only after the message has been validated and is known
to be batchable (`DefaultMQProducer#sendByAccumulator`). Delayed, retry-topic,
producer-group-tagged and invalid messages no longer leak `totalBatchMaxBytes`.
`tryAddMessage` now uses a CAS loop instead of a global `synchronized`.
- Each `MessageAccumulation` completes exactly one `CompletableFuture`. Its
completion handler releases the hold size **once**, splits the result, and
dispatches it to every message. Each user callback is isolated, so a callback
that throws no longer affects the other callbacks of the batch. This also
covers a `sendDirect` that throws synchronously on the async path.
- Every sync caller in a failed batch now receives the real exception (for
example `MQBrokerException`) instead of a `NullPointerException`.
- A sync caller whose message is already in a batch waits for that batch
even if it is interrupted, then restores the interrupt flag. The message can no
longer be stranded in the batch.
- A batch removes itself from its map as soon as it is closed, so sent
batches and their message bodies are no longer retained per aggregate key.
**Scalability**
- The network send no longer runs while a batch monitor is held, and sync
callers no longer wait on the batch monitor. `GuardForSyncSendService` is
removed: the caller that opened a batch waits with the hold timeout and sends
the batch when the timeout expires, and the other callers wait for their own
result without a timeout. As a result, there is one timer per batch instead of
one per caller, and no monitor is held while blocking, which means no carrier
pinning for virtual threads on JDK < 24.
- `GuardForAsyncSendService` now only sends expired async batches.
**Simplification**
`ProduceAccumulator` shrinks from 526 to 425 lines. Batch state is guarded
by the batch's own lock: the `synchronized(closed)`, `AtomicBoolean`,
`AtomicInteger` and `count` / `sendResults` fields are gone. The four `send`
entry points are reduced to the two that `DefaultMQProducer` actually uses.
There are no public API changes; only package-private members changed.
**Notes for reviewers**
- The branch has three commits (the fix, then a refactor of the batch
lifecycle, then the single-timer optimization). Squash-merging them is fine.
- #10937 (shared accumulator lifecycle, #10935) also touches
`ProduceAccumulator`. Whichever PR is merged second will need a rebase; I'm
happy to do that. With this PR, the sync path no longer depends on the
guard-thread lifecycle.
- Behavior change: if the caller that triggers a batch send has been
interrupted, its interrupt flag is cleared during the send and restored
afterwards. A batch contains other callers' messages, so one caller's interrupt
must not abort it.
### How Did You Test This Change?
- New regression tests in `ProduceAccumulatorTest` and
`DefaultMQProducerTest`. Every test that covers a bug fails on `develop`
(results are in table A of #11537):
- `testAutoBatch_unbatchableMessageReleasesHoldSize`,
`testAutoBatch_invalidMessageReleasesHoldSize`
- `testAsyncSend_synchronousSendFailureReleasesHoldSize`,
`testAsyncSend_throwingCallbackDoesNotAffectOthers`
- `testSyncSend_failurePropagatesToEveryMessageInBatch`,
`testSyncSend_interruptWhileWaitingDoesNotStrandMessage`,
`testSyncSend_slowBatchDoesNotBlockOtherBatches`
- `testSentBatchesAreRemoved`
- `testSyncSend_concurrentNoLossNoDuplicate` (32 threads × 300 messages
over 3 queues: no loss, no duplicates)
- `testTryAddMessage_concurrentAccountingIsExact`,
`testSyncSend_firstCallerSendsBatchOnHoldTimeout`
- `mvn -pl client test -Dtest=DefaultMQProducerTest,ProduceAccumulatorTest`
with Checkstyle and SpotBugs enabled: 55/55 pass on JDK 8 (Corretto 8u492, the
CI version) and on JDK 21.
- `ProduceAccumulatorTest` repeated 20 times on JDK 21 and 10 times on JDK
25: all runs pass.
- Benchmark with a mock broker (source in #11537), single producer, default
settings. Every run confirms that acknowledged == sent to broker, and the hold
size is 0 after draining.
- 64 platform threads, 4 queues, 1 ms RTT: 4.4k → 5.7k TPS, p99 15.8 →
11.5 ms. At 20 ms RTT, `develop` alternates between 1.4k and 2.1k TPS from run
to run, while the PR stays at the closed-loop limit of ~2.1k TPS.
- 50,000 virtual threads, 3 queues, JDK 21: 1ms RTT 17.2k → 3.29M TPS (on
`develop`, 49,744 threads made no progress); 20ms RTT 7.1k → 2.39M TPS.
- Same setup on JDK 25: 1ms RTT 0.95M → 3.49M TPS; 20ms RTT 0.91M → 2.33M
TPS, p50 52 → 21 ms.
--
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]