World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#10327] improvement(server): ensure shutdown hook runs app cleanup on SIGTERM (#10435)
Jalina200772182ff66a6a
diff --git a/server/src/main/java/org/apache/gravitino/server/GravitinoServer.java b/server/src/main/java/org/apache/gravitino/server/GravitinoServer.javaindex 90a537f885..1f96d7543b 100644--- a/server/src/main/java/org/apache/gravitino/server/GravitinoServer.java+++ b/server/src/main/java/org/apache/gravitino/server/GravitinoServer.java@@ -22,6 +22,7 @@ import java.io.File;import java.io.IOException;import java.util.HashSet;import java.util.Properties;+import java.util.concurrent.atomic.AtomicBoolean;import javax.inject.Singleton;import javax.servlet.Servlet;import org.apache.gravitino.Configs;@@ -88,6 +89,8 @@ public class GravitinoServer extends ResourceConfig {private final LineageService lineageService;+ private final AtomicBoolean isStopped = new AtomicBoolean(false);+public GravitinoServer(ServerConfig config, GravitinoEnv gravitinoEnv) {this.serverConfig = config;this.server = new JettyServer();@@ -198,6 +201,13 @@ public class GravitinoServer extends ResourceConfig {}}+ void gracefulStop() throws IOException {+ if (!isStopped.compareAndSet(false, true)) {+ return;+ }+ stop();+ }+public static void main(String[] args) {LOG.info("Starting Gravitino Server");String confPath = System.getenv("GRAVITINO_TEST") == null ? "" : args[0];@@ -219,8 +229,8 @@ public class GravitinoServer extends ResourceConfig {new Thread(() -> {try {- // Register some clean-up tasks that need to be done before shutting downThread.sleep(server.serverConfig.get(ServerConfig.SERVER_SHUTDOWN_TIMEOUT));+ server.gracefulStop();} catch (InterruptedException e) {Thread.currentThread().interrupt();LOG.error("Interrupted exception:", e);@@ -233,7 +243,7 @@ public class GravitinoServer extends ResourceConfig {LOG.info("Shutting down Gravitino Server ... ");try {- server.stop();+ server.gracefulStop();LOG.info("Gravitino Server has shut down.");} catch (Exception e) {LOG.error("Error while stopping Gravitino Server", e);diff --git a/server/src/test/java/org/apache/gravitino/server/TestGravitinoServer.java b/server/src/test/java/org/apache/gravitino/server/TestGravitinoServer.javaindex 2155047c9e..0c5cfa48e6 100644--- a/server/src/test/java/org/apache/gravitino/server/TestGravitinoServer.java+++ b/server/src/test/java/org/apache/gravitino/server/TestGravitinoServer.java@@ -20,9 +20,11 @@ package org.apache.gravitino.server;import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PATH;import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.junit.jupiter.api.Assertions.assertTrue;import com.google.common.collect.ImmutableMap;import java.io.IOException;+import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import org.apache.commons.io.FileUtils;@@ -116,4 +118,22 @@ public class TestGravitinoServer {// TODO: Exception due to environment variable not set. Is this the right exception?assertThrows(IllegalArgumentException.class, () -> config.loadFromFile("config"));}++ @Test+ public void testMainShutdownHookShouldInvokeServerStop() throws IOException {+ Path sourceFile = Path.of("src/main/java/org/apache/gravitino/server/GravitinoServer.java");+ String source = Files.readString(sourceFile);++ int hookStart = source.indexOf("addShutdownHook");+ int joinIndex = source.indexOf("server.join();", hookStart);++ assertTrue(hookStart >= 0, "Main should register a shutdown hook");+ assertTrue(+ joinIndex > hookStart, "Main should call server.join() after registering shutdown hook");++ String hookBlock = source.substring(hookStart, joinIndex);+ assertTrue(+ hookBlock.contains("server.gracefulStop()"),+ "Shutdown hook should invoke server.gracefulStop() so app-level cleanup runs on SIGTERM");+ }}