Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature][REST-API] Add threaddump rest api #7615

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,27 @@ public void testEncryptConfig() {
});
}

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
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> {

Expand Down Expand Up @@ -144,6 +145,8 @@ public void handle(HttpGetCommand httpGetCommand) {
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);
}
Expand Down Expand Up @@ -203,6 +206,26 @@ public void overView(HttpGetCommand command, String uri) {
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;
Expand Down
Loading