JacksonYao287 commented on a change in pull request #2808: URL: https://github.com/apache/ozone/pull/2808#discussion_r751160266
########## File path: hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/FindSourceGreedy.java ########## @@ -0,0 +1,150 @@ +/* + * 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; + +/** + * 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 List<DatanodeUsageInfo> potentialSources; + private NodeManager nodeManager; + private ContainerBalancerConfiguration config; + private Double lowerLimit; + + FindSourceGreedy(NodeManager nodeManager) { + sizeLeavingNode = new HashMap<>(); + this.nodeManager = nodeManager; + } + + private void setLowerLimit(Double lowerLimit) { + this.lowerLimit = lowerLimit; + } + + private void setPotentialSources( + List<DatanodeUsageInfo> potentialSources) { + this.potentialSources = potentialSources; + sizeLeavingNode.clear(); + potentialSources.forEach( + c -> sizeLeavingNode.put(c.getDatanodeDetails(), 0L)); + } + + 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); + return; + } + LOG.warn("Cannot find datanode {} in candidate source datanodes", + dui.getUuid()); + } + + /** + * get the next candidate source data node according to + * the strategy. + * + * @return the nex candidate source data node. + */ + @Override + public DatanodeDetails getNextCandidateSourceDataNode() { + if (potentialSources.isEmpty()) { + LOG.info("no more candidate source data node"); + return null; + } + //TODO:use a more quick data structure, which will hava a + // better performance when changing or deleting one element at once + potentialSources.sort((a, b) -> { Review comment: TreeSet is implemented by a binary search tree. when searching or inserting any element , it will take O(logN) of time. priority queue is implemented with heap sort, which is very quick to get the top 1 element after initialized, and I have considered using it。 here, we always try to get the element with the biggest usage(top 1). so i think it is better off using priority queue here. -- 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]
