Skip to content

Commit 13e7ca4

Browse files
Merge pull request #2126 from warunalakshitha/fix_hang
Modify dump logic
2 parents 8fa7eaa + 720dd3a commit 13e7ca4

File tree

11 files changed

+40
-38
lines changed

11 files changed

+40
-38
lines changed

ballerina-tests/graphql-advanced-test-suite/Dependencies.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[ballerina]
77
dependencies-toml-version = "2"
8-
distribution-version = "2201.11.0-20241204-121300-fc33b755"
8+
distribution-version = "2201.11.0-20241209-162400-0c015833"
99

1010
[[package]]
1111
org = "ballerina"

ballerina-tests/graphql-client-test-suite/Dependencies.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[ballerina]
77
dependencies-toml-version = "2"
8-
distribution-version = "2201.11.0-20241204-121300-fc33b755"
8+
distribution-version = "2201.11.0-20241209-162400-0c015833"
99

1010
[[package]]
1111
org = "ballerina"

ballerina-tests/graphql-dataloader-test-suite/Dependencies.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[ballerina]
77
dependencies-toml-version = "2"
8-
distribution-version = "2201.11.0-20241204-121300-fc33b755"
8+
distribution-version = "2201.11.0-20241209-162400-0c015833"
99

1010
[[package]]
1111
org = "ballerina"

ballerina-tests/graphql-interceptor-test-suite/Dependencies.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[ballerina]
77
dependencies-toml-version = "2"
8-
distribution-version = "2201.11.0-20241204-121300-fc33b755"
8+
distribution-version = "2201.11.0-20241209-162400-0c015833"
99

1010
[[package]]
1111
org = "ballerina"

ballerina-tests/graphql-security-test-suite/Dependencies.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[ballerina]
77
dependencies-toml-version = "2"
8-
distribution-version = "2201.11.0-20241204-121300-fc33b755"
8+
distribution-version = "2201.11.0-20241209-162400-0c015833"
99

1010
[[package]]
1111
org = "ballerina"

ballerina-tests/graphql-service-test-suite/Dependencies.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[ballerina]
77
dependencies-toml-version = "2"
8-
distribution-version = "2201.11.0-20241204-121300-fc33b755"
8+
distribution-version = "2201.11.0-20241209-162400-0c015833"
99

1010
[[package]]
1111
org = "ballerina"

ballerina-tests/graphql-subgraph-test-suite/Dependencies.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[ballerina]
77
dependencies-toml-version = "2"
8-
distribution-version = "2201.11.0-20241204-121300-fc33b755"
8+
distribution-version = "2201.11.0-20241209-162400-0c015833"
99

1010
[[package]]
1111
org = "ballerina"

ballerina-tests/graphql-subscription-test-suite/Dependencies.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[ballerina]
77
dependencies-toml-version = "2"
8-
distribution-version = "2201.11.0-20241204-121300-fc33b755"
8+
distribution-version = "2201.11.0-20241209-162400-0c015833"
99

1010
[[package]]
1111
org = "ballerina"

ballerina-tests/graphql-test-common/Dependencies.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[ballerina]
77
dependencies-toml-version = "2"
8-
distribution-version = "2201.11.0-20241204-121300-fc33b755"
8+
distribution-version = "2201.11.0-20241209-162400-0c015833"
99

1010
[[package]]
1111
org = "ballerina"

native/src/main/java/io/ballerina/stdlib/graphql/runtime/utils/Utils.java

+30-29
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package io.ballerina.stdlib.graphql.runtime.utils;
2020

21+
import com.sun.management.HotSpotDiagnosticMXBean;
2122
import io.ballerina.runtime.api.creators.ErrorCreator;
2223
import io.ballerina.runtime.api.types.ArrayType;
2324
import io.ballerina.runtime.api.types.Type;
@@ -27,10 +28,15 @@
2728
import io.ballerina.runtime.api.values.BObject;
2829
import io.ballerina.runtime.api.values.BString;
2930

30-
import java.io.BufferedReader;
31-
import java.io.InputStreamReader;
31+
import java.io.File;
32+
import java.io.IOException;
3233
import java.io.PrintStream;
3334
import java.lang.management.ManagementFactory;
35+
import java.nio.file.Files;
36+
import java.nio.file.Paths;
37+
import java.time.LocalDateTime;
38+
39+
import javax.management.MBeanServer;
3440

3541
import static io.ballerina.stdlib.graphql.runtime.utils.ModuleUtils.getModule;
3642

@@ -45,15 +51,19 @@ private Utils() {
4551
Thread.startVirtualThread(() -> {
4652
try {
4753
Thread.sleep(5 * 60 * 1000);
48-
dumpThreads();
49-
} catch (InterruptedException e) {
54+
getStrandDump();
55+
} catch (InterruptedException | IOException e) {
5056
throw new RuntimeException(e);
5157
}
5258
});
5359
}
5460

5561
public static final PrintStream OUT = System.out;
5662
public static final PrintStream ERROR = System.err;
63+
private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
64+
private static final String WORKING_DIR = System.getProperty("user.dir") + "/";
65+
private static final String FILENAME = "threadDump" + LocalDateTime.now();
66+
private static volatile HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean;
5767

5868
// Inter-op function names
5969
private static final String EXECUTE_RESOURCE_FUNCTION = "executeQueryResource";
@@ -125,32 +135,23 @@ public static void handleBErrorAndExit(BError bError) {
125135
System.exit(1);
126136
}
127137

128-
public static void dumpThreads() {
129-
try {
130-
// Get the current process ID (PID)
131-
String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
132-
133-
// Build the jcmd command
134-
String command = "jcmd " + pid;
135-
136-
// Execute the jcmd command
137-
Process process = Runtime.getRuntime().exec(command);
138-
139-
// Read the output of the command
140-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
141-
String line;
142-
while ((line = reader.readLine()) != null) {
143-
OUT.println(line);
144-
}
145-
}
138+
public static void getStrandDump() throws IOException {
139+
getStrandDump(WORKING_DIR + FILENAME);
140+
String dump = new String(Files.readAllBytes(Paths.get(FILENAME)));
141+
File fileObj = new File(FILENAME);
142+
fileObj.delete();
143+
OUT.println(dump);
144+
}
146145

147-
// Wait for the process to complete
148-
int exitCode = process.waitFor();
149-
if (exitCode != 0) {
150-
ERROR.println("jcmd command failed with exit code: " + exitCode);
151-
}
152-
} catch (Exception error) {
153-
error.printStackTrace();
146+
private static void getStrandDump(String fileName) throws IOException {
147+
if (hotSpotDiagnosticMXBean == null) {
148+
hotSpotDiagnosticMXBean = getHotSpotDiagnosticMXBean();
154149
}
150+
hotSpotDiagnosticMXBean.dumpThreads(fileName, HotSpotDiagnosticMXBean.ThreadDumpFormat.TEXT_PLAIN);
151+
}
152+
153+
private static HotSpotDiagnosticMXBean getHotSpotDiagnosticMXBean() throws IOException {
154+
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
155+
return ManagementFactory.newPlatformMXBeanProxy(mBeanServer, HOT_SPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
155156
}
156157
}

native/src/main/java/module-info.java

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
requires io.ballerina.lang;
2424
requires jdk.jshell;
2525
requires java.management;
26+
requires jdk.management;
2627
}

0 commit comments

Comments
 (0)