|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.fs.store.logging; |
| 20 | + |
| 21 | +import org.apache.hadoop.fs.statistics.IOStatistics; |
| 22 | +import org.apache.hadoop.fs.store.shim.impl.Invocation; |
| 23 | +import org.apache.hadoop.fs.store.shim.impl.ShimReflectionSupport; |
| 24 | + |
| 25 | +import static org.apache.hadoop.fs.store.shim.impl.Invocation.unavailable; |
| 26 | +import static org.apache.hadoop.fs.store.shim.impl.ShimReflectionSupport.loadClass; |
| 27 | +import static org.apache.hadoop.fs.store.shim.impl.ShimReflectionSupport.loadInvocation; |
| 28 | + |
| 29 | +/** |
| 30 | + * Support for IO statistics (initially through reflection). |
| 31 | + */ |
| 32 | +public class IOStatisticsIntegration { |
| 33 | + |
| 34 | + public static final String CLASSNAME_IOSTATISTICS = "org.apache.hadoop.fs.statistics.IOStatistics"; |
| 35 | + |
| 36 | + public static final String CLASSNAME_IOSTATISTICS_LOGGING = "org.apache.hadoop.fs.statistics.IOStatisticsLogging"; |
| 37 | + public static final String CLASSNAME_IOSTATISTICS_SUPPORT = "org.apache.hadoop.fs.statistics.IOStatisticsSupport"; |
| 38 | + |
| 39 | + private final Class<?> ioStatisticsClass; |
| 40 | + |
| 41 | + private final Class<?> ioStatisticsLogging; |
| 42 | + private final Class<?> ioStatisticsSupport; |
| 43 | + |
| 44 | + private final Invocation<String> _ioStatisticsToPrettyString; |
| 45 | + |
| 46 | + private final Invocation<?> _retrieveIOStatistics; |
| 47 | + |
| 48 | + public IOStatisticsIntegration() { |
| 49 | + // try to load the class |
| 50 | + ioStatisticsClass = loadClass(CLASSNAME_IOSTATISTICS); |
| 51 | + if (ioStatisticsClass == null) { |
| 52 | + // if that class is missing, so is the rest. |
| 53 | + ioStatisticsSupport = null; |
| 54 | + ioStatisticsLogging = null; |
| 55 | + _ioStatisticsToPrettyString = unavailable("ioStatisticsToPrettyString"); |
| 56 | + _retrieveIOStatistics = unavailable("retrieveIOStatistics"); |
| 57 | + } else { |
| 58 | + ioStatisticsSupport = loadClass(CLASSNAME_IOSTATISTICS_SUPPORT); |
| 59 | + _retrieveIOStatistics = loadInvocation(ioStatisticsSupport, |
| 60 | + ioStatisticsClass, "retrieveIOStatistics", Object.class); |
| 61 | + |
| 62 | + ioStatisticsLogging = loadClass(CLASSNAME_IOSTATISTICS_LOGGING); |
| 63 | + |
| 64 | + _ioStatisticsToPrettyString = loadInvocation(ioStatisticsLogging, |
| 65 | + String.class, "ioStatisticsToPrettyString", ioStatisticsClass); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public boolean available() { |
| 70 | + return ioStatisticsClass != null; |
| 71 | + } |
| 72 | + |
| 73 | + public String ioStatisticsToPrettyString(Object source) { |
| 74 | + if (!available()) { |
| 75 | + return ""; |
| 76 | + } |
| 77 | + return _ioStatisticsToPrettyString.invokeUnchecked(null, |
| 78 | + _retrieveIOStatistics.invokeUnchecked(null, source)); |
| 79 | + } |
| 80 | +} |
0 commit comments