poorbarcode commented on code in PR #19585:
URL: https://github.com/apache/pulsar/pull/19585#discussion_r1113952856


##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java:
##########
@@ -1075,6 +1076,45 @@ private void closeConsumerTasks() {
         }
         negativeAcksTracker.close();
         stats.getStatTimeout().ifPresent(Timeout::cancel);
+        releaseMsgIfEnabledPooledMsg();
+    }
+
+    /**
+     * If enabled pooled messages, we should release the messages after 
closing consumer.
+     *   1. Call "clearIncomingMessages" regardless of whether 
"internalPinnedExecutor" has shutdown or not.
+     *   2. Increment epoch will auto release all messages which received 
after close.
+     *   3. Call "clearIncomingMessages" in "internalPinnedExecutor" is used 
to clear messages in flight.
+     */
+    private void releaseMsgIfEnabledPooledMsg() {
+        if (!poolMessages) {
+            return;
+        }
+        // Increment epoch.
+        CONSUMER_EPOCH.incrementAndGet(this);
+        // Try clear the incoming queue in internalPinnedExecutor-thread.
+        boolean executorIsShutdown = false;
+        try {
+            if (!internalPinnedExecutor.isShutdown()) {
+                internalPinnedExecutor.execute(this::clearIncomingMessages);
+            }
+        } catch (RejectedExecutionException rejectedExecutionException) {
+            executorIsShutdown = true;
+        }
+        // If internalPinnedExecutor is shutdown, try await termination and 
clear the incoming queue.
+        if (executorIsShutdown) {

Review Comment:
   There are scenario reasons why this code is necessary:
   
   #### 1. in-flight task, which will push message into the queue
   | thread: receive messages from broker | thread: close consumer | thread: 
`internalPinnedExecutor.shutdown` |
   | --- | --- | --- |
   | handle cmd: MESSAGE |
   | build messages |
   | check epoch | | do shutdown |
   | push task into `internalPinnedExecutor` | close consumer |
   | | the executor is shutdown, so clear incoming queue on current thread |
   | If `shutdown` is used to stop the executor instead of `shutdownNow`. The 
executor continues to execute the added task |
   | push the new messages into the queue  |
   | bingo: then the new messages will not be released | 
   
   <strong>(Highlight)</strong> so we should call `clear messages` after 
termination of the executor
   
   ----
   
   #### 2. `executor.shutdown` and `consumer.close` are executed concurrently
   | thread: `executor.shutdown` | thread: `consumer.close` |
   | --- | --- |
   | | check `executor.isShutdown: false` |
   | `executor.shutdown` |
   | | `executor.execute(this::clearIncomingMessages)` |
   | | throws RejectedExecutionException |
   | | <strong>(Highlight)</strong>then we should call `clear messages` after 
termination of the executor |



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