001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.io; 019 020import com.google.errorprone.annotations.RestrictedApi; 021import java.util.function.Supplier; 022import org.apache.hadoop.hbase.CompatibilitySingletonFactory; 023import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSourceFactory; 024import org.apache.yetus.audience.InterfaceAudience; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028@InterfaceAudience.Private 029public class MetricsIO { 030 031 private static final Logger LOG = LoggerFactory.getLogger(MetricsIO.class); 032 033 /** 034 * Shared instance returned while IO metrics are disabled. It discards all updates and never 035 * registers anything with the metrics system. 036 */ 037 private static final MetricsIO DISABLED_INSTANCE = 038 new MetricsIO(new NoOpMetricsIOSource(), new MetricsIOWrapperImpl()); 039 040 private static volatile boolean enabled = false; 041 private static volatile MetricsIO instance; 042 private final MetricsIOSource source; 043 private final MetricsIOWrapper wrapper; 044 045 @RestrictedApi(explanation = "Should only be called in TestMetricsIO", link = "", 046 allowedOnPath = ".*/(MetricsIO|TestMetricsIO).java") 047 MetricsIO(MetricsIOWrapper wrapper) { 048 this(CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class) 049 .createIO(wrapper), wrapper); 050 } 051 052 MetricsIO(MetricsIOSource source, MetricsIOWrapper wrapper) { 053 this.source = source; 054 this.wrapper = wrapper; 055 } 056 057 /** 058 * Enables IO metrics for this process. Until this is called, {@link #getInstance()} returns an 059 * instance that discards all updates, so processes that only read or write HFiles as a client 060 * (bulk load, MapReduce tasks, command line tools) do not start the Hadoop metrics system or 061 * register a "RegionServer" metrics source. Server processes call this during startup. The 062 * metrics source itself is still created lazily, on first use after this call. See HBASE-30231 063 * for more details. 064 */ 065 public static void enable() { 066 enabled = true; 067 } 068 069 /** Returns whether IO metrics have been enabled for this process. */ 070 public static boolean isEnabled() { 071 return enabled; 072 } 073 074 @RestrictedApi(explanation = "Should only be called in tests", link = "", 075 allowedOnPath = ".*/src/test/.*") 076 public static synchronized void resetForTesting() { 077 enabled = false; 078 instance = null; 079 } 080 081 /** 082 * Get a static instance for the MetricsIO so that accessors access the same instance. We want to 083 * lazy initialize so that correct process name is in place. See HBASE-27966 for more details. 084 * <p> 085 * If IO metrics have not been enabled with {@link #enable()}, a shared instance that discards all 086 * updates is returned. If the metrics source cannot be created, a no-op source is used instead, 087 * so that a metrics failure never fails HFile reads or writes. See HBASE-30231 for more details. 088 */ 089 public static MetricsIO getInstance() { 090 if (!enabled) { 091 return DISABLED_INSTANCE; 092 } 093 if (instance == null) { 094 synchronized (MetricsIO.class) { 095 if (instance == null) { 096 MetricsIOWrapper wrapper = new MetricsIOWrapperImpl(); 097 instance = createInstance(() -> new MetricsIO(wrapper), wrapper); 098 } 099 } 100 } 101 return instance; 102 } 103 104 /** 105 * Creates a {@link MetricsIO} with the given supplier, falling back to one backed by a no-op 106 * {@link MetricsIOSource} if the supplier fails. 107 * <p> 108 * Registering the metrics source with the Hadoop metrics system reserves its name before the 109 * registration has fully completed. If anything fails after that point, every later attempt to 110 * register the same source fails with "Metrics source ... already exists!". Retrying would 111 * therefore fail on every call for the rest of the JVM's life, so we fall back to a no-op source 112 * once and never retry. 113 */ 114 static MetricsIO createInstance(Supplier<MetricsIO> supplier, MetricsIOWrapper wrapper) { 115 try { 116 return supplier.get(); 117 } catch (RuntimeException | LinkageError e) { 118 LOG.warn("Failed to initialize the {} metrics source; FileSystem IO metrics will not be" 119 + " reported by this process", MetricsIOSource.METRICS_JMX_CONTEXT, e); 120 return new MetricsIO(new NoOpMetricsIOSource(), wrapper); 121 } 122 } 123 124 public MetricsIOSource getMetricsSource() { 125 return source; 126 } 127 128 public MetricsIOWrapper getWrapper() { 129 return wrapper; 130 } 131 132 public void updateFsReadTime(long t) { 133 source.updateFsReadTime(t); 134 } 135 136 public void updateFsPreadTime(long t) { 137 source.updateFsPReadTime(t); 138 } 139 140 public void updateFsWriteTime(long t) { 141 source.updateFsWriteTime(t); 142 } 143 144 public void incrSlowFsRead() { 145 source.incrSlowFsRead(); 146 } 147 148 /** 149 * A {@link MetricsIOSource} that discards all updates. Used while IO metrics are disabled, or 150 * when the real metrics source could not be created. 151 */ 152 static final class NoOpMetricsIOSource implements MetricsIOSource { 153 154 @Override 155 public void updateFsReadTime(long t) { 156 } 157 158 @Override 159 public void updateFsPReadTime(long t) { 160 } 161 162 @Override 163 public void updateFsWriteTime(long t) { 164 } 165 166 @Override 167 public void incrSlowFsRead() { 168 } 169 170 @Override 171 public void init() { 172 } 173 174 @Override 175 public void setGauge(String gaugeName, long value) { 176 } 177 178 @Override 179 public void incGauge(String gaugeName, long delta) { 180 } 181 182 @Override 183 public void decGauge(String gaugeName, long delta) { 184 } 185 186 @Override 187 public void removeMetric(String key) { 188 } 189 190 @Override 191 public void incCounters(String counterName, long delta) { 192 } 193 194 @Override 195 public void updateHistogram(String name, long value) { 196 } 197 198 @Override 199 public String getMetricsContext() { 200 return METRICS_CONTEXT; 201 } 202 203 @Override 204 public String getMetricsDescription() { 205 return METRICS_DESCRIPTION; 206 } 207 208 @Override 209 public String getMetricsJmxContext() { 210 return METRICS_JMX_CONTEXT; 211 } 212 213 @Override 214 public String getMetricsName() { 215 return METRICS_NAME; 216 } 217 } 218}