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

Fix IndexOutOfBoundsException in Strand Dump Tool and refactor logic #43707

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -28,7 +28,9 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

Expand All @@ -45,10 +47,10 @@
private static final String WORKING_DIR = System.getProperty("user.dir") + "/";
private static final String FILENAME = "threadDump" + LocalDateTime.now();
private static final String VIRTUAL_THREAD_IDENTIFIER = "virtual";
private static final String ISOLATED_WORKER_IDENTIFIER = "io.ballerina.runtime.internal.scheduling." +
"Scheduler.lambda$startIsolatedWorker";
private static final String NON_ISOLATED_WORKER_IDENTIFIER = "io.ballerina.runtime.internal.scheduling." +
"Scheduler.lambda$startNonIsolatedWorker";
private static final String ISOLATED_IDENTIFIER = "io.ballerina.runtime.internal.scheduling." +
"Scheduler.lambda$startIsolated";
private static final String NON_ISOLATED_IDENTIFIER = "io.ballerina.runtime.internal.scheduling." +
"Scheduler.lambda$startNonIsolated";
private static final String JAVA_TRACE_PATTERN = "java\\.|\\.java(?::\\d+)?"; // .java, java., .java:(any number)
private static final String BAL_TRACE_PATTERN = "\\.bal:\\d+"; // .bal:(any number)
private static volatile HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean;
Expand All @@ -69,16 +71,16 @@
private static String generateOutput(String dump) {
String[] dumpItems = dump.split("\\n\\n");
int id = 0;
Set<Integer> isolatedWorkerList = new HashSet<>();
Set<Integer> nonIsolatedWorkerList = new HashSet<>();
ArrayList<ArrayList<String>> balTraces = new ArrayList<>();
Set<Integer> isolatedStrandList = new HashSet<>();
Set<Integer> nonIsolatedStrandList = new HashSet<>();
Map<Integer, ArrayList<String>> balTraces = new HashMap<>();
Pattern javaPattern = Pattern.compile(JAVA_TRACE_PATTERN);
Pattern balPattern = Pattern.compile(BAL_TRACE_PATTERN);
for (String item : dumpItems) {
String[] lines = item.split("\\n");
String[] subitems = lines[0].split("\" ");
ArrayList<String> balTraceItems = new ArrayList<>();
boolean balStrand = false;
boolean isBalStrand = false;
if (subitems.length > 1 && subitems[1].equals(VIRTUAL_THREAD_IDENTIFIER)) {
balTraceItems.add("\tStrand " + lines[0].replace(VIRTUAL_THREAD_IDENTIFIER, ":") + "\n\t\tat");
String prefix = " ";
Expand All @@ -87,21 +89,21 @@
balTraceItems.add(prefix + line + "\n");
prefix = "\t\t ";
if (balPattern.matcher(line).find()) {
balStrand = true;
isBalStrand = true;
}
} else {
if (line.contains(ISOLATED_WORKER_IDENTIFIER)) {
isolatedWorkerList.add(id);
} else if (line.contains(NON_ISOLATED_WORKER_IDENTIFIER)) {
nonIsolatedWorkerList.add(id);
if (line.contains(ISOLATED_IDENTIFIER)) {
isolatedStrandList.add(id);
} else if (line.contains(NON_ISOLATED_IDENTIFIER)) {
nonIsolatedStrandList.add(id);
}
}
}
if (balStrand) {
balTraces.add(balTraceItems);
if (isBalStrand) {
balTraces.put(id, balTraceItems);
} else {
isolatedWorkerList.remove(id);
nonIsolatedWorkerList.remove(id);
isolatedStrandList.remove(id);
nonIsolatedStrandList.remove(id);

Check warning on line 106 in bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/troubleshoot/StrandDump.java

View check run for this annotation

Codecov / codecov/patch

bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/troubleshoot/StrandDump.java#L105-L106

Added lines #L105 - L106 were not covered by tests
}
id++;
}
Expand All @@ -112,19 +114,23 @@
outputStr.append(dateTimeFormatter.format(localDateTime));
outputStr.append("]\n===============================================================\n\n");
outputStr.append("Total Strand count \t\t\t:\t").append(balTraces.size()).append("\n\n");
outputStr.append("Total Isolated Worker count \t\t:\t").append(isolatedWorkerList.size()).append("\n\n");
outputStr.append("Total Non Isolated Worker count \t\t:\t").append(nonIsolatedWorkerList.size()).
outputStr.append("Total Isolated Strand count \t\t:\t").append(isolatedStrandList.size()).append("\n\n");
outputStr.append("Total Non Isolated Strand count \t\t:\t").append(nonIsolatedStrandList.size()).
append("\n\n");
outputStr.append("================================================================\n");
outputStr.append("\nIsolated Workers:\n\n");
for (int strandId: isolatedWorkerList) {
balTraces.get(strandId).forEach(outputStr::append);
outputStr.append("\n");
outputStr.append("\nIsolated Strands:\n\n");
for (int strandId: isolatedStrandList) {
if (balTraces.containsKey(strandId)) {
balTraces.get(strandId).forEach(outputStr::append);
outputStr.append("\n");
}
}
outputStr.append("Non Isolated Workers:\n\n");
for (int strandId: nonIsolatedWorkerList) {
balTraces.get(strandId).forEach(outputStr::append);
outputStr.append("\n");
outputStr.append("Non Isolated Strands:\n\n");
for (int strandId: nonIsolatedStrandList) {
if (balTraces.containsKey(strandId)) {
balTraces.get(strandId).forEach(outputStr::append);
outputStr.append("\n");
}
}
return outputStr.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ Ballerina Strand Dump \[\d*/\d*/\d* \d*:\d*:\d*\]

Total Strand count \t\t\t:\t9

Total Isolated Worker count \t\t:\t2
Total Isolated Strand count \t\t:\t2

Total Non Isolated Worker count \t\t:\t7
Total Non Isolated Strand count \t\t:\t7

================================================================

Isolated Workers:
Isolated Strands:

\tStrand #\d* \"\w*\" :
\t\tat balProgram1.\$lambda\$_0\(balProgram1.bal:\d*\)
Expand All @@ -19,7 +19,7 @@ Isolated Workers:
\t\tat balProgram1.\$lambda\$_1\(balProgram1.bal:\d*\)
\t\t lambdas.\$_generated1balProgram1.\$lambda\$_1\$lambda1\$\(balProgram1.bal:\d*\)

Non Isolated Workers:
Non Isolated Strands:

\tStrand #\d* \"\w*\" :
\t\tat balProgram1.\$lambda\$_2\(balProgram1.bal:\d*\)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ Ballerina Strand Dump \[\d*/\d*/\d* \d*:\d*:\d*\]

Total Strand count \t\t\t:\t7

Total Isolated Worker count \t\t:\t0
Total Isolated Strand count \t\t:\t0

Total Non Isolated Worker count \t\t:\t7
Total Non Isolated Strand count \t\t:\t7

================================================================

Isolated Workers:
Isolated Strands:

Non Isolated Workers:
Non Isolated Strands:

\tStrand #\d* \"\w*\" :
\t\tat testOrg.testPackageWithModules.0.main.bar\(main.bal:\d*\)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ Ballerina Strand Dump \[\d*/\d*/\d* \d*:\d*:\d*\]

Total Strand count \t\t\t:\t17

Total Isolated Worker count \t\t:\t4
Total Isolated Strand count \t\t:\t4

Total Non Isolated Worker count \t\t:\t13
Total Non Isolated Strand count \t\t:\t13

================================================================

Isolated Workers:
Isolated Strands:

\tStrand #\d* \"\w*\" :
\t\tat ballerina.lang&0046runtime.0.runtime.sleep\(runtime.bal:\d*\)
Expand All @@ -32,7 +32,7 @@ Isolated Workers:
\t\t testOrg.testPackageWithModules&0046anotherutils.0.creators.\$_function_calls.call\(Unknown Source\)
\t\t testOrg.testPackageWithModules.0.lambdas.\$_generated1main.entryfunc\$lambda\$2\$\(main.bal:\d*\)

Non Isolated Workers:
Non Isolated Strands:

\tStrand #\d* \"\w*\" :
\t\tat testOrg.testPackageWithModules.0.main.\$lambda\$_4\(main.bal:\d*\)
Expand Down
Loading