sohami commented on a change in pull request #1677: DRILL-7068: Support memory 
adjustment framework for resource manageme…
URL: https://github.com/apache/drill/pull/1677#discussion_r263256282
 
 

 ##########
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/planner/fragment/QueueQueryParallelizer.java
 ##########
 @@ -0,0 +1,175 @@
+/*
+ * 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.drill.exec.planner.fragment;
+
+import org.apache.calcite.util.Pair;
+import org.apache.drill.common.util.function.CheckedConsumer;
+import org.apache.drill.exec.ops.QueryContext;
+import org.apache.drill.exec.physical.PhysicalOperatorSetupException;
+import org.apache.drill.exec.physical.base.PhysicalOperator;
+import org.apache.drill.exec.planner.cost.NodeResource;
+import org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint;
+import org.apache.drill.exec.util.MemoryAllocationUtilities;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Collection;
+import java.util.Set;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.function.BiFunction;
+import java.util.stream.Collectors;
+
+/**
+ * Paralellizer specialized for managing resources for a query based on 
Queues. This parallelizer
+ * does not deal with increase/decrease of the parallelization of a query plan 
based on the current
+ * cluster state. However, the memory assignment for each operator, minor 
fragment and major
+ * fragment is based on the cluster state and provided queue configuration.
+ */
+public class QueueQueryParallelizer extends SimpleParallelizer {
+  private final boolean enableMemoryPlanning;
+  private final QueryContext queryContext;
+  private final Map<DrillbitEndpoint, Map<PhysicalOperator, Long>> operators;
+
+  public QueueQueryParallelizer(boolean memoryPlanning, QueryContext 
queryContext) {
+    super(queryContext);
+    this.enableMemoryPlanning = memoryPlanning;
+    this.queryContext = queryContext;
+    this.operators = new HashMap<>();
+  }
+
+  // return the memory computed for a physical operator on a drillbitendpoint.
+  public BiFunction<DrillbitEndpoint, PhysicalOperator, Long> getMemory() {
+    return (endpoint, operator) -> {
+      if (enableMemoryPlanning) {
+        return operators.get(endpoint).get(operator);
+      }
+      else {
+        return operator.getMaxAllocation();
+      }
+    };
+  }
+
+  /**
+   * Function called by the SimpleParallelizer to adjust the memory post 
parallelization.
+   * The overall logic is to traverse the fragment tree and call the 
MemoryCalculator on
+   * each major fragment. Once the memory is computed, resource requirement 
are accumulated
+   * per drillbit.
+   *
+   * The total resource requirements are used to select a queue. If the 
selected queue's
+   * resource limit is more/less than the query's requirement than the memory 
will be re-adjusted.
+   *
+   * @param planningSet context of the fragments.
+   * @param roots root fragments.
+   * @param activeEndpoints currently active endpoints.
+   * @throws PhysicalOperatorSetupException
+   */
+  public void adjustMemory(PlanningSet planningSet, Set<Wrapper> roots,
+                           Collection<DrillbitEndpoint> activeEndpoints) 
throws PhysicalOperatorSetupException {
+
+    if (enableMemoryPlanning) {
+      List<PhysicalOperator> bufferedOpers = 
planningSet.getRootWrapper().getNode().getBufferedOperators();
+      
MemoryAllocationUtilities.setupBufferedOpsMemoryAllocations(enableMemoryPlanning,
 bufferedOpers, queryContext);
+      return;
+    }
+    // total node resources for the query plan maintained per drillbit.
+    final Map<DrillbitEndpoint, NodeResource> totalNodeResources =
+            activeEndpoints.stream().collect(Collectors.toMap(x ->x,
+                                                              x -> 
NodeResource.create()));
+
+    // list of the physical operators and their memory requirements per 
drillbit.
+    final Map<DrillbitEndpoint, List<Pair<PhysicalOperator, Long>>> operators =
+            activeEndpoints.stream().collect(Collectors.toMap(x -> x,
+                                                              x -> new 
ArrayList<>()));
+
+    for (Wrapper wrapper : roots) {
+      traverse(wrapper, CheckedConsumer.throwingConsumerWrapper((Wrapper 
fragment) -> {
+        MemoryCalculator calculator = new MemoryCalculator(planningSet, 
queryContext);
+        fragment.getNode().getRoot().accept(calculator, fragment);
+        NodeResource.merge(totalNodeResources, fragment.getResourceMap());
+        operators.entrySet()
+                  .stream()
+                  .forEach((entry) -> entry.getValue()
+                                           
.addAll(calculator.getBufferedOperators(entry.getKey())));
+      }));
+    }
+    //queryrm.selectQueue( pass the max node Resource) returns queue 
configuration.
+    Map<DrillbitEndpoint, List<Pair<PhysicalOperator, Long>>> 
memoryAdjustedOperators = adjustMemoryForOperators(operators, 
totalNodeResources, 10);
+    memoryAdjustedOperators.entrySet().stream().forEach((x) -> {
+      Map<PhysicalOperator, Long> memoryPerOperator = x.getValue().stream()
+                                                                  
.collect(Collectors.toMap(operatorLongPair -> operatorLongPair.left,
+                                                                               
             operatorLongPair -> operatorLongPair.right));
 
 Review comment:
   I think here each Physical Operator is only generated for physical plan and 
not for each minor fragment. Each wrapper object knows how many minor fragments 
needs to be spawned and on which DrillbitEndpoint. So there are chances that 
each Drillbit can be assigned same PhysicalOperator multiple time if there are 
multiple minor fragments running on it for that Wrapper or MajorFragment. So 
the `toMap` operation will fail unless we use different signature and provide a 
mergeFunction in case of duplicates

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to