HADOOP-14638. Replace commons-logging APIs with slf4j in StreamPumper.

This closes #247

Signed-off-by: Akira Ajisaka <aajisaka@apache.org>
This commit is contained in:
wenxinhe 2017-07-10 11:37:48 +08:00 committed by Akira Ajisaka
parent f1efa14fc6
commit fce7951014
No known key found for this signature in database
GPG Key ID: C1EDBB9CA400FD50
5 changed files with 65 additions and 19 deletions

View File

@ -25,10 +25,10 @@ import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Fencer method that uses PowerShell to remotely connect to a machine and kill
@ -41,7 +41,8 @@ import org.apache.hadoop.util.StringUtils;
*/
public class PowerShellFencer extends Configured implements FenceMethod {
private static final Log LOG = LogFactory.getLog(PowerShellFencer.class);
private static final Logger LOG = LoggerFactory.getLogger(PowerShellFencer
.class);
@Override

View File

@ -21,12 +21,12 @@ import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configured;
import com.google.common.annotations.VisibleForTesting;
import org.apache.hadoop.util.Shell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Fencing method that runs a shell command. It should be specified
@ -61,8 +61,7 @@ public class ShellCommandFencer
private static final String TARGET_PREFIX = "target_";
@VisibleForTesting
static Log LOG = LogFactory.getLog(
ShellCommandFencer.class);
static Logger LOG = LoggerFactory.getLogger(ShellCommandFencer.class);
@Override
public void checkArgs(String args) throws BadFencingConfigurationException {

View File

@ -32,6 +32,8 @@ import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This fencing implementation sshes to the target node and uses
@ -58,8 +60,7 @@ import com.jcraft.jsch.Session;
public class SshFenceByTcpPort extends Configured
implements FenceMethod {
static final Log LOG = LogFactory.getLog(
SshFenceByTcpPort.class);
static final Logger LOG = LoggerFactory.getLogger(SshFenceByTcpPort.class);
static final String CONF_CONNECT_TIMEOUT_KEY =
"dfs.ha.fencing.ssh.connect-timeout";

View File

@ -17,14 +17,14 @@
*/
package org.apache.hadoop.ha;
import org.slf4j.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import org.apache.commons.logging.Log;
/**
* Class responsible for pumping the streams of the subprocess
* out to log4j. stderr is pumped to WARN level and stdout is
@ -35,7 +35,7 @@ class StreamPumper {
STDOUT, STDERR;
}
private final Log log;
private final Logger log;
final Thread thread;
final String logPrefix;
@ -43,7 +43,7 @@ class StreamPumper {
private final InputStream stream;
private boolean started = false;
StreamPumper(final Log log, final String logPrefix,
StreamPumper(final Logger log, final String logPrefix,
final InputStream stream, final StreamType type) {
this.log = log;
this.logPrefix = logPrefix;

View File

@ -19,28 +19,41 @@ package org.apache.hadoop.ha;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.util.List;
import com.google.common.collect.Lists;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ha.HAServiceProtocol.HAServiceState;
import org.apache.hadoop.util.Shell;
import org.apache.hadoop.util.StringUtils;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.mock;
public class TestShellCommandFencer {
private ShellCommandFencer fencer = createFencer();
private static final HAServiceTarget TEST_TARGET =
new DummyHAService(HAServiceState.ACTIVE,
new InetSocketAddress("dummyhost", 1234));
private static final Logger LOG = ShellCommandFencer.LOG;
@BeforeClass
public static void setupLogSpy() {
ShellCommandFencer.LOG = spy(ShellCommandFencer.LOG);
public static void setupLogMock() {
ShellCommandFencer.LOG = mock(Logger.class, new LogAnswer());
}
@AfterClass
public static void tearDownLogMock() throws Exception {
ShellCommandFencer.LOG = LOG;
}
@Before
@ -173,4 +186,36 @@ public class TestShellCommandFencer {
assertEquals("a...gh", ShellCommandFencer.abbreviate("abcdefgh", 6));
assertEquals("ab...gh", ShellCommandFencer.abbreviate("abcdefgh", 7));
}
/**
* An answer simply delegate some basic log methods to real LOG.
*/
private static class LogAnswer implements Answer {
private static final List<String> DELEGATE_METHODS = Lists.asList("error",
new String[]{"warn", "info", "debug", "trace"});
@Override
public Object answer(InvocationOnMock invocation) {
String methodName = invocation.getMethod().getName();
if (!DELEGATE_METHODS.contains(methodName)) {
return null;
}
try {
String msg = invocation.getArguments()[0].toString();
Method delegateMethod = LOG.getClass().getMethod(methodName,
msg.getClass());
delegateMethod.invoke(LOG, msg);
} catch (Throwable e) {
throw new IllegalStateException(
"Unsupported delegate method: " + methodName);
}
return null;
}
}
}