Skip to content

Commit

Permalink
HDDS-11078. Remove usage of sun.misc.Signal (#6876)
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroszlai authored Jul 4, 2024
1 parent 34362b0 commit 3f14219
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,9 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.SystemUtils;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.utils.SignalLogger;
import org.apache.hadoop.hdds.utils.VersionInfo;
import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.util.ShutdownHookManager;
import org.apache.ratis.thirdparty.io.netty.buffer.Unpooled;
import org.slf4j.Logger;

/**
* Simple utility class to collection string conversion methods.
Expand All @@ -44,11 +34,6 @@ private StringUtils() {

private static final Charset UTF8 = StandardCharsets.UTF_8;

/**
* Priority of the StringUtils shutdown hook.
*/
private static final int SHUTDOWN_HOOK_PRIORITY = 0;

/**
* Decode a specific range of bytes of the given byte array to a string
* using UTF8.
Expand Down Expand Up @@ -107,70 +92,6 @@ public static byte[] string2Bytes(String str) {
return str.getBytes(UTF8);
}

/**
* Return a message for logging.
* @param prefix prefix keyword for the message
* @param msg content of the message
* @return a message for logging
*/
public static String toStartupShutdownString(String prefix, String... msg) {
StringBuilder b = new StringBuilder(prefix);
b.append("\n/************************************************************");
for (String s : msg) {
b.append("\n").append(prefix).append(s);
}
b.append("\n************************************************************/");
return b.toString();
}

public static void startupShutdownMessage(VersionInfo versionInfo,
Class<?> clazz, String[] args, Logger log, OzoneConfiguration conf) {
final String hostname = NetUtils.getHostname();
final String className = clazz.getSimpleName();

if (log.isInfoEnabled()) {
log.info(createStartupShutdownMessage(versionInfo, className, hostname,
args, HddsUtils.processForLogging(conf)));
}

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(
() -> log.info(toStartupShutdownString("SHUTDOWN_MSG: ",
"Shutting down " + className + " at " + hostname)),
SHUTDOWN_HOOK_PRIORITY);

}

/**
* Generate the text for the startup/shutdown message of processes.
* @param className short name of the class
* @param hostname hostname
* @param args Command arguments
* @return a string to log.
*/
public static String createStartupShutdownMessage(VersionInfo versionInfo,
String className, String hostname, String[] args,
Map<String, String> conf) {
return toStartupShutdownString("STARTUP_MSG: ",
"Starting " + className,
" host = " + hostname,
" args = " + (args != null ? Arrays.asList(args) : new ArrayList<>()),
" version = " + versionInfo.getVersion(),
" classpath = " + System.getProperty("java.class.path"),
" build = " + versionInfo.getUrl() + "/"
+ versionInfo.getRevision()
+ " ; compiled by '" + versionInfo.getUser()
+ "' on " + versionInfo.getDate(),
" java = " + System.getProperty("java.version"),
" conf = " + conf);
}

public static String appendIfNotPresent(String str, char c) {
Preconditions.checkNotNull(str, "Input string is null");
return str.isEmpty() || str.charAt(str.length() - 1) != c ? str + c : str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.apache.hadoop.hdds.DFSConfigKeysLegacy;
import org.apache.hadoop.hdds.DatanodeVersion;
import org.apache.hadoop.hdds.HddsUtils;
import org.apache.hadoop.hdds.StringUtils;
import org.apache.hadoop.hdds.cli.GenericCli;
import org.apache.hadoop.hdds.cli.HddsVersionProvider;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
Expand Down Expand Up @@ -172,7 +171,7 @@ public static Logger getLogger() {
public Void call() throws Exception {
OzoneConfiguration configuration = createOzoneConfiguration();
if (printBanner) {
StringUtils.startupShutdownMessage(HddsVersionInfo.HDDS_VERSION_INFO,
HddsServerUtil.startupShutdownMessage(HddsVersionInfo.HDDS_VERSION_INFO,
HddsDatanodeService.class, args, LOG, configuration);
}
start(configuration);
Expand Down
4 changes: 4 additions & 0 deletions hadoop-hdds/framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-posix</artifactId>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.concurrent.TimeUnit;
Expand All @@ -39,8 +41,10 @@
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdds.DFSConfigKeysLegacy;
import org.apache.hadoop.hdds.HddsUtils;
import org.apache.hadoop.hdds.conf.ConfigurationSource;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.protocol.SCMSecurityProtocol;
Expand Down Expand Up @@ -99,6 +103,7 @@
import static org.apache.hadoop.hdds.server.ServerUtils.sanitizeUserArgs;
import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_DATANODE_CONTAINER_DB_DIR;

import org.apache.hadoop.util.ShutdownHookManager;
import org.rocksdb.RocksDBException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -111,6 +116,8 @@ public final class HddsServerUtil {
private HddsServerUtil() {
}

private static final int SHUTDOWN_HOOK_PRIORITY = 0;

public static final String OZONE_RATIS_SNAPSHOT_COMPLETE_FLAG_NAME =
"OZONE_RATIS_SNAPSHOT_COMPLETE";

Expand Down Expand Up @@ -678,4 +685,68 @@ public static IOException toIOException(String msg, RocksDBException e) {
public static void addSuppressedLoggingExceptions(RPC.Server server) {
server.addSuppressedLoggingExceptions(ServerNotLeaderException.class);
}

public static void startupShutdownMessage(VersionInfo versionInfo,
Class<?> clazz, String[] args, Logger log, OzoneConfiguration conf) {
final String hostname = NetUtils.getHostname();
final String className = clazz.getSimpleName();

if (log.isInfoEnabled()) {
log.info(createStartupShutdownMessage(versionInfo, className, hostname,
args, HddsUtils.processForLogging(conf)));
}

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(
() -> log.info(toStartupShutdownString("SHUTDOWN_MSG: ",
"Shutting down " + className + " at " + hostname)),
SHUTDOWN_HOOK_PRIORITY);
}

/**
* Return a message for logging.
* @param prefix prefix keyword for the message
* @param msg content of the message
* @return a message for logging
*/
public static String toStartupShutdownString(String prefix, String... msg) {
StringBuilder b = new StringBuilder(prefix);
b.append("\n/************************************************************");
for (String s : msg) {
b.append("\n").append(prefix).append(s);
}
b.append("\n************************************************************/");
return b.toString();
}

/**
* Generate the text for the startup/shutdown message of processes.
* @param className short name of the class
* @param hostname hostname
* @param args Command arguments
* @return a string to log.
*/
public static String createStartupShutdownMessage(VersionInfo versionInfo,
String className, String hostname, String[] args,
Map<String, String> conf) {
return toStartupShutdownString("STARTUP_MSG: ",
"Starting " + className,
" host = " + hostname,
" args = " + (args != null ? Arrays.asList(args) : new ArrayList<>()),
" version = " + versionInfo.getVersion(),
" classpath = " + System.getProperty("java.class.path"),
" build = " + versionInfo.getUrl() + "/"
+ versionInfo.getRevision()
+ " ; compiled by '" + versionInfo.getUser()
+ "' on " + versionInfo.getDate(),
" java = " + System.getProperty("java.version"),
" conf = " + conf);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@

package org.apache.hadoop.hdds.utils;

import jnr.constants.platform.Signal;
import jnr.posix.POSIX;
import jnr.posix.POSIXFactory;
import jnr.posix.SignalHandler;
import jnr.posix.util.DefaultPOSIXHandler;
import org.apache.hadoop.hdds.annotation.InterfaceAudience;
import org.apache.hadoop.hdds.annotation.InterfaceStability;
import org.slf4j.Logger;

import java.util.EnumSet;
import java.util.Set;

/**
* 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.
Expand All @@ -34,20 +42,18 @@ public enum SignalLogger {

INSTANCE;

private static final String[] SIGNALS = new String[] {"TERM", "HUP", "INT"};
private static final Set<Signal> SIGNALS = EnumSet.of(Signal.SIGHUP, Signal.SIGINT, Signal.SIGTERM);

private boolean registered = false;

/**
* Our signal handler.
*/
private static class Handler implements sun.misc.SignalHandler {
private static class Handler implements SignalHandler {
private final Logger log;
private final sun.misc.SignalHandler prevHandler;

Handler(String name, Logger log) {
Handler(Logger log) {
this.log = log;
prevHandler = sun.misc.Signal.handle(new sun.misc.Signal(name), this);
}

/**
Expand All @@ -56,10 +62,8 @@ private static class Handler implements sun.misc.SignalHandler {
* @param signal The incoming signal
*/
@Override
public void handle(sun.misc.Signal signal) {
log.error("RECEIVED SIGNAL {}: SIG{}",
signal.getNumber(), signal.getName());
prevHandler.handle(signal);
public void handle(int signal) {
log.error("RECEIVED SIGNAL {}: {}", signal, Signal.valueOf(signal));
}
}

Expand All @@ -76,10 +80,12 @@ public void register(final Logger log) {
StringBuilder bld = new StringBuilder();
bld.append("registered UNIX signal handlers for [");
String separator = "";
for (String signalName : SIGNALS) {
final POSIX posix = POSIXFactory.getPOSIX(new DefaultPOSIXHandler(), true);
final Handler handler = new Handler(log);
for (Signal signal : SIGNALS) {
try {
new Handler(signalName, log);
bld.append(separator).append(signalName);
posix.signal(signal, handler);
bld.append(separator).append(signal.name());
separator = ", ";
} catch (Exception e) {
log.debug("", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
*/
package org.apache.hadoop.hdds.scm.server;

import org.apache.hadoop.hdds.StringUtils;
import org.apache.hadoop.hdds.cli.GenericCli;
import org.apache.hadoop.hdds.cli.HddsVersionProvider;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.tracing.TracingUtil;
import org.apache.hadoop.hdds.utils.HddsServerUtil;
import org.apache.hadoop.hdds.utils.HddsVersionInfo;
import org.apache.hadoop.ozone.common.StorageInfo;
import org.apache.hadoop.ozone.util.OzoneNetUtils;
Expand Down Expand Up @@ -155,7 +155,7 @@ private void commonInit() {

String[] originalArgs = getCmd().getParseResult().originalArgs()
.toArray(new String[0]);
StringUtils.startupShutdownMessage(HddsVersionInfo.HDDS_VERSION_INFO,
HddsServerUtil.startupShutdownMessage(HddsVersionInfo.HDDS_VERSION_INFO,
StorageContainerManager.class, originalArgs, LOG, conf);
}

Expand Down
4 changes: 4 additions & 0 deletions hadoop-ozone/csi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ http://maven.apache.org/xsd/maven-4.0.0.xsd">
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-server-framework</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

import java.util.concurrent.Callable;

import org.apache.hadoop.hdds.StringUtils;
import org.apache.hadoop.hdds.cli.GenericCli;
import org.apache.hadoop.hdds.cli.HddsVersionProvider;
import org.apache.hadoop.hdds.conf.Config;
import org.apache.hadoop.hdds.conf.ConfigGroup;
import org.apache.hadoop.hdds.conf.ConfigTag;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.utils.HddsServerUtil;
import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneClientFactory;
import org.apache.hadoop.ozone.util.OzoneVersionInfo;
Expand Down Expand Up @@ -55,7 +55,7 @@ public Void call() throws Exception {
String[] originalArgs = getCmd().getParseResult().originalArgs()
.toArray(new String[0]);
OzoneConfiguration ozoneConfiguration = createOzoneConfiguration();
StringUtils.startupShutdownMessage(OzoneVersionInfo.OZONE_VERSION_INFO,
HddsServerUtil.startupShutdownMessage(OzoneVersionInfo.OZONE_VERSION_INFO,
CsiServer.class, originalArgs, LOG, ozoneConfiguration);
CsiConfig csiConfig = ozoneConfiguration.getObject(CsiConfig.class);

Expand Down
Loading

0 comments on commit 3f14219

Please sign in to comment.