You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "LakshSingla (via GitHub)" <gi...@apache.org> on 2023/02/06 05:06:28 UTC

[GitHub] [druid] LakshSingla opened a new pull request, #13755: Add a readOnly() method for PartitionedOutputChannel

LakshSingla opened a new pull request, #13755:
URL: https://github.com/apache/druid/pull/13755

   ### Description
   
   With SuperSorter using the PartitionedOutputChannels for sorting, it might OOM on inputs of reasonable size because the channel consists of both the writable frame channel and the frame allocator, both of which are not required once the output channel has been written to.
   This change adds a readOnly to the output channel which contains only the readable channel, due to which unnecessary memory references to the writable channel and the memory allocator are lost once the output channel has been written to, preventing the OOM.  
   
   <hr>
   
   ##### Key changed/added classes in this PR
    * `PartitionedOutputChannel`
    * `SuperSorter`
   
   <hr>
   
   This PR has:
   
   - [x] been self-reviewed.
      - [ ] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.)
   - [ ] added documentation for new or modified features or behaviors.
   - [ ] a release note entry in the PR description.
   - [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
   - [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
   - [x] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
   - [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for [code coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met.
   - [ ] added integration tests.
   - [ ] been tested in a test Druid cluster.
   


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

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


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


[GitHub] [druid] cryptoe commented on a diff in pull request #13755: Add a readOnly() method for PartitionedOutputChannel

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #13755:
URL: https://github.com/apache/druid/pull/13755#discussion_r1118172512


##########
processing/src/main/java/org/apache/druid/frame/processor/ComposingOutputChannelFactory.java:
##########
@@ -114,7 +114,7 @@ public PartitionedOutputChannel openPartitionedChannel(String name, boolean dele
             }
           })::get;
       writableFrameChannelsBuilder.add(() -> channel.get().getWritableChannel());
-      readableFrameChannelSuppliersBuilder.add(() -> channel.get().getReadableChannelSupplier().get());
+      readableFrameChannelSuppliersBuilder.add(() -> channel.get().readOnly().getReadableChannelSupplier().get());

Review Comment:
   This will open a readOnly channel for all ComposingOutputChannel's. That shouldn't be the case no ?
   



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

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


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


[GitHub] [druid] cryptoe merged pull request #13755: Add a readOnly() method for PartitionedOutputChannel

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


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

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


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


[GitHub] [druid] cryptoe commented on a diff in pull request #13755: Add a readOnly() method for PartitionedOutputChannel

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #13755:
URL: https://github.com/apache/druid/pull/13755#discussion_r1125981981


##########
processing/src/main/java/org/apache/druid/frame/processor/ComposingOutputChannelFactory.java:
##########
@@ -114,7 +114,7 @@ public PartitionedOutputChannel openPartitionedChannel(String name, boolean dele
             }
           })::get;
       writableFrameChannelsBuilder.add(() -> channel.get().getWritableChannel());
-      readableFrameChannelSuppliersBuilder.add(() -> channel.get().getReadableChannelSupplier().get());
+      readableFrameChannelSuppliersBuilder.add(() -> channel.get().readOnly().getReadableChannelSupplier().get());

Review Comment:
   Can we please add a comment saying that you should only request a readable channel if the outputchannel is completely written too. 



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

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


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


[GitHub] [druid] LakshSingla commented on pull request #13755: Add a readOnly() method for PartitionedOutputChannel

Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla commented on PR #13755:
URL: https://github.com/apache/druid/pull/13755#issuecomment-1453045496

   After debugging, it was found that there can be the following places of memory leaks (before this patch):
   1. The SuperSorter holds the PartitionedOutputChannels' map which contains the writableChannel and the memory allocator, which are not useful once the channel has been written to. Therefore while storing the PartitionedOutputChannels, we convert them to readOnly() which throws away the reference to the writableChannel and the memory allocator allowing them to be GCed and thereby reducing the footprint of the SuperSorter. Attached below is a heap dump before the PR
   <img width="764" alt="Screenshot 2023-03-03 at 11 52 00 AM" src="https://user-images.githubusercontent.com/30999375/222646939-d73a486f-05c2-4391-8290-f4833ffa8421.png">
   
   2. The output channels created by `ComposingOutputChannelFactory` contain a collection of writableChannels and the readableChannelSuppliers (from which it is composed off). Even if we throw away the reference to the frame memory allocators of the original channel, the readableChannelSuppliers still hold the reference to the memory allocators of the individual output channels. To alleviate this, while building the readableChannelSuppliers in the ComposingOutputChannelFactory, we only get the readOnly() version of the output channel.
   
   3. Another potential memory hog is when the output channels created by the `ComposingOutputChannelFactory` contain more than one outputChannel. Once an output channel is exhausted in the composition, we move on to the next output channel and never write to the older one again. However, we still hold the reference to the memory allocator of the older channels because the code assumes that we can write on it again (till the composition itself is marked as readOnly()). Therefore multiple memory allocators can be held up for a composition, even though we require one at a time. While the future ones are created lazily, the older ones are not closed, and this PR addresses that as well.


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

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


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


[GitHub] [druid] cryptoe commented on a diff in pull request #13755: Add a readOnly() method for PartitionedOutputChannel

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #13755:
URL: https://github.com/apache/druid/pull/13755#discussion_r1126149130


##########
processing/src/test/java/org/apache/druid/frame/channel/ComposingWritableFrameChannelTest.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.frame.channel;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.apache.druid.frame.Frame;
+import org.apache.druid.frame.allocation.ArenaMemoryAllocator;
+import org.apache.druid.frame.processor.OutputChannel;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.query.ResourceLimitExceededException;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.function.Supplier;
+
+
+public class ComposingWritableFrameChannelTest
+{
+  @Test
+  public void testComposingWritableChannelSwitchesProperly() throws IOException
+  {
+
+    // This frame channel writes a single frame
+    WritableFrameChannel writableFrameChannel1 = new LimitedWritableFrameChannel(2);
+    WritableFrameChannel writableFrameChannel2 = new LimitedWritableFrameChannel(100);
+
+    Supplier<ReadableFrameChannel> readableFrameChannelSupplier1 = () -> null;
+    Supplier<ReadableFrameChannel> readableFrameChannelSupplier2 = () -> null;
+
+    OutputChannel outputChannel1 = OutputChannel.pair(
+        writableFrameChannel1,
+        ArenaMemoryAllocator.createOnHeap(1),
+        readableFrameChannelSupplier1,
+        1
+    );
+    OutputChannel outputChannel2 = OutputChannel.pair(
+        writableFrameChannel2,
+        ArenaMemoryAllocator.createOnHeap(1),
+        readableFrameChannelSupplier2,
+        2
+    );
+
+    Map<Integer, HashSet<Integer>> partitionToChannelMap = new HashMap();
+
+    ComposingWritableFrameChannel composingWritableFrameChannel = new ComposingWritableFrameChannel(
+        ImmutableList.of(
+            () -> outputChannel1,
+            () -> outputChannel2
+        ),
+        null,
+        ImmutableList.of(
+            () -> writableFrameChannel1,
+            () -> writableFrameChannel2
+        ),
+        partitionToChannelMap
+    );
+
+    composingWritableFrameChannel.write(new FrameWithPartition(Mockito.mock(Frame.class), 1));
+    composingWritableFrameChannel.write(new FrameWithPartition(Mockito.mock(Frame.class), 2));
+    composingWritableFrameChannel.write(new FrameWithPartition(Mockito.mock(Frame.class), 3));
+
+    partitionToChannelMap.get(0);

Review Comment:
   This should be empty no ?



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

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


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


[GitHub] [druid] cryptoe commented on pull request #13755: Add a readOnly() method for PartitionedOutputChannel

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on PR #13755:
URL: https://github.com/apache/druid/pull/13755#issuecomment-1457557473

   @LakshSingla The failures look legit. 


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

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


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