Skip to content

Commit a15e94a

Browse files
bbeaudreaultJosh Elser
authored andcommitted
HBASE-26147 Add a dry run mode to the balancer, where moves are calculated but not actually executed
Closes #3630 Signed-off-by: Duo Zhang <zhangduo@apache.org> Signed-off-by: Josh Elser <elserj@apache.org
1 parent f62caa5 commit a15e94a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+966
-205
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/client/Admin.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,20 @@ default void unassign(byte[] regionName, boolean force) throws IOException {
831831
* @return <code>true</code> if balancer ran, <code>false</code> otherwise.
832832
* @throws IOException if a remote or network exception occurs
833833
*/
834-
boolean balance() throws IOException;
834+
default boolean balance() throws IOException {
835+
return balance(BalanceRequest.defaultInstance())
836+
.isBalancerRan();
837+
}
838+
839+
/**
840+
* Invoke the balancer with the given balance request. The BalanceRequest defines how the
841+
* balancer will run. See {@link BalanceRequest} for more details.
842+
*
843+
* @param request defines how the balancer should run
844+
* @return {@link BalanceResponse} with details about the results of the invocation.
845+
* @throws IOException if a remote or network exception occurs
846+
*/
847+
BalanceResponse balance(BalanceRequest request) throws IOException;
835848

836849
/**
837850
* Invoke the balancer. Will run the balancer and if regions to move, it will
@@ -841,8 +854,17 @@ default void unassign(byte[] regionName, boolean force) throws IOException {
841854
* @param force whether we should force balance even if there is region in transition
842855
* @return <code>true</code> if balancer ran, <code>false</code> otherwise.
843856
* @throws IOException if a remote or network exception occurs
857+
* @deprecated Since 2.5.0. Will be removed in 4.0.0.
858+
* Use {@link #balance(BalanceRequest)} instead.
844859
*/
845-
boolean balance(boolean force) throws IOException;
860+
@Deprecated
861+
default boolean balance(boolean force) throws IOException {
862+
return balance(
863+
BalanceRequest.newBuilder()
864+
.setIgnoreRegionsInTransition(force)
865+
.build()
866+
).isBalancerRan();
867+
}
846868

847869
/**
848870
* Query the current state of the balancer.
@@ -2494,10 +2516,20 @@ Pair<List<String>, List<TableName>> getConfiguredNamespacesAndTablesInRSGroup(St
24942516
/**
24952517
* Balance regions in the given RegionServer group
24962518
* @param groupName the group name
2497-
* @return boolean Whether balance ran or not
2519+
* @return BalanceResponse details about the balancer run
24982520
* @throws IOException if a remote or network exception occurs
24992521
*/
2500-
boolean balanceRSGroup(String groupName) throws IOException;
2522+
default BalanceResponse balanceRSGroup(String groupName) throws IOException {
2523+
return balanceRSGroup(groupName, BalanceRequest.defaultInstance());
2524+
}
2525+
2526+
/**
2527+
* Balance regions in the given RegionServer group, running based on
2528+
* the given {@link BalanceRequest}.
2529+
*
2530+
* @return BalanceResponse details about the balancer run
2531+
*/
2532+
BalanceResponse balanceRSGroup(String groupName, BalanceRequest request) throws IOException;
25012533

25022534
/**
25032535
* Rename rsgroup

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AdminOverAsyncAdmin.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ public boolean balancerSwitch(boolean onOrOff, boolean synchronous) throws IOExc
374374
return get(admin.balancerSwitch(onOrOff, synchronous));
375375
}
376376

377+
378+
public BalanceResponse balance(BalanceRequest request) throws IOException {
379+
return get(admin.balance(request));
380+
}
381+
377382
@Override
378383
public boolean balance() throws IOException {
379384
return get(admin.balance());
@@ -1006,8 +1011,8 @@ public void removeRSGroup(String groupName) throws IOException {
10061011
}
10071012

10081013
@Override
1009-
public boolean balanceRSGroup(String groupName) throws IOException {
1010-
return get(admin.balanceRSGroup(groupName));
1014+
public BalanceResponse balanceRSGroup(String groupName, BalanceRequest request) throws IOException {
1015+
return get(admin.balanceRSGroup(groupName, request));
10111016
}
10121017

10131018
@Override

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,8 @@ default CompletableFuture<Boolean> balancerSwitch(boolean on) {
12871287
* {@link CompletableFuture}.
12881288
*/
12891289
default CompletableFuture<Boolean> balance() {
1290-
return balance(false);
1290+
return balance(BalanceRequest.defaultInstance())
1291+
.thenApply(BalanceResponse::isBalancerRan);
12911292
}
12921293

12931294
/**
@@ -1297,8 +1298,25 @@ default CompletableFuture<Boolean> balance() {
12971298
* @param forcible whether we should force balance even if there is region in transition.
12981299
* @return True if balancer ran, false otherwise. The return value will be wrapped by a
12991300
* {@link CompletableFuture}.
1301+
* @deprecated Since 2.5.0. Will be removed in 4.0.0.
1302+
* Use {@link #balance(BalanceRequest)} instead.
1303+
*/
1304+
default CompletableFuture<Boolean> balance(boolean forcible) {
1305+
return balance(
1306+
BalanceRequest.newBuilder()
1307+
.setIgnoreRegionsInTransition(forcible)
1308+
.build()
1309+
).thenApply(BalanceResponse::isBalancerRan);
1310+
}
1311+
1312+
/**
1313+
* Invoke the balancer with the given balance request. The BalanceRequest defines how the
1314+
* balancer will run. See {@link BalanceRequest} for more details.
1315+
*
1316+
* @param request defines how the balancer should run
1317+
* @return {@link BalanceResponse} with details about the results of the invocation.
13001318
*/
1301-
CompletableFuture<Boolean> balance(boolean forcible);
1319+
CompletableFuture<BalanceResponse> balance(BalanceRequest request);
13021320

13031321
/**
13041322
* Query the current state of the balancer.
@@ -1722,10 +1740,21 @@ default CompletableFuture<List<OnlineLogRecord>> getSlowLogResponses(
17221740
/**
17231741
* Balance regions in the given RegionServer group
17241742
* @param groupName the group name
1725-
* @return boolean Whether balance ran or not
1743+
* @return BalanceResponse details about the balancer run
1744+
* @throws IOException if a remote or network exception occurs
1745+
*/
1746+
default CompletableFuture<BalanceResponse> balanceRSGroup(String groupName) {
1747+
return balanceRSGroup(groupName, BalanceRequest.defaultInstance());
1748+
}
1749+
1750+
/**
1751+
* Balance regions in the given RegionServer group
1752+
* @param groupName the group name
1753+
* @param request options to define how the balancer should run
1754+
* @return BalanceResponse details about the balancer run
17261755
* @throws IOException if a remote or network exception occurs
17271756
*/
1728-
CompletableFuture<Boolean> balanceRSGroup(String groupName);
1757+
CompletableFuture<BalanceResponse> balanceRSGroup(String groupName, BalanceRequest request);
17291758

17301759
/**
17311760
* Rename rsgroup

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ public CompletableFuture<Boolean> balancerSwitch(boolean on, boolean drainRITs)
694694
}
695695

696696
@Override
697-
public CompletableFuture<Boolean> balance(boolean forcible) {
698-
return wrap(rawAdmin.balance(forcible));
697+
public CompletableFuture<BalanceResponse> balance(BalanceRequest request) {
698+
return wrap(rawAdmin.balance(request));
699699
}
700700

701701
@Override
@@ -883,8 +883,8 @@ public CompletableFuture<Void> removeRSGroup(String groupName) {
883883
}
884884

885885
@Override
886-
public CompletableFuture<Boolean> balanceRSGroup(String groupName) {
887-
return wrap(rawAdmin.balanceRSGroup(groupName));
886+
public CompletableFuture<BalanceResponse> balanceRSGroup(String groupName, BalanceRequest request) {
887+
return wrap(rawAdmin.balanceRSGroup(groupName, request));
888888
}
889889

890890
@Override
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.apache.hadoop.hbase.client;
20+
21+
import org.apache.yetus.audience.InterfaceAudience;
22+
23+
/**
24+
* Encapsulates options for executing a run of the Balancer.
25+
*/
26+
@InterfaceAudience.Public
27+
public final class BalanceRequest {
28+
private static final BalanceRequest DEFAULT = BalanceRequest.newBuilder().build();
29+
30+
/**
31+
* Builder for constructing a {@link BalanceRequest}
32+
*/
33+
@InterfaceAudience.Public
34+
public final static class Builder {
35+
private boolean dryRun = false;
36+
private boolean ignoreRegionsInTransition = false;
37+
38+
private Builder() {}
39+
40+
/**
41+
* Creates a BalancerRequest which runs the balancer in dryRun mode.
42+
* In this mode, the balancer will try to find a plan but WILL NOT
43+
* execute any region moves or call any coprocessors.
44+
*
45+
* You can run in dryRun mode regardless of whether the balancer switch
46+
* is enabled or disabled, but dryRun mode will not run over an existing
47+
* request or chore.
48+
*
49+
* Dry run is useful for testing out new balance configs. See the logs
50+
* on the active HMaster for the results of the dry run.
51+
*/
52+
public Builder setDryRun(boolean dryRun) {
53+
this.dryRun = dryRun;
54+
return this;
55+
}
56+
57+
/**
58+
* Creates a BalancerRequest to cause the balancer to run even if there
59+
* are regions in transition.
60+
*
61+
* WARNING: Advanced usage only, this could cause more issues than it fixes.
62+
*/
63+
public Builder setIgnoreRegionsInTransition(boolean ignoreRegionsInTransition) {
64+
this.ignoreRegionsInTransition = ignoreRegionsInTransition;
65+
return this;
66+
}
67+
68+
/**
69+
* Build the {@link BalanceRequest}
70+
*/
71+
public BalanceRequest build() {
72+
return new BalanceRequest(dryRun, ignoreRegionsInTransition);
73+
}
74+
}
75+
76+
/**
77+
* Create a builder to construct a custom {@link BalanceRequest}.
78+
*/
79+
public static Builder newBuilder() {
80+
return new Builder();
81+
}
82+
83+
/**
84+
* Get a BalanceRequest for a default run of the balancer. The default mode executes
85+
* any moves calculated and will not run if regions are already in transition.
86+
*/
87+
public static BalanceRequest defaultInstance() {
88+
return DEFAULT;
89+
}
90+
91+
private final boolean dryRun;
92+
private final boolean ignoreRegionsInTransition;
93+
94+
private BalanceRequest(boolean dryRun, boolean ignoreRegionsInTransition) {
95+
this.dryRun = dryRun;
96+
this.ignoreRegionsInTransition = ignoreRegionsInTransition;
97+
}
98+
99+
/**
100+
* Returns true if the balancer should run in dry run mode, otherwise false. In
101+
* dry run mode, moves will be calculated but not executed.
102+
*/
103+
public boolean isDryRun() {
104+
return dryRun;
105+
}
106+
107+
/**
108+
* Returns true if the balancer should execute even if regions are in transition, otherwise
109+
* false. This is an advanced usage feature, as it can cause more issues than it fixes.
110+
*/
111+
public boolean isIgnoreRegionsInTransition() {
112+
return ignoreRegionsInTransition;
113+
}
114+
}

0 commit comments

Comments
 (0)