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

wuchunfu pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/seatunnel.git


The following commit(s) were added to refs/heads/dev by this push:
     new 701bb3ee02 [Feature][REST-API] Add threaddump rest api (#7615)
701bb3ee02 is described below

commit 701bb3ee020b358b263c7272f6547aa7ba09a76d
Author: Guangdong Liu <[email protected]>
AuthorDate: Wed Sep 11 11:27:21 2024 +0800

    [Feature][REST-API] Add threaddump rest api (#7615)
    
    * add threaddump rest api
    
    * add threaddump rest api
    
    * add threaddump rest api
---
 .../org/apache/seatunnel/engine/e2e/RestApiIT.java | 21 ++++++++++++++++++++
 .../seatunnel/engine/server/rest/RestConstant.java |  1 +
 .../server/rest/RestHttpGetCommandProcessor.java   | 23 ++++++++++++++++++++++
 3 files changed, 45 insertions(+)

diff --git 
a/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/RestApiIT.java
 
b/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/RestApiIT.java
index 5e912e845f..23478da37c 100644
--- 
a/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/RestApiIT.java
+++ 
b/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/RestApiIT.java
@@ -454,6 +454,27 @@ public class RestApiIT {
                         });
     }
 
+    @Test
+    public void testGetThreadDump() {
+        Arrays.asList(node2, node1)
+                .forEach(
+                        instance -> {
+                            given().get(
+                                            HOST
+                                                    + instance.getCluster()
+                                                            .getLocalMember()
+                                                            .getAddress()
+                                                            .getPort()
+                                                    + RestConstant.THREAD_DUMP)
+                                    .then()
+                                    .statusCode(200)
+                                    .body("[0].threadName", notNullValue())
+                                    .body("[0].threadState", notNullValue())
+                                    .body("[0].stackTrace", notNullValue())
+                                    .body("[0].threadId", notNullValue());
+                        });
+    }
+
     @AfterEach
     void afterClass() {
         if (engineClient != null) {
diff --git 
a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/RestConstant.java
 
b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/RestConstant.java
index 949e174476..3cb3938bb4 100644
--- 
a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/RestConstant.java
+++ 
b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/RestConstant.java
@@ -51,6 +51,7 @@ public class RestConstant {
     public static final String JOB_INFO_URL = "/hazelcast/rest/maps/job-info";
     public static final String FINISHED_JOBS_INFO = 
"/hazelcast/rest/maps/finished-jobs";
     public static final String ENCRYPT_CONFIG = 
"/hazelcast/rest/maps/encrypt-config";
+    public static final String THREAD_DUMP = 
"/hazelcast/rest/maps/thread-dump";
 
     // only for test use
     public static final String RUNNING_THREADS = 
"/hazelcast/rest/maps/running-threads";
diff --git 
a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/RestHttpGetCommandProcessor.java
 
b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/RestHttpGetCommandProcessor.java
index baea0d06aa..997ab92cb5 100644
--- 
a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/RestHttpGetCommandProcessor.java
+++ 
b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/RestHttpGetCommandProcessor.java
@@ -93,6 +93,7 @@ import static 
org.apache.seatunnel.engine.server.rest.RestConstant.RUNNING_THREA
 import static 
org.apache.seatunnel.engine.server.rest.RestConstant.SYSTEM_MONITORING_INFORMATION;
 import static 
org.apache.seatunnel.engine.server.rest.RestConstant.TELEMETRY_METRICS_URL;
 import static 
org.apache.seatunnel.engine.server.rest.RestConstant.TELEMETRY_OPEN_METRICS_URL;
+import static org.apache.seatunnel.engine.server.rest.RestConstant.THREAD_DUMP;
 
 public class RestHttpGetCommandProcessor extends 
HttpCommandProcessor<HttpGetCommand> {
 
@@ -144,6 +145,8 @@ public class RestHttpGetCommandProcessor extends 
HttpCommandProcessor<HttpGetCom
                 handleMetrics(httpGetCommand, TextFormat.CONTENT_TYPE_004);
             } else if (uri.equals(TELEMETRY_OPEN_METRICS_URL)) {
                 handleMetrics(httpGetCommand, 
TextFormat.CONTENT_TYPE_OPENMETRICS_100);
+            } else if (uri.startsWith(THREAD_DUMP)) {
+                getThreadDump(httpGetCommand);
             } else {
                 original.handle(httpGetCommand);
             }
@@ -203,6 +206,26 @@ public class RestHttpGetCommandProcessor extends 
HttpCommandProcessor<HttpGetCom
                 
JsonUtil.toJsonObject(JsonUtils.toMap(JsonUtils.toJsonString(overviewInfo))));
     }
 
+    public void getThreadDump(HttpGetCommand command) {
+        Map<Thread, StackTraceElement[]> threadStacks = 
Thread.getAllStackTraces();
+        JsonArray threadInfoList = new JsonArray();
+        for (Map.Entry<Thread, StackTraceElement[]> entry : 
threadStacks.entrySet()) {
+            StringBuilder stackTraceBuilder = new StringBuilder();
+            for (StackTraceElement element : entry.getValue()) {
+                stackTraceBuilder.append(element.toString()).append("\n");
+            }
+            String stackTrace = stackTraceBuilder.toString().trim();
+            JsonObject threadInfo = new JsonObject();
+            threadInfo.add("threadName", entry.getKey().getName());
+            threadInfo.add("threadId", entry.getKey().getId());
+            threadInfo.add("threadState", entry.getKey().getState().name());
+            threadInfo.add("stackTrace", stackTrace);
+            threadInfoList.add(threadInfo);
+        }
+
+        this.prepareResponse(command, threadInfoList);
+    }
+
     private void getSystemMonitoringInformation(HttpGetCommand command) {
         Cluster cluster = 
textCommandService.getNode().hazelcastInstance.getCluster();
         nodeEngine = 
textCommandService.getNode().hazelcastInstance.node.nodeEngine;

Reply via email to