diff --git a/hadoop-common-project/hadoop-common/CHANGES.HDFS-1623.txt b/hadoop-common-project/hadoop-common/CHANGES.HDFS-1623.txt index 56e1d8f823d..0547c3b3b70 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.HDFS-1623.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.HDFS-1623.txt @@ -18,3 +18,6 @@ HADOOP-7921. StandbyException should extend IOException (todd) HADOOP-7928. HA: Client failover policy is incorrectly trying to fail over all IOExceptions (atm) + +HADOOP-7925. Add interface and update CLI to query current state to + HAServiceProtocol (eli via todd) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/HAAdmin.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/HAAdmin.java index b880311da41..fff82e83b40 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/HAAdmin.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/HAAdmin.java @@ -24,6 +24,7 @@ import java.util.Map; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.conf.Configured; +import org.apache.hadoop.ha.HAServiceProtocol.HAServiceState; import org.apache.hadoop.ipc.RPC; import org.apache.hadoop.net.NetUtils; import org.apache.hadoop.util.Tool; @@ -44,7 +45,9 @@ public class HAAdmin extends Configured implements Tool { .put("-transitionToActive", new UsageInfo("", "Transitions the daemon into Active state")) .put("-transitionToStandby", - new UsageInfo("", "Transitions the daemon into Passive state")) + new UsageInfo("", "Transitions the daemon into Standby state")) + .put("-getServiceState", + new UsageInfo("", "Returns the state of the daemon")) .put("-checkHealth", new UsageInfo("", "Requests that the daemon perform a health check.\n" + @@ -123,6 +126,19 @@ public class HAAdmin extends Configured implements Tool { return 0; } + private int getServiceState(final String[] argv) + throws IOException, ServiceFailedException { + if (argv.length != 2) { + errOut.println("getServiceState: incorrect number of arguments"); + printUsage(errOut, "-getServiceState"); + return -1; + } + + HAServiceProtocol proto = getProtocol(argv[1]); + out.println(proto.getServiceState()); + return 0; + } + /** * Return a proxy to the specified target host:port. */ @@ -155,6 +171,8 @@ public class HAAdmin extends Configured implements Tool { return transitionToActive(argv); } else if ("-transitionToStandby".equals(cmd)) { return transitionToStandby(argv); + } else if ("-getServiceState".equals(cmd)) { + return getServiceState(argv); } else if ("-checkHealth".equals(cmd)) { return checkHealth(argv); } else if ("-help".equals(cmd)) { @@ -182,7 +200,7 @@ public class HAAdmin extends Configured implements Tool { return -1; } - errOut .println(cmd + " [" + usageInfo.args + "]: " + usageInfo.help); + errOut.println(cmd + " [" + usageInfo.args + "]: " + usageInfo.help); return 1; } diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/HAServiceProtocol.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/HAServiceProtocol.java index 20f0d04bc21..86f5f675ce3 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/HAServiceProtocol.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/HAServiceProtocol.java @@ -35,6 +35,24 @@ public interface HAServiceProtocol extends VersionedProtocol { */ public static final long versionID = 1L; + /** + * An HA service may be in active or standby state. + */ + public enum HAServiceState { + ACTIVE("active"), + STANDBY("standby"); + + private String name; + + HAServiceState(String name) { + this.name = name; + } + + public String toString() { + return name; + } + } + /** * Monitor the health of service. This periodically called by the HA * frameworks to monitor the health of the service. @@ -69,4 +87,9 @@ public interface HAServiceProtocol extends VersionedProtocol { * if transition from active to standby fails. */ public void transitionToStandby() throws ServiceFailedException; + + /** + * Return the current state of the service. + */ + public HAServiceState getServiceState(); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestHAAdmin.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestHAAdmin.java index 3cddbbe8a21..b465029d47f 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestHAAdmin.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestHAAdmin.java @@ -79,7 +79,7 @@ public class TestHAAdmin { assertOutputContains("transitionToActive: incorrect number of arguments"); assertEquals(-1, runTool("-transitionToActive", "x", "y")); assertOutputContains("transitionToActive: incorrect number of arguments"); -} + } @Test public void testHelp() throws Exception { @@ -99,7 +99,13 @@ public class TestHAAdmin { assertEquals(0, runTool("-transitionToStandby", "xxx")); Mockito.verify(mockProtocol).transitionToStandby(); } - + + @Test + public void testGetServiceState() throws Exception { + assertEquals(0, runTool("-getServiceState", "xxx")); + Mockito.verify(mockProtocol).getServiceState(); + } + @Test public void testCheckHealth() throws Exception { assertEquals(0, runTool("-checkHealth", "xxx"));