You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by "Jackie-Jiang (via GitHub)" <gi...@apache.org> on 2025/09/23 00:45:36 UTC

[PR] Reduce memory footprint for MailboxContentObserver and ReceivingMailbox [pinot]

Jackie-Jiang opened a new pull request, #16872:
URL: https://github.com/apache/pinot/pull/16872

   - In `MailboxContentObserver`, clear the `_mailboxBuffers` when stream completes
   - In `ReceivingMailbox`, specialize the handling for single `ByteBuffer` to avoid creating `CompoundDataBuffer`


-- 
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@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


Re: [PR] Reduce memory footprint for MailboxContentObserver and ReceivingMailbox [pinot]

Posted by "yashmayya (via GitHub)" <gi...@apache.org>.
yashmayya commented on code in PR #16872:
URL: https://github.com/apache/pinot/pull/16872#discussion_r2373058072


##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/ReceivingMailbox.java:
##########
@@ -134,6 +125,28 @@ public ReceivingMailboxStatus offerRaw(List<ByteBuffer> byteBuffers, long timeou
     return offerPrivate(block, dataBlock.getStatsByStage(), timeoutMs);
   }
 
+  private DataBlock deserialize(List<ByteBuffer> byteBuffers)
+      throws IOException {
+    long startTimeMs = System.currentTimeMillis();
+    int totalBytes;
+    DataBlock dataBlock;
+    if (byteBuffers.size() == 1) {
+      ByteBuffer byteBuffer = byteBuffers.get(0);
+      totalBytes = byteBuffer.remaining();
+      dataBlock = DataBlockUtils.readFrom(byteBuffer);
+    } else {
+      totalBytes = 0;
+      for (ByteBuffer bb : byteBuffers) {
+        totalBytes += bb.remaining();
+      }
+      dataBlock = DataBlockUtils.deserialize(byteBuffers);
+    }

Review Comment:
   I think it might be better to move this optimization into the `DataBlockUtils.deserialize` method itself?



-- 
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@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


Re: [PR] Reduce memory footprint for MailboxContentObserver and ReceivingMailbox [pinot]

Posted by "Jackie-Jiang (via GitHub)" <gi...@apache.org>.
Jackie-Jiang commented on code in PR #16872:
URL: https://github.com/apache/pinot/pull/16872#discussion_r2380604756


##########
pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcSendingMailboxTest.java:
##########
@@ -46,55 +46,53 @@ public class GrpcSendingMailboxTest {
 
   @Test(dataProvider = "byteBuffersDataProvider")
   public void testByteBuffersToByteStrings(int[] byteBufferSizes, int maxByteStringSize) {
-    List<ByteBuffer> input = Arrays.stream(byteBufferSizes)
-        .mapToObj(this::randomByteBuffer).collect(Collectors.toList());
+    List<ByteBuffer> input =
+        Arrays.stream(byteBufferSizes).mapToObj(this::randomByteBuffer).collect(Collectors.toList());
     ByteBuffer expected = concatenateBuffers(input);
 
     List<ByteString> output = GrpcSendingMailbox.toByteStrings(input, maxByteStringSize);
-    for (ByteString chunk: output.subList(0, output.size() - 1)) {
+    for (ByteString chunk : output.subList(0, output.size() - 1)) {
       assertEquals(chunk.size(), maxByteStringSize);
     }
     assertTrue(output.get(output.size() - 1).size() <= maxByteStringSize);
-    ByteBuffer actual = concatenateBuffers(
-        output.stream().map(ByteString::asReadOnlyByteBuffer).collect(Collectors.toList()));
+    ByteBuffer actual =
+        concatenateBuffers(output.stream().map(ByteString::asReadOnlyByteBuffer).collect(Collectors.toList()));
 
     assertEquals(actual, expected);
   }
 
   @Test(dataProvider = "testDataBlockToByteStringsProvider")
-  public void testDataBlockToByteStrings(String name, int maxByteStringSize) throws IOException {
+  public void testDataBlockToByteStrings(String name, int maxByteStringSize)
+      throws IOException {
     DataBlock dataBlock = buildTestDataBlock();
     List<ByteString> output = GrpcSendingMailbox.toByteStrings(dataBlock, maxByteStringSize);
-    for (ByteString chunk: output) {
+    for (ByteString chunk : output) {
       assertTrue(chunk.size() <= maxByteStringSize);
     }
 
-    DataBlock actual = DataBlockUtils.deserialize(
-        output.stream().map(ByteString::asReadOnlyByteBuffer).collect(Collectors.toList()));
+    DataBlock deserialized = DataBlockUtils.deserialize(
+        output.stream().map(byteString -> byteString.asReadOnlyByteBuffer().slice()).collect(Collectors.toList()));

Review Comment:
   Yes because the test is not really serializing the chunk, thus the position is not 0. This `slice()` mimics the transfer



-- 
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@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


Re: [PR] Reduce memory footprint for MailboxContentObserver and ReceivingMailbox [pinot]

Posted by "Jackie-Jiang (via GitHub)" <gi...@apache.org>.
Jackie-Jiang merged PR #16872:
URL: https://github.com/apache/pinot/pull/16872


-- 
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@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


Re: [PR] Reduce memory footprint for MailboxContentObserver and ReceivingMailbox [pinot]

Posted by "Jackie-Jiang (via GitHub)" <gi...@apache.org>.
Jackie-Jiang commented on code in PR #16872:
URL: https://github.com/apache/pinot/pull/16872#discussion_r2380612015


##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/MailboxContentObserver.java:
##########
@@ -48,16 +48,15 @@ public class MailboxContentObserver implements StreamObserver<MailboxContent> {
 
   private final MailboxService _mailboxService;
   private final StreamObserver<MailboxStatus> _responseObserver;
-  private final List<ByteBuffer> _mailboxBuffers;
+  private final List<ByteBuffer> _mailboxBuffers = Collections.synchronizedList(new ArrayList<>());

Review Comment:
   I'm being cautious here since I don't completely understand the gRPC behavior. Here is what I got from ChatGPT:
   <img width="979" height="1016" alt="Screenshot 2025-09-25 at 17 50 46" src="https://github.com/user-attachments/assets/66966df7-efd4-47a8-b26f-2d43733c99c5" />
   
   For the same reason, we might need to define `_mailbox` as `volatile`



-- 
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@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


Re: [PR] Reduce memory footprint for MailboxContentObserver and ReceivingMailbox [pinot]

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

   ## [Codecov](https://app.codecov.io/gh/apache/pinot/pull/16872?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `72.00000%` with `7 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 56.34%. Comparing base ([`96a5895`](https://app.codecov.io/gh/apache/pinot/commit/96a5895f2d8da95efee243c8ac62267d1fd132f7?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`8f0c061`](https://app.codecov.io/gh/apache/pinot/commit/8f0c0611daf57afc530587c3a39fd6283f45a9bd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/pinot/pull/16872?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...g/apache/pinot/query/mailbox/ReceivingMailbox.java](https://app.codecov.io/gh/apache/pinot/pull/16872?src=pr&el=tree&filepath=pinot-query-runtime%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpinot%2Fquery%2Fmailbox%2FReceivingMailbox.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGlub3QtcXVlcnktcnVudGltZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3QvcXVlcnkvbWFpbGJveC9SZWNlaXZpbmdNYWlsYm94LmphdmE=) | 66.66% | [5 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/pinot/pull/16872?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [.../query/mailbox/channel/MailboxContentObserver.java](https://app.codecov.io/gh/apache/pinot/pull/16872?src=pr&el=tree&filepath=pinot-query-runtime%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpinot%2Fquery%2Fmailbox%2Fchannel%2FMailboxContentObserver.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGlub3QtcXVlcnktcnVudGltZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3QvcXVlcnkvbWFpbGJveC9jaGFubmVsL01haWxib3hDb250ZW50T2JzZXJ2ZXIuamF2YQ==) | 85.71% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/pinot/pull/16872?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@             Coverage Diff              @@
   ##             master   #16872      +/-   ##
   ============================================
   - Coverage     56.34%   56.34%   -0.01%     
     Complexity      700      700              
   ============================================
     Files          2398     2398              
     Lines        133373   133382       +9     
     Branches      21213    21214       +1     
   ============================================
   + Hits          75148    75150       +2     
   - Misses        51967    51968       +1     
   - Partials       6258     6264       +6     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/pinot/pull/16872/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [java-21](https://app.codecov.io/gh/apache/pinot/pull/16872/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `56.34% <72.00%> (-0.01%)` | :arrow_down: |
   | [temurin](https://app.codecov.io/gh/apache/pinot/pull/16872/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `56.34% <72.00%> (-0.01%)` | :arrow_down: |
   | [unittests](https://app.codecov.io/gh/apache/pinot/pull/16872/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `56.34% <72.00%> (-0.01%)` | :arrow_down: |
   | [unittests1](https://app.codecov.io/gh/apache/pinot/pull/16872/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `56.34% <72.00%> (-0.01%)` | :arrow_down: |
   
   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.
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/pinot/pull/16872?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
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@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


Re: [PR] Reduce memory footprint for MailboxContentObserver and ReceivingMailbox [pinot]

Posted by "gortiz (via GitHub)" <gi...@apache.org>.
gortiz commented on code in PR #16872:
URL: https://github.com/apache/pinot/pull/16872#discussion_r2378011147


##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/MailboxContentObserver.java:
##########
@@ -48,16 +48,15 @@ public class MailboxContentObserver implements StreamObserver<MailboxContent> {
 
   private final MailboxService _mailboxService;
   private final StreamObserver<MailboxStatus> _responseObserver;
-  private final List<ByteBuffer> _mailboxBuffers;
+  private final List<ByteBuffer> _mailboxBuffers = Collections.synchronizedList(new ArrayList<>());

Review Comment:
   I don't understand this change. As defined by `StreamObserver` javadoc, the class doesn't have to be thread safe. It explicitly says that if multiple threads need to use the observer access must be synchronized.



##########
pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcSendingMailboxTest.java:
##########
@@ -46,55 +46,53 @@ public class GrpcSendingMailboxTest {
 
   @Test(dataProvider = "byteBuffersDataProvider")
   public void testByteBuffersToByteStrings(int[] byteBufferSizes, int maxByteStringSize) {
-    List<ByteBuffer> input = Arrays.stream(byteBufferSizes)
-        .mapToObj(this::randomByteBuffer).collect(Collectors.toList());
+    List<ByteBuffer> input =
+        Arrays.stream(byteBufferSizes).mapToObj(this::randomByteBuffer).collect(Collectors.toList());
     ByteBuffer expected = concatenateBuffers(input);
 
     List<ByteString> output = GrpcSendingMailbox.toByteStrings(input, maxByteStringSize);
-    for (ByteString chunk: output.subList(0, output.size() - 1)) {
+    for (ByteString chunk : output.subList(0, output.size() - 1)) {
       assertEquals(chunk.size(), maxByteStringSize);
     }
     assertTrue(output.get(output.size() - 1).size() <= maxByteStringSize);
-    ByteBuffer actual = concatenateBuffers(
-        output.stream().map(ByteString::asReadOnlyByteBuffer).collect(Collectors.toList()));
+    ByteBuffer actual =
+        concatenateBuffers(output.stream().map(ByteString::asReadOnlyByteBuffer).collect(Collectors.toList()));
 
     assertEquals(actual, expected);
   }
 
   @Test(dataProvider = "testDataBlockToByteStringsProvider")
-  public void testDataBlockToByteStrings(String name, int maxByteStringSize) throws IOException {
+  public void testDataBlockToByteStrings(String name, int maxByteStringSize)
+      throws IOException {
     DataBlock dataBlock = buildTestDataBlock();
     List<ByteString> output = GrpcSendingMailbox.toByteStrings(dataBlock, maxByteStringSize);
-    for (ByteString chunk: output) {
+    for (ByteString chunk : output) {
       assertTrue(chunk.size() <= maxByteStringSize);
     }
 
-    DataBlock actual = DataBlockUtils.deserialize(
-        output.stream().map(ByteString::asReadOnlyByteBuffer).collect(Collectors.toList()));
+    DataBlock deserialized = DataBlockUtils.deserialize(
+        output.stream().map(byteString -> byteString.asReadOnlyByteBuffer().slice()).collect(Collectors.toList()));

Review Comment:
   Do we actually need to slice it?



##########
pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcSendingMailboxTest.java:
##########
@@ -46,55 +46,53 @@ public class GrpcSendingMailboxTest {
 
   @Test(dataProvider = "byteBuffersDataProvider")
   public void testByteBuffersToByteStrings(int[] byteBufferSizes, int maxByteStringSize) {
-    List<ByteBuffer> input = Arrays.stream(byteBufferSizes)
-        .mapToObj(this::randomByteBuffer).collect(Collectors.toList());
+    List<ByteBuffer> input =
+        Arrays.stream(byteBufferSizes).mapToObj(this::randomByteBuffer).collect(Collectors.toList());
     ByteBuffer expected = concatenateBuffers(input);
 
     List<ByteString> output = GrpcSendingMailbox.toByteStrings(input, maxByteStringSize);
-    for (ByteString chunk: output.subList(0, output.size() - 1)) {
+    for (ByteString chunk : output.subList(0, output.size() - 1)) {
       assertEquals(chunk.size(), maxByteStringSize);
     }
     assertTrue(output.get(output.size() - 1).size() <= maxByteStringSize);
-    ByteBuffer actual = concatenateBuffers(
-        output.stream().map(ByteString::asReadOnlyByteBuffer).collect(Collectors.toList()));
+    ByteBuffer actual =
+        concatenateBuffers(output.stream().map(ByteString::asReadOnlyByteBuffer).collect(Collectors.toList()));

Review Comment:
   We need to reduce the style changes. This appears to be a reformatting done by the IDE. These changes are opinionated (not mandated by the coding style), subjective (some people may think the previous code is easier to read), and they make the PR more difficult to review. 



-- 
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@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


Re: [PR] Reduce memory footprint for MailboxContentObserver and ReceivingMailbox [pinot]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #16872:
URL: https://github.com/apache/pinot/pull/16872#discussion_r2370729641


##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/MailboxContentObserver.java:
##########
@@ -72,9 +71,8 @@ public void onNext(MailboxContent mailboxContent) {
     }
     try {
       long timeoutMs = Context.current().getDeadline().timeRemaining(TimeUnit.MILLISECONDS);
-      List<ByteBuffer> buffers = new ArrayList<>(_mailboxBuffers);
+      ReceivingMailbox.ReceivingMailboxStatus status = _mailbox.offerRaw(_mailboxBuffers, timeoutMs);
       _mailboxBuffers.clear();

Review Comment:
   Potential race condition: _mailboxBuffers could be modified by another thread between the call to offerRaw() and clear(). Consider clearing within a synchronized block or using a local copy as in the original code.



##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/MailboxContentObserver.java:
##########
@@ -48,16 +48,15 @@ public class MailboxContentObserver implements StreamObserver<MailboxContent> {
 
   private final MailboxService _mailboxService;
   private final StreamObserver<MailboxStatus> _responseObserver;
-  private final List<ByteBuffer> _mailboxBuffers;
+  private final List<ByteBuffer> _mailboxBuffers = Collections.synchronizedList(new ArrayList<>());

Review Comment:
   Using Collections.synchronizedList() provides coarse-grained synchronization which can become a bottleneck under high concurrency. Consider using ConcurrentLinkedQueue or explicit locking if this collection is accessed frequently from multiple threads.



-- 
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@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org