HADOOP-16753. Refactor HAAdmin. Contributed by Xieming Li.

This commit is contained in:
Akira Ajisaka 2020-01-21 10:58:32 +09:00
parent 6a859d33aa
commit 1defe3a65a
No known key found for this signature in database
GPG Key ID: C1EDBB9CA400FD50
4 changed files with 300 additions and 203 deletions

View File

@ -39,7 +39,6 @@
import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; import org.apache.hadoop.util.ToolRunner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -53,15 +52,14 @@
public abstract class HAAdmin extends Configured implements Tool { public abstract class HAAdmin extends Configured implements Tool {
private static final String FORCEFENCE = "forcefence"; protected static final String FORCEACTIVE = "forceactive";
private static final String FORCEACTIVE = "forceactive";
/** /**
* Undocumented flag which allows an administrator to use manual failover * Undocumented flag which allows an administrator to use manual failover
* state transitions even when auto-failover is enabled. This is an unsafe * state transitions even when auto-failover is enabled. This is an unsafe
* operation, which is why it is not documented in the usage below. * operation, which is why it is not documented in the usage below.
*/ */
private static final String FORCEMANUAL = "forcemanual"; protected static final String FORCEMANUAL = "forcemanual";
private static final Logger LOG = LoggerFactory.getLogger(HAAdmin.class); private static final Logger LOG = LoggerFactory.getLogger(HAAdmin.class);
private int rpcTimeoutForChecks = -1; private int rpcTimeoutForChecks = -1;
@ -72,15 +70,6 @@ public abstract class HAAdmin extends Configured implements Tool {
new UsageInfo("[--"+FORCEACTIVE+"] <serviceId>", "Transitions the service into Active state")) new UsageInfo("[--"+FORCEACTIVE+"] <serviceId>", "Transitions the service into Active state"))
.put("-transitionToStandby", .put("-transitionToStandby",
new UsageInfo("<serviceId>", "Transitions the service into Standby state")) new UsageInfo("<serviceId>", "Transitions the service into Standby state"))
.put("-transitionToObserver",
new UsageInfo("<serviceId>",
"Transitions the service into Observer state"))
.put("-failover",
new UsageInfo("[--"+FORCEFENCE+"] [--"+FORCEACTIVE+"] <serviceId> <serviceId>",
"Failover from the first service to the second.\n" +
"Unconditionally fence services if the --"+FORCEFENCE+" option is used.\n" +
"Try to failover to the target service even if it is not ready if the " +
"--" + FORCEACTIVE + " option is used."))
.put("-getServiceState", .put("-getServiceState",
new UsageInfo("<serviceId>", "Returns the state of the service")) new UsageInfo("<serviceId>", "Returns the state of the service"))
.put("-getAllServiceState", .put("-getAllServiceState",
@ -99,6 +88,14 @@ public abstract class HAAdmin extends Configured implements Tool {
protected PrintStream out = System.out; protected PrintStream out = System.out;
private RequestSource requestSource = RequestSource.REQUEST_BY_USER; private RequestSource requestSource = RequestSource.REQUEST_BY_USER;
protected RequestSource getRequestSource() {
return requestSource;
}
protected void setRequestSource(RequestSource requestSource) {
this.requestSource = requestSource;
}
protected HAAdmin() { protected HAAdmin() {
super(); super();
} }
@ -118,34 +115,44 @@ protected String getUsageString() {
return "Usage: HAAdmin"; return "Usage: HAAdmin";
} }
protected void printUsage(PrintStream errOut) { protected void printUsage(PrintStream pStr,
errOut.println(getUsageString()); Map<String, UsageInfo> helpEntries) {
for (Map.Entry<String, UsageInfo> e : USAGE.entrySet()) { pStr.println(getUsageString());
for (Map.Entry<String, UsageInfo> e : helpEntries.entrySet()) {
String cmd = e.getKey(); String cmd = e.getKey();
UsageInfo usage = e.getValue(); UsageInfo usage = e.getValue();
if (usage.args == null) { if (usage.args == null) {
errOut.println(" [" + cmd + "]"); pStr.println(" [" + cmd + "]");
} else { } else {
errOut.println(" [" + cmd + " " + usage.args + "]"); pStr.println(" [" + cmd + " " + usage.args + "]");
} }
} }
errOut.println(); pStr.println();
ToolRunner.printGenericCommandUsage(errOut); ToolRunner.printGenericCommandUsage(pStr);
} }
private void printUsage(PrintStream errOut, String cmd) { protected void printUsage(PrintStream pStr) {
UsageInfo usage = USAGE.get(cmd); printUsage(pStr, USAGE);
}
protected void printUsage(PrintStream pStr, String cmd,
Map<String, UsageInfo> helpEntries) {
UsageInfo usage = helpEntries.get(cmd);
if (usage == null) { if (usage == null) {
throw new RuntimeException("No usage for cmd " + cmd); throw new RuntimeException("No usage for cmd " + cmd);
} }
if (usage.args == null) { if (usage.args == null) {
errOut.println(getUsageString() + " [" + cmd + "]"); pStr.println(getUsageString() + " [" + cmd + "]");
} else { } else {
errOut.println(getUsageString() + " [" + cmd + " " + usage.args + "]"); pStr.println(getUsageString() + " [" + cmd + " " + usage.args + "]");
} }
} }
protected void printUsage(PrintStream pStr, String cmd) {
printUsage(pStr, cmd, USAGE);
}
private int transitionToActive(final CommandLine cmd) private int transitionToActive(final CommandLine cmd)
throws IOException, ServiceFailedException { throws IOException, ServiceFailedException {
String[] argv = cmd.getArgs(); String[] argv = cmd.getArgs();
@ -225,27 +232,6 @@ private int transitionToStandby(final CommandLine cmd)
return 0; return 0;
} }
private int transitionToObserver(final CommandLine cmd)
throws IOException, ServiceFailedException {
String[] argv = cmd.getArgs();
if (argv.length != 1) {
errOut.println("transitionToObserver: incorrect number of arguments");
printUsage(errOut, "-transitionToObserver");
return -1;
}
HAServiceTarget target = resolveTarget(argv[0]);
if (!checkSupportObserver(target)) {
return -1;
}
if (!checkManualStateManagementOK(target)) {
return -1;
}
HAServiceProtocol proto = target.getProxy(getConf(), 0);
HAServiceProtocolHelper.transitionToObserver(proto, createReqInfo());
return 0;
}
/** /**
* Ensure that we are allowed to manually manage the HA state of the target * Ensure that we are allowed to manually manage the HA state of the target
* service. If automatic failover is configured, then the automatic * service. If automatic failover is configured, then the automatic
@ -255,7 +241,7 @@ private int transitionToObserver(final CommandLine cmd)
* @param target the target to check * @param target the target to check
* @return true if manual state management is allowed * @return true if manual state management is allowed
*/ */
private boolean checkManualStateManagementOK(HAServiceTarget target) { protected boolean checkManualStateManagementOK(HAServiceTarget target) {
if (target.isAutoFailoverEnabled()) { if (target.isAutoFailoverEnabled()) {
if (requestSource != RequestSource.REQUEST_BY_USER_FORCED) { if (requestSource != RequestSource.REQUEST_BY_USER_FORCED) {
errOut.println( errOut.println(
@ -274,84 +260,10 @@ private boolean checkManualStateManagementOK(HAServiceTarget target) {
return true; return true;
} }
/** protected StateChangeRequestInfo createReqInfo() {
* Check if the target supports the Observer state.
* @param target the target to check
* @return true if the target support Observer state, false otherwise.
*/
private boolean checkSupportObserver(HAServiceTarget target) {
if (target.supportObserver()) {
return true;
} else {
errOut.println(
"The target " + target + " doesn't support Observer state.");
return false;
}
}
private StateChangeRequestInfo createReqInfo() {
return new StateChangeRequestInfo(requestSource); return new StateChangeRequestInfo(requestSource);
} }
private int failover(CommandLine cmd)
throws IOException, ServiceFailedException {
boolean forceFence = cmd.hasOption(FORCEFENCE);
boolean forceActive = cmd.hasOption(FORCEACTIVE);
int numOpts = cmd.getOptions() == null ? 0 : cmd.getOptions().length;
final String[] args = cmd.getArgs();
if (numOpts > 3 || args.length != 2) {
errOut.println("failover: incorrect arguments");
printUsage(errOut, "-failover");
return -1;
}
HAServiceTarget fromNode = resolveTarget(args[0]);
HAServiceTarget toNode = resolveTarget(args[1]);
// Check that auto-failover is consistently configured for both nodes.
Preconditions.checkState(
fromNode.isAutoFailoverEnabled() ==
toNode.isAutoFailoverEnabled(),
"Inconsistent auto-failover configs between %s and %s!",
fromNode, toNode);
if (fromNode.isAutoFailoverEnabled()) {
if (forceFence || forceActive) {
// -forceActive doesn't make sense with auto-HA, since, if the node
// is not healthy, then its ZKFC will immediately quit the election
// again the next time a health check runs.
//
// -forceFence doesn't seem to have any real use cases with auto-HA
// so it isn't implemented.
errOut.println(FORCEFENCE + " and " + FORCEACTIVE + " flags not " +
"supported with auto-failover enabled.");
return -1;
}
try {
return gracefulFailoverThroughZKFCs(toNode);
} catch (UnsupportedOperationException e){
errOut.println("Failover command is not supported with " +
"auto-failover enabled: " + e.getLocalizedMessage());
return -1;
}
}
FailoverController fc = new FailoverController(getConf(),
requestSource);
try {
fc.failover(fromNode, toNode, forceFence, forceActive);
out.println("Failover from "+args[0]+" to "+args[1]+" successful");
} catch (FailoverFailedException ffe) {
errOut.println("Failover failed: " + ffe.getLocalizedMessage());
return -1;
}
return 0;
}
/** /**
* Initiate a graceful failover by talking to the target node's ZKFC. * Initiate a graceful failover by talking to the target node's ZKFC.
* This sends an RPC to the ZKFC, which coordinates the failover. * This sends an RPC to the ZKFC, which coordinates the failover.
@ -360,7 +272,7 @@ private int failover(CommandLine cmd)
* @return status code (0 for success) * @return status code (0 for success)
* @throws IOException if failover does not succeed * @throws IOException if failover does not succeed
*/ */
private int gracefulFailoverThroughZKFCs(HAServiceTarget toNode) protected int gracefulFailoverThroughZKFCs(HAServiceTarget toNode)
throws IOException { throws IOException {
int timeout = FailoverController.getRpcTimeoutToNewActive(getConf()); int timeout = FailoverController.getRpcTimeoutToNewActive(getConf());
@ -444,44 +356,51 @@ public int run(String[] argv) throws Exception {
} }
} }
protected int runCmd(String[] argv) throws Exception { protected boolean checkParameterValidity(String[] argv,
Map<String, UsageInfo> helpEntries){
if (argv.length < 1) { if (argv.length < 1) {
printUsage(errOut); printUsage(errOut, helpEntries);
return false;
}
String cmd = argv[0];
if (!cmd.startsWith("-")) {
errOut.println("Bad command '" + cmd +
"': expected command starting with '-'");
printUsage(errOut, helpEntries);
return false;
}
if (!helpEntries.containsKey(cmd)) {
errOut.println(cmd.substring(1) + ": Unknown command");
printUsage(errOut, helpEntries);
return false;
}
return true;
}
protected boolean checkParameterValidity(String[] argv){
return checkParameterValidity(argv, USAGE);
}
protected int runCmd(String[] argv) throws Exception {
if (!checkParameterValidity(argv, USAGE)){
return -1; return -1;
} }
String cmd = argv[0]; String cmd = argv[0];
if (!cmd.startsWith("-")) {
errOut.println("Bad command '" + cmd + "': expected command starting with '-'");
printUsage(errOut);
return -1;
}
if (!USAGE.containsKey(cmd)) {
errOut.println(cmd.substring(1) + ": Unknown command");
printUsage(errOut);
return -1;
}
Options opts = new Options(); Options opts = new Options();
// Add command-specific options // Add command-specific options
if ("-failover".equals(cmd)) {
addFailoverCliOpts(opts);
}
if("-transitionToActive".equals(cmd)) { if("-transitionToActive".equals(cmd)) {
addTransitionToActiveCliOpts(opts); addTransitionToActiveCliOpts(opts);
} }
// Mutative commands take FORCEMANUAL option // Mutative commands take FORCEMANUAL option
if ("-transitionToActive".equals(cmd) || if ("-transitionToActive".equals(cmd) ||
"-transitionToStandby".equals(cmd) || "-transitionToStandby".equals(cmd)) {
"-transitionToObserver".equals(cmd) ||
"-failover".equals(cmd)) {
opts.addOption(FORCEMANUAL, false, opts.addOption(FORCEMANUAL, false,
"force manual control even if auto-failover is enabled"); "force manual control even if auto-failover is enabled");
} }
CommandLine cmdLine = parseOpts(cmd, opts, argv); CommandLine cmdLine = parseOpts(cmd, opts, argv);
if (cmdLine == null) { if (cmdLine == null) {
// error already printed // error already printed
@ -502,10 +421,6 @@ protected int runCmd(String[] argv) throws Exception {
return transitionToActive(cmdLine); return transitionToActive(cmdLine);
} else if ("-transitionToStandby".equals(cmd)) { } else if ("-transitionToStandby".equals(cmd)) {
return transitionToStandby(cmdLine); return transitionToStandby(cmdLine);
} else if ("-transitionToObserver".equals(cmd)) {
return transitionToObserver(cmdLine);
} else if ("-failover".equals(cmd)) {
return failover(cmdLine);
} else if ("-getServiceState".equals(cmd)) { } else if ("-getServiceState".equals(cmd)) {
return getServiceState(cmdLine); return getServiceState(cmdLine);
} else if ("-getAllServiceState".equals(cmd)) { } else if ("-getAllServiceState".equals(cmd)) {
@ -544,7 +459,7 @@ protected int getAllServiceState() {
return 0; return 0;
} }
private boolean confirmForceManual() throws IOException { protected boolean confirmForceManual() throws IOException {
return ToolRunner.confirmPrompt( return ToolRunner.confirmPrompt(
"You have specified the --" + FORCEMANUAL + " flag. This flag is " + "You have specified the --" + FORCEMANUAL + " flag. This flag is " +
"dangerous, as it can induce a split-brain scenario that WILL " + "dangerous, as it can induce a split-brain scenario that WILL " +
@ -559,16 +474,7 @@ private boolean confirmForceManual() throws IOException {
"Are you sure you want to continue?"); "Are you sure you want to continue?");
} }
/**
* Add CLI options which are specific to the failover command and no
* others.
*/
private void addFailoverCliOpts(Options failoverOpts) {
failoverOpts.addOption(FORCEFENCE, false, "force fencing");
failoverOpts.addOption(FORCEACTIVE, false, "force failover");
// Don't add FORCEMANUAL, since that's added separately for all commands
// that change state.
}
/** /**
* Add CLI options which are specific to the transitionToActive command and * Add CLI options which are specific to the transitionToActive command and
@ -578,7 +484,8 @@ private void addTransitionToActiveCliOpts(Options transitionToActiveCliOpts) {
transitionToActiveCliOpts.addOption(FORCEACTIVE, false, "force active"); transitionToActiveCliOpts.addOption(FORCEACTIVE, false, "force active");
} }
private CommandLine parseOpts(String cmdName, Options opts, String[] argv) { protected CommandLine parseOpts(String cmdName, Options opts, String[] argv,
Map<String, UsageInfo> helpEntries) {
try { try {
// Strip off the first arg, since that's just the command name // Strip off the first arg, since that's just the command name
argv = Arrays.copyOfRange(argv, 1, argv.length); argv = Arrays.copyOfRange(argv, 1, argv.length);
@ -586,27 +493,34 @@ private CommandLine parseOpts(String cmdName, Options opts, String[] argv) {
} catch (ParseException pe) { } catch (ParseException pe) {
errOut.println(cmdName.substring(1) + errOut.println(cmdName.substring(1) +
": incorrect arguments"); ": incorrect arguments");
printUsage(errOut, cmdName); printUsage(errOut, cmdName, helpEntries);
return null; return null;
} }
} }
private int help(String[] argv) { protected CommandLine parseOpts(String cmdName, Options opts, String[] argv) {
return parseOpts(cmdName, opts, argv, USAGE);
}
protected int help(String[] argv) {
return help(argv, USAGE);
}
protected int help(String[] argv, Map<String, UsageInfo> helpEntries) {
if (argv.length == 1) { // only -help if (argv.length == 1) { // only -help
printUsage(out); printUsage(out, helpEntries);
return 0; return 0;
} else if (argv.length != 2) { } else if (argv.length != 2) {
printUsage(errOut, "-help"); printUsage(errOut, "-help", helpEntries);
return -1; return -1;
} }
String cmd = argv[1]; String cmd = argv[1];
if (!cmd.startsWith("-")) { if (!cmd.startsWith("-")) {
cmd = "-" + cmd; cmd = "-" + cmd;
} }
UsageInfo usageInfo = USAGE.get(cmd); UsageInfo usageInfo = helpEntries.get(cmd);
if (usageInfo == null) { if (usageInfo == null) {
errOut.println(cmd + ": Unknown command"); errOut.println(cmd + ": Unknown command");
printUsage(errOut); printUsage(errOut, helpEntries);
return -1; return -1;
} }

View File

@ -83,11 +83,6 @@ public void testAdminUsage() throws Exception {
assertOutputContains("transitionToActive: incorrect number of arguments"); assertOutputContains("transitionToActive: incorrect number of arguments");
assertEquals(-1, runTool("-transitionToActive", "x", "y")); assertEquals(-1, runTool("-transitionToActive", "x", "y"));
assertOutputContains("transitionToActive: incorrect number of arguments"); assertOutputContains("transitionToActive: incorrect number of arguments");
assertEquals(-1, runTool("-failover"));
assertOutputContains("failover: incorrect arguments");
assertOutputContains("failover: incorrect arguments");
assertEquals(-1, runTool("-failover", "foo:1234"));
assertOutputContains("failover: incorrect arguments");
} }
@Test @Test

View File

@ -17,15 +17,28 @@
*/ */
package org.apache.hadoop.hdfs.tools; package org.apache.hadoop.hdfs.tools;
import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Map;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.hadoop.ha.FailoverController;
import org.apache.hadoop.ha.FailoverFailedException;
import org.apache.hadoop.ha.HAServiceProtocol;
import org.apache.hadoop.ha.HAServiceProtocolHelper;
import org.apache.hadoop.ha.ServiceFailedException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.CommonConfigurationKeys; import org.apache.hadoop.fs.CommonConfigurationKeys;
import org.apache.hadoop.ha.HAAdmin; import org.apache.hadoop.ha.HAAdmin;
import org.apache.hadoop.ha.HAServiceProtocol.RequestSource;
import org.apache.hadoop.ha.HAServiceTarget; import org.apache.hadoop.ha.HAServiceTarget;
import org.apache.hadoop.hdfs.DFSConfigKeys; import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.DFSUtil; import org.apache.hadoop.hdfs.DFSUtil;
@ -38,9 +51,29 @@
*/ */
public class DFSHAAdmin extends HAAdmin { public class DFSHAAdmin extends HAAdmin {
private static final String FORCEFENCE = "forcefence";
private static final Logger LOG = LoggerFactory.getLogger(DFSHAAdmin.class); private static final Logger LOG = LoggerFactory.getLogger(DFSHAAdmin.class);
private String nameserviceId; private String nameserviceId;
private final static Map<String, UsageInfo> USAGE_DFS_ONLY =
ImmutableMap.<String, UsageInfo> builder()
.put("-transitionToObserver", new UsageInfo("<serviceId>",
"Transitions the service into Observer state"))
.put("-failover", new UsageInfo(
"[--"+FORCEFENCE+"] [--"+FORCEACTIVE+"] "
+ "<serviceId> <serviceId>",
"Failover from the first service to the second.\n"
+ "Unconditionally fence services if the --" + FORCEFENCE
+ " option is used.\n"
+ "Try to failover to the target service "
+ "even if it is not ready if the "
+ "--" + FORCEACTIVE + " option is used.")).build();
private final static Map<String, UsageInfo> USAGE_DFS_MERGED =
ImmutableSortedMap.<String, UsageInfo> naturalOrder()
.putAll(USAGE)
.putAll(USAGE_DFS_ONLY)
.build();
protected void setErrOut(PrintStream errOut) { protected void setErrOut(PrintStream errOut) {
this.errOut = errOut; this.errOut = errOut;
@ -93,42 +126,203 @@ protected String getUsageString() {
return "Usage: haadmin [-ns <nameserviceId>]"; return "Usage: haadmin [-ns <nameserviceId>]";
} }
/**
* Add CLI options which are specific to the failover command and no
* others.
*/
private void addFailoverCliOpts(Options failoverOpts) {
failoverOpts.addOption(FORCEFENCE, false, "force fencing");
failoverOpts.addOption(FORCEACTIVE, false, "force failover");
// Don't add FORCEMANUAL, since that's added separately for all commands
// that change state.
}
@Override
protected boolean checkParameterValidity(String[] argv){
return checkParameterValidity(argv, USAGE_DFS_MERGED);
}
@Override @Override
protected int runCmd(String[] argv) throws Exception { protected int runCmd(String[] argv) throws Exception {
if (argv.length < 1) {
printUsage(errOut); if(argv.length < 1){
printUsage(errOut, USAGE_DFS_MERGED);
return -1; return -1;
} }
int i = 0; int i = 0;
String cmd = argv[i++]; String cmd = argv[i++];
//Process "-ns" Option
if ("-ns".equals(cmd)) { if ("-ns".equals(cmd)) {
if (i == argv.length) { if (i == argv.length) {
errOut.println("Missing nameservice ID"); errOut.println("Missing nameservice ID");
printUsage(errOut); printUsage(errOut, USAGE_DFS_MERGED);
return -1; return -1;
} }
nameserviceId = argv[i++]; nameserviceId = argv[i++];
if (i >= argv.length) { if (i >= argv.length) {
errOut.println("Missing command"); errOut.println("Missing command");
printUsage(errOut); printUsage(errOut, USAGE_DFS_MERGED);
return -1; return -1;
} }
argv = Arrays.copyOfRange(argv, i, argv.length); argv = Arrays.copyOfRange(argv, i, argv.length);
cmd = argv[0];
} }
if (!checkParameterValidity(argv)){
return -1;
}
/*
"-help" command has to to be handled here because it should
be supported both by HAAdmin and DFSHAAdmin but it is contained in
USAGE_DFS_ONLY
*/
if ("-help".equals(cmd)){
return help(argv, USAGE_DFS_MERGED);
}
if (!USAGE_DFS_ONLY.containsKey(cmd)) {
return super.runCmd(argv); return super.runCmd(argv);
} }
Options opts = new Options();
// Add command-specific options
if ("-failover".equals(cmd)) {
addFailoverCliOpts(opts);
}
// Mutative commands take FORCEMANUAL option
if ("-transitionToObserver".equals(cmd) ||
"-failover".equals(cmd)) {
opts.addOption(FORCEMANUAL, false,
"force manual control even if auto-failover is enabled");
}
CommandLine cmdLine = parseOpts(cmd, opts, argv, USAGE_DFS_MERGED);
if (cmdLine == null) {
return -1;
}
if (cmdLine.hasOption(FORCEMANUAL)) {
if (!confirmForceManual()) {
LOG.error("Aborted");
return -1;
}
// Instruct the NNs to honor this request even if they're
// configured for manual failover.
setRequestSource(RequestSource.REQUEST_BY_USER_FORCED);
}
if ("-transitionToObserver".equals(cmd)) {
return transitionToObserver(cmdLine);
} else if ("-failover".equals(cmd)) {
return failover(cmdLine);
} else {
// This line should not be reached
throw new AssertionError("Should not get here, command: " + cmd);
}
}
/** /**
* returns the list of all namenode ids for the given configuration * returns the list of all namenode ids for the given configuration.
*/ */
@Override @Override
protected Collection<String> getTargetIds(String namenodeToActivate) { protected Collection<String> getTargetIds(String namenodeToActivate) {
return DFSUtilClient.getNameNodeIds(getConf(), return DFSUtilClient.getNameNodeIds(
(nameserviceId != null) ? nameserviceId : DFSUtil.getNamenodeNameServiceId( getConf(), (nameserviceId != null)?
getConf())); nameserviceId : DFSUtil.getNamenodeNameServiceId(getConf()));
}
/**
* Check if the target supports the Observer state.
* @param target the target to check
* @return true if the target support Observer state, false otherwise.
*/
private boolean checkSupportObserver(HAServiceTarget target) {
if (target.supportObserver()) {
return true;
} else {
errOut.println(
"The target " + target + " doesn't support Observer state.");
return false;
}
}
private int transitionToObserver(final CommandLine cmd)
throws IOException, ServiceFailedException {
String[] argv = cmd.getArgs();
if (argv.length != 1) {
errOut.println("transitionToObserver: incorrect number of arguments");
printUsage(errOut, "-transitionToObserver", USAGE_DFS_MERGED);
return -1;
}
HAServiceTarget target = resolveTarget(argv[0]);
if (!checkSupportObserver(target)) {
return -1;
}
if (!checkManualStateManagementOK(target)) {
return -1;
}
HAServiceProtocol proto = target.getProxy(getConf(), 0);
HAServiceProtocolHelper.transitionToObserver(proto, createReqInfo());
return 0;
}
private int failover(CommandLine cmd)
throws IOException, ServiceFailedException {
boolean forceFence = cmd.hasOption(FORCEFENCE);
boolean forceActive = cmd.hasOption(FORCEACTIVE);
int numOpts = cmd.getOptions() == null ? 0 : cmd.getOptions().length;
final String[] args = cmd.getArgs();
if (numOpts > 3 || args.length != 2) {
errOut.println("failover: incorrect arguments");
printUsage(errOut, "-failover", USAGE_DFS_MERGED);
return -1;
}
HAServiceTarget fromNode = resolveTarget(args[0]);
HAServiceTarget toNode = resolveTarget(args[1]);
// Check that auto-failover is consistently configured for both nodes.
Preconditions.checkState(
fromNode.isAutoFailoverEnabled() ==
toNode.isAutoFailoverEnabled(),
"Inconsistent auto-failover configs between %s and %s!",
fromNode, toNode);
if (fromNode.isAutoFailoverEnabled()) {
if (forceFence || forceActive) {
// -forceActive doesn't make sense with auto-HA, since, if the node
// is not healthy, then its ZKFC will immediately quit the election
// again the next time a health check runs.
//
// -forceFence doesn't seem to have any real use cases with auto-HA
// so it isn't implemented.
errOut.println(FORCEFENCE + " and " + FORCEACTIVE + " flags not " +
"supported with auto-failover enabled.");
return -1;
}
try {
return gracefulFailoverThroughZKFCs(toNode);
} catch (UnsupportedOperationException e){
errOut.println("Failover command is not supported with " +
"auto-failover enabled: " + e.getLocalizedMessage());
return -1;
}
}
FailoverController fc =
new FailoverController(getConf(), getRequestSource());
try {
fc.failover(fromNode, toNode, forceFence, forceActive);
out.println("Failover from "+args[0]+" to "+args[1]+" successful");
} catch (FailoverFailedException ffe) {
errOut.println("Failover failed: " + ffe.getLocalizedMessage());
return -1;
}
return 0;
} }
public static void main(String[] argv) throws Exception { public static void main(String[] argv) throws Exception {

View File

@ -186,9 +186,7 @@ protected void setOut(PrintStream out) {
private static void appendHAUsage(final StringBuilder usageBuilder) { private static void appendHAUsage(final StringBuilder usageBuilder) {
for (Map.Entry<String,UsageInfo> cmdEntry : USAGE.entrySet()) { for (Map.Entry<String,UsageInfo> cmdEntry : USAGE.entrySet()) {
if (cmdEntry.getKey().equals("-help") if (cmdEntry.getKey().equals("-help")) {
|| cmdEntry.getKey().equals("-failover")
|| cmdEntry.getKey().equals("-transitionToObserver")) {
continue; continue;
} }
UsageInfo usageInfo = cmdEntry.getValue(); UsageInfo usageInfo = cmdEntry.getValue();
@ -251,8 +249,7 @@ private static void buildUsageMsg(StringBuilder builder,
if (isHAEnabled) { if (isHAEnabled) {
for (Map.Entry<String,UsageInfo> cmdEntry : USAGE.entrySet()) { for (Map.Entry<String,UsageInfo> cmdEntry : USAGE.entrySet()) {
String cmdKey = cmdEntry.getKey(); String cmdKey = cmdEntry.getKey();
if (!cmdKey.equals("-help") && !cmdKey.equals("-failover") if (!cmdKey.equals("-help")) {
&& !cmdKey.equals("-transitionToObserver")) {
UsageInfo usageInfo = cmdEntry.getValue(); UsageInfo usageInfo = cmdEntry.getValue();
if (usageInfo.args == null) { if (usageInfo.args == null) {
builder.append(" " + cmdKey + "\n"); builder.append(" " + cmdKey + "\n");
@ -304,8 +301,7 @@ private static void printHelp(String cmd, boolean isHAEnabled) {
} }
if (isHAEnabled) { if (isHAEnabled) {
for (String cmdKey : USAGE.keySet()) { for (String cmdKey : USAGE.keySet()) {
if (!cmdKey.equals("-help") && !cmdKey.equals("-failover") if (!cmdKey.equals("-help")) {
&& !cmdKey.equals("-transitionToObserver")) {
buildHelpMsg(cmdKey, helpBuilder); buildHelpMsg(cmdKey, helpBuilder);
helpBuilder.append("\n"); helpBuilder.append("\n");
} }
@ -324,8 +320,7 @@ private static void printHelp(String cmd, boolean isHAEnabled) {
*/ */
private static void printUsage(String cmd, boolean isHAEnabled) { private static void printUsage(String cmd, boolean isHAEnabled) {
StringBuilder usageBuilder = new StringBuilder(); StringBuilder usageBuilder = new StringBuilder();
if (ADMIN_USAGE.containsKey(cmd) || USAGE.containsKey(cmd) if (ADMIN_USAGE.containsKey(cmd) || USAGE.containsKey(cmd)) {
&& (!cmd.equals("-failover") && !cmd.equals("-transitionToObserver"))) {
buildIndividualUsageMsg(cmd, usageBuilder); buildIndividualUsageMsg(cmd, usageBuilder);
} else { } else {
buildUsageMsg(usageBuilder, isHAEnabled); buildUsageMsg(usageBuilder, isHAEnabled);
@ -732,8 +727,7 @@ public int run(String[] args) throws Exception {
return exitCode; return exitCode;
} }
if (USAGE.containsKey(cmd) && !cmd.equals("-failover") if (USAGE.containsKey(cmd)) {
&& !cmd.equals("-transitionToObserver")) {
if (isHAEnabled) { if (isHAEnabled) {
return super.run(args); return super.run(args);
} }