JacksonYao287 commented on a change in pull request #2808:
URL: https://github.com/apache/ozone/pull/2808#discussion_r773826918



##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/FindSourceGreedy.java
##########
@@ -0,0 +1,158 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hadoop.hdds.scm.container.balancer;
+
+import org.apache.hadoop.hdds.protocol.DatanodeDetails;
+import org.apache.hadoop.hdds.scm.node.DatanodeUsageInfo;
+import org.apache.hadoop.hdds.scm.node.NodeManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.UUID;
+
+/**
+ * The selection criteria for selecting source datanodes , the containers of
+ * which will be moved out.
+ */
+public class FindSourceGreedy implements FindSourceStrategy{
+  private static final Logger LOG =
+      LoggerFactory.getLogger(FindSourceGreedy.class);
+  private Map<DatanodeDetails, Long> sizeLeavingNode;
+  private PriorityQueue<DatanodeUsageInfo> potentialSources;
+  private NodeManager nodeManager;
+  private ContainerBalancerConfiguration config;
+  private Double lowerLimit;
+
+  FindSourceGreedy(NodeManager nodeManager) {
+    sizeLeavingNode = new HashMap<>();
+    potentialSources = new PriorityQueue<>((a, b) -> {
+      double currentUsageOfA = a.calculateUtilization(
+          -sizeLeavingNode.get(a.getDatanodeDetails()));
+      double currentUsageOfB = b.calculateUtilization(
+          -sizeLeavingNode.get(b.getDatanodeDetails()));
+      //in descending order
+      int ret = Double.compare(currentUsageOfB, currentUsageOfA);
+      if (ret != 0) {
+        return ret;
+      }
+      UUID uuidA = a.getDatanodeDetails().getUuid();
+      UUID uuidB = b.getDatanodeDetails().getUuid();
+      return uuidA.compareTo(uuidB);
+    });
+    this.nodeManager = nodeManager;
+  }
+
+  private void setLowerLimit(Double lowerLimit) {
+    this.lowerLimit = lowerLimit;
+  }
+
+  private void setPotentialSources(
+      List<DatanodeUsageInfo> potentialSourceDataNodes) {
+    potentialSources.clear();
+    sizeLeavingNode.clear();
+    potentialSourceDataNodes.forEach(
+        c -> sizeLeavingNode.put(c.getDatanodeDetails(), 0L));
+    potentialSources.addAll(potentialSourceDataNodes);
+  }
+
+  private void setConfiguration(ContainerBalancerConfiguration conf) {
+    this.config = conf;
+  }
+
+  /**
+   * increase the Leaving size of a candidate source data node.
+   */
+  @Override
+  public void increaseSizeLeaving(DatanodeDetails dui, long size) {
+    Long currentSize = sizeLeavingNode.get(dui);
+    if(currentSize != null) {
+      sizeLeavingNode.put(dui, currentSize + size);
+      //reorder according to the latest sizeLeavingNode
+      potentialSources.add(nodeManager.getUsageInfo(dui));
+      return;

Review comment:
       let me explain this. 
   when selecting a source datanode, we always want to the select the one which 
has a largest storage usage. here, i use `PriorityQueue`, which is fast to get 
the top one. when calling `getNextCandidateSourceDataNode`, 
`PriorityQueue#poll` is called , which will get and remove the top source data 
node for this PriorityQueue.
   
   actually, now we supporting move multiple containers from one datanode , so 
a data node can be selected as source for multiple times in one iteration. 
   
   here are two reasons to call 
`potentialSources.add(nodeManager.getUsageInfo(dui));` 
   1 add the data node back  to the PriorityQueue again, so it can be selected 
as a source again.
   2 when we update `sizeLeavingNode`,  the usage of this data node will be 
considered changed (the reported usage - sizeLeaving), so we need to sort all 
the candidate source datanodes according to the latest usage and get the top 
one. when adding the data node back to the  PriorityQueue, PriorityQueue will 
sort all the datanode again(it will use heap sort , so very fast), so we can 
get the next top one.
   
   i am not sure is it clear to you now?
   
   




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

To unsubscribe, e-mail: [email protected]

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