abstractdog commented on code in PR #299:
URL: https://github.com/apache/tez/pull/299#discussion_r1250827594


##########
tez-runtime-internals/src/main/java/org/apache/tez/runtime/TezThreadDumpHelper.java:
##########
@@ -0,0 +1,142 @@
+/**
+ * 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.tez.runtime;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FSDataOutputStream;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.util.Time;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+public class TezThreadDumpHelper {
+
+  private final long duration;
+  Path basePath;
+  FileSystem fs;
+
+  static private final ThreadMXBean threadBean = 
ManagementFactory.getThreadMXBean();
+  private ScheduledExecutorService periodicThreadDumpServiceExecutor;
+
+  public TezThreadDumpHelper( long duration, Path basePath, Configuration 
conf) throws IOException {
+    this.duration = duration;
+    this.basePath = basePath;
+    this.fs = basePath.getFileSystem(conf);
+  }
+
+  public void schedulePeriodicThreadDumpService(String dagName) {
+    periodicThreadDumpServiceExecutor = Executors.newScheduledThreadPool(1,
+        new 
ThreadFactoryBuilder().setDaemon(true).setNameFormat("PeriodicThreadDumpService{"
 + dagName + "} #%d").build());
+    Runnable threadDumpCollector = new ThreadDumpCollector(basePath, dagName, 
fs);
+    periodicThreadDumpServiceExecutor.schedule(threadDumpCollector, duration, 
TimeUnit.MILLISECONDS);
+  }
+
+  public void shutdownPeriodicThreadDumpService() {
+    if (periodicThreadDumpServiceExecutor != null) {
+      periodicThreadDumpServiceExecutor.shutdown();
+
+      try {
+        if (!periodicThreadDumpServiceExecutor.awaitTermination(100, 
TimeUnit.MILLISECONDS)) {
+          periodicThreadDumpServiceExecutor.shutdownNow();
+        }
+      } catch (InterruptedException ignored) {
+        // Ignore interrupt, will attempt a final shutdown below.
+      }
+      periodicThreadDumpServiceExecutor.shutdownNow();
+      periodicThreadDumpServiceExecutor = null;
+    }
+  }
+
+  private static class ThreadDumpCollector implements Runnable {
+
+    Path path;
+
+    String dagName;
+    FileSystem fs;
+
+    ThreadDumpCollector(Path path, String dagName, FileSystem fs) {
+      this.path = path;
+      this.fs = fs;
+      this.dagName = dagName;
+    }
+
+    @Override
+    public void run() {
+      if (!Thread.interrupted()) {
+        try (FSDataOutputStream fsStream = fs.create(
+            new Path(path, dagName + "_" + Time.monotonicNow() + ".jstack"));

Review Comment:
   can you use other implementation than monotonicNow? this adds timestamps 
like "333*" which is hard to read for humans, I would expect ms since epoch 
like "1688387810605", I think the currentTimeMillis is more than enough here 
(we don't need extra safety that monotonicNow brings when it comes to interval 
calculation)



-- 
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: issues-unsubscr...@tez.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to