Repository: drill
Updated Branches:
  refs/heads/master f7a0d38ea -> 653388772


DRILL-3543: Add stats for external sort to a query profile

this closes #280


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

Branch: refs/heads/master
Commit: 653388772550936bc132caf4175c3a7eb0b592f2
Parents: f7a0d38
Author: Hakim <[email protected]>
Authored: Mon Nov 23 12:36:30 2015 -0800
Committer: adeneche <[email protected]>
Committed: Mon Nov 23 14:19:48 2015 -0800

----------------------------------------------------------------------
 .../drill/exec/ops/OperatorMetricRegistry.java  |  2 ++
 .../physical/impl/xsort/ExternalSortBatch.java  | 26 +++++++++++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/65338877/exec/java-exec/src/main/java/org/apache/drill/exec/ops/OperatorMetricRegistry.java
----------------------------------------------------------------------
diff --git 
a/exec/java-exec/src/main/java/org/apache/drill/exec/ops/OperatorMetricRegistry.java
 
b/exec/java-exec/src/main/java/org/apache/drill/exec/ops/OperatorMetricRegistry.java
index 75426d5..b704bb6 100644
--- 
a/exec/java-exec/src/main/java/org/apache/drill/exec/ops/OperatorMetricRegistry.java
+++ 
b/exec/java-exec/src/main/java/org/apache/drill/exec/ops/OperatorMetricRegistry.java
@@ -25,6 +25,7 @@ import org.apache.drill.exec.physical.impl.join.HashJoinBatch;
 import org.apache.drill.exec.physical.impl.mergereceiver.MergingRecordBatch;
 import 
org.apache.drill.exec.physical.impl.partitionsender.PartitionSenderRootExec;
 import 
org.apache.drill.exec.physical.impl.unorderedreceiver.UnorderedReceiverBatch;
+import org.apache.drill.exec.physical.impl.xsort.ExternalSortBatch;
 import org.apache.drill.exec.proto.UserBitShared.CoreOperatorType;
 
 /**
@@ -45,6 +46,7 @@ public class OperatorMetricRegistry {
     register(CoreOperatorType.UNORDERED_RECEIVER_VALUE, 
UnorderedReceiverBatch.Metric.class);
     register(CoreOperatorType.HASH_AGGREGATE_VALUE, 
HashAggTemplate.Metric.class);
     register(CoreOperatorType.HASH_JOIN_VALUE, HashJoinBatch.Metric.class);
+    register(CoreOperatorType.EXTERNAL_SORT_VALUE, 
ExternalSortBatch.Metric.class);
   }
 
   private static void register(final int operatorType, final Class<? extends 
MetricDef> metricDef) {

http://git-wip-us.apache.org/repos/asf/drill/blob/65338877/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 1f3c2d0..a99a986 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
@@ -48,6 +48,7 @@ import org.apache.drill.exec.expr.TypeHelper;
 import org.apache.drill.exec.expr.fn.FunctionGenerationHelper;
 import org.apache.drill.exec.memory.BufferAllocator;
 import org.apache.drill.exec.ops.FragmentContext;
+import org.apache.drill.exec.ops.MetricDef;
 import org.apache.drill.exec.physical.config.ExternalSort;
 import org.apache.drill.exec.physical.impl.sort.RecordBatchData;
 import org.apache.drill.exec.physical.impl.sort.SortRecordBatchBuilder;
@@ -67,7 +68,6 @@ import 
org.apache.drill.exec.record.selection.SelectionVector2;
 import org.apache.drill.exec.record.selection.SelectionVector4;
 import org.apache.drill.exec.testing.ControlsInjector;
 import org.apache.drill.exec.testing.ControlsInjectorFactory;
-import org.apache.drill.exec.util.BatchPrinter;
 import org.apache.drill.exec.vector.CopyUtil;
 import org.apache.drill.exec.vector.ValueVector;
 import org.apache.drill.exec.vector.complex.AbstractContainerVector;
@@ -119,6 +119,8 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
   private int targetRecordCount;
   private final String fileName;
   private int firstSpillBatchCount = 0;
+  private long peakSizeInMemory = -1;
+  private int peakNumBatches = -1;
 
   /**
    * The copier uses the COPIER_BATCH_MEM_LIMIT to estimate the target
@@ -130,6 +132,16 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
   public static final String INTERRUPTION_AFTER_SETUP = "after-setup";
   public static final String INTERRUPTION_WHILE_SPILLING = "spilling";
 
+  public enum Metric implements MetricDef {
+    SPILL_COUNT,            // number of times operator spilled to disk
+    PEAK_SIZE_IN_MEMORY,    // peak value for totalSizeInMemory
+    PEAK_BATCHES_IN_MEMORY; // maximum number of batches kept in memory
+
+    @Override
+    public int metricId() {
+      return ordinal();
+    }
+  }
 
   public ExternalSortBatch(ExternalSort popConfig, FragmentContext context, 
RecordBatch incoming) throws OutOfMemoryException {
     super(popConfig, context, true);
@@ -340,6 +352,11 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
             }
           }
           totalSizeInMemory += getBufferSize(convertedBatch);
+          if (peakSizeInMemory < totalSizeInMemory) {
+            peakSizeInMemory = totalSizeInMemory;
+            stats.setLongStat(Metric.PEAK_SIZE_IN_MEMORY, peakSizeInMemory);
+          }
+
           int count = sv2.getCount();
           totalCount += count;
           sorter.setup(context, sv2, convertedBatch);
@@ -349,6 +366,11 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
           try {
             rbd.setSv2(sv2);
             batchGroups.add(new BatchGroup(rbd.getContainer(), rbd.getSv2(), 
oContext));
+            if (peakNumBatches < batchGroups.size()) {
+              peakNumBatches = batchGroups.size();
+              stats.setLongStat(Metric.PEAK_BATCHES_IN_MEMORY, peakNumBatches);
+            }
+
             batchesSinceLastSpill++;
             if (// We have spilled at least once and the current memory used 
is more than the 75% of peak memory used.
                 (spillCount > 0 && totalSizeInMemory > .75 * highWaterMark) ||
@@ -544,6 +566,8 @@ public class ExternalSortBatch extends 
AbstractRecordBatch<ExternalSort> {
     c1.setRecordCount(count);
 
     String outputFile = Joiner.on("/").join(dirs.next(), fileName, 
spillCount++);
+    stats.setLongStat(Metric.SPILL_COUNT, spillCount);
+
     BatchGroup newGroup = new BatchGroup(c1, fs, outputFile, oContext);
     boolean threw = true; // true if an exception is thrown in the try block 
below
     logger.info("Merging and spilling to {}", outputFile);

Reply via email to