首先看balance_switch命令对应的rb文件源码:module Shellmodule Commands
class BalanceSwitch < Command def help
<<-EOF
Enable/Disable balancer. Returns previous balancer state.Examples:
hbase> balance_switch truehbase> balance_switch falseEOF
end
def command(enableDisable)
prev_state = admin.balance_switch(enableDisable) ? 'true' : 'false'
formatter.row(["Previous balancer state : #{prev_state}"])
prev_state
endend
endend该命令输出的是之前balancer的状态,其次再看balance_switch的处理源码:public SetBalancerRunningResponse setBalancerRunning(RpcController c,
SetBalancerRunningRequest req) throws ServiceException {try { master.checkInitialized(); boolean prevValue = (req.getSynchronous())?
synchronousBalanceSwitch(req.getOn()) : master.balanceSwitch(req.getOn());
return SetBalancerRunningResponse.newBuilder().setPrevBalanceValue(prevValue).build();} catch (IOException ioe) { throw new ServiceException(ioe);}
}
public boolean balanceSwitch(final boolean b) throws IOException {
return getMasterRpcServices().switchBalancer(b, BalanceSwitchMode.ASYNC);
}
/**
Assigns balancer switch according to BalanceSwitchMode
@param b new balancer switch
@param mode BalanceSwitchMode
@return old balancer switch
*/
boolean switchBalancer(final boolean b, BalanceSwitchMode mode) throws IOException {
boolean oldValue = master.loadBalancerTracker.isBalancerOn();boolean newValue = b;try { if (master.cpHost != null) {
master.cpHost.preBalanceSwitch(newValue);
} try {
if (mode == BalanceSwitchMode.SYNC) {
synchronized (master.getLoadBalancer()) {
master.loadBalancerTracker.setBalancerOn(newValue);
}
} else {
master.loadBalancerTracker.setBalancerOn(newValue);
}
} catch (KeeperException ke) {
throw new IOException(ke);
} LOG.info(master.getClientIdAuditPrefix() " set balanceSwitch=" newValue); if (master.cpHost != null) {
master.cpHost.postBalanceSwitch(oldValue, newValue);
}} catch (IOException ioe) { LOG.warn("Error flipping balance switch", ioe);}return oldValue;
}从switchBalancer方法也能看到返回的是之前balancer的状态值。至于status参数设置的balanceSwitch为false是因为admin.rb文件在处理参数的时候如果传status则java.lang.Boolean.valueOf(enableDisable)为false,并且shell.rb文件指定了哪些参数是符合要求的,status是其中之一,而其他的譬如state这种参数会报异常。----------------------------------------------------------------------------------------------Enable/disable balancerReturns previous balancer switch setting.
def balance_switch(enableDisable)
@admin.setBalancerRunning(
java.lang.Boolean.valueOf(enableDisable), java.lang.Boolean.valueOf(false)
)
endLoad all commands
Shell.load_command_group('general',full_name: 'GENERAL HBASE SHELL COMMANDS',commands: %w[
statusversiontable_helpwhoamiprocesslist
])