Skip to content

Commit eeb6f13

Browse files
Add async-profiler to dumper executions
1 parent 39bda42 commit eeb6f13

File tree

5 files changed

+78
-20
lines changed

5 files changed

+78
-20
lines changed

dumper/app/build.gradle

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ dependencies {
128128
implementation libs.logback.classic
129129
implementation libs.jcl.over.slf4j
130130

131+
implementation libs.async.profiler
132+
131133
runtimeOnly libs.postgresql
132134
runtimeOnly libs.snowflake.jdbc
133135
runtimeOnly libs.redshift.jdbc
@@ -241,23 +243,23 @@ tasks.named('distZip') {
241243
}
242244

243245
tasks.register('generateSourceMirror', Copy) {
244-
dependsOn 'compileJava'
245-
from {
246-
dependencies.createArtifactResolutionQuery()
247-
.forComponents(
248-
configurations.runtimeClasspath.incoming.resolutionResult
249-
.allDependencies.collect { it.selected.id }
250-
)
251-
.withArtifacts(JvmLibrary, SourcesArtifact)
252-
.execute()
253-
.resolvedComponents
254-
.collectMany {
255-
it.artifactResults
256-
.collect { it.file.path }
257-
}
258-
}
259-
into layout.buildDirectory.dir('mirror/sources')
260-
outputs.dir "mirror/sources"
246+
// dependsOn 'compileJava'
247+
// from {
248+
// dependencies.createArtifactResolutionQuery()
249+
// .forComponents(
250+
// configurations.runtimeClasspath.incoming.resolutionResult
251+
// .allDependencies.collect { it.selected.id }
252+
// )
253+
// .withArtifacts(JvmLibrary, SourcesArtifact)
254+
// .execute()
255+
// .resolvedComponents
256+
// .collectMany {
257+
// it.artifactResults
258+
// .collect { it.file.path }
259+
// }
260+
// }
261+
// into layout.buildDirectory.dir('mirror/sources')
262+
// outputs.dir "mirror/sources"
261263
}
262264

263265
tasks.register('copyGceLauncher', Copy) {

dumper/app/gradle.lockfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
ch.qos.logback:logback-classic:1.3.16=compileClasspath,runtimeClasspath,sources,testCompileClasspath,testFixturesRuntimeClasspath,testRuntimeClasspath
55
ch.qos.logback:logback-core:1.3.16=compileClasspath,runtimeClasspath,testCompileClasspath,testFixturesRuntimeClasspath,testRuntimeClasspath
66
com.amazon.redshift:redshift-jdbc42:2.1.0.32=runtimeClasspath,testFixturesRuntimeClasspath,testRuntimeClasspath
7+
tools.profiler:async-profiler:4.2=compileClasspath,runtimeClasspath,testCompileClasspath,testFixturesRuntimeClasspath,testRuntimeClasspath
78
com.amazonaws:aws-java-sdk-cloudwatch:1.12.791=compileClasspath,runtimeClasspath,testCompileClasspath,testFixturesRuntimeClasspath,testRuntimeClasspath
89
com.amazonaws:aws-java-sdk-core:1.12.791=compileClasspath,runtimeClasspath,testCompileClasspath,testFixturesRuntimeClasspath,testRuntimeClasspath
910
com.amazonaws:aws-java-sdk-redshift:1.12.791=compileClasspath,runtimeClasspath,testCompileClasspath,testFixturesRuntimeClasspath,testRuntimeClasspath

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/Main.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020

2121
import com.google.common.base.Throwables;
2222
import com.google.common.collect.ImmutableList;
23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.net.URI;
26+
import java.nio.file.*;
27+
import java.util.Collections;
28+
import java.util.Map;
2329
import java.util.Objects;
30+
import one.profiler.AsyncProfiler;
2431
import org.slf4j.Logger;
2532
import org.slf4j.LoggerFactory;
2633

@@ -51,13 +58,21 @@ private static void printErrorMessages(Throwable e) {
5158

5259
public static void main(String... args) throws Exception {
5360
try {
61+
AsyncProfiler asyncProfiler = AsyncProfiler.getInstance();
62+
asyncProfiler.execute("start,event=cpu,interval=10ms");
63+
5464
StartUpMetaInfoProcessor.printMetaInfo();
5565

5666
if (args.length == 0) {
5767
args = new String[] {"--help"};
5868
}
5969

60-
MetadataDumper metadataDumper = new MetadataDumper(args);
70+
MetadataDumper metadataDumper =
71+
new MetadataDumper(
72+
(outputFileLocation) -> {
73+
stopProfileAndAppendToZip(asyncProfiler, outputFileLocation);
74+
},
75+
args);
6176

6277
if (!metadataDumper.run()) {
6378
System.exit(1);
@@ -74,4 +89,29 @@ public static void main(String... args) throws Exception {
7489
System.exit(1);
7590
}
7691
}
92+
93+
private static void stopProfileAndAppendToZip(
94+
AsyncProfiler asyncProfiler, String outputFileLocation) {
95+
try {
96+
File tempFlameGraph = File.createTempFile("flamegraph", ".html");
97+
String stopCommand = "stop,output=flamegraph,file=" + tempFlameGraph.getAbsolutePath();
98+
asyncProfiler.execute(stopCommand);
99+
100+
moveFileToZip(outputFileLocation, tempFlameGraph, "flamegraph.html");
101+
} catch (Exception ignored) {
102+
}
103+
}
104+
105+
private static void moveFileToZip(String zipFile, File file, String entryName)
106+
throws IOException {
107+
Map<String, String> env = Collections.singletonMap("create", "false");
108+
URI zipUri = URI.create("jar:" + Paths.get(zipFile).toUri());
109+
110+
try (FileSystem zipFs = FileSystems.newFileSystem(zipUri, env)) {
111+
Path pathInZip = zipFs.getPath(entryName);
112+
113+
Files.copy(file.toPath(), pathInZip, StandardCopyOption.REPLACE_EXISTING);
114+
Files.deleteIfExists(file.toPath());
115+
}
116+
}
77117
}

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/MetadataDumper.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,24 @@ public class MetadataDumper {
6161
private static final Pattern GCS_PATH_PATTERN =
6262
Pattern.compile("gs://(?<bucket>[^/]+)/(?<path>.*)");
6363

64-
private TelemetryProcessor telemetryProcessor;
65-
private ConnectorArguments connectorArguments;
64+
private final TelemetryProcessor telemetryProcessor;
65+
private final ConnectorArguments connectorArguments;
66+
private final ShutdownHook shutdownHook;
6667

6768
public MetadataDumper(String... args) throws Exception {
69+
this((zipPath) -> {}, args);
70+
}
71+
72+
public MetadataDumper(ShutdownHook shutdownHook, String... args) throws Exception {
6873
this.connectorArguments = new ConnectorArguments(JsonResponseFile.addResponseFiles(args));
6974
telemetryProcessor =
7075
new TelemetryProcessor(
7176
TelemetryStrategyFactory.createStrategy(connectorArguments.isTelemetryOn()));
7277
if (connectorArguments.saveResponseFile()) {
7378
JsonResponseFile.save(connectorArguments);
7479
}
80+
81+
this.shutdownHook = shutdownHook;
7582
}
7683

7784
public boolean run() throws Exception {
@@ -158,6 +165,8 @@ protected boolean run(@Nonnull Connector connector) throws Exception {
158165
connectorArguments, state, stopwatch, requiredTaskSucceeded);
159166
telemetryProcessor.processTelemetry(fileSystem);
160167
} finally {
168+
shutdownHook.shutdown(outputFileLocation);
169+
161170
// We must do this in finally after the ZipFileSystem has been closed.
162171
File outputFile = new File(outputFileLocation);
163172
if (outputFile.isFile()) {
@@ -301,4 +310,9 @@ private void logFinalSummary(
301310
linePrinter.println("Dumper execution: " + stateToPrint);
302311
});
303312
}
313+
314+
@FunctionalInterface
315+
public interface ShutdownHook {
316+
void shutdown(String outputzip);
317+
}
304318
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ zookeeper = "3.9.4"
8181
common-configuration2 = "2.12.0"
8282

8383
[libraries]
84+
async-profiler = {module = "tools.profiler:async-profiler", version="4.2"}
8485
apache-avro = { module = "org.apache.avro:avro", version.ref = "apache-avro" }
8586
apache-commons-compress = { module = "org.apache.commons:commons-compress", version.ref = "apache-commons-compress" }
8687
apache-commons-csv = { module = "org.apache.commons:commons-csv", version.ref = "apache-commons-csv" }

0 commit comments

Comments
 (0)