HADOOP-9318. When exiting on a signal, print the signal name first. Contributed by Colin Patrick McCabe.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1457124 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9a9d40200f
commit
7319e42239
|
@ -18,4 +18,4 @@
|
|||
|
||||
OK_RELEASEAUDIT_WARNINGS=0
|
||||
OK_FINDBUGS_WARNINGS=0
|
||||
OK_JAVADOC_WARNINGS=8
|
||||
OK_JAVADOC_WARNINGS=13
|
||||
|
|
|
@ -25,6 +25,9 @@ Release 2.0.5-beta - UNRELEASED
|
|||
|
||||
HADOOP-9343. Allow additional exceptions through the RPC layer. (sseth)
|
||||
|
||||
HADOOP-9318. When exiting on a signal, print the signal name first. (Colin
|
||||
Patrick McCabe via atm)
|
||||
|
||||
OPTIMIZATIONS
|
||||
|
||||
BUG FIXES
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.hadoop.util;
|
||||
|
||||
import sun.misc.Signal;
|
||||
import sun.misc.SignalHandler;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.hadoop.classification.InterfaceAudience;
|
||||
import org.apache.hadoop.classification.InterfaceStability;
|
||||
|
||||
/**
|
||||
* This class logs a message whenever we're about to exit on a UNIX signal.
|
||||
* This is helpful for determining the root cause of a process' exit.
|
||||
* For example, if the process exited because the system administrator
|
||||
* ran a standard "kill," you would see 'EXITING ON SIGNAL SIGTERM' in the log.
|
||||
*/
|
||||
@InterfaceAudience.Private
|
||||
@InterfaceStability.Unstable
|
||||
public enum SignalLogger {
|
||||
INSTANCE;
|
||||
|
||||
private boolean registered = false;
|
||||
|
||||
/**
|
||||
* Our signal handler.
|
||||
*/
|
||||
private static class Handler implements SignalHandler {
|
||||
final private org.apache.commons.logging.Log LOG;
|
||||
final private SignalHandler prevHandler;
|
||||
|
||||
Handler(String name, Log LOG) {
|
||||
this.LOG = LOG;
|
||||
prevHandler = Signal.handle(new Signal(name), this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming signal.
|
||||
*
|
||||
* @param signal The incoming signal
|
||||
*/
|
||||
@Override
|
||||
public void handle(Signal signal) {
|
||||
LOG.error("RECEIVED SIGNAL " + signal.getNumber() +
|
||||
": SIG" + signal.getName());
|
||||
prevHandler.handle(signal);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register some signal handlers.
|
||||
*
|
||||
* @param LOG The log4j logfile to use in the signal handlers.
|
||||
*/
|
||||
public void register(final Log LOG) {
|
||||
if (registered) {
|
||||
throw new IllegalStateException("Can't re-install the signal handlers.");
|
||||
}
|
||||
registered = true;
|
||||
StringBuilder bld = new StringBuilder();
|
||||
bld.append("registered UNIX signal handlers for [");
|
||||
final String SIGNALS[] = { "TERM", "HUP", "INT" };
|
||||
String separator = "";
|
||||
for (String signalName : SIGNALS) {
|
||||
try {
|
||||
new Handler(signalName, LOG);
|
||||
bld.append(separator);
|
||||
bld.append(signalName);
|
||||
separator = ", ";
|
||||
} catch (Exception e) {
|
||||
LOG.debug(e);
|
||||
}
|
||||
}
|
||||
bld.append("]");
|
||||
LOG.info(bld.toString());
|
||||
}
|
||||
}
|
|
@ -32,6 +32,7 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
import org.apache.hadoop.classification.InterfaceAudience;
|
||||
import org.apache.hadoop.classification.InterfaceStability;
|
||||
import org.apache.hadoop.fs.Path;
|
||||
|
@ -588,6 +589,13 @@ public class StringUtils {
|
|||
)
|
||||
);
|
||||
|
||||
if (SystemUtils.IS_OS_UNIX) {
|
||||
try {
|
||||
SignalLogger.INSTANCE.register(LOG);
|
||||
} catch (Throwable t) {
|
||||
LOG.warn("failed to register any UNIX signal loggers: ", t);
|
||||
}
|
||||
}
|
||||
ShutdownHookManager.get().addShutdownHook(
|
||||
new Runnable() {
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.hadoop.util;
|
||||
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestSignalLogger {
|
||||
public static final Log LOG = LogFactory.getLog(TestSignalLogger.class);
|
||||
|
||||
@Test(timeout=60000)
|
||||
public void testInstall() throws Exception {
|
||||
Assume.assumeTrue(SystemUtils.IS_OS_UNIX);
|
||||
SignalLogger.INSTANCE.register(LOG);
|
||||
try {
|
||||
SignalLogger.INSTANCE.register(LOG);
|
||||
Assert.fail("expected IllegalStateException from double registration");
|
||||
} catch (IllegalStateException e) {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue