diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 8cc17855695..7f73f4a4336 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -253,6 +253,8 @@ Release 0.23.3 - UNRELEASED HADOOP-8189. LdapGroupsMapping shouldn't throw away IOException. (Jonathan Natkins via atm) + HADOOP-8191. SshFenceByTcpPort uses netcat incorrectly (todd) + BREAKDOWN OF HADOOP-7454 SUBTASKS HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/SshFenceByTcpPort.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/SshFenceByTcpPort.java index cec731cf20b..00b9a83a572 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/SshFenceByTcpPort.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/SshFenceByTcpPort.java @@ -74,9 +74,7 @@ public class SshFenceByTcpPort extends Configured @Override public void checkArgs(String argStr) throws BadFencingConfigurationException { if (argStr != null) { - // Use a dummy service when checking the arguments defined - // in the configuration are parseable. - new Args(new InetSocketAddress("localhost", 8020), argStr); + new Args(argStr); } } @@ -84,29 +82,30 @@ public class SshFenceByTcpPort extends Configured public boolean tryFence(InetSocketAddress serviceAddr, String argsStr) throws BadFencingConfigurationException { - Args args = new Args(serviceAddr, argsStr); - + Args args = new Args(argsStr); + String host = serviceAddr.getHostName(); + Session session; try { - session = createSession(args); + session = createSession(serviceAddr.getHostName(), args); } catch (JSchException e) { LOG.warn("Unable to create SSH session", e); return false; } - LOG.info("Connecting to " + args.host + "..."); + LOG.info("Connecting to " + host + "..."); try { session.connect(getSshConnectTimeout()); } catch (JSchException e) { - LOG.warn("Unable to connect to " + args.host + LOG.warn("Unable to connect to " + host + " as user " + args.user, e); return false; } - LOG.info("Connected to " + args.host); + LOG.info("Connected to " + host); try { - return doFence(session, args.targetPort); + return doFence(session, serviceAddr); } catch (JSchException e) { LOG.warn("Unable to achieve fencing on remote host", e); return false; @@ -116,19 +115,21 @@ public class SshFenceByTcpPort extends Configured } - private Session createSession(Args args) throws JSchException { + private Session createSession(String host, Args args) throws JSchException { JSch jsch = new JSch(); for (String keyFile : getKeyFiles()) { jsch.addIdentity(keyFile); } JSch.setLogger(new LogAdapter()); - Session session = jsch.getSession(args.user, args.host, args.sshPort); + Session session = jsch.getSession(args.user, host, args.sshPort); session.setConfig("StrictHostKeyChecking", "no"); return session; } - private boolean doFence(Session session, int port) throws JSchException { + private boolean doFence(Session session, InetSocketAddress serviceAddr) + throws JSchException { + int port = serviceAddr.getPort(); try { LOG.info("Looking for process running on port " + port); int rc = execCommand(session, @@ -145,7 +146,8 @@ public class SshFenceByTcpPort extends Configured LOG.info( "Indeterminate response from trying to kill service. " + "Verifying whether it is running using nc..."); - rc = execCommand(session, "nc -z localhost 8020"); + rc = execCommand(session, "nc -z " + serviceAddr.getHostName() + + " " + serviceAddr.getPort()); if (rc == 0) { // the service is still listening - we are unable to fence LOG.warn("Unable to fence - it is running but we cannot kill it"); @@ -229,15 +231,11 @@ public class SshFenceByTcpPort extends Configured private static final int DEFAULT_SSH_PORT = 22; - String host; - int targetPort; String user; int sshPort; - public Args(InetSocketAddress serviceAddr, String arg) + public Args(String arg) throws BadFencingConfigurationException { - host = serviceAddr.getHostName(); - targetPort = serviceAddr.getPort(); user = System.getProperty("user.name"); sshPort = DEFAULT_SSH_PORT; diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestSshFenceByTcpPort.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestSshFenceByTcpPort.java index f89df6a21ff..554a7abca5f 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestSshFenceByTcpPort.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestSshFenceByTcpPort.java @@ -71,37 +71,25 @@ public class TestSshFenceByTcpPort { @Test public void testArgsParsing() throws BadFencingConfigurationException { - InetSocketAddress addr = new InetSocketAddress("bar.com", 1234); - - Args args = new SshFenceByTcpPort.Args(addr, null); - assertEquals("bar.com", args.host); - assertEquals(1234, args.targetPort); + Args args = new SshFenceByTcpPort.Args(null); assertEquals(System.getProperty("user.name"), args.user); assertEquals(22, args.sshPort); - args = new SshFenceByTcpPort.Args(addr, ""); - assertEquals("bar.com", args.host); - assertEquals(1234, args.targetPort); + args = new SshFenceByTcpPort.Args(""); assertEquals(System.getProperty("user.name"), args.user); assertEquals(22, args.sshPort); - args = new SshFenceByTcpPort.Args(addr, "12345"); - assertEquals("bar.com", args.host); - assertEquals(1234, args.targetPort); + args = new SshFenceByTcpPort.Args("12345"); assertEquals("12345", args.user); assertEquals(22, args.sshPort); - args = new SshFenceByTcpPort.Args(addr, ":12345"); - assertEquals("bar.com", args.host); - assertEquals(1234, args.targetPort); + args = new SshFenceByTcpPort.Args(":12345"); assertEquals(System.getProperty("user.name"), args.user); assertEquals(12345, args.sshPort); - args = new SshFenceByTcpPort.Args(addr, "foo:8020"); - assertEquals("bar.com", args.host); - assertEquals(1234, args.targetPort); + args = new SshFenceByTcpPort.Args("foo:2222"); assertEquals("foo", args.user); - assertEquals(8020, args.sshPort); + assertEquals(2222, args.sshPort); } @Test @@ -113,9 +101,8 @@ public class TestSshFenceByTcpPort { } private void assertBadArgs(String argStr) { - InetSocketAddress addr = new InetSocketAddress("bar.com", 1234); try { - new Args(addr, argStr); + new Args(argStr); fail("Did not fail on bad args: " + argStr); } catch (BadFencingConfigurationException e) { // Expected