-
Notifications
You must be signed in to change notification settings - Fork 340
Fixed race condition on dumping future cleanup. #9607
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
53f8800
Debug dump cancellation logic.
AlexeyKuznetsov-DD 88ceeb6
Debug dump cancellation logic.
AlexeyKuznetsov-DD 623bfcf
Debug dump cancellation logic.
AlexeyKuznetsov-DD 421ef58
WIP 4.
AlexeyKuznetsov-DD ac925ba
WIP 5.
AlexeyKuznetsov-DD 03d94c5
WIP 6.
AlexeyKuznetsov-DD f517caf
Merge branch 'master' into alexeyk/debug-dump-logic
AlexeyKuznetsov-DD 8417a81
WIP 7.
AlexeyKuznetsov-DD f15d8b2
WIP 8.
AlexeyKuznetsov-DD 6806086
WIP 9.
AlexeyKuznetsov-DD 2aab088
Merge branch 'master' into alexeyk/debug-dump-logic
AlexeyKuznetsov-DD d6a1e75
Merge branch 'master' into alexeyk/debug-dump-logic
AlexeyKuznetsov-DD 383fc15
WIP 10.
AlexeyKuznetsov-DD 274c614
Merge branch 'master' into alexeyk/debug-dump-logic
AlexeyKuznetsov-DD 370d08e
Refactored to Kotlin plugin
AlexeyKuznetsov-DD 4132ac3
Merge branch 'master' into alexeyk/debug-dump-logic
AlexeyKuznetsov-DD cd4d8d3
Refactored to use Gradle lifecycle.
AlexeyKuznetsov-DD 424563b
Merge branch 'master' into alexeyk/debug-dump-logic
AlexeyKuznetsov-DD 059ce54
Applied review comments and covered with tests.
AlexeyKuznetsov-DD e1ce1f1
Fixed review notes.
AlexeyKuznetsov-DD 6a92882
Merge branch 'master' into alexeyk/debug-dump-logic
AlexeyKuznetsov-DD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,83 +1,92 @@ | ||
| import java.util.concurrent.Executors | ||
| import java.util.concurrent.TimeUnit | ||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| // Schedule thread and heap dumps collection near test timeout. | ||
| tasks.withType(Test).configureEach { testTask -> | ||
| doFirst { | ||
| def scheduler = Executors.newSingleThreadScheduledExecutor({ r -> | ||
| Thread t = new Thread(r, 'dump-scheduler') | ||
| t.daemon = true | ||
| t | ||
| }) | ||
| if (!project.ext.has('tasksDumps')) { | ||
| project.ext.tasksDumps = new ConcurrentHashMap<String, Timer>() | ||
| } | ||
|
|
||
| if (project.ext.tasksDumps.containsKey(testTask.path)) { | ||
| logger.warn("Taking dumps already scheduled: ${testTask.path}; skipping.") | ||
|
AlexeyKuznetsov-DD marked this conversation as resolved.
Outdated
|
||
| return | ||
| } | ||
|
|
||
| if (!testTask.timeout.present) { | ||
| logger.info("No timeout for ${testTask.path}; skipping dumps scheduler.") | ||
| return | ||
| } | ||
|
|
||
| // Calculate delay for taking dumps as test timeout minus 1 minutes, but no less than 1 minute. | ||
| def delayMinutes = Math.max(1L, timeout.get().minusMinutes(1).toMinutes()) | ||
| def delayMinutes = Math.max(1L, testTask.timeout.get().minusMinutes(1).toMinutes()) | ||
|
|
||
| def future = scheduler.schedule({ | ||
| logger.warn("Taking dumps for: ${testTask.getPath()} after ${delayMinutes} minutes.") | ||
| // Create timer backed by daemon thread. | ||
| def timer = new Timer(true) | ||
|
AlexeyKuznetsov-DD marked this conversation as resolved.
Outdated
|
||
| timer.schedule(new TimerTask() { | ||
| @Override | ||
| void run() { | ||
| logger.warn("Taking dumps for: ${testTask.path} after ${delayMinutes} minutes.") | ||
|
|
||
| try { | ||
| // Use Gradle's build dir and adjust for CI artifacts collection if needed. | ||
| def dumpsDir = layout.buildDirectory.dir('dumps').map { | ||
| if (providers.environmentVariable("CI").isPresent()) { | ||
| // Move reports into the folder collected by the collect_reports.sh script. | ||
| new File(it.getAsFile().absolutePath.replace('dd-trace-java/dd-java-agent', 'dd-trace-java/workspace/dd-java-agent')) | ||
| } else { | ||
| it.asFile | ||
| } | ||
| }.get() | ||
| try { | ||
| // Use Gradle's build dir and adjust for CI artifacts collection if needed. | ||
| def dumpsDir = layout.buildDirectory.dir('dumps').map { | ||
| if (providers.environmentVariable("CI").isPresent()) { | ||
| // Move reports into the folder collected by the collect_reports.sh script. | ||
| new File(it.asFile.absolutePath.replace('dd-trace-java/dd-java-agent', 'dd-trace-java/workspace/dd-java-agent')) | ||
| } else { | ||
| it.asFile | ||
| } | ||
| }.get() | ||
|
|
||
| dumpsDir.mkdirs() | ||
| dumpsDir.mkdirs() | ||
|
|
||
| // For simplicity, use `0` as the PID, which collects all thread dumps across JVMs. | ||
| // Single file can be useful for quick search. | ||
| def threadDumpsFile = new File(dumpsDir, "all-thread-dumps-${System.currentTimeMillis()}.log") | ||
| new ProcessBuilder("jcmd", "0", "Thread.print", "-l") | ||
| .redirectErrorStream(true) | ||
| .redirectOutput(threadDumpsFile) | ||
| .start().waitFor() | ||
| // For simplicity, use `0` as the PID, which collects all thread dumps across JVMs. | ||
| // Single file can be useful for quick search. | ||
| def threadDumpsFile = new File(dumpsDir, "all-thread-dumps-${System.currentTimeMillis()}.log") | ||
| new ProcessBuilder("jcmd", "0", "Thread.print", "-l") | ||
| .redirectErrorStream(true) | ||
| .redirectOutput(threadDumpsFile) | ||
| .start().waitFor() | ||
|
|
||
| // Collect PIDs of all Java processes. | ||
| def jvmProcesses = 'jcmd -l'.execute().text.readLines() | ||
| // Collect PIDs of all Java processes. | ||
| def jvmProcesses = 'jcmd -l'.execute().text.readLines() | ||
|
|
||
| // Collect pids for 'Gradle test executors'. | ||
| def pids = jvmProcesses | ||
| .findAll({ it.contains('Gradle Test Executor') }) | ||
| .collect({ it.substring(0, it.indexOf(' ')) }) | ||
| // Collect pids for 'Gradle test executors'. | ||
| def pids = jvmProcesses | ||
| .findAll({ it.contains('Gradle Test Executor') }) | ||
| .collect({ it.substring(0, it.indexOf(' ')) }) | ||
|
|
||
| pids.each { pid -> | ||
| // Collect heap dump by pid. | ||
| def heapDumpFile = new File(dumpsDir, "${pid}-heap-dump-${System.currentTimeMillis()}.hprof").absolutePath | ||
| def cmd = "jcmd ${pid} GC.heap_dump ${heapDumpFile}" | ||
| cmd.execute().waitFor() | ||
| pids.each { pid -> | ||
| // Collect heap dump by pid. | ||
| def heapDumpFile = new File(dumpsDir, "${pid}-heap-dump-${System.currentTimeMillis()}.hprof").absolutePath | ||
| def cmd = "jcmd ${pid} GC.heap_dump ${heapDumpFile}" | ||
| cmd.execute().waitFor() | ||
|
|
||
| // Collect thread dump by pid. | ||
| def threadDumpFile = new File(dumpsDir, "${pid}-thread-dump-${System.currentTimeMillis()}.log") | ||
| new ProcessBuilder('jcmd', pid, 'Thread.print', '-l') | ||
| .redirectErrorStream(true) | ||
| .redirectOutput(threadDumpFile) | ||
| .start() | ||
| .waitFor() | ||
| // Collect thread dump by pid. | ||
| def threadDumpFile = new File(dumpsDir, "${pid}-thread-dump-${System.currentTimeMillis()}.log") | ||
| new ProcessBuilder('jcmd', pid, 'Thread.print', '-l') | ||
| .redirectErrorStream(true) | ||
| .redirectOutput(threadDumpFile) | ||
| .start() | ||
| .waitFor() | ||
| } | ||
| } catch (Throwable e) { | ||
| logger.warn("Dumping failed: ${e.message}") | ||
| } | ||
| } | ||
| } catch (Throwable e) { | ||
| logger.warn("Dumping failed: ${e.message}") | ||
| } | ||
| finally { | ||
| scheduler.shutdown() | ||
| } | ||
| }, delayMinutes, TimeUnit.MINUTES) | ||
| }, delayMinutes * 60_000) | ||
|
|
||
| // Store handles for cancellation in doLast. | ||
| testTask.ext.dumpFuture = future | ||
| testTask.ext.dumpScheduler = scheduler | ||
| // Store timer for cancellation in doLast. | ||
| project.ext.tasksDumps.put(testTask.path, timer) | ||
| } | ||
|
|
||
| doLast { | ||
| // Cancel if the task finished before the scheduled dump. | ||
| // Cancel if the test finished before scheduled dump. | ||
| try { | ||
| testTask.ext.dumpFuture?.cancel(false) | ||
| } finally { | ||
| testTask.ext.dumpScheduler?.shutdownNow() | ||
| def timer = project.ext.tasksDumps.remove(testTask.path) | ||
| timer?.cancel() | ||
| } catch (Throwable e) { | ||
| logger.warn("Failed to cancel dump future for ${testTask.path}: ${e.message}") | ||
| } | ||
|
AlexeyKuznetsov-DD marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.