Repository: drill
Updated Branches:
  refs/heads/master c30a2687a -> d7eebec41


DRILL-2274: Unable to allocate sv2 buffer after repeated attempts : JOIN, Order 
by used in query

- when ExternalSortBatch.newSV2() couldn't allocate a new SV2 it spills the 
spilledBatchGroups instead of batchGroups, otherwise no memory will be released 
for the sort's allocator
- copierAllocator is closed as soon as possible to free more memory for the sort

this closes #172


Project: http://git-wip-us.apache.org/repos/asf/drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/d7eebec4
Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/d7eebec4
Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/d7eebec4

Branch: refs/heads/master
Commit: d7eebec41a1636055be1b2c79b693d76c52d8932
Parents: c30a268
Author: adeneche <[email protected]>
Authored: Mon Sep 21 11:59:56 2015 -0700
Committer: adeneche <[email protected]>
Committed: Mon Sep 28 17:56:41 2015 -0700

----------------------------------------------------------------------
 .../physical/impl/xsort/ExternalSortBatch.java  | 59 +++++++-------------
 1 file changed, 20 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/d7eebec4/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/xsort/ExternalSortBatch.java
----------------------------------------------------------------------
diff --git 
a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/xsort/ExternalSortBatch.java
 
b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/xsort/ExternalSortBatch.java
index 282edb1..49a64cf 100644
--- 
a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/xsort/ExternalSortBatch.java
+++ 
b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/xsort/ExternalSortBatch.java
@@ -92,10 +92,9 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
 
   private final int SPILL_BATCH_GROUP_SIZE;
   private final int SPILL_THRESHOLD;
-  private final List<String> SPILL_DIRECTORIES;
   private final Iterator<String> dirs;
   private final RecordBatch incoming;
-  private final BufferAllocator copierAllocator;
+  private BufferAllocator copierAllocator;
 
   private BatchSchema schema;
   private SingleBatchSorter sorter;
@@ -144,8 +143,7 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
     }
     SPILL_BATCH_GROUP_SIZE = 
config.getInt(ExecConstants.EXTERNAL_SORT_SPILL_GROUP_SIZE);
     SPILL_THRESHOLD = 
config.getInt(ExecConstants.EXTERNAL_SORT_SPILL_THRESHOLD);
-    SPILL_DIRECTORIES = 
config.getStringList(ExecConstants.EXTERNAL_SORT_SPILL_DIRS);
-    dirs = Iterators.cycle(Lists.newArrayList(SPILL_DIRECTORIES));
+    dirs = 
Iterators.cycle(config.getStringList(ExecConstants.EXTERNAL_SORT_SPILL_DIRS));
     copierAllocator = oContext.getAllocator().getChildAllocator(
         context, PriorityQueueCopier.INITIAL_ALLOCATION, 
PriorityQueueCopier.MAX_ALLOCATION, true);
     FragmentHandle handle = context.getHandle();
@@ -203,7 +201,9 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
       if (copier != null) {
         copier.close();
       }
-      copierAllocator.close();
+      if (copierAllocator != null) {
+        copierAllocator.close();
+      }
       super.close();
 
       if(mSorter != null) {
@@ -269,8 +269,6 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
     try{
       container.clear();
       outer: while (true) {
-        Stopwatch watch = new Stopwatch();
-        watch.start();
         IterOutcome upstream;
         if (first) {
           upstream = IterOutcome.OK_NEW_SCHEMA;
@@ -280,7 +278,6 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
         if (upstream == IterOutcome.OK && sorter == null) {
           upstream = IterOutcome.OK_NEW_SCHEMA;
         }
-//        logger.debug("Took {} us to get next", 
watch.elapsed(TimeUnit.MICROSECONDS));
         switch (upstream) {
         case NONE:
           if (first) {
@@ -330,10 +327,7 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
           int count = sv2.getCount();
           totalCount += count;
           sorter.setup(context, sv2, incoming);
-          Stopwatch w = new Stopwatch();
-          w.start();
           sorter.sort(sv2);
-//          logger.debug("Took {} us to sort {} records", 
w.elapsed(TimeUnit.MICROSECONDS), count);
           RecordBatchData rbd = new RecordBatchData(incoming);
           boolean success = false;
           try {
@@ -371,8 +365,6 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
                 batchesSinceLastSpill = 0;
               }
             }
-            long t = w.elapsed(TimeUnit.MICROSECONDS);
-//          logger.debug("Took {} us to sort {} records", t, count);
             success = true;
           } finally {
             if (!success) {
@@ -403,8 +395,6 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
         return IterOutcome.NONE;
       }
       if (spillCount == 0) {
-        Stopwatch watch = new Stopwatch();
-        watch.start();
 
         if (builder != null) {
           builder.clear();
@@ -436,8 +426,6 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
         injector.injectUnchecked(context.getExecutionControls(), 
INTERRUPTION_AFTER_SORT);
         sv4 = mSorter.getSV4();
 
-        long t = watch.elapsed(TimeUnit.MICROSECONDS);
-//        logger.debug("Took {} us to sort {} records", t, 
sv4.getTotalCount());
         container.buildSchema(SelectionVectorMode.FOUR_BYTE);
       } else { // some batches were spilled
         final BatchGroup merged = mergeAndSpill(batchGroups);
@@ -446,6 +434,11 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
         }
         batchGroups.addAll(spilledBatchGroups);
         spilledBatchGroups = null; // no need to cleanup spilledBatchGroups, 
all it's batches are in batchGroups now
+
+        // copierAllocator is no longer needed now. Closing it will free 
memory for this operator
+        copierAllocator.close();
+        copierAllocator = null;
+
         logger.warn("Starting to merge. {} batch groups. Current allocated 
memory: {}", batchGroups.size(), oContext.getAllocator().getAllocatedMemory());
         VectorContainer hyperBatch = constructHyperBatch(batchGroups);
         createCopier(hyperBatch, batchGroups, container, false);
@@ -615,11 +608,18 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
     SelectionVector2 sv2 = new SelectionVector2(oContext.getAllocator());
     if (!sv2.allocateNewSafe(incoming.getRecordCount())) {
       try {
-        spilledBatchGroups.addFirst(mergeAndSpill(batchGroups));
+        // Not being able to allocate sv2 means this operator's allocator 
reached it's maximum capacity.
+        // Spilling this.batchGroups won't help here as they are owned by 
upstream operator,
+        // but spilling spilledBatchGroups may free enough memory
+        final BatchGroup merged = mergeAndSpill(spilledBatchGroups);
+        if (merged != null) {
+          spilledBatchGroups.addFirst(merged);
+        } else {
+          throw new OutOfMemoryException("Unable to allocate sv2, and not 
enough batchGroups to spill");
+        }
       } catch (SchemaChangeException e) {
         throw new RuntimeException(e);
       }
-      batchesSinceLastSpill = 0;
       int waitTime = 1;
       while (true) {
         try {
@@ -775,9 +775,7 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
         outputContainer.add(v);
       }
       copier.setup(context, allocator, batch, batchGroupList, outputContainer);
-    } catch (ClassTransformationException e) {
-      throw new RuntimeException(e);
-    } catch (IOException e) {
+    } catch (ClassTransformationException | IOException e) {
       throw new RuntimeException(e);
     }
   }
@@ -793,21 +791,4 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
     incoming.kill(sendUpstream);
   }
 
-  private String getFileName(int spill) {
-     /*
-     * From the context, get the query id, major fragment id, minor fragment 
id. This will be used as the file name to
-     * which we will dump the incoming buffer data
-     */
-    FragmentHandle handle = context.getHandle();
-
-    String qid = QueryIdHelper.getQueryId(handle.getQueryId());
-
-    int majorFragmentId = handle.getMajorFragmentId();
-    int minorFragmentId = handle.getMinorFragmentId();
-
-    String fileName = 
String.format("%s//%s//major_fragment_%s//minor_fragment_%s//operator_%s//%s", 
dirs.next(), qid, majorFragmentId, minorFragmentId, popConfig.getOperatorId(), 
spill);
-
-    return fileName;
-  }
-
 }

Reply via email to