1818package org .apache .hadoop .hbase .io ;
1919
2020import com .google .errorprone .annotations .RestrictedApi ;
21+ import java .util .function .Supplier ;
2122import org .apache .hadoop .hbase .CompatibilitySingletonFactory ;
2223import org .apache .hadoop .hbase .regionserver .MetricsRegionServerSourceFactory ;
2324import org .apache .yetus .audience .InterfaceAudience ;
25+ import org .slf4j .Logger ;
26+ import org .slf4j .LoggerFactory ;
2427
2528@ InterfaceAudience .Private
2629public class MetricsIO {
2730
31+ private static final Logger LOG = LoggerFactory .getLogger (MetricsIO .class );
32+
33+ /**
34+ * Shared instance returned while IO metrics are disabled. It discards all updates and never
35+ * registers anything with the metrics system.
36+ */
37+ private static final MetricsIO DISABLED_INSTANCE =
38+ new MetricsIO (new NoOpMetricsIOSource (), new MetricsIOWrapperImpl ());
39+
40+ private static volatile boolean enabled = false ;
2841 private static volatile MetricsIO instance ;
2942 private final MetricsIOSource source ;
3043 private final MetricsIOWrapper wrapper ;
@@ -41,21 +54,73 @@ public class MetricsIO {
4154 this .wrapper = wrapper ;
4255 }
4356
57+ /**
58+ * Enables IO metrics for this process. Until this is called, {@link #getInstance()} returns an
59+ * instance that discards all updates, so processes that only read or write HFiles as a client
60+ * (bulk load, MapReduce tasks, command line tools) do not start the Hadoop metrics system or
61+ * register a "RegionServer" metrics source. Server processes call this during startup. The
62+ * metrics source itself is still created lazily, on first use after this call. See HBASE-30231
63+ * for more details.
64+ */
65+ public static void enable () {
66+ enabled = true ;
67+ }
68+
69+ /** Returns whether IO metrics have been enabled for this process. */
70+ public static boolean isEnabled () {
71+ return enabled ;
72+ }
73+
74+ @ RestrictedApi (explanation = "Should only be called in tests" , link = "" ,
75+ allowedOnPath = ".*/src/test/.*" )
76+ public static synchronized void resetForTesting () {
77+ enabled = false ;
78+ instance = null ;
79+ }
80+
4481 /**
4582 * Get a static instance for the MetricsIO so that accessors access the same instance. We want to
4683 * lazy initialize so that correct process name is in place. See HBASE-27966 for more details.
84+ * <p>
85+ * If IO metrics have not been enabled with {@link #enable()}, a shared instance that discards all
86+ * updates is returned. If the metrics source cannot be created, a no-op source is used instead,
87+ * so that a metrics failure never fails HFile reads or writes. See HBASE-30231 for more details.
4788 */
4889 public static MetricsIO getInstance () {
90+ if (!enabled ) {
91+ return DISABLED_INSTANCE ;
92+ }
4993 if (instance == null ) {
5094 synchronized (MetricsIO .class ) {
5195 if (instance == null ) {
52- instance = new MetricsIO (new MetricsIOWrapperImpl ());
96+ MetricsIOWrapper wrapper = new MetricsIOWrapperImpl ();
97+ instance = createInstance (() -> new MetricsIO (wrapper ), wrapper );
5398 }
5499 }
55100 }
56101 return instance ;
57102 }
58103
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+
59124 public MetricsIOSource getMetricsSource () {
60125 return source ;
61126 }
@@ -79,4 +144,75 @@ public void updateFsWriteTime(long t) {
79144 public void incrSlowFsRead () {
80145 source .incrSlowFsRead ();
81146 }
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+ }
82218}
0 commit comments