HADOOP-8191. SshFenceByTcpPort uses netcat incorrectly. Contributed by Todd Lipcon.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.23@1303147 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Todd Lipcon 2012-03-20 21:01:28 +00:00
parent 658aac369d
commit d31bade781
3 changed files with 26 additions and 39 deletions

View File

@ -145,6 +145,8 @@ Release 0.23.3 - UNRELEASED
HADOOP-8189. LdapGroupsMapping shouldn't throw away IOException. (Jonathan Natkins via atm) HADOOP-8189. LdapGroupsMapping shouldn't throw away IOException. (Jonathan Natkins via atm)
HADOOP-8191. SshFenceByTcpPort uses netcat incorrectly (todd)
BREAKDOWN OF HADOOP-7454 SUBTASKS BREAKDOWN OF HADOOP-7454 SUBTASKS
HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh) HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh)

View File

@ -74,9 +74,7 @@ public class SshFenceByTcpPort extends Configured
@Override @Override
public void checkArgs(String argStr) throws BadFencingConfigurationException { public void checkArgs(String argStr) throws BadFencingConfigurationException {
if (argStr != null) { if (argStr != null) {
// Use a dummy service when checking the arguments defined new Args(argStr);
// in the configuration are parseable.
new Args(new InetSocketAddress("localhost", 8020), argStr);
} }
} }
@ -84,29 +82,30 @@ public class SshFenceByTcpPort extends Configured
public boolean tryFence(InetSocketAddress serviceAddr, String argsStr) public boolean tryFence(InetSocketAddress serviceAddr, String argsStr)
throws BadFencingConfigurationException { throws BadFencingConfigurationException {
Args args = new Args(serviceAddr, argsStr); Args args = new Args(argsStr);
String host = serviceAddr.getHostName();
Session session; Session session;
try { try {
session = createSession(args); session = createSession(serviceAddr.getHostName(), args);
} catch (JSchException e) { } catch (JSchException e) {
LOG.warn("Unable to create SSH session", e); LOG.warn("Unable to create SSH session", e);
return false; return false;
} }
LOG.info("Connecting to " + args.host + "..."); LOG.info("Connecting to " + host + "...");
try { try {
session.connect(getSshConnectTimeout()); session.connect(getSshConnectTimeout());
} catch (JSchException e) { } catch (JSchException e) {
LOG.warn("Unable to connect to " + args.host LOG.warn("Unable to connect to " + host
+ " as user " + args.user, e); + " as user " + args.user, e);
return false; return false;
} }
LOG.info("Connected to " + args.host); LOG.info("Connected to " + host);
try { try {
return doFence(session, args.targetPort); return doFence(session, serviceAddr);
} catch (JSchException e) { } catch (JSchException e) {
LOG.warn("Unable to achieve fencing on remote host", e); LOG.warn("Unable to achieve fencing on remote host", e);
return false; 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(); JSch jsch = new JSch();
for (String keyFile : getKeyFiles()) { for (String keyFile : getKeyFiles()) {
jsch.addIdentity(keyFile); jsch.addIdentity(keyFile);
} }
JSch.setLogger(new LogAdapter()); 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"); session.setConfig("StrictHostKeyChecking", "no");
return session; 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 { try {
LOG.info("Looking for process running on port " + port); LOG.info("Looking for process running on port " + port);
int rc = execCommand(session, int rc = execCommand(session,
@ -145,7 +146,8 @@ public class SshFenceByTcpPort extends Configured
LOG.info( LOG.info(
"Indeterminate response from trying to kill service. " + "Indeterminate response from trying to kill service. " +
"Verifying whether it is running using nc..."); "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) { if (rc == 0) {
// the service is still listening - we are unable to fence // the service is still listening - we are unable to fence
LOG.warn("Unable to fence - it is running but we cannot kill it"); 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; private static final int DEFAULT_SSH_PORT = 22;
String host;
int targetPort;
String user; String user;
int sshPort; int sshPort;
public Args(InetSocketAddress serviceAddr, String arg) public Args(String arg)
throws BadFencingConfigurationException { throws BadFencingConfigurationException {
host = serviceAddr.getHostName();
targetPort = serviceAddr.getPort();
user = System.getProperty("user.name"); user = System.getProperty("user.name");
sshPort = DEFAULT_SSH_PORT; sshPort = DEFAULT_SSH_PORT;

View File

@ -71,37 +71,25 @@ public class TestSshFenceByTcpPort {
@Test @Test
public void testArgsParsing() throws BadFencingConfigurationException { public void testArgsParsing() throws BadFencingConfigurationException {
InetSocketAddress addr = new InetSocketAddress("bar.com", 1234); Args args = new SshFenceByTcpPort.Args(null);
Args args = new SshFenceByTcpPort.Args(addr, null);
assertEquals("bar.com", args.host);
assertEquals(1234, args.targetPort);
assertEquals(System.getProperty("user.name"), args.user); assertEquals(System.getProperty("user.name"), args.user);
assertEquals(22, args.sshPort); assertEquals(22, args.sshPort);
args = new SshFenceByTcpPort.Args(addr, ""); args = new SshFenceByTcpPort.Args("");
assertEquals("bar.com", args.host);
assertEquals(1234, args.targetPort);
assertEquals(System.getProperty("user.name"), args.user); assertEquals(System.getProperty("user.name"), args.user);
assertEquals(22, args.sshPort); assertEquals(22, args.sshPort);
args = new SshFenceByTcpPort.Args(addr, "12345"); args = new SshFenceByTcpPort.Args("12345");
assertEquals("bar.com", args.host);
assertEquals(1234, args.targetPort);
assertEquals("12345", args.user); assertEquals("12345", args.user);
assertEquals(22, args.sshPort); assertEquals(22, args.sshPort);
args = new SshFenceByTcpPort.Args(addr, ":12345"); args = new SshFenceByTcpPort.Args(":12345");
assertEquals("bar.com", args.host);
assertEquals(1234, args.targetPort);
assertEquals(System.getProperty("user.name"), args.user); assertEquals(System.getProperty("user.name"), args.user);
assertEquals(12345, args.sshPort); assertEquals(12345, args.sshPort);
args = new SshFenceByTcpPort.Args(addr, "foo:8020"); args = new SshFenceByTcpPort.Args("foo:2222");
assertEquals("bar.com", args.host);
assertEquals(1234, args.targetPort);
assertEquals("foo", args.user); assertEquals("foo", args.user);
assertEquals(8020, args.sshPort); assertEquals(2222, args.sshPort);
} }
@Test @Test
@ -113,9 +101,8 @@ public class TestSshFenceByTcpPort {
} }
private void assertBadArgs(String argStr) { private void assertBadArgs(String argStr) {
InetSocketAddress addr = new InetSocketAddress("bar.com", 1234);
try { try {
new Args(addr, argStr); new Args(argStr);
fail("Did not fail on bad args: " + argStr); fail("Did not fail on bad args: " + argStr);
} catch (BadFencingConfigurationException e) { } catch (BadFencingConfigurationException e) {
// Expected // Expected