abstractdog commented on code in PR #299: URL: https://github.com/apache/tez/pull/299#discussion_r1254027384
########## tez-runtime-internals/src/main/java/org/apache/tez/runtime/TezThreadDumpHelper.java: ########## @@ -0,0 +1,188 @@ +/** + * 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.log4j.Appender; +import org.apache.tez.common.TezContainerLogAppender; +import org.apache.tez.dag.api.TezConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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; + +import static org.apache.hadoop.yarn.conf.YarnConfiguration.DEFAULT_NM_REMOTE_APP_LOG_DIR; +import static org.apache.hadoop.yarn.conf.YarnConfiguration.NM_REMOTE_APP_LOG_DIR; +import static org.apache.tez.dag.api.TezConfiguration.TEZ_THREAD_DUMP_INTERVAL; +import static org.apache.tez.dag.api.TezConfiguration.TEZ_THREAD_DUMP_INTERVAL_DEFAULT; + +public final class TezThreadDumpHelper { + + private final long duration; + private final Path basePath; + private final FileSystem fs; + + private static final ThreadMXBean THREAD_BEAN = ManagementFactory.getThreadMXBean(); + private static final Logger LOG = LoggerFactory.getLogger(TezThreadDumpHelper.class); + + private ScheduledExecutorService periodicThreadDumpServiceExecutor; + + private TezThreadDumpHelper(long duration, Configuration conf) throws IOException { + this.duration = duration; + Appender appender = org.apache.log4j.Logger.getRootLogger().getAppender(TezConstants.TEZ_CONTAINER_LOGGER_NAME); + if (appender instanceof TezContainerLogAppender) { + this.basePath = new Path(((TezContainerLogAppender) appender).getContainerLogDir()); + this.fs = FileSystem.getLocal(conf); + } else { + // Fallback, if it is any other appender or if none is configured. + this.basePath = new Path(conf.get(NM_REMOTE_APP_LOG_DIR, DEFAULT_NM_REMOTE_APP_LOG_DIR)); + this.fs = this.basePath.getFileSystem(conf); + } + LOG.info("Periodic Thread Dump Capture Service Configured to capture Thread Dumps at {} ms frequency and at " + + "path: {}", duration, basePath); + } + + public static TezThreadDumpHelper getTezThreadDumpHelper(Configuration conf) { + long periodicThreadDumpFrequency = + conf.getTimeDuration(TEZ_THREAD_DUMP_INTERVAL, TEZ_THREAD_DUMP_INTERVAL_DEFAULT, TimeUnit.MILLISECONDS); + + if (periodicThreadDumpFrequency > 0) { + try { + return new TezThreadDumpHelper(periodicThreadDumpFrequency, conf); + } catch (IOException e) { + LOG.warn("Can not initialize periodic thread dump service", e); + } + } + return null; + } + + public static void schedulePeriodicThreadDumpService(TezThreadDumpHelper threadDumpHelper, String name) { + if (threadDumpHelper != null) { + threadDumpHelper.schedulePeriodicThreadDumpService(name); + } + } + + private void schedulePeriodicThreadDumpService(String name) { Review Comment: maybe a slightly different name for the private method for better readability? I think the public ones could be simple (start, stop) -- 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