Skip to content

Commit a2f967d

Browse files
committed
HDFS-17646. Add Option to limit Balancer prefer highly utilized nodes num in each iteration.
1 parent 78a08b3 commit a2f967d

File tree

3 files changed

+126
-7
lines changed

3 files changed

+126
-7
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/balancer/Balancer.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ public class Balancer {
208208
+ "\n\t[-sortTopNodes]"
209209
+ "\tSort datanodes based on the utilization so "
210210
+ "that highly utilized datanodes get scheduled first."
211+
+ "\n\t[-limitTopNodes <maxNodesNum>]"
212+
+ "\tUse the '-sortTopNodes' parameter to limit the number of top nodes"
211213
+ "\n\t[-hotBlockTimeInterval]\tprefer to move cold blocks.";
212214

213215
@VisibleForTesting
@@ -227,6 +229,7 @@ public class Balancer {
227229
private final long maxSizeToMove;
228230
private final long defaultBlockSize;
229231
private final boolean sortTopNodes;
232+
private final int limitTopNodesNum;
230233
private final BalancerMetrics metrics;
231234

232235
// all data node lists
@@ -352,6 +355,7 @@ static int getFailedTimesSinceLastSuccessfulBalance() {
352355
this.sourceNodes = p.getSourceNodes();
353356
this.runDuringUpgrade = p.getRunDuringUpgrade();
354357
this.sortTopNodes = p.getSortTopNodes();
358+
this.limitTopNodesNum = p.getLimitTopNodesNum();
355359

356360
this.maxSizeToMove = getLongBytes(conf,
357361
DFSConfigKeys.DFS_BALANCER_MAX_SIZE_TO_MOVE_KEY,
@@ -452,6 +456,7 @@ private long init(List<DatanodeStorageReport> reports) {
452456
}
453457
}
454458

459+
int overUtilizedDiffNum = Math.max(overUtilized.size() - limitTopNodesNum, 0);
455460
if (sortTopNodes) {
456461
sortOverUtilized(overUtilizedPercentage);
457462
}
@@ -461,27 +466,31 @@ private long init(List<DatanodeStorageReport> reports) {
461466
metrics.setNumOfUnderUtilizedNodes(underUtilized.size());
462467

463468
Preconditions.checkState(dispatcher.getStorageGroupMap().size()
464-
== overUtilized.size() + underUtilized.size() + aboveAvgUtilized.size()
465-
+ belowAvgUtilized.size(),
469+
== overUtilizedDiffNum + overUtilized.size() + underUtilized.size()
470+
+ aboveAvgUtilized.size() + belowAvgUtilized.size(),
466471
"Mismatched number of storage groups");
467472

468473
// return number of bytes to be moved in order to make the cluster balanced
469474
return Math.max(overLoadedBytes, underLoadedBytes);
470475
}
471476

472477
private void sortOverUtilized(Map<Source, Double> overUtilizedPercentage) {
473-
Preconditions.checkState(overUtilized instanceof List,
474-
"Collection overUtilized is not a List.");
478+
Preconditions.checkState(overUtilized instanceof LinkedList,
479+
"Collection overUtilized is not a LinkedList.");
475480

476481
LOG.info("Sorting over-utilized nodes by capacity" +
477482
" to bring down top used datanode capacity faster");
478483

479-
List<Source> list = (List<Source>) overUtilized;
484+
LinkedList<Source> list = (LinkedList<Source>) overUtilized;
480485
list.sort(
481486
(Source source1, Source source2) ->
482487
(Double.compare(overUtilizedPercentage.get(source2),
483488
overUtilizedPercentage.get(source1)))
484489
);
490+
int size = overUtilized.size();
491+
for (int i = 0; i < size - limitTopNodesNum; i++) {
492+
list.removeLast();
493+
}
485494
}
486495

487496
private static long computeMaxSize2Move(final long capacity, final long remaining,
@@ -1071,6 +1080,12 @@ static BalancerParameters parse(String[] args) {
10711080
b.setSortTopNodes(true);
10721081
LOG.info("Balancer will sort nodes by" +
10731082
" capacity usage percentage to prioritize top used nodes");
1083+
} else if ("-limitTopNodesNum".equalsIgnoreCase(args[i])) {
1084+
Preconditions.checkArgument(++i < args.length,
1085+
"limitTopNodesNum value is missing: args = " + Arrays.toString(args));
1086+
int limitNum = Integer.parseInt(args[i]);
1087+
LOG.info("Using a limitTopNodesNum of {}", limitNum);
1088+
b.setLimitTopNodesNum(limitNum);
10741089
} else {
10751090
throw new IllegalArgumentException("args = "
10761091
+ Arrays.toString(args));

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/balancer/BalancerParameters.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ final class BalancerParameters {
5050

5151
private final boolean sortTopNodes;
5252

53+
private final int limitTopNodesNum;
54+
5355
static final BalancerParameters DEFAULT = new BalancerParameters();
5456

5557
private BalancerParameters() {
@@ -67,6 +69,7 @@ private BalancerParameters(Builder builder) {
6769
this.runDuringUpgrade = builder.runDuringUpgrade;
6870
this.runAsService = builder.runAsService;
6971
this.sortTopNodes = builder.sortTopNodes;
72+
this.limitTopNodesNum = builder.limitTopNodesNum;
7073
this.hotBlockTimeInterval = builder.hotBlockTimeInterval;
7174
}
7275

@@ -110,6 +113,10 @@ boolean getSortTopNodes() {
110113
return this.sortTopNodes;
111114
}
112115

116+
int getLimitTopNodesNum() {
117+
return this.limitTopNodesNum;
118+
}
119+
113120
long getHotBlockTimeInterval() {
114121
return this.hotBlockTimeInterval;
115122
}
@@ -120,12 +127,12 @@ public String toString() {
120127
+ " max idle iteration = %s," + " #excluded nodes = %s,"
121128
+ " #included nodes = %s," + " #source nodes = %s,"
122129
+ " #blockpools = %s," + " run during upgrade = %s,"
123-
+ " sort top nodes = %s,"
130+
+ " sort top nodes = %s," + " limit top nodes num = %s"
124131
+ " hot block time interval = %s]",
125132
Balancer.class.getSimpleName(), getClass().getSimpleName(), policy,
126133
threshold, maxIdleIteration, excludedNodes.size(),
127134
includedNodes.size(), sourceNodes.size(), blockpools.size(),
128-
runDuringUpgrade, sortTopNodes, hotBlockTimeInterval);
135+
runDuringUpgrade, sortTopNodes, limitTopNodesNum, hotBlockTimeInterval);
129136
}
130137

131138
static class Builder {
@@ -141,6 +148,7 @@ static class Builder {
141148
private boolean runDuringUpgrade = false;
142149
private boolean runAsService = false;
143150
private boolean sortTopNodes = false;
151+
private int limitTopNodesNum = Integer.MAX_VALUE;
144152
private long hotBlockTimeInterval = 0;
145153

146154
Builder() {
@@ -201,6 +209,11 @@ Builder setSortTopNodes(boolean shouldSortTopNodes) {
201209
return this;
202210
}
203211

212+
Builder setLimitTopNodesNum(int limitTopNodesNum) {
213+
this.limitTopNodesNum = limitTopNodesNum;
214+
return this;
215+
}
216+
204217
BalancerParameters build() {
205218
return new BalancerParameters(this);
206219
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/balancer/TestBalancerLongRunningTasks.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,97 @@ public void testBalancerWithSortTopNodes() throws Exception {
672672
assertEquals(900, maxUsage);
673673
}
674674

675+
@Test(timeout = 60000)
676+
public void testBalancerWithLimitTopNodesNum() throws Exception {
677+
final Configuration conf = new HdfsConfiguration();
678+
// Init the config (block size to 100)
679+
initConf(conf);
680+
conf.setInt(DFS_HEARTBEAT_INTERVAL_KEY, 30000);
681+
682+
final long totalCapacity = 1000L;
683+
final int diffBetweenNodes = 50;
684+
685+
// Set up the nodes with two groups:
686+
// 5 over-utilized nodes with 80%, 85%, 90%, 95%, 100% usage
687+
// 2 under-utilized nodes with 0%, 5% usage
688+
// With sortTopNodes and limitTopNodesNum option, 100% used ones will be chosen
689+
final int numOfOverUtilizedDn = 5;
690+
final int numOfUnderUtilizedDn = 2;
691+
final int totalNumOfDn = numOfOverUtilizedDn + numOfUnderUtilizedDn;
692+
final long[] capacityArray = new long[totalNumOfDn];
693+
Arrays.fill(capacityArray, totalCapacity);
694+
695+
try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf)
696+
.numDataNodes(totalNumOfDn)
697+
.simulatedCapacities(capacityArray)
698+
.build()) {
699+
cluster.setDataNodesDead();
700+
List<DataNode> dataNodes = cluster.getDataNodes();
701+
// Create top used nodes
702+
for (int i = 0; i < numOfOverUtilizedDn; i++) {
703+
// Bring one node alive
704+
DataNodeTestUtils.triggerHeartbeat(dataNodes.get(i));
705+
DataNodeTestUtils.triggerBlockReport(dataNodes.get(i));
706+
// Create nodes with: 80%, 85%, 90%, 95%, 100%
707+
int nodeCapacity = (int) totalCapacity - diffBetweenNodes * (numOfOverUtilizedDn - i - 1);
708+
TestBalancer.createFile(cluster, new Path("test_big" + i), nodeCapacity, (short) 1, 0);
709+
cluster.setDataNodesDead();
710+
}
711+
712+
// Create under utilized nodes
713+
for (int i = 0; i < numOfUnderUtilizedDn; i++) {
714+
// Bring one node alive
715+
DataNodeTestUtils.triggerHeartbeat(dataNodes.get(i));
716+
DataNodeTestUtils.triggerBlockReport(dataNodes.get(i));
717+
// Create nodes with: 5%, 0%
718+
int nodeCapacity = diffBetweenNodes * i;
719+
TestBalancer.createFile(cluster, new Path("test_small" + i), nodeCapacity, (short) 1, 0);
720+
cluster.setDataNodesDead();
721+
}
722+
723+
// Bring all nodes alive
724+
cluster.triggerHeartbeats();
725+
cluster.triggerBlockReports();
726+
cluster.waitFirstBRCompleted(0, 6000);
727+
728+
final BalancerParameters balancerParameters = Balancer.Cli.parse(new String[] {
729+
"-policy", BalancingPolicy.Node.INSTANCE.getName(),
730+
"-threshold", "1",
731+
"-sortTopNodes",
732+
"-limitTopNodesNum", "1"
733+
});
734+
735+
client = NameNodeProxies.createProxy(conf, cluster.getFileSystem(0)
736+
.getUri(), ClientProtocol.class)
737+
.getProxy();
738+
739+
// Set max-size-to-move to small number
740+
// so only top two nodes will be chosen in one iteration
741+
conf.setLong(DFS_BALANCER_MAX_SIZE_TO_MOVE_KEY, 99L);
742+
final Collection<URI> namenodes = DFSUtil.getInternalNsRpcUris(conf);
743+
List<NameNodeConnector> connectors =
744+
NameNodeConnector.newNameNodeConnectors(namenodes, Balancer.class.getSimpleName(),
745+
Balancer.BALANCER_ID_PATH, conf, BalancerParameters.DEFAULT.getMaxIdleIteration());
746+
final Balancer balancer = new Balancer(connectors.get(0), balancerParameters, conf);
747+
Balancer.Result balancerResult = balancer.runOneIteration();
748+
749+
cluster.triggerDeletionReports();
750+
cluster.triggerBlockReports();
751+
cluster.triggerHeartbeats();
752+
753+
DatanodeInfo[] datanodeReport =
754+
client.getDatanodeReport(HdfsConstants.DatanodeReportType.ALL);
755+
long maxUsage = 0;
756+
for (int i = 0; i < totalNumOfDn; i++) {
757+
maxUsage = Math.max(maxUsage, datanodeReport[i].getDfsUsed());
758+
}
759+
// The maxUsage value is 950, only 100% of the nodes will be balanced
760+
assertEquals(950, maxUsage);
761+
assertTrue("BalancerResult is not as expected. " + balancerResult,
762+
(balancerResult.getBytesAlreadyMoved() == 100 && balancerResult.getBlocksMoved() == 1));
763+
}
764+
}
765+
675766
@Test(timeout = 100000)
676767
public void testMaxIterationTime() throws Exception {
677768
final Configuration conf = new HdfsConfiguration();

0 commit comments

Comments
 (0)