siddhantsangwan commented on a change in pull request #2230: URL: https://github.com/apache/ozone/pull/2230#discussion_r631619226
########## File path: hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/balancer/TestContainerBalancer.java ########## @@ -0,0 +1,213 @@ +/* + * 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.hadoop.hdds.scm.container.balancer; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.protocol.MockDatanodeDetails; +import org.apache.hadoop.hdds.scm.container.ContainerManagerV2; +import org.apache.hadoop.hdds.scm.container.MockNodeManager; +import org.apache.hadoop.hdds.scm.container.ReplicationManager; +import org.apache.hadoop.hdds.scm.container.placement.metrics.SCMNodeStat; +import org.apache.hadoop.hdds.scm.node.DatanodeUsageInfo; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; + +public class TestContainerBalancer { + + private static final Logger LOG = + LoggerFactory.getLogger(TestContainerBalancer.class); + + private ReplicationManager replicationManager; + private ContainerManagerV2 containerManager; + private ContainerBalancer containerBalancer; + private MockNodeManager mockNodeManager; + private OzoneConfiguration conf; + private ContainerBalancerConfiguration balancerConfiguration; + private List<DatanodeUsageInfo> nodesInCluster; + private List<Double> nodeUtilizations; + private double averageUtilization; + private int numberOfNodes; + + /** + * Sets up configuration values and creates a mock cluster. + */ + @Before + public void setup() { + conf = new OzoneConfiguration(); + containerManager = Mockito.mock(ContainerManagerV2.class); + replicationManager = Mockito.mock(ReplicationManager.class); + + balancerConfiguration = new ContainerBalancerConfiguration(); + balancerConfiguration.setThreshold("0.1"); + balancerConfiguration.setMaxDatanodesToBalance(10); + balancerConfiguration.setMaxSizeToMove(500L); + conf.setFromObject(balancerConfiguration); + + this.numberOfNodes = 10; + generateUtilizations(numberOfNodes); + + // create datanodes with the generated nodeUtilization values + this.averageUtilization = createNodesInCluster(); + mockNodeManager = new MockNodeManager(nodesInCluster); + containerBalancer = new ContainerBalancer(mockNodeManager, containerManager, + replicationManager, conf); + } + + /** + * Checks whether ContainerBalancer is correctly updating the list of source + * nodes with varying values of Threshold. + */ + @Test + public void initializeIterationShouldUpdateSourceNodesWhenThresholdChanges() { + List<DatanodeUsageInfo> expectedSourceNodes; + List<DatanodeUsageInfo> sourceNodesAccordingToBalancer; + + // check for random threshold values + for (int i = 0; i < 50; i++) { + double randomThreshold = Math.random(); + + balancerConfiguration.setThreshold(String.valueOf(randomThreshold)); + containerBalancer.start(balancerConfiguration); + expectedSourceNodes = determineExpectedSourceNodes(randomThreshold); + sourceNodesAccordingToBalancer = containerBalancer.getSourceNodes(); + + Assert.assertEquals( + expectedSourceNodes.size(), sourceNodesAccordingToBalancer.size()); + + for (int j = 0; j < expectedSourceNodes.size(); j++) { + Assert.assertEquals(expectedSourceNodes.get(j).getDatanodeDetails(), + sourceNodesAccordingToBalancer.get(j).getDatanodeDetails()); + } + containerBalancer.stop(); + } + + } + + /** + * Checks whether the list of source is empty when the cluster is balanced. + */ + @Test + public void sourceNodesListShouldBeEmptyWhenClusterIsBalanced() { + balancerConfiguration.setThreshold("0.99"); + containerBalancer.start(balancerConfiguration); + + Assert.assertEquals(0, containerBalancer.getSourceNodes().size()); + containerBalancer.stop(); + } + + /** + * Checks whether ContainerBalancer stops when the limit of + * MaxDatanodesToBalance is reached. + */ + @Test + public void containerBalancerShouldStopWhenMaxDatanodesToBalanceIsReached() { + balancerConfiguration.setMaxDatanodesToBalance(2); + balancerConfiguration.setThreshold("0"); + containerBalancer.start(balancerConfiguration); + + Assert.assertFalse(containerBalancer.isBalancerRunning()); + containerBalancer.stop(); + } + + /** + * Determines source nodes, that is, over and under utilized nodes, + * according to the generated utilization values for nodes and the threshold. + * + * @param threshold A fraction from range 0 to 1. + * @return List of DatanodeUsageInfo containing the expected(correct) + * source nodes. + */ + private List<DatanodeUsageInfo> determineExpectedSourceNodes( + double threshold) { + double lowerLimit = averageUtilization - threshold; + double upperLimit = averageUtilization + threshold; + + // use node utilizations to determine over and under utilized nodes + List<DatanodeUsageInfo> expectedSourceNodes = new ArrayList<>(); + for (int i = 0; i < numberOfNodes; i++) { + if (nodeUtilizations.get(numberOfNodes - i - 1) > upperLimit) { Review comment: By using a separate method, varying thresholds can be passed as argument to determine the expected list for each threshold. -- 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]
