From 67f87962c567fdd6868eb4b8d2c8d580019d9bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20B=C4=85czkowski?= Date: Mon, 21 Oct 2024 13:44:22 +0200 Subject: [PATCH 1/3] Add signal handler that prints thread dump Adds a signal handler that will print thread dump to standard output on the following signals: SIGABRT, SIGTERM, SIGINT. --- .../org/apache/cassandra/stress/Stress.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tools/stress/src/org/apache/cassandra/stress/Stress.java b/tools/stress/src/org/apache/cassandra/stress/Stress.java index 3c0fa96fe2..8050cc98c4 100644 --- a/tools/stress/src/org/apache/cassandra/stress/Stress.java +++ b/tools/stress/src/org/apache/cassandra/stress/Stress.java @@ -18,6 +18,9 @@ package org.apache.cassandra.stress; import java.io.*; +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadInfo; +import java.lang.management.ThreadMXBean; import java.net.Socket; import java.net.SocketException; @@ -27,6 +30,9 @@ import org.apache.cassandra.utils.FBUtilities; import org.apache.cassandra.utils.WindowsTimer; +import sun.misc.Signal; +import sun.misc.SignalHandler; + public final class Stress { @@ -56,6 +62,8 @@ public final class Stress public static void main(String[] arguments) throws Exception { + registerSignalHandler(); + if (FBUtilities.isWindows) WindowsTimer.startTimerPeriod(1); @@ -196,4 +204,32 @@ public void run() } } + private static String threadDump(boolean lockedMonitors, boolean lockedSynchronizers) { + StringBuffer threadDump = new StringBuffer(System.lineSeparator()); + ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); + for(ThreadInfo threadInfo : threadMXBean.dumpAllThreads(lockedMonitors, lockedSynchronizers)) { + threadDump.append(threadInfo.toString()); + } + return threadDump.toString(); + } + + private static void registerSignalHandler() { + SignalHandler handler = signal -> { + System.out.println("Caught signal: " + signal.getName()); + System.out.println(threadDump(true, true)); + switch (signal.getName()) { + case "ABRT": + System.exit(128 + 6); + case "TERM": + System.exit(128 + 15); + case "INT": + System.exit(128 + 2); + default: + System.exit(1); + } + }; + Signal.handle(new Signal("ABRT"), handler); + Signal.handle(new Signal("TERM"), handler); + Signal.handle(new Signal("INT"), handler); + } } From 59432cb818b04c99caff098cec7ff1d0bcbe8026 Mon Sep 17 00:00:00 2001 From: Dusan Malusev Date: Mon, 21 Oct 2024 17:02:28 +0200 Subject: [PATCH 2/3] Update tools/stress/src/org/apache/cassandra/stress/Stress.java Co-authored-by: Dmitry Kropachev --- tools/stress/src/org/apache/cassandra/stress/Stress.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/stress/src/org/apache/cassandra/stress/Stress.java b/tools/stress/src/org/apache/cassandra/stress/Stress.java index 8050cc98c4..87f7ebc1a3 100644 --- a/tools/stress/src/org/apache/cassandra/stress/Stress.java +++ b/tools/stress/src/org/apache/cassandra/stress/Stress.java @@ -231,5 +231,7 @@ private static void registerSignalHandler() { Signal.handle(new Signal("ABRT"), handler); Signal.handle(new Signal("TERM"), handler); Signal.handle(new Signal("INT"), handler); + Signal.handle(new Signal("TERM"), handler); + Signal.handle(new Signal("INT"), handler); } } From 211f2d4809685b490b694e22e1b74172199ca3c1 Mon Sep 17 00:00:00 2001 From: Dusan Malusev Date: Mon, 21 Oct 2024 17:03:07 +0200 Subject: [PATCH 3/3] Stress.java --- tools/stress/src/org/apache/cassandra/stress/Stress.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/stress/src/org/apache/cassandra/stress/Stress.java b/tools/stress/src/org/apache/cassandra/stress/Stress.java index 87f7ebc1a3..8050cc98c4 100644 --- a/tools/stress/src/org/apache/cassandra/stress/Stress.java +++ b/tools/stress/src/org/apache/cassandra/stress/Stress.java @@ -231,7 +231,5 @@ private static void registerSignalHandler() { Signal.handle(new Signal("ABRT"), handler); Signal.handle(new Signal("TERM"), handler); Signal.handle(new Signal("INT"), handler); - Signal.handle(new Signal("TERM"), handler); - Signal.handle(new Signal("INT"), handler); } }