You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "mattisonchao (via GitHub)" <gi...@apache.org> on 2023/07/17 10:53:50 UTC

[GitHub] [pulsar] mattisonchao opened a new pull request, #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory

mattisonchao opened a new pull request, #20823:
URL: https://github.com/apache/pulsar/pull/20823

   ### Motivation
   
   [There](https://github.com/apache/pulsar/blob/57fbee446aebb5c9f4db3bed559cb7bef3749731/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/AbstractBaseDispatcher.java#L205C16-L205C16) is a memory leak if the delayed tracker throws an `OutOfDirectMemory` exception. The entries will never release.
   
   ```
   
   Jul 16, 2023 @ 18:35:58.575 | 2023-07-16T10:35:58,574+0000 [broker-topic-workers-OrderedExecutor-0-0] ERROR org.apache.bookkeeper.common.util.SingleThreadExecutor - Error while running task: failed to allocate 16777216 byte(s) of direct memory (used: 847249415, max: 858993459)
     | Jul 16, 2023 @ 18:35:58.575 | at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[io.netty-netty-common-4.1.93.Final.jar:4.1.93.Final]
     | Jul 16, 2023 @ 18:35:58.575 | at org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumers.trySendMessagesToConsumers(PersistentDispatcherMultipleConsumers.java:717) ~[io.streamnative-pulsar-broker-3.0.0.4.jar:3.0.0.4]
     | Jul 16, 2023 @ 18:35:58.575 | at org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumers.trackDelayedDelivery(PersistentDispatcherMultipleConsumers.java:1068) ~[io.streamnative-pulsar-broker-3.0.0.4.jar:3.0.0.4]
     | Jul 16, 2023 @ 18:35:58.575 | at io.netty.buffer.PoolArena.allocate(PoolArena.java:144) ~[io.netty-netty-buffer-4.1.93.Final.jar:4.1.93.Final]
     | Jul 16, 2023 @ 18:35:58.575 | at io.netty.util.internal.PlatformDependent.allocateDirectNoCleaner(PlatformDependent.java:774) ~[io.netty-netty-common-4.1.93.Final.jar:4.1.93.Final]
     | Jul 16, 2023 @ 18:35:58.575 | at org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumers.sendMessagesToConsumers(PersistentDispatcherMultipleConsumers.java:622) ~[io.streamnative-pulsar-broker-3.0.0.4.jar:3.0.0.4]
     | Jul 16, 2023 @ 18:35:58.575 | at org.apache.pulsar.broker.delayed.InMemoryDelayedDeliveryTracker.addMessage(InMemoryDelayedDeliveryTracker.java:81) ~[io.streamnative-pulsar-broker-3.0.0.4.jar:3.0.0.4]
     | Jul 16, 2023 @ 18:35:58.575 | at io.netty.buffer.PoolArena.allocate(PoolArena.java:129) ~[io.netty-netty-buffer-4.1.93.Final.jar:4.1.93.Final]
     | Jul 16, 2023 @ 18:35:58.575 | at io.netty.buffer.PoolArena$DirectArena.newUnpooledChunk(PoolArena.java:690) ~[io.netty-netty-buffer-4.1.93.Final.jar:4.1.93.Final]
     | Jul 16, 2023 @ 18:35:58.575 | io.netty.util.internal.OutOfDirectMemoryError: failed to allocate 16777216 byte(s) of direct memory (used: 847249415, max: 858993459)
     | Jul 16, 2023 @ 18:35:58.575 | at org.apache.bookkeeper.common.util.SingleThreadExecutor.safeRunTask(SingleThreadExecutor.java:137) ~[org.apache.bookkeeper-bookkeeper-common-4.16.2.jar:4.16.2] 
     | Jul 16, 2023 @ 18:35:58.575 | at org.apache.pulsar.common.util.collections.TripleLongPriorityQueue.add(TripleLongPriorityQueue.java:94) ~[io.streamnative-pulsar-common-3.0.0.4.jar:3.0.0.4]
     | Jul 16, 2023 @ 18:35:58.575 | at io.netty.buffer.PoolArena.allocateHuge(PoolArena.java:226) ~[io.netty-netty-buffer-4.1.93.Final.jar:4.1.93.Final]
     | Jul 16, 2023 @ 18:35:58.575 | at org.apache.bookkeeper.common.util.SingleThreadExecutor.run(SingleThreadExecutor.java:113) ~[org.apache.bookkeeper-bookkeeper-common-4.16.2.jar:4.16.2]
   ```
   
   
   ### Modifications
   
   - Add exception catcher to catch Direct memory OOM exception and then try to recover it since we control the direct memory ourselves.
   
   
   ### Verifying this change
   
   - [x] Make sure that the change passes the CI checks.
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc` <!-- Your PR contains doc changes. -->
   - [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
   - [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on a diff in pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on code in PR #20823:
URL: https://github.com/apache/pulsar/pull/20823#discussion_r1266408122


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java:
##########
@@ -1065,7 +1066,17 @@ public boolean trackDelayedDelivery(long ledgerId, long entryId, MessageMetadata
             delayedDeliveryTracker.get().resetTickTime(topic.getDelayedDeliveryTickTimeMillis());
 
             long deliverAtTime = msgMetadata.hasDeliverAtTime() ? msgMetadata.getDeliverAtTime() : -1L;
-            return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            try {
+                return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            } catch (OutOfDirectMemoryError ex) {
+                log.error("Out of direct memory while trying add message index to delayed tracker."
+                        + " fallback to redelivery messages(heap) and waiting for enough direct memory space."
+                        + " error info: {}", ex.getMessage());
+                // fall back to redelivery queue(heap) waiting for enough direct memory.
+                redeliveryMessages.add(ledgerId, entryId);

Review Comment:
   This makes the delayed messages never delay, right?



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on a diff in pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on code in PR #20823:
URL: https://github.com/apache/pulsar/pull/20823#discussion_r1266184168


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java:
##########
@@ -1065,7 +1066,17 @@ public boolean trackDelayedDelivery(long ledgerId, long entryId, MessageMetadata
             delayedDeliveryTracker.get().resetTickTime(topic.getDelayedDeliveryTickTimeMillis());
 
             long deliverAtTime = msgMetadata.hasDeliverAtTime() ? msgMetadata.getDeliverAtTime() : -1L;
-            return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            try {
+                return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            } catch (OutOfDirectMemoryError ex) {

Review Comment:
   In general, the Error type is not recommended to catch, we should use these two ways to solve the problem
   - Set the maximum memory usage of the delay message. If the delay message usage exceeds the maximum memory usage, stuck new read
   - we should kill the process if the error occurs
   



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] mattisonchao commented on a diff in pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory

Posted by "mattisonchao (via GitHub)" <gi...@apache.org>.
mattisonchao commented on code in PR #20823:
URL: https://github.com/apache/pulsar/pull/20823#discussion_r1266238658


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java:
##########
@@ -1065,7 +1066,17 @@ public boolean trackDelayedDelivery(long ledgerId, long entryId, MessageMetadata
             delayedDeliveryTracker.get().resetTickTime(topic.getDelayedDeliveryTickTimeMillis());
 
             long deliverAtTime = msgMetadata.hasDeliverAtTime() ? msgMetadata.getDeliverAtTime() : -1L;
-            return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            try {
+                return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            } catch (OutOfDirectMemoryError ex) {

Review Comment:
   The current implementation is the first approach, but we don't introduce a fixed value.  Instead, we use a dynamic value to do it.
   
   > Set the maximum memory usage of the delay message.
   This is hard to set a reasonable value.
   
   > stuck new read
   when we recover?
   



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java:
##########
@@ -1065,7 +1066,17 @@ public boolean trackDelayedDelivery(long ledgerId, long entryId, MessageMetadata
             delayedDeliveryTracker.get().resetTickTime(topic.getDelayedDeliveryTickTimeMillis());
 
             long deliverAtTime = msgMetadata.hasDeliverAtTime() ? msgMetadata.getDeliverAtTime() : -1L;
-            return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            try {
+                return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            } catch (OutOfDirectMemoryError ex) {

Review Comment:
   The current implementation is the first approach, but we don't introduce a fixed value.  Instead, we use a dynamic value to do it.
   
   > Set the maximum memory usage of the delay message.
   
   This is hard to set a reasonable value.
   
   > stuck new read
   
   when we recover?
   



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] mattisonchao commented on a diff in pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory

Posted by "mattisonchao (via GitHub)" <gi...@apache.org>.
mattisonchao commented on code in PR #20823:
URL: https://github.com/apache/pulsar/pull/20823#discussion_r1266238658


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java:
##########
@@ -1065,7 +1066,17 @@ public boolean trackDelayedDelivery(long ledgerId, long entryId, MessageMetadata
             delayedDeliveryTracker.get().resetTickTime(topic.getDelayedDeliveryTickTimeMillis());
 
             long deliverAtTime = msgMetadata.hasDeliverAtTime() ? msgMetadata.getDeliverAtTime() : -1L;
-            return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            try {
+                return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            } catch (OutOfDirectMemoryError ex) {

Review Comment:
   The current implementation is the first approach, but we don't introduce a fixed value.  Instead, we use a dynamic value to do it.
   
   > Set the maximum memory usage of the delay message.
   
   This is hard to set a reasonable value.
   
   > stuck new read
   
   This is an ideal approach. what is the good time value to recover?
   



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] mattisonchao closed pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory

Posted by "mattisonchao (via GitHub)" <gi...@apache.org>.
mattisonchao closed pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory
URL: https://github.com/apache/pulsar/pull/20823


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] codecov-commenter commented on pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #20823:
URL: https://github.com/apache/pulsar/pull/20823#issuecomment-1643740248

   ## [Codecov](https://app.codecov.io/gh/apache/pulsar/pull/20823?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   > Merging [#20823](https://app.codecov.io/gh/apache/pulsar/pull/20823?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (b0742f0) into [master](https://app.codecov.io/gh/apache/pulsar/commit/e96b3398912163eb6e0528c10aed3507c95952fd?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (e96b339) will **increase** coverage by `39.29%`.
   > The diff coverage is `100.00%`.
   
   [![Impacted file tree graph](https://app.codecov.io/gh/apache/pulsar/pull/20823/graphs/tree.svg?width=650&height=150&src=pr&token=acYqCpsK9J&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)](https://app.codecov.io/gh/apache/pulsar/pull/20823?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   ```diff
   @@              Coverage Diff              @@
   ##             master   #20823       +/-   ##
   =============================================
   + Coverage     33.44%   72.73%   +39.29%     
   - Complexity    12097    32232    +20135     
   =============================================
     Files          1612     1856      +244     
     Lines        126568   138999    +12431     
     Branches      13824    15302     +1478     
   =============================================
   + Hits          42328   101106    +58778     
   + Misses        78686    29884    -48802     
   - Partials       5554     8009     +2455     
   ```
   
   | Flag | Coverage Δ | |
   |---|---|---|
   | inttests | `24.21% <25.00%> (+0.12%)` | :arrow_up: |
   | unittests | `72.49% <100.00%> (+40.55%)` | :arrow_up: |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   
   | [Impacted Files](https://app.codecov.io/gh/apache/pulsar/pull/20823?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [...ar/common/util/collections/SegmentedLongArray.java](https://app.codecov.io/gh/apache/pulsar/pull/20823?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWNvbW1vbi9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NvbW1vbi91dGlsL2NvbGxlY3Rpb25zL1NlZ21lbnRlZExvbmdBcnJheS5qYXZh) | `92.85% <100.00%> (+30.35%)` | :arrow_up: |
   
   ... and [1494 files with indirect coverage changes](https://app.codecov.io/gh/apache/pulsar/pull/20823/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] mattisonchao merged pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory

Posted by "mattisonchao (via GitHub)" <gi...@apache.org>.
mattisonchao merged PR #20823:
URL: https://github.com/apache/pulsar/pull/20823


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] mattisonchao closed pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory

Posted by "mattisonchao (via GitHub)" <gi...@apache.org>.
mattisonchao closed pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory
URL: https://github.com/apache/pulsar/pull/20823


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on a diff in pull request #20823: [fix][broker] Fix direct memory leak by delayed index OutOfDirectMemory

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on code in PR #20823:
URL: https://github.com/apache/pulsar/pull/20823#discussion_r1266423667


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java:
##########
@@ -1065,7 +1066,17 @@ public boolean trackDelayedDelivery(long ledgerId, long entryId, MessageMetadata
             delayedDeliveryTracker.get().resetTickTime(topic.getDelayedDeliveryTickTimeMillis());
 
             long deliverAtTime = msgMetadata.hasDeliverAtTime() ? msgMetadata.getDeliverAtTime() : -1L;
-            return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            try {
+                return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            } catch (OutOfDirectMemoryError ex) {

Review Comment:
   Just like the implementation of JVM, if could not allocate more memory, the progress will be killed by OOM. It did not do `yield` and try another once.
   
   >> Set the maximum memory usage of the delay message. If the delay message usage exceeds the maximum memory usage, stuck new read
   > This is an ideal approach. what is the good time value to recover? 
   
   In this patch, you mv this message into `replay queue`, and the next reading will get it again, loop. The result is actually the same as doing a stuck new read.



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java:
##########
@@ -1065,7 +1066,17 @@ public boolean trackDelayedDelivery(long ledgerId, long entryId, MessageMetadata
             delayedDeliveryTracker.get().resetTickTime(topic.getDelayedDeliveryTickTimeMillis());
 
             long deliverAtTime = msgMetadata.hasDeliverAtTime() ? msgMetadata.getDeliverAtTime() : -1L;
-            return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            try {
+                return delayedDeliveryTracker.get().addMessage(ledgerId, entryId, deliverAtTime);
+            } catch (OutOfDirectMemoryError ex) {
+                log.error("Out of direct memory while trying add message index to delayed tracker."
+                        + " fallback to redelivery messages(heap) and waiting for enough direct memory space."
+                        + " error info: {}", ex.getMessage());
+                // fall back to redelivery queue(heap) waiting for enough direct memory.
+                redeliveryMessages.add(ledgerId, entryId);

Review Comment:
   This makes the delayed messages never delay, right?



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org