zratkai commented on code in PR #5557: URL: https://github.com/apache/hive/pull/5557#discussion_r1856322962
########## llap-common/src/java/org/apache/hadoop/hive/llap/LlapThreadLocalStatistics.java: ########## @@ -0,0 +1,176 @@ +/* + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.hadoop.hive.llap; + +import com.google.common.annotations.VisibleForTesting; +import org.apache.hadoop.fs.FileSystem; +import org.apache.tez.common.counters.FileSystemCounter; +import org.apache.tez.common.counters.TezCounters; +import java.lang.management.ThreadMXBean; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +/** + * Convenience class for handling thread local statistics for different schemes in LLAP. + * The motivation is that thread local stats (available from FileSystem.getAllStatistics()) are in a List, which + * doesn't guarantee that for a single scheme, a single Statistics object is returned (e.g. in case of multiple + * namenodes). This class encapsulates that data, and takes care of merging them transparently. + * LlapThreadLocalStatistics is used in LLAP's task ThreadPool, where the current thread's statistics is calculated by a + * simple delta computation (before/after running the task callable), like: + * + * LlapThreadLocalStatistics statsBefore = new LlapThreadLocalStatistics(...); + * LlapThreadLocalStatistics diff = new LlapThreadLocalStatistics(...).subtract(statsBefore); + */ +public class LlapThreadLocalStatistics { + + /** + * LLAP IO related counters. + */ + public enum LlapExecutorCounters { + EXECUTOR_CPU_NS, + EXECUTOR_USER_NS; + } + + @VisibleForTesting + Map<String, LlapThreadLocalStatistics.StatisticsData> schemeToThreadLocalStats = new HashMap<>(); + @VisibleForTesting + long cpuTime; + @VisibleForTesting + long userTime; + + /** + * In this constructor we create a snapshot of the current thread local statistics and take care of merging the + * ones that belong to the same scheme. + */ + public LlapThreadLocalStatistics(ThreadMXBean mxBean) { + this(mxBean, FileSystem.getAllStatistics()); + } + + /** + * Merges the list to a map. + * Input list: + * 1. FileSystem.Statistics (scheme: file) + * 2. FileSystem.Statistics (scheme: hdfs) + * 3. FileSystem.Statistics (scheme: hdfs) + * Output map: + * file : LlapThreadLocalStatistics.StatisticsData (1) + * hdfs : LlapThreadLocalStatistics.StatisticsData (2 + 3) + */ + public LlapThreadLocalStatistics(ThreadMXBean mxBean, List<FileSystem.Statistics> allStatistics) { + cpuTime = mxBean == null ? -1 : mxBean.getCurrentThreadCpuTime(); + userTime = mxBean == null ? -1 : mxBean.getCurrentThreadUserTime(); + + for (FileSystem.Statistics statistics : allStatistics) { + schemeToThreadLocalStats.merge(statistics.getScheme(), + new StatisticsData(statistics.getThreadStatistics()), + (statsCurrent, statsNew) -> statsCurrent.merge(statistics.getThreadStatistics())); + } + } + + // This method iterates on the other LlapThreadLocalStatistics's schemes, and subtract them from this one if it's + // present here too. + public LlapThreadLocalStatistics subtract(LlapThreadLocalStatistics other) { + for (Map.Entry<String, LlapThreadLocalStatistics.StatisticsData> otherStats : + other.schemeToThreadLocalStats.entrySet()){ + schemeToThreadLocalStats.computeIfPresent(otherStats.getKey(), + (thisScheme, stats) -> stats.subtract(otherStats.getValue())); + } + + cpuTime -= other.cpuTime; + userTime -= other.userTime; + + return this; + } + + public void fill(TezCounters tezCounters) { + for (Map.Entry<String, LlapThreadLocalStatistics.StatisticsData> threadLocalStats : + schemeToThreadLocalStats.entrySet()){ + String scheme = threadLocalStats.getKey(); + StatisticsData stats = threadLocalStats.getValue(); + tezCounters.findCounter(scheme, FileSystemCounter.BYTES_READ).increment(stats.bytesRead); + tezCounters.findCounter(scheme, FileSystemCounter.BYTES_WRITTEN).increment(stats.bytesWritten); + tezCounters.findCounter(scheme, FileSystemCounter.READ_OPS).increment(stats.readOps); + tezCounters.findCounter(scheme, FileSystemCounter.LARGE_READ_OPS).increment(stats.largeReadOps); + tezCounters.findCounter(scheme, FileSystemCounter.WRITE_OPS).increment(stats.writeOps); + } + + if (cpuTime >= 0 && userTime >= 0) { + tezCounters.findCounter(LlapThreadLocalStatistics.LlapExecutorCounters.EXECUTOR_CPU_NS).increment(cpuTime); + tezCounters.findCounter(LlapThreadLocalStatistics.LlapExecutorCounters.EXECUTOR_USER_NS).increment(userTime); + } + } + + public String toString(){ + return String.format("LlapThreadLocalStatistics: %s", schemeToThreadLocalStats.toString()); + } + + /** + * Convenience class over Hadoop's FileSystem.Statistics.StatisticsData. + * Unfortunately, neither the fields, nor the convenience methods (e.g. StatisticsData.add, StatisticsData.negate) + * are available here as they are package protected, so we cannot reuse them. + */ + public static class StatisticsData { Review Comment: Your choice. ;) -- 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: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org