siddhantsangwan commented on a change in pull request #2441: URL: https://github.com/apache/ozone/pull/2441#discussion_r686018632
########## File path: hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/FindTargetGreedy.java ########## @@ -0,0 +1,135 @@ +/* + * 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.ContainerPlacementStatus; +import org.apache.hadoop.hdds.scm.PlacementPolicy; +import org.apache.hadoop.hdds.scm.container.ContainerID; +import org.apache.hadoop.hdds.scm.container.ContainerInfo; +import org.apache.hadoop.hdds.scm.container.ContainerManagerV2; +import org.apache.hadoop.hdds.scm.container.ContainerNotFoundException; +import org.apache.hadoop.hdds.scm.container.ContainerReplica; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.List; +import java.util.Set; +import java.util.function.BiFunction; +import java.util.stream.Collectors; + +/** + * Find a target giving preference to more under-utilized nodes. + */ +public class FindTargetGreedy implements FindTargetStrategy { + private static final Logger LOG = + LoggerFactory.getLogger(FindTargetGreedy.class); + + private ContainerManagerV2 containerManager; + private PlacementPolicy placementPolicy; + + public FindTargetGreedy( + ContainerManagerV2 containerManager, + PlacementPolicy placementPolicy) { + this.containerManager = containerManager; + this.placementPolicy = placementPolicy; + } + + /** + * Find a {@link ContainerMoveSelection} consisting of a target and + * container to move for a source datanode. Favours more under-utilized nodes. + * @param source Datanode to find a target for + * @param potentialTargets Collection of potential target datanodes + * @param candidateContainers Set of candidate containers satisfying + * selection criteria + * {@link ContainerBalancerSelectionCriteria} + * @param canSizeEnterTarget A functional interface whose apply + * (DatanodeDetails, Long) method returns true if the size specified in the + * second argument can enter the specified DatanodeDetails node + * @return Found target and container + */ + @Override + public ContainerMoveSelection findTargetForContainerMove( + DatanodeDetails source, Collection<DatanodeDetails> potentialTargets, + Set<ContainerID> candidateContainers, + BiFunction<DatanodeDetails, Long, Boolean> canSizeEnterTarget) { + for (DatanodeDetails target : potentialTargets) { + for (ContainerID container : candidateContainers) { + Set<ContainerReplica> replicas; + ContainerInfo containerInfo; + + try { + replicas = containerManager.getContainerReplicas(container); + containerInfo = containerManager.getContainer(container); + } catch (ContainerNotFoundException e) { + LOG.warn("Could not get Container {} from Container Manager for " + + "obtaining replicas in Container Balancer.", container, e); + continue; + } + + if (replicas.stream().noneMatch( + replica -> replica.getDatanodeDetails().equals(target)) && + containerMoveSatisfiesPlacementPolicy(container, replicas, source, + target) && + canSizeEnterTarget.apply(target, containerInfo.getUsedBytes())) { Review comment: Yes, we can do this in another jira. -- 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]
