Skip to content

Commit 787c350

Browse files
committed
Use a BalancerRequest for handling all of the balance args
1 parent 2df1125 commit 787c350

File tree

24 files changed

+265
-205
lines changed

24 files changed

+265
-205
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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;
20+
21+
import org.apache.yetus.audience.InterfaceAudience;
22+
import org.apache.yetus.audience.InterfaceStability;
23+
24+
/**
25+
* Encapsulates options for executing an unscheduled run of the Balancer.
26+
*/
27+
@InterfaceAudience.Public
28+
@InterfaceStability.Evolving
29+
public class BalanceRequest {
30+
31+
/**
32+
* Dictates how the balancer should be run based on the request
33+
*/
34+
@InterfaceAudience.Private
35+
public enum RunMode {
36+
/**
37+
* Used internally for when the scheduled chore runs the balancer
38+
*/
39+
CHORE,
40+
/**
41+
* Used when a user requests the balancer to run
42+
*/
43+
REQUEST,
44+
45+
/**
46+
* Used when a user requests the balancer to run, but wants to force it to
47+
* run even if it normally wouldn't
48+
*/
49+
FORCED_REQUEST,
50+
51+
/**
52+
* Used when the user wants to run the balancer in dry run mode, where the balancer
53+
* attempts to find a balance plan but does not actually execute it.
54+
*/
55+
DRY_RUN;
56+
}
57+
58+
private final RunMode runMode;
59+
60+
private BalanceRequest(RunMode runMode) {
61+
this.runMode = runMode;
62+
}
63+
64+
/**
65+
* Creates a BalancerRequest which runs the balancer in execute mode.
66+
* In this mode, the balancer will execute all region moves if an improved
67+
* plan is found.
68+
*
69+
* The balancer will not run if the balance switch is disabled or there are
70+
* currently regions in transition. You can force the balancer to run
71+
* despite regions being in transition by using {@link #forExecutionIgnoringRegionsInTransition()}
72+
*/
73+
public static BalanceRequest forExecution() {
74+
return new BalanceRequest(RunMode.REQUEST);
75+
}
76+
77+
/**
78+
* Creates a BalancerRequest which runs the balancer in execute mode.
79+
* In this mode, the balancer will execute all region moves if an improved
80+
* plan is found.
81+
*
82+
* Forces balancer to run even if regions are currently in transition. This
83+
* should be used with caution.
84+
*/
85+
public static BalanceRequest forExecutionIgnoringRegionsInTransition() {
86+
return new BalanceRequest(RunMode.FORCED_REQUEST);
87+
}
88+
89+
/**
90+
* Creates a BalancerRequest which runs the balancer in dryRun mode.
91+
* In this mode, the balancer will try to find a plan but WILL NOT
92+
* execute any region moves or call any coprocessors.
93+
*
94+
* You can run in dryRun mode regardless of whether the balancer switch
95+
* is enabled or disabled, but dryRun mode will not run over an existing
96+
* request or chore.
97+
*
98+
* Dry run is useful for testing out new balance configs. See the logs
99+
* on the active HMaster for the results of the dry run.
100+
*/
101+
public static BalanceRequest forDryRun() {
102+
return new BalanceRequest(RunMode.DRY_RUN);
103+
}
104+
105+
106+
/**
107+
* For internal use in converting across RPC layer. Use one of {@link #forExecution()},
108+
* {@link #forExecutionIgnoringRegionsInTransition()}, or {@link #forDryRun()}.
109+
*/
110+
@InterfaceAudience.Private
111+
public static BalanceRequest forRunMode(RunMode runMode) {
112+
return new BalanceRequest(runMode);
113+
}
114+
115+
public RunMode getRunMode() {
116+
return runMode;
117+
}
118+
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.stream.Collectors;
3434
import org.apache.hadoop.conf.Configuration;
3535
import org.apache.hadoop.hbase.Abortable;
36+
import org.apache.hadoop.hbase.BalanceRequest;
3637
import org.apache.hadoop.hbase.CacheEvictionStats;
3738
import org.apache.hadoop.hbase.ClusterMetrics;
3839
import org.apache.hadoop.hbase.ClusterMetrics.Option;
@@ -1251,16 +1252,19 @@ default boolean balancer() throws IOException {
12511252
* @return <code>true</code> if balancer ran, <code>false</code> otherwise.
12521253
* @throws IOException if a remote or network exception occurs
12531254
*/
1254-
boolean balance() throws IOException;
1255+
default boolean balance() throws IOException {
1256+
return balance(BalanceRequest.forExecution());
1257+
}
12551258

12561259
/**
1257-
* Invoke the balancer in dry run mode. Will show plan but not actually move any regions.
1258-
* Can NOT run for various reasons. Check logs.
1260+
* Invoke the balancer with the given balance request. The BalanceRequest defines how the
1261+
* balancer will run. See {@link BalanceRequest} for more details.
12591262
*
1260-
* @return <code>true</code> if dry run ran, <code>false</code> otherwise.
1263+
* @param request defines how the balancer should run
1264+
* @return <code>true</code> if balancer ran, <code>false</code> otherwise.
12611265
* @throws IOException if a remote or network exception occurs
12621266
*/
1263-
boolean dryRunBalance() throws IOException;
1267+
boolean balance(BalanceRequest request) throws IOException;
12641268

12651269
/**
12661270
* Invoke the balancer. Will run the balancer and if regions to move, it will
@@ -1271,7 +1275,7 @@ default boolean balancer() throws IOException {
12711275
* @return <code>true</code> if balancer ran, <code>false</code> otherwise.
12721276
* @throws IOException if a remote or network exception occurs
12731277
* @deprecated Since 2.0.0. Will be removed in 3.0.0.
1274-
* Use {@link #balance(boolean)} instead.
1278+
* Use {@link #balance(BalanceRequest)} instead.
12751279
*/
12761280
@Deprecated
12771281
default boolean balancer(boolean force) throws IOException {
@@ -1286,8 +1290,13 @@ default boolean balancer(boolean force) throws IOException {
12861290
* @param force whether we should force balance even if there is region in transition
12871291
* @return <code>true</code> if balancer ran, <code>false</code> otherwise.
12881292
* @throws IOException if a remote or network exception occurs
1293+
* @deprecated Since 2.5.0. Will be removed in 4.0.0.
1294+
* Use {@link #balance(BalanceRequest)} instead.
12891295
*/
1290-
boolean balance(boolean force) throws IOException;
1296+
@Deprecated
1297+
default boolean balance(boolean force) throws IOException {
1298+
return balance(force ? BalanceRequest.forExecutionIgnoringRegionsInTransition() : BalanceRequest.forExecution());
1299+
}
12911300

12921301
/**
12931302
* Query the current state of the balancer.

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.function.Function;
3434
import java.util.regex.Pattern;
3535
import java.util.stream.Collectors;
36+
import org.apache.hadoop.hbase.BalanceRequest;
3637
import org.apache.hadoop.hbase.CacheEvictionStats;
3738
import org.apache.hadoop.hbase.ClusterMetrics;
3839
import org.apache.hadoop.hbase.ClusterMetrics.Option;
@@ -1258,7 +1259,7 @@ default CompletableFuture<Boolean> balancerSwitch(boolean on) {
12581259
* {@link CompletableFuture}.
12591260
*/
12601261
default CompletableFuture<Boolean> balance() {
1261-
return balance(false);
1262+
return balance(BalanceRequest.forExecution());
12621263
}
12631264

12641265
/**
@@ -1268,16 +1269,14 @@ default CompletableFuture<Boolean> balance() {
12681269
* @param forcible whether we should force balance even if there is region in transition.
12691270
* @return True if balancer ran, false otherwise. The return value will be wrapped by a
12701271
* {@link CompletableFuture}.
1272+
* @deprecated Since 2.5.0. Will be removed in 4.0.0.
1273+
* Use {@link #balance(BalanceRequest)} instead.
12711274
*/
1272-
CompletableFuture<Boolean> balance(boolean forcible);
1275+
default CompletableFuture<Boolean> balance(boolean forcible) {
1276+
return balance(forcible ? BalanceRequest.forExecutionIgnoringRegionsInTransition() : BalanceRequest.forExecution());
1277+
}
12731278

1274-
/**
1275-
* Invoke the balancer in dry run mode. Will show plan but not actually move any regions.
1276-
* Can NOT run for various reasons. Check logs.
1277-
*
1278-
* @return <code>true</code> if dry run ran, <code>false</code> otherwise.
1279-
*/
1280-
CompletableFuture<Boolean> dryRunBalance();
1279+
CompletableFuture<Boolean> balance(BalanceRequest request);
12811280

12821281
/**
12831282
* Query the current state of the balancer.

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.concurrent.ExecutorService;
2929
import java.util.function.Function;
3030
import java.util.regex.Pattern;
31+
import org.apache.hadoop.hbase.BalanceRequest;
3132
import org.apache.hadoop.hbase.CacheEvictionStats;
3233
import org.apache.hadoop.hbase.ClusterMetrics;
3334
import org.apache.hadoop.hbase.ClusterMetrics.Option;
@@ -684,13 +685,8 @@ public CompletableFuture<Boolean> balancerSwitch(boolean on, boolean drainRITs)
684685
}
685686

686687
@Override
687-
public CompletableFuture<Boolean> balance(boolean forcible) {
688-
return wrap(rawAdmin.balance(forcible));
689-
}
690-
691-
@Override
692-
public CompletableFuture<Boolean> dryRunBalance() {
693-
return wrap(rawAdmin.dryRunBalance());
688+
public CompletableFuture<Boolean> balance(BalanceRequest request) {
689+
return wrap(rawAdmin.balance(request));
694690
}
695691

696692
@Override

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,12 +1488,6 @@ public MasterProtos.BalanceResponse balance(RpcController controller,
14881488
return stub.balance(controller, request);
14891489
}
14901490

1491-
@Override
1492-
public MasterProtos.BalanceResponse dryRunBalance(RpcController controller,
1493-
MasterProtos.BalanceRequest request) throws ServiceException {
1494-
return stub.dryRunBalance(controller, request);
1495-
}
1496-
14971491
@Override
14981492
public MasterProtos.SetBalancerRunningResponse setBalancerRunning(
14991493
RpcController controller, MasterProtos.SetBalancerRunningRequest request)

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.stream.Stream;
4848
import org.apache.hadoop.conf.Configuration;
4949
import org.apache.hadoop.hbase.Abortable;
50+
import org.apache.hadoop.hbase.BalanceRequest;
5051
import org.apache.hadoop.hbase.CacheEvictionStats;
5152
import org.apache.hadoop.hbase.CacheEvictionStatsBuilder;
5253
import org.apache.hadoop.hbase.ClusterMetrics;
@@ -1476,35 +1477,11 @@ protected Boolean rpcCall() throws Exception {
14761477
});
14771478
}
14781479

1479-
@Override
1480-
public boolean balance() throws IOException {
1481-
return executeCallable(new MasterCallable<Boolean>(getConnection(), getRpcControllerFactory()) {
1482-
@Override
1483-
protected Boolean rpcCall() throws Exception {
1484-
return master.balance(getRpcController(),
1485-
RequestConverter.buildBalanceRequest(false)).getBalancerRan();
1486-
}
1487-
});
1488-
}
1489-
1490-
@Override
1491-
public boolean balance(final boolean force) throws IOException {
1480+
@Override public boolean balance(BalanceRequest request) throws IOException {
14921481
return executeCallable(new MasterCallable<Boolean>(getConnection(), getRpcControllerFactory()) {
1493-
@Override
1494-
protected Boolean rpcCall() throws Exception {
1495-
return master.balance(getRpcController(),
1496-
RequestConverter.buildBalanceRequest(force)).getBalancerRan();
1497-
}
1498-
});
1499-
}
1500-
1501-
@Override
1502-
public boolean dryRunBalance() throws IOException {
1503-
return executeCallable(new MasterCallable<Boolean>(getConnection(), getRpcControllerFactory()) {
1504-
@Override
1505-
protected Boolean rpcCall() throws Exception {
1506-
return master.dryRunBalance(getRpcController(),
1507-
RequestConverter.buildBalanceRequest(false)).getBalancerRan();
1482+
@Override protected Boolean rpcCall() throws Exception {
1483+
return master.balance(getRpcController(), RequestConverter.toBalanceRequest(request))
1484+
.getBalancerRan();
15081485
}
15091486
});
15101487
}

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
import org.apache.hadoop.conf.Configuration;
5151
import org.apache.hadoop.hbase.AsyncMetaTableAccessor;
52+
import org.apache.hadoop.hbase.BalanceRequest;
5253
import org.apache.hadoop.hbase.CacheEvictionStats;
5354
import org.apache.hadoop.hbase.CacheEvictionStatsAggregator;
5455
import org.apache.hadoop.hbase.ClusterMetrics;
@@ -126,14 +127,13 @@
126127
import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.ProcedureDescription;
127128
import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType;
128129
import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.TableSchema;
130+
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos;
129131
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.AbortProcedureRequest;
130132
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.AbortProcedureResponse;
131133
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.AddColumnRequest;
132134
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.AddColumnResponse;
133135
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.AssignRegionRequest;
134136
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.AssignRegionResponse;
135-
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.BalanceRequest;
136-
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.BalanceResponse;
137137
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ClearDeadServersRequest;
138138
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ClearDeadServersResponse;
139139
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.CreateNamespaceRequest;
@@ -3210,24 +3210,15 @@ public CompletableFuture<Boolean> balancerSwitch(boolean on, boolean drainRITs)
32103210
}
32113211

32123212
@Override
3213-
public CompletableFuture<Boolean> balance(boolean forcible) {
3213+
public CompletableFuture<Boolean> balance(BalanceRequest request) {
32143214
return this
32153215
.<Boolean> newMasterCaller()
32163216
.action(
3217-
(controller, stub) -> this.<BalanceRequest, BalanceResponse, Boolean> call(controller,
3218-
stub, RequestConverter.buildBalanceRequest(forcible),
3217+
(controller, stub) -> this.<MasterProtos.BalanceRequest, MasterProtos.BalanceResponse, Boolean> call(controller,
3218+
stub, RequestConverter.toBalanceRequest(request),
32193219
(s, c, req, done) -> s.balance(c, req, done), (resp) -> resp.getBalancerRan())).call();
32203220
}
32213221

3222-
@Override
3223-
public CompletableFuture<Boolean> dryRunBalance() {
3224-
return this
3225-
.<Boolean> newMasterCaller()
3226-
.action(
3227-
(controller, stub) -> this.<BalanceRequest, BalanceResponse, Boolean> call(controller,
3228-
stub, RequestConverter.buildBalanceRequest(false),
3229-
(s, c, req, done) -> s.dryRunBalance(c, req, done), (resp) -> resp.getBalancerRan())).call();
3230-
}
32313222

32323223
@Override
32333224
public CompletableFuture<Boolean> isBalancerEnabled() {

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,6 @@ public BalanceResponse balance(RpcController controller, BalanceRequest request)
599599
return stub.balance(controller, request);
600600
}
601601

602-
@Override
603-
public BalanceResponse dryRunBalance(RpcController controller, BalanceRequest request)
604-
throws ServiceException {
605-
return stub.dryRunBalance(controller, request);
606-
}
607-
608602
@Override
609603
public AssignRegionResponse assignRegion(RpcController controller, AssignRegionRequest request)
610604
throws ServiceException {

0 commit comments

Comments
 (0)