Skip to content

Commit

Permalink
Refactoring more tests from TestContainerBalancerTask: use MockedSCM …
Browse files Browse the repository at this point in the history
…instance for them
  • Loading branch information
Montura committed May 2, 2024
1 parent a658802 commit 14e70dd
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public class ContainerBalancerTask implements Runnable {
private OzoneConfiguration ozoneConfiguration;
private ContainerBalancer containerBalancer;
private final SCMContext scmContext;
private double threshold;
private int totalNodesInCluster;
private double maxDatanodesRatioToInvolvePerIteration;
private long maxSizeToMovePerIteration;
Expand All @@ -84,17 +83,13 @@ public class ContainerBalancerTask implements Runnable {
// count actual size moved in bytes
private long sizeActuallyMovedInLatestIteration;
private int iterations;
private List<DatanodeUsageInfo> unBalancedNodes;
private List<DatanodeUsageInfo> overUtilizedNodes;
private List<DatanodeUsageInfo> underUtilizedNodes;
private final List<DatanodeUsageInfo> overUtilizedNodes;
private final List<DatanodeUsageInfo> underUtilizedNodes;
private List<DatanodeUsageInfo> withinThresholdUtilizedNodes;
private Set<String> excludeNodes;
private Set<String> includeNodes;
private ContainerBalancerConfiguration config;
private ContainerBalancerMetrics metrics;
private long clusterCapacity;
private long clusterRemaining;
private double clusterAvgUtilisation;
private PlacementPolicyValidateProxy placementPolicyValidateProxy;
private NetworkTopology networkTopology;
private double upperLimit;
Expand Down Expand Up @@ -150,7 +145,6 @@ public ContainerBalancerTask(StorageContainerManager scm,
this.overUtilizedNodes = new ArrayList<>();
this.underUtilizedNodes = new ArrayList<>();
this.withinThresholdUtilizedNodes = new ArrayList<>();
this.unBalancedNodes = new ArrayList<>();
this.placementPolicyValidateProxy = scm.getPlacementPolicyValidateProxy();
this.networkTopology = scm.getClusterMap();
this.nextIterationIndex = nextIterationIndex;
Expand Down Expand Up @@ -348,7 +342,6 @@ private boolean initializeIteration() {
return false;
}

this.threshold = config.getThresholdAsRatio();
this.maxDatanodesRatioToInvolvePerIteration =
config.getMaxDatanodesRatioToInvolvePerIteration();
this.maxSizeToMovePerIteration = config.getMaxSizeToMovePerIteration();
Expand All @@ -368,22 +361,19 @@ private boolean initializeIteration() {

this.totalNodesInCluster = datanodeUsageInfos.size();

clusterAvgUtilisation = calculateAvgUtilization(datanodeUsageInfos);
double clusterAvgUtilisation = calculateAvgUtilization(datanodeUsageInfos);
if (LOG.isDebugEnabled()) {
LOG.debug("Average utilization of the cluster is {}",
clusterAvgUtilisation);
LOG.debug("Average utilization of the cluster is {}", clusterAvgUtilisation);
}

// over utilized nodes have utilization(that is, used / capacity) greater
// than upper limit
double threshold = config.getThresholdAsRatio();
// over utilized nodes have utilization(that is, used / capacity) greater than upper limit
this.upperLimit = clusterAvgUtilisation + threshold;
// under utilized nodes have utilization(that is, used / capacity) less
// than lower limit
// under utilized nodes have utilization(that is, used / capacity) less than lower limit
this.lowerLimit = clusterAvgUtilisation - threshold;

if (LOG.isDebugEnabled()) {
LOG.debug("Lower limit for utilization is {} and Upper limit for " +
"utilization is {}", lowerLimit, upperLimit);
LOG.debug("Lower limit for utilization is {} and Upper limit for utilization is {}", lowerLimit, upperLimit);
}

long totalOverUtilizedBytes = 0L, totalUnderUtilizedBytes = 0L;
Expand Down Expand Up @@ -433,12 +423,7 @@ private boolean initializeIteration() {
OzoneConsts.GB);
Collections.reverse(underUtilizedNodes);

unBalancedNodes = new ArrayList<>(
overUtilizedNodes.size() + underUtilizedNodes.size());
unBalancedNodes.addAll(overUtilizedNodes);
unBalancedNodes.addAll(underUtilizedNodes);

if (unBalancedNodes.isEmpty()) {
if (overUtilizedNodes.isEmpty() && underUtilizedNodes.isEmpty()) {
LOG.info("Did not find any unbalanced Datanodes.");
return false;
}
Expand Down Expand Up @@ -487,7 +472,7 @@ private IterationResult doIteration() {
findTargetStrategy.reInitialize(potentialTargets, config, upperLimit);
findSourceStrategy.reInitialize(getPotentialSources(), config, lowerLimit);

moveSelectionToFutureMap = new HashMap<>(unBalancedNodes.size());
moveSelectionToFutureMap = new HashMap<>(underUtilizedNodes.size() + overUtilizedNodes.size());
boolean isMoveGeneratedInThisIteration = false;
iterationResult = IterationResult.ITERATION_COMPLETED;
boolean canAdaptWhenNearingLimits = true;
Expand Down Expand Up @@ -965,8 +950,8 @@ private long ratioToBytes(Long nodeCapacity, double utilizationRatio) {
* @return Average utilization value
*/
@VisibleForTesting
double calculateAvgUtilization(List<DatanodeUsageInfo> nodes) {
if (nodes.size() == 0) {
public static double calculateAvgUtilization(List<DatanodeUsageInfo> nodes) {
if (nodes.isEmpty()) {
LOG.warn("No nodes to calculate average utilization for in " +
"ContainerBalancer.");
return 0;
Expand All @@ -976,8 +961,8 @@ private long ratioToBytes(Long nodeCapacity, double utilizationRatio) {
for (DatanodeUsageInfo node : nodes) {
aggregatedStats.add(node.getScmNodeStat());
}
clusterCapacity = aggregatedStats.getCapacity().get();
clusterRemaining = aggregatedStats.getRemaining().get();
long clusterCapacity = aggregatedStats.getCapacity().get();
long clusterRemaining = aggregatedStats.getRemaining().get();

return (clusterCapacity - clusterRemaining) / (double) clusterCapacity;
}
Expand Down Expand Up @@ -1060,11 +1045,8 @@ private void incSizeSelectedForMoving(DatanodeDetails source,
*/
private void resetState() {
moveManager.resetState();
this.clusterCapacity = 0L;
this.clusterRemaining = 0L;
this.overUtilizedNodes.clear();
this.underUtilizedNodes.clear();
this.unBalancedNodes.clear();
this.containerToSourceMap.clear();
this.containerToTargetMap.clear();
this.selectedSources.clear();
Expand All @@ -1090,15 +1072,14 @@ private boolean isBalancerRunning() {
return taskStatus == Status.RUNNING;
}

/**
* Gets the list of unBalanced nodes, that is, the over and under utilized
* nodes in the cluster.
*
* @return List of DatanodeUsageInfo containing unBalanced nodes.
*/
@VisibleForTesting
List<DatanodeUsageInfo> getUnBalancedNodes() {
return unBalancedNodes;
public List<DatanodeUsageInfo> getOverUtilizedNodes() {
return overUtilizedNodes;
}

@VisibleForTesting
public List<DatanodeUsageInfo> getUnderUtilizedNodes() {
return underUtilizedNodes;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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>
*
* 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.
Expand Down Expand Up @@ -137,7 +137,6 @@ public String toString() {
}

public @Nonnull ContainerBalancerTask startBalancerTask(@Nonnull ContainerBalancerConfiguration config) {
init(config);
return startBalancerTask(new ContainerBalancer(scm), config);
}

Expand Down
Loading

0 comments on commit 14e70dd

Please sign in to comment.