Skip to content

Commit

Permalink
IGNITE-23920 Shutdown node on SIGTERM (apache#5010)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pochatkin authored Jan 14, 2025
1 parent e7701a7 commit 0700f33
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ ext {
]

compilerArgs = [
"--add-exports=java.base/sun.nio.ch=ALL-UNNAMED"
"--add-exports=java.base/sun.nio.ch=ALL-UNNAMED",
"--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED"
]

product = "Apache Ignite"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.nio.file.Path;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import jdk.internal.misc.Signal;
import jdk.internal.misc.Signal.Handler;
import org.apache.ignite.IgniteServer;
import picocli.CommandLine;
import picocli.CommandLine.Command;
Expand Down Expand Up @@ -68,14 +71,37 @@ public static IgniteServer start(String... args) {
* @param args CLI args to start a new node.
*/
public static void main(String[] args) {
IgniteServer server = start(args);
AtomicBoolean shutdown = new AtomicBoolean(false);

Handler handler = sig -> {
try {
System.out.println("Ignite node shutting down...");
shutdown.set(true);
server.shutdown();
} catch (Throwable t) {
System.out.println("Failed to shutdown: " + t.getMessage());

t.printStackTrace(System.out);
}

// Copy-paste from default JVM signal handler java.lang.Terminator#setup.
System.exit(sig.getNumber() + 0200);
};

Signal.handle(new Signal("INT"), handler);
Signal.handle(new Signal("TERM"), handler);

try {
start(args).waitForInitAsync().get();
server.waitForInitAsync().get();
} catch (ExecutionException | InterruptedException e) {
System.out.println("Error when starting the node: " + e.getMessage());
if (!shutdown.get()) {
System.out.println("Error when starting the node: " + e.getMessage());

e.printStackTrace(System.out);
e.printStackTrace(System.out);

System.exit(1);
System.exit(1);
}
}
}
}

0 comments on commit 0700f33

Please sign in to comment.