yx9o commented on code in PR #11538:
URL: https://github.com/apache/rocketmq/pull/11538#discussion_r4643843722


##########
client/src/main/java/org/apache/rocketmq/client/producer/ProduceAccumulator.java:
##########
@@ -328,94 +263,126 @@ public int hashCode() {
         }
     }
 
+    private enum AddResult {
+        /** the message is the first of the batch */
+        OPENED,
+        /** the message joined a batch that already had messages */
+        JOINED,
+        /** the batch is closed and the message was not added */
+        REJECTED
+    }
+
+    /**
+     * One batch of messages sharing an {@link AggregateKey}. Lifecycle: open 
(accepting messages) -> closed (removed
+     * from its map and handed to sendDirect exactly once) -> completed 
({@link #sent} resolved, hold size released and
+     * each message's callback invoked in add order).
+     */
     private class MessageAccumulation {
+        private final AggregateKey aggregateKey;
+        private final Map<AggregateKey, MessageAccumulation> batches;
+        private final boolean asyncSend;
         private final DefaultMQProducer defaultMQProducer;
-        private LinkedList<Message> messages;
-        private LinkedList<SendCallback> sendCallbacks;
-        private Set<String> keys;
-        private final AtomicBoolean closed;
-        private SendResult[] sendResults;
-        private AggregateKey aggregateKey;
-        private AtomicInteger messagesSize;
-        private int count;
-        private long createTime;
-
-        public MessageAccumulation(AggregateKey aggregateKey, 
DefaultMQProducer defaultMQProducer) {
-            this.defaultMQProducer = defaultMQProducer;
-            this.messages = new LinkedList<Message>();
-            this.sendCallbacks = new LinkedList<SendCallback>();
-            this.keys = new HashSet<String>();
-            this.closed = new AtomicBoolean(false);
-            this.messagesSize = new AtomicInteger(0);
+        private final long createTime = System.currentTimeMillis();
+        private final CompletableFuture<SendResult> sent = new 
CompletableFuture<>();
+        // guarded by this; frozen once closed
+        private final List<Message> messages = new ArrayList<>();
+        private final List<SendCallback> sendCallbacks = new ArrayList<>();
+        private final Set<String> keys = new HashSet<>();
+        private long messagesSize;
+        private boolean closed;
+
+        MessageAccumulation(AggregateKey aggregateKey, Map<AggregateKey, 
MessageAccumulation> batches,
+            boolean asyncSend, DefaultMQProducer defaultMQProducer) {
             this.aggregateKey = aggregateKey;
-            this.count = 0;
-            this.createTime = System.currentTimeMillis();
+            this.batches = batches;
+            this.asyncSend = asyncSend;
+            this.defaultMQProducer = defaultMQProducer;
+            this.sent.whenComplete(this::complete);
         }
 
-        private boolean readyToSend() {
-            if (this.messagesSize.get() > holdSize
-                || System.currentTimeMillis() >= this.createTime + holdMs) {
-                return true;
-            }
-            return false;
+        synchronized boolean readyToSend() {
+            return !messages.isEmpty() && (messagesSize > holdSize || 
remainingHoldMs() <= 0);
         }
 
-        public int add(Message msg) throws InterruptedException, 
MQBrokerException, RemotingException, MQClientException {
-            int ret = -1;
-            synchronized (this.closed) {
-                if (this.closed.get()) {
-                    return ret;
-                }
-                ret = this.count++;
-                this.messages.add(msg);
-                int bodySize = null == msg.getBody() ? 0 : 
msg.getBody().length;
-                if (bodySize > 0) {
-                    messagesSize.addAndGet(bodySize);
+        long remainingHoldMs() {
+            return createTime + holdMs - System.currentTimeMillis();
+        }
+
+        AddResult add(Message msg, SendCallback sendCallback) {
+            boolean opened;
+            boolean ready;
+            synchronized (this) {
+                if (closed) {
+                    return AddResult.REJECTED;
                 }
+                opened = messages.isEmpty();
+                messages.add(msg);
+                sendCallbacks.add(sendCallback);
+                messagesSize += null == msg.getBody() ? 0 : 
msg.getBody().length;
                 String msgKeys = msg.getKeys();
                 if (msgKeys != null) {
-                    
this.keys.addAll(Arrays.asList(msgKeys.split(MessageConst.KEY_SEPARATOR)));
+                    
keys.addAll(Arrays.asList(msgKeys.split(MessageConst.KEY_SEPARATOR)));
                 }
+                ready = readyToSend();
             }
-            synchronized (this) {
-                while (!this.closed.get()) {
-                    if (readyToSend()) {
-                        this.send();
-                        break;
-                    } else {
-                        this.wait();
-                    }
-                }
-                return ret;
+            if (ready) {
+                send();
             }
+            return opened ? AddResult.OPENED : AddResult.JOINED;
         }
 
-        public boolean add(Message msg,
-            SendCallback sendCallback) throws InterruptedException, 
RemotingException, MQClientException {
-            synchronized (this.closed) {
-                if (this.closed.get()) {
-                    return false;
-                }
-                this.count++;
-                this.messages.add(msg);
-                this.sendCallbacks.add(sendCallback);
-                int bodySize = null == msg.getBody() ? 0 : 
msg.getBody().length;
-                if (bodySize > 0) {
-                    messagesSize.addAndGet(bodySize);
+        /**
+         * Closes the batch and sends it. Only the first caller sends; later 
calls are no-ops.
+         */
+        void send() {
+            synchronized (this) {
+                if (closed) {
+                    return;
                 }
+                closed = true;
+                batches.remove(aggregateKey, this);
             }
-            if (readyToSend()) {
-                this.send(sendCallback);
+            // the batch carries other callers' messages, so the triggering 
caller's interrupt must not abort it
+            boolean interrupted = Thread.interrupted();

Review Comment:
   This only clears interrupts that are already pending. If the triggering 
thread is interrupted while `sendDirect` waits for the broker, every caller in 
the batch gets `InterruptedException`; could we decouple the shared send from 
that caller’s interruption?



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