leizhiyuan commented on PR #11538:
URL: https://github.com/apache/rocketmq/pull/11538#issuecomment-7075062620

   Good catch, thanks. You are right: `Thread.interrupted()` only clears an 
interrupt that is already pending when the send starts. If the sending caller 
is interrupted while `sendDirect` waits for the broker, that 
`InterruptedException` failed the entire batch, including the messages of the 
other callers.
   
   Fixed in 095ff57. The batch send now goes through a new 
`DefaultMQProducer#sendBatchDirect`, which is only used for accumulator batches:
   
   - if the send is interrupted, it clears the interrupt status recorded by 
that attempt, sends the batch again once, and restores the interrupt status 
when it returns. The batch send is unchanged otherwise: still a sync send with 
the same `retryTimesWhenSendFailed` configuration (`timesTotal = 1 + 
retryTimesWhenSendFailed`), so it does not lose the sync retries;
   - if the retry is interrupted as well, it does not retry again. The batch 
fails with `InterruptedException`, as it does today;
   - the accumulator's own `Thread.interrupted()` handling is removed, since 
the interrupt is now handled where the send happens.
   
   One trade-off worth calling out: this makes an interrupted batch send 
at-least-once. The first attempt may have reached the broker before the 
interrupt, so that message can be stored twice. I chose this over failing the 
whole batch, because the batch carries other callers' messages that have 
nothing to do with that interrupt. It also matches the documented behavior of 
`DefaultMQProducer#send`, which may deliver duplicates when it retries. If you 
would prefer duplication to be impossible here, the alternative is to run the 
sync send on a separate thread and make the caller wait uninterruptibly, at the 
cost of one thread hop per batch.
   
   Test: 
`ProduceAccumulatorTest#testSyncSend_batchSendIsNotAbortedByCallerInterrupt` 
puts two messages in one batch, interrupts the caller that sends it while the 
send is in flight, and asserts that the other message still receives its own 
result, the sending caller returns a `SendResult` with its interrupt status 
restored, and the hold size returns to 0. It fails on the previous revision 
with `InterruptedException` for both messages.
   
   `DefaultMQProducerTest` and `ProduceAccumulatorTest`: 56/56 pass with 
Checkstyle and SpotBugs enabled. `ProduceAccumulatorTest` repeated 5 times on 
JDK 8 and 5 times on JDK 25.
   


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