lukecwik commented on a change in pull request #17151: URL: https://github.com/apache/beam/pull/17151#discussion_r837957687
########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/status/JfrzServlet.java ########## @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.runners.dataflow.worker.status; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.beam.runners.dataflow.worker.util.MemoryMonitor; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.ByteStreams; + +/** + * Respond to /jfrz with a page allowing downloading of the heap dumps. Review comment: ```suggestion * Respond to /jfrz with a page allowing downloading of the JFR profiles. ``` ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/status/JfrzServlet.java ########## @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.runners.dataflow.worker.status; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.beam.runners.dataflow.worker.util.MemoryMonitor; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.ByteStreams; + +/** + * Respond to /jfrz with a page allowing downloading of the heap dumps. + * + * <p><b>Not actually serializable</b>. Its superclass is serializable but this subclass is not. + */ +@SuppressFBWarnings("SE_BAD_FIELD") // not serializable +public class JfrzServlet extends BaseStatusServlet { + + private final MemoryMonitor memoryMonitor; + + public JfrzServlet(MemoryMonitor memoryMonitor) { + super("jfrz"); + this.memoryMonitor = memoryMonitor; + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + String durationStr = req.getParameter("duration"); + Duration duration; + if (durationStr == null) { + duration = Duration.ofSeconds(60); + } else { + duration = Duration.parse(durationStr); + } + + ServletOutputStream writer = resp.getOutputStream(); + InputStream jfrStream; + try { + jfrStream = memoryMonitor.runJfrProfile(duration).get(); + } catch (Exception e) { + resp.setContentType("text/html;charset=utf-8"); + resp.setStatus(HttpServletResponse.SC_OK); + + writer.println("<html>\nFailed run JFR profile: <br>\n<pre>"); Review comment: ```suggestion writer.println("<html>\nFailed to run JFR profile: <br>\n<pre>"); ``` ########## File path: runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/util/MemoryMonitorTest.java ########## @@ -137,9 +140,33 @@ public void uploadToGcs() throws Exception { assertThat(files[0].getAbsolutePath(), Matchers.containsString("hprof")); } + @Test + public void uploadJfrProfilesOnThrashing() throws Exception { + if (Environments.getJavaVersion() != Environments.JavaVersion.java8) { + File remoteFolder = tempFolder.newFolder(); + monitor = + MemoryMonitor.forTest( + provider, + 10, + 0, + true, + 50.0, + remoteFolder.getPath(), + localDumpFolder, + Duration.ofMillis(100)); + + monitor.runJfrProfileOnHeapThrashing().get(); + + File[] files = remoteFolder.listFiles(); + assertThat(files, Matchers.arrayWithSize(1)); + assertThat(files[0].getAbsolutePath(), Matchers.containsString("jfr")); + assertThat(files[0].getAbsolutePath(), Matchers.containsString(".jfr")); Review comment: Consider using http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/core/StringEndsWith.html#endsWith(java.lang.String) ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/util/MemoryMonitor.java ########## @@ -515,6 +562,10 @@ public void run() { if (isThrashing.get()) { currentThrashingCount++; + if (currentThrashingCount == 1) { + runJfrProfileOnHeapThrashing(); + } + Review comment: ```suggestion if (currentThrashingCount == 1) { runJfrProfileOnHeapThrashing(); } ``` ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/util/MemoryMonitor.java ########## @@ -588,6 +639,61 @@ public File dumpHeap() return dumpHeap(localDumpFolder); } + /** + * Run a JFR profile, returning the profile data as an InputStream when complete. + * + * @param duration The amount of time to record for. + */ + public CompletableFuture<InputStream> runJfrProfile(Duration duration) { + if (jfrInterop == null) { + throw new IllegalStateException("unable to run JFR profile because JFR was not enabled"); Review comment: ```suggestion throw new IllegalStateException("Unable to run JFR profile because JFR was not enabled. You must use Java 9+ to enable JFR."); ``` ########## File path: runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/util/MemoryMonitorTest.java ########## @@ -137,9 +140,33 @@ public void uploadToGcs() throws Exception { assertThat(files[0].getAbsolutePath(), Matchers.containsString("hprof")); } + @Test + public void uploadJfrProfilesOnThrashing() throws Exception { + if (Environments.getJavaVersion() != Environments.JavaVersion.java8) { Review comment: Use Assume#assumeFalse https://junit.org/junit4/javadoc/4.12/org/junit/Assume.html#assumeFalse(boolean) instead of an if. ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/status/JfrzServlet.java ########## @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.runners.dataflow.worker.status; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.beam.runners.dataflow.worker.util.MemoryMonitor; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.ByteStreams; + +/** + * Respond to /jfrz with a page allowing downloading of the heap dumps. + * + * <p><b>Not actually serializable</b>. Its superclass is serializable but this subclass is not. + */ +@SuppressFBWarnings("SE_BAD_FIELD") // not serializable +public class JfrzServlet extends BaseStatusServlet { + + private final MemoryMonitor memoryMonitor; + + public JfrzServlet(MemoryMonitor memoryMonitor) { + super("jfrz"); + this.memoryMonitor = memoryMonitor; + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + String durationStr = req.getParameter("duration"); + Duration duration; + if (durationStr == null) { + duration = Duration.ofSeconds(60); Review comment: Would it make sense to use the PipelineOption? ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/status/JfrzServlet.java ########## @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.runners.dataflow.worker.status; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.beam.runners.dataflow.worker.util.MemoryMonitor; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.ByteStreams; + +/** + * Respond to /jfrz with a page allowing downloading of the heap dumps. + * + * <p><b>Not actually serializable</b>. Its superclass is serializable but this subclass is not. + */ +@SuppressFBWarnings("SE_BAD_FIELD") // not serializable +public class JfrzServlet extends BaseStatusServlet { + + private final MemoryMonitor memoryMonitor; + + public JfrzServlet(MemoryMonitor memoryMonitor) { + super("jfrz"); + this.memoryMonitor = memoryMonitor; + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + String durationStr = req.getParameter("duration"); + Duration duration; + if (durationStr == null) { + duration = Duration.ofSeconds(60); + } else { + duration = Duration.parse(durationStr); + } + + ServletOutputStream writer = resp.getOutputStream(); + InputStream jfrStream; + try { + jfrStream = memoryMonitor.runJfrProfile(duration).get(); + } catch (Exception e) { + resp.setContentType("text/html;charset=utf-8"); Review comment: Why do we want to return OK? We could let the exception propagate and we'll get the default http 500 page with a stack trace I believe. This will also be the default when Duration parsing or ByteStreams.copy throw an exception. ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/status/JfrzServlet.java ########## @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.runners.dataflow.worker.status; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.beam.runners.dataflow.worker.util.MemoryMonitor; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.ByteStreams; + +/** + * Respond to /jfrz with a page allowing downloading of the heap dumps. + * + * <p><b>Not actually serializable</b>. Its superclass is serializable but this subclass is not. + */ +@SuppressFBWarnings("SE_BAD_FIELD") // not serializable +public class JfrzServlet extends BaseStatusServlet { + + private final MemoryMonitor memoryMonitor; + + public JfrzServlet(MemoryMonitor memoryMonitor) { + super("jfrz"); + this.memoryMonitor = memoryMonitor; + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + String durationStr = req.getParameter("duration"); + Duration duration; + if (durationStr == null) { + duration = Duration.ofSeconds(60); + } else { + duration = Duration.parse(durationStr); Review comment: If this throws a DateTimeParseException, we should return a 400 or we can let it propagate up as a 500 error. ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/util/MemoryMonitor.java ########## @@ -310,7 +350,8 @@ boolean tryUploadHeapDumpIfItExists() { if (localSource.exists()) { LOG.warn("Heap dump {} detected, attempting to upload to GCS", localSource); String remoteDest = - String.format("%s/heap_dump%s.hprof", uploadToGCSPath, UUID.randomUUID().toString()); + String.format( + "%s/heap_dump-%s-%s.hprof", uploadToGCSPath, this.workerId, UUID.randomUUID()); Review comment: nice, Add date and time as well? ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/util/JfrInterop.java ########## @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.runners.dataflow.worker.util; + +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.time.Duration; +import java.time.Instant; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Exposes methods to interop with JFR. This is only supported on java 9 and up, java 8 does not + * include JFR and will throw an exception when instantiated. + * + * <p>Note, since beam needs to compile against java 8 still, we can't directly bind to JFR classes + * at compile time. Instead, we need to use reflection to create/invoke JFR methods. + */ +class JfrInterop { + // ensure only a single JFR profile is running at once + private static final ExecutorService JFR_EXECUTOR = Executors.newSingleThreadExecutor(); + + private final Constructor<?> recordingCtor; + private final Method recordingStart; + private final Method recordingStop; + private final Method recordingGetStream; + private final Method recordingClose; + private final Object jfrConfig; + + @SuppressWarnings("nullness") + JfrInterop() { + try { + Class<?> configClass = Class.forName("jdk.jfr.Configuration"); Review comment: There is a way to use gradle to compile this using Java 9 compiler and package it into the jar but likely not worth the effort. ########## File path: runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/util/MemoryMonitorTest.java ########## @@ -137,9 +140,33 @@ public void uploadToGcs() throws Exception { assertThat(files[0].getAbsolutePath(), Matchers.containsString("hprof")); } + @Test + public void uploadJfrProfilesOnThrashing() throws Exception { + if (Environments.getJavaVersion() != Environments.JavaVersion.java8) { + File remoteFolder = tempFolder.newFolder(); + monitor = + MemoryMonitor.forTest( + provider, + 10, + 0, + true, + 50.0, + remoteFolder.getPath(), + localDumpFolder, + Duration.ofMillis(100)); + + monitor.runJfrProfileOnHeapThrashing().get(); + + File[] files = remoteFolder.listFiles(); + assertThat(files, Matchers.arrayWithSize(1)); + assertThat(files[0].getAbsolutePath(), Matchers.containsString("jfr")); Review comment: ```suggestion ``` ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/util/MemoryMonitor.java ########## @@ -222,15 +248,18 @@ static MemoryMonitor forTest( boolean canDumpHeap, double gcThrashingPercentagePerPeriod, @Nullable String uploadToGCSPath, - File localDumpFolder) { + File localDumpFolder, + @Nullable Duration jfrProfileDuration) { return new MemoryMonitor( gcStatsProvider, sleepTimeMillis, shutDownAfterNumGCThrashing, canDumpHeap, gcThrashingPercentagePerPeriod, uploadToGCSPath, - localDumpFolder); + localDumpFolder, + "test-worker", Review comment: You might as well allow for this parameter to be passed in and then you can also validate that the filename contains the worker id. ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/util/JfrInterop.java ########## @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.runners.dataflow.worker.util; + +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.time.Duration; +import java.time.Instant; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Exposes methods to interop with JFR. This is only supported on java 9 and up, java 8 does not + * include JFR and will throw an exception when instantiated. + * + * <p>Note, since beam needs to compile against java 8 still, we can't directly bind to JFR classes Review comment: ```suggestion * <p>Note, since Apache Beam needs to compile against Java 8, we can't directly bind to JFR classes. ``` ########## File path: runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/util/MemoryMonitor.java ########## @@ -198,20 +208,36 @@ public long totalGCTimeMilliseconds() { private final File localDumpFolder; + private final String workerId; + + private final @Nullable JfrInterop jfrInterop; + public static MemoryMonitor fromOptions(PipelineOptions options) { DataflowPipelineDebugOptions debugOptions = options.as(DataflowPipelineDebugOptions.class); + DataflowWorkerHarnessOptions workerHarnessOptions = + options.as(DataflowWorkerHarnessOptions.class); String uploadToGCSPath = debugOptions.getSaveHeapDumpsToGcsPath(); + String workerId = workerHarnessOptions.getWorkerId(); boolean canDumpHeap = uploadToGCSPath != null || debugOptions.getDumpHeapOnOOM(); double gcThrashingPercentagePerPeriod = debugOptions.getGCThrashingPercentagePerPeriod(); + Duration jfrProfileDuration; + if (uploadToGCSPath != null && debugOptions.getRecordJfrOnGcThrashing()) { + jfrProfileDuration = Duration.ofSeconds(debugOptions.getJfrRecordingDurationSec()); Review comment: We should throw an exception here if we aren't using Java 9+ so that users learn quickly that it won't work especially if they think it is setup but won't actually work in practice. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
