From 293fb1ec1e77084945a048bdde1683ad975b11bf Mon Sep 17 00:00:00 2001 From: Suresh Srinivas Date: Sun, 28 Apr 2013 23:16:47 +0000 Subject: [PATCH] HADOOP-9524. Fix ShellCommandFencer to work on Windows. Contributed by Arpit Agarwal. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1476856 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 +++ .../apache/hadoop/ha/ShellCommandFencer.java | 19 +++++++++++++------ .../org/apache/hadoop/ha/TestNodeFencer.java | 15 +++++++++++++-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 68f4a0ae9b7..d6de34f0022 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -528,6 +528,9 @@ Trunk (Unreleased) HADOOP-9490. LocalFileSystem#reportChecksumFailure not closing the checksum file handle before rename. (Ivan Mitic via suresh) + + HADOOP-9524. Fix ShellCommandFencer to work on Windows. + (Arpit Agarwal via suresh) Release 2.0.5-beta - UNRELEASED diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ShellCommandFencer.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ShellCommandFencer.java index c8b7a30dd13..15edee927de 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ShellCommandFencer.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ShellCommandFencer.java @@ -26,6 +26,7 @@ import org.apache.hadoop.conf.Configured; import com.google.common.annotations.VisibleForTesting; +import org.apache.hadoop.util.Shell; /** * Fencing method that runs a shell command. It should be specified @@ -33,8 +34,8 @@ * * shell(/path/to/my/script.sh arg1 arg2 ...) *
- * The string between '(' and ')' is passed directly to a bash shell and - * may not include any closing parentheses.

+ * The string between '(' and ')' is passed directly to a bash shell + * (cmd.exe on Windows) and may not include any closing parentheses.

* * The shell command will be run with an environment set up to contain * all of the current Hadoop configuration variables, with the '_' character @@ -58,11 +59,11 @@ public class ShellCommandFencer /** Prefix for target parameters added to the environment */ private static final String TARGET_PREFIX = "target_"; - + @VisibleForTesting static Log LOG = LogFactory.getLog( ShellCommandFencer.class); - + @Override public void checkArgs(String args) throws BadFencingConfigurationException { if (args == null || args.isEmpty()) { @@ -74,8 +75,14 @@ public void checkArgs(String args) throws BadFencingConfigurationException { @Override public boolean tryFence(HAServiceTarget target, String cmd) { - ProcessBuilder builder = new ProcessBuilder( - "bash", "-e", "-c", cmd); + ProcessBuilder builder; + + if (!Shell.WINDOWS) { + builder = new ProcessBuilder("bash", "-e", "-c", cmd); + } else { + builder = new ProcessBuilder("cmd.exe", "/c", cmd); + } + setConfAsEnvVars(builder.environment()); addTargetInfoAsEnvVars(target, builder.environment()); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestNodeFencer.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestNodeFencer.java index 53474823155..b929bcb2490 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestNodeFencer.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestNodeFencer.java @@ -24,6 +24,7 @@ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; +import org.apache.hadoop.util.Shell; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -33,7 +34,12 @@ public class TestNodeFencer { private HAServiceTarget MOCK_TARGET; - + + // Fencer shell commands that always return true on Unix and Windows + // respectively. Lacking the POSIX 'true' command on Windows, we use + // the batch command 'rem'. + private static String FENCER_TRUE_COMMAND_UNIX = "shell(true)"; + private static String FENCER_TRUE_COMMAND_WINDOWS = "shell(rem)"; @Before public void clearMockState() { @@ -48,6 +54,11 @@ public void clearMockState() { .when(MOCK_TARGET).getAddress(); } + private static String getFencerTrueCommand() { + return Shell.WINDOWS ? + FENCER_TRUE_COMMAND_WINDOWS : FENCER_TRUE_COMMAND_UNIX; + } + @Test public void testSingleFencer() throws BadFencingConfigurationException { NodeFencer fencer = setupFencer( @@ -100,7 +111,7 @@ public void testArglessFencer() throws BadFencingConfigurationException { @Test public void testShortNameShell() throws BadFencingConfigurationException { - NodeFencer fencer = setupFencer("shell(true)"); + NodeFencer fencer = setupFencer(getFencerTrueCommand()); assertTrue(fencer.fence(MOCK_TARGET)); }