This is an automated email from the ASF dual-hosted git repository.

yunhong pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fluss.git


The following commit(s) were added to refs/heads/main by this push:
     new babf91b53 [server] Register signal handlers to log termination due to 
SIGTERM, SIGHUP and SIGINT (#2012)
babf91b53 is described below

commit babf91b5370b4f7a191a0b1b186bceb75e075027
Author: Liebing <[email protected]>
AuthorDate: Tue Nov 25 10:21:45 2025 +0800

    [server] Register signal handlers to log termination due to SIGTERM, SIGHUP 
and SIGINT (#2012)
---
 .../java/org/apache/fluss/server/ServerBase.java   |  2 +
 .../apache/fluss/server/tablet/TabletServer.java   |  2 +-
 .../apache/fluss/server/utils/SignalHandler.java   | 98 ++++++++++++++++++++++
 3 files changed, 101 insertions(+), 1 deletion(-)

diff --git a/fluss-server/src/main/java/org/apache/fluss/server/ServerBase.java 
b/fluss-server/src/main/java/org/apache/fluss/server/ServerBase.java
index 4acbafd09..2e24aa093 100644
--- a/fluss-server/src/main/java/org/apache/fluss/server/ServerBase.java
+++ b/fluss-server/src/main/java/org/apache/fluss/server/ServerBase.java
@@ -32,6 +32,7 @@ import org.apache.fluss.server.tablet.TabletServer;
 import org.apache.fluss.server.utils.ConfigurationParserUtils;
 import org.apache.fluss.server.utils.FatalErrorHandler;
 import org.apache.fluss.server.utils.ShutdownHookUtil;
+import org.apache.fluss.server.utils.SignalHandler;
 import org.apache.fluss.utils.AutoCloseableAsync;
 import org.apache.fluss.utils.ExceptionUtils;
 import org.apache.fluss.utils.concurrent.FutureUtils;
@@ -113,6 +114,7 @@ public abstract class ServerBase implements 
AutoCloseableAsync, FatalErrorHandle
     }
 
     public void start() throws Exception {
+        SignalHandler.register(LOG);
         try {
             addShutDownHook();
 
diff --git 
a/fluss-server/src/main/java/org/apache/fluss/server/tablet/TabletServer.java 
b/fluss-server/src/main/java/org/apache/fluss/server/tablet/TabletServer.java
index eef925b50..677035039 100644
--- 
a/fluss-server/src/main/java/org/apache/fluss/server/tablet/TabletServer.java
+++ 
b/fluss-server/src/main/java/org/apache/fluss/server/tablet/TabletServer.java
@@ -284,7 +284,7 @@ public class TabletServer extends ServerBase {
     @Override
     protected CompletableFuture<Result> closeAsync(Result result) {
         if (isShutDown.compareAndSet(false, true)) {
-
+            LOG.info("Shutting down Tablet server ({}).", result);
             controlledShutDown();
 
             CompletableFuture<Void> serviceShutdownFuture = stopServices();
diff --git 
a/fluss-server/src/main/java/org/apache/fluss/server/utils/SignalHandler.java 
b/fluss-server/src/main/java/org/apache/fluss/server/utils/SignalHandler.java
new file mode 100644
index 000000000..92c72873b
--- /dev/null
+++ 
b/fluss-server/src/main/java/org/apache/fluss/server/utils/SignalHandler.java
@@ -0,0 +1,98 @@
+/*
+ * 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.fluss.server.utils;
+
+import org.apache.fluss.utils.OperatingSystem;
+
+import org.slf4j.Logger;
+import sun.misc.Signal;
+
+/* This file is based on source code of Apache Flink Project 
(https://flink.apache.org/), licensed by the Apache
+ * Software Foundation (ASF) under the Apache License, Version 2.0. See the 
NOTICE file distributed with this work for
+ * additional information regarding copyright ownership. */
+
+/**
+ * This signal handler / signal logger is based on Apache Hadoop's
+ * org.apache.hadoop.util.SignalLogger.
+ */
+public class SignalHandler {
+
+    private static boolean registered = false;
+
+    /** Our signal handler. */
+    private static class Handler implements sun.misc.SignalHandler {
+
+        private final Logger log;
+        private final sun.misc.SignalHandler prevHandler;
+
+        Handler(String name, Logger 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.warn(
+                    "RECEIVED SIGNAL {}: SIG{}. Shutting down as requested.",
+                    signal.getNumber(),
+                    signal.getName());
+            prevHandler.handle(signal);
+        }
+    }
+
+    /**
+     * Register some signal handlers.
+     *
+     * @param log The slf4j logger
+     */
+    public static void register(final Logger log) {
+        synchronized (SignalHandler.class) {
+            if (registered) {
+                return;
+            }
+            registered = true;
+
+            final String[] signals =
+                    OperatingSystem.isWindows()
+                            ? new String[] {"TERM", "INT"}
+                            : new String[] {"TERM", "HUP", "INT"};
+
+            StringBuilder bld = new StringBuilder();
+            bld.append("Registered UNIX signal handlers for [");
+
+            String separator = "";
+            for (String signalName : signals) {
+                try {
+                    new Handler(signalName, log);
+                    bld.append(separator);
+                    bld.append(signalName);
+                    separator = ", ";
+                } catch (Exception e) {
+                    log.info("Error while registering signal handler", e);
+                }
+            }
+            bld.append("]");
+            log.info(bld.toString());
+        }
+    }
+}

Reply via email to