You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2020/05/27 04:43:29 UTC

[GitHub] [druid] gianm commented on a change in pull request #9928: Fix various processing buffer leaks and simplify BlockingPool.

gianm commented on a change in pull request #9928:
URL: https://github.com/apache/druid/pull/9928#discussion_r430853709



##########
File path: processing/src/main/java/org/apache/druid/query/topn/PooledTopNAlgorithm.java
##########
@@ -243,27 +240,38 @@ public PooledTopNParams makeInitParams(ColumnSelectorPlus selectorPlus, Cursor c
       }
     };
 
-    final int numBytesToWorkWith = resultsBuf.remaining();
-    final int[] aggregatorSizes = new int[query.getAggregatorSpecs().size()];
-    int numBytesPerRecord = 0;
+    final ResourceHolder<ByteBuffer> resultsBufHolder = bufferPool.take();
 
-    for (int i = 0; i < query.getAggregatorSpecs().size(); ++i) {
-      aggregatorSizes[i] = query.getAggregatorSpecs().get(i).getMaxIntermediateSizeWithNulls();
-      numBytesPerRecord += aggregatorSizes[i];
-    }
+    try {
+      final ByteBuffer resultsBuf = resultsBufHolder.get();
+      resultsBuf.clear();
+
+      final int numBytesToWorkWith = resultsBuf.remaining();
+      final int[] aggregatorSizes = new int[query.getAggregatorSpecs().size()];
+      int numBytesPerRecord = 0;
 
-    final int numValuesPerPass = numBytesPerRecord > 0 ? numBytesToWorkWith / numBytesPerRecord : cardinality;
-
-    return PooledTopNParams.builder()
-                           .withSelectorPlus(selectorPlus)
-                           .withCursor(cursor)
-                           .withResultsBufHolder(resultsBufHolder)
-                           .withResultsBuf(resultsBuf)
-                           .withArrayProvider(arrayProvider)
-                           .withNumBytesPerRecord(numBytesPerRecord)
-                           .withNumValuesPerPass(numValuesPerPass)
-                           .withAggregatorSizes(aggregatorSizes)
-                           .build();
+      for (int i = 0; i < query.getAggregatorSpecs().size(); ++i) {
+        aggregatorSizes[i] = query.getAggregatorSpecs().get(i).getMaxIntermediateSizeWithNulls();
+        numBytesPerRecord += aggregatorSizes[i];
+      }
+
+      final int numValuesPerPass = numBytesPerRecord > 0 ? numBytesToWorkWith / numBytesPerRecord : cardinality;

Review comment:
       It's mostly for good practice (when you get a buffer, you better make sure you either free it or pass it along to someone that will free it). I don't think any lines here are very likely to throw exceptions. However, it is possible. I think the most likely is this one:
   
   ```java
            aggregatorSizes[i] = query.getAggregatorSpecs().get(i).getMaxIntermediateSizeWithNulls();
   ```
   
   An aggregator could potentially throw an exception from that method for some reason (maybe illegal state, or something). It isn't in the control of the query engine. However, I don't have any examples of where this can actually happen.




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

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