Skip to content

Commit fa1999d

Browse files
authored
[server] Disable shutdown hook for log4j to ensure Fluss log in shutdown hook can be collected (#2020)
* [server] Disable shutdown hook for log4j to ensure Fluss log in shutdown hook can be collected * fix comments
1 parent 590e91c commit fa1999d

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

fluss-dist/src/main/resources/bin/fluss-daemon.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ FLUSS_LOG_PREFIX="${FLUSS_LOG_DIR}/fluss-${FLUSS_IDENT_STRING}-${DAEMON}-${id}-$
7979
log="${FLUSS_LOG_PREFIX}.log"
8080
out="${FLUSS_LOG_PREFIX}.out"
8181

82-
log_setting=("-Dlog.file=${log}" "-Dlog4j.configuration=file:${FLUSS_CONF_DIR}/log4j.properties" "-Dlog4j.configurationFile=file:${FLUSS_CONF_DIR}/log4j.properties" "-Dlogback.configurationFile=file:${FLUSS_CONF_DIR}/logback.xml")
82+
log_setting=("-Dlog.file=${log}" "-Dlog4j.shutdownHookEnabled=false" "-Dlog4j.configuration=file:${FLUSS_CONF_DIR}/log4j.properties" "-Dlog4j.configurationFile=file:${FLUSS_CONF_DIR}/log4j.properties" "-Dlogback.configurationFile=file:${FLUSS_CONF_DIR}/logback.xml")
8383

8484
function guaranteed_kill {
8585
to_stop_pid=$1

fluss-server/src/main/java/org/apache/fluss/server/ServerBase.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import java.util.concurrent.TimeUnit;
5050
import java.util.concurrent.TimeoutException;
5151

52+
import static org.apache.fluss.server.utils.LogShutdownUtil.shutdownLogIfPossible;
53+
5254
/** An abstract base server class for {@link CoordinatorServer} & {@link TabletServer}. */
5355
public abstract class ServerBase implements AutoCloseableAsync, FatalErrorHandler {
5456

@@ -146,7 +148,12 @@ public void start() throws Exception {
146148
private void addShutDownHook() {
147149
shutDownHook =
148150
ShutdownHookUtil.addShutdownHook(
149-
() -> this.closeAsync(Result.JVM_SHUTDOWN).join(), getServerName(), LOG);
151+
() -> {
152+
this.closeAsync(Result.JVM_SHUTDOWN).join();
153+
shutdownLogIfPossible();
154+
},
155+
getServerName(),
156+
LOG);
150157
}
151158

152159
@Override
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.fluss.server.utils;
19+
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
/** Utility class to shut down log system. */
24+
public class LogShutdownUtil {
25+
26+
private static final Logger LOG = LoggerFactory.getLogger(LogShutdownUtil.class);
27+
28+
public static void shutdownLogIfPossible() {
29+
// To avoid binding to a specific logging implementation, we use reflection here.
30+
try {
31+
// To ensure that logs within the JVM shutdown hook can be printed when using Log4j2,
32+
// Fluss has disabled Log4j2's shutdown hook; therefore, manual shutdown is required
33+
// here.
34+
Class<?> logManager = Class.forName("org.apache.logging.log4j.LogManager");
35+
logManager.getMethod("shutdown").invoke(null);
36+
} catch (ClassNotFoundException e) {
37+
LOG.error("Class org.apache.logging.log4j.LogManager not found", e);
38+
} catch (Exception e) {
39+
LOG.error("Error to invoke shutdown method of org.apache.logging.log4j.LogManager", e);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)