siddhantsangwan commented on a change in pull request #2230:
URL: https://github.com/apache/ozone/pull/2230#discussion_r629901382



##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancer.java
##########
@@ -59,74 +80,305 @@ public ContainerBalancer(
     this.ozoneConfiguration = ozoneConfiguration;
     this.balancerRunning = false;
     this.config = new ContainerBalancerConfiguration();
+    this.metrics = new ContainerBalancerMetrics();
   }
 
   /**
-   * Start ContainerBalancer. Current implementation is incomplete.
+   * Starts ContainerBalancer. Current implementation is incomplete.
    *
    * @param balancerConfiguration Configuration values.
    */
   public void start(ContainerBalancerConfiguration balancerConfiguration) {
+    if (balancerRunning) {
+      LOG.info("Container Balancer is already running.");
+      throw new RuntimeException();
+    }
     this.balancerRunning = true;
-
     ozoneConfiguration = new OzoneConfiguration();
 
-    // initialise configs
     this.config = balancerConfiguration;
     this.threshold = config.getThreshold();
-    this.maxDatanodesToBalance =
-        config.getMaxDatanodesToBalance();
+    this.maxDatanodesToBalance = config.getMaxDatanodesToBalance();
     this.maxSizeToMove = config.getMaxSizeToMove();
 
+    this.clusterCapacity = 0L;
+    this.clusterUsed = 0L;
+    this.clusterRemaining = 0L;
+
+    this.overUtilizedNodes = new ArrayList<>();
+    this.underUtilizedNodes = new ArrayList<>();
+    this.aboveAverageUtilizedNodes = new ArrayList<>();
+    this.belowAverageUtilizedNodes = new ArrayList<>();
+    this.sourceNodes = new ArrayList<>();
+
     LOG.info("Starting Container Balancer...");
+    LOG.info(toString());
+
+    balance();
+  }
 
-    // sorted list in order from most to least used
-    List<DatanodeUsageInfo> nodes = nodeManager.
-        getMostOrLeastUsedDatanodes(true);
-    double avgUtilisation = calculateAvgUtilisation(nodes);
+  /**
+   * Balances the cluster.
+   */
+  private void balance() {
+    boolean initialized = initializeIteration();
+  }
+
+  /**
+   * Initializes an iteration during balancing. Recognizes over, under,
+   * below-average,and under-average utilizes nodes. Decides whether
+   * balancing needs to continue or should be stopped.
+   */
+  private boolean initializeIteration() {
+    List<DatanodeUsageInfo> nodes;
+    try {
+      // sorted list in order from most to least used
+      nodes = nodeManager.getMostOrLeastUsedDatanodes(true);
+    } catch (NullPointerException e) {
+      LOG.error("Container Balancer could not retrieve nodes from Node " +
+          "Manager.", e);
+      stop();
+      return false;
+    }
+
+    clusterAvgUtilisation = calculateAvgUtilization(nodes);
+    LOG.info("Average utilization of the cluster is {}", 
clusterAvgUtilisation);
 
     // under utilized nodes have utilization(that is, used / capacity) less
     // than lower limit
-    double lowerLimit = avgUtilisation - threshold;
+    double lowerLimit = clusterAvgUtilisation - threshold;
 
     // over utilized nodes have utilization(that is, used / capacity) greater
     // than upper limit
-    double upperLimit = avgUtilisation + threshold;
+    double upperLimit = clusterAvgUtilisation + threshold;
+
     LOG.info("Lower limit for utilization is {}", lowerLimit);
     LOG.info("Upper limit for utilization is {}", upperLimit);
 
-    // find over utilised(source) and under utilised(target) nodes
-    sourceNodes = new ArrayList<>();
-    targetNodes = new ArrayList<>();
-//    for (DatanodeUsageInfo node : nodes) {
-//      SCMNodeStat stat = node.getScmNodeStat();
-//      double utilization = stat.getScmUsed().get().doubleValue() /
-//          stat.getCapacity().get().doubleValue();
-//      if (utilization > upperLimit) {
-//        sourceNodes.add(node);
-//      } else if (utilization < lowerLimit || utilization < avgUtilisation) {
-//        targetNodes.add(node);
-//      }
-//    }
-  }
-
-  // calculate the average datanode utilisation across the cluster
-  private double calculateAvgUtilisation(List<DatanodeUsageInfo> nodes) {
+    long numDatanodesToBalance = 0L;
+    double overLoadedBytes = 0D, underLoadedBytes = 0D;
+
+    // find over and under utilized nodes
+    for (DatanodeUsageInfo node : nodes) {
+      double utilization = calculateUtilization(node);
+      if (utilization > clusterAvgUtilisation) {
+        if (utilization > upperLimit) {
+          overUtilizedNodes.add(node);
+          numDatanodesToBalance += 1;
+
+          // amount of bytes greater than upper limit in this node
+          overLoadedBytes +=
+              ratioToBytes(node.getScmNodeStat().getCapacity().get(),
+                  utilization) -
+                  ratioToBytes(node.getScmNodeStat().getCapacity().get(),
+                      upperLimit);
+        } else {
+          aboveAverageUtilizedNodes.add(node);
+        }
+      } else if (utilization < clusterAvgUtilisation) {
+        if (utilization < lowerLimit) {
+          underUtilizedNodes.add(node);
+          numDatanodesToBalance += 1;
+
+          // amount of bytes lesser than lower limit in this node
+          underLoadedBytes +=
+              ratioToBytes(node.getScmNodeStat().getCapacity().get(),
+                  lowerLimit) -
+                  ratioToBytes(node.getScmNodeStat().getCapacity().get(),
+                      utilization);
+        } else {
+          belowAverageUtilizedNodes.add(node);
+        }
+      }
+    }
+
+    Collections.reverse(underUtilizedNodes);
+    Collections.reverse(belowAverageUtilizedNodes);
+
+    long numDatanodesBalanced = 0;
+    // count number of nodes that were balanced in previous iteration
+    for (DatanodeUsageInfo node : sourceNodes) {
+      if (!containsNode(overUtilizedNodes, node) &&
+          !containsNode(underUtilizedNodes, node)) {
+        numDatanodesBalanced += 1;
+      }
+    }
+    metrics.setNumDatanodesBalanced(new LongMetric(numDatanodesBalanced));
+    sourceNodes = new ArrayList<>(
+        overUtilizedNodes.size() + underUtilizedNodes.size());
+
+    if (numDatanodesBalanced + numDatanodesToBalance > maxDatanodesToBalance) {
+      LOG.info("Approaching Max Datanodes To Balance limit in Container " +
+          "Balancer. Stopping Balancer.");
+      stop();
+      return false;
+    } else {
+      sourceNodes.addAll(overUtilizedNodes);
+      sourceNodes.addAll(underUtilizedNodes);

Review comment:
       Yes. However, the meaning of source nodes might change according to the 
algorithm for moving containers. The term will be definite once the exact 
algorithm is final.




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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to