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 static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertInstanceOf;
023import static org.junit.jupiter.api.Assertions.assertNotSame;
024import static org.junit.jupiter.api.Assertions.assertSame;
025import static org.junit.jupiter.api.Assertions.assertTrue;
026
027import org.apache.hadoop.hbase.CompatibilityFactory;
028import org.apache.hadoop.hbase.test.MetricsAssertHelper;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.metrics2.MetricsException;
032import org.junit.jupiter.api.AfterEach;
033import org.junit.jupiter.api.BeforeEach;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036
037@Tag(SmallTests.TAG)
038@Tag(MiscTests.TAG)
039public class TestMetricsIO {
040
041  public MetricsAssertHelper HELPER = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
042
043  @BeforeEach
044  public void setUp() {
045    // Several tests register the same source, which the metrics system only allows in this mode.
046    HELPER.init();
047    MetricsIO.resetForTesting();
048  }
049
050  @AfterEach
051  public void tearDown() {
052    MetricsIO.resetForTesting();
053  }
054
055  @Test
056  public void testDisabledByDefault() {
057    assertFalse(MetricsIO.isEnabled());
058    MetricsIO metrics = MetricsIO.getInstance();
059    assertInstanceOf(MetricsIO.NoOpMetricsIOSource.class, metrics.getMetricsSource());
060    assertSame(metrics, MetricsIO.getInstance());
061    // Updates must be silently discarded while disabled.
062    metrics.updateFsReadTime(100);
063    metrics.updateFsPreadTime(200);
064    metrics.updateFsWriteTime(300);
065    metrics.incrSlowFsRead();
066  }
067
068  @Test
069  public void testEnable() {
070    MetricsIO disabled = MetricsIO.getInstance();
071    MetricsIO.enable();
072    assertTrue(MetricsIO.isEnabled());
073    MetricsIO enabled = MetricsIO.getInstance();
074    assertNotSame(disabled, enabled);
075    assertFalse(enabled.getMetricsSource() instanceof MetricsIO.NoOpMetricsIOSource);
076    assertSame(enabled, MetricsIO.getInstance());
077
078    enabled.updateFsWriteTime(100);
079    HELPER.assertCounter("fsWriteTime_numOps", 1, enabled.getMetricsSource());
080  }
081
082  @Test
083  public void testMetrics() {
084    MetricsIO metrics = new MetricsIO(new MetricsIOWrapper() {
085      @Override
086      public long getChecksumFailures() {
087        return 40;
088      }
089    });
090
091    metrics.updateFsReadTime(100);
092    metrics.updateFsReadTime(200);
093
094    metrics.updateFsPreadTime(300);
095
096    metrics.updateFsWriteTime(400);
097    metrics.updateFsWriteTime(500);
098    metrics.updateFsWriteTime(600);
099
100    HELPER.assertCounter("fsChecksumFailureCount", 40, metrics.getMetricsSource());
101
102    HELPER.assertCounter("fsReadTime_numOps", 2, metrics.getMetricsSource());
103    HELPER.assertCounter("fsPReadTime_numOps", 1, metrics.getMetricsSource());
104    HELPER.assertCounter("fsWriteTime_numOps", 3, metrics.getMetricsSource());
105  }
106
107  @Test
108  public void testCreateInstanceUsesSupplierResult() {
109    MetricsIOWrapper wrapper = () -> 0;
110    MetricsIO expected = new MetricsIO(new MetricsIO.NoOpMetricsIOSource(), wrapper);
111    assertSame(expected, MetricsIO.createInstance(() -> expected, wrapper));
112  }
113
114  @Test
115  public void testFallbackToNoOpSourceOnRuntimeException() {
116    MetricsIOWrapper wrapper = () -> 0;
117    MetricsIO metrics = MetricsIO.createInstance(() -> {
118      // What a second registration attempt of the source fails with, see HBASE-30231
119      throw new MetricsException("Metrics source RegionServer,sub=IO already exists!");
120    }, wrapper);
121    assertFallback(metrics, wrapper);
122  }
123
124  @Test
125  public void testFallbackToNoOpSourceOnLinkageError() {
126    MetricsIOWrapper wrapper = () -> 0;
127    MetricsIO metrics = MetricsIO.createInstance(() -> {
128      throw new ExceptionInInitializerError("simulated static initializer failure");
129    }, wrapper);
130    assertFallback(metrics, wrapper);
131  }
132
133  private static void assertFallback(MetricsIO metrics, MetricsIOWrapper wrapper) {
134    assertInstanceOf(MetricsIO.NoOpMetricsIOSource.class, metrics.getMetricsSource());
135    assertSame(wrapper, metrics.getWrapper());
136    assertEquals(MetricsIOSource.METRICS_JMX_CONTEXT,
137      metrics.getMetricsSource().getMetricsJmxContext());
138    // Updates must be silently discarded rather than failing the caller's HFile read or write.
139    metrics.updateFsReadTime(100);
140    metrics.updateFsPreadTime(200);
141    metrics.updateFsWriteTime(300);
142    metrics.incrSlowFsRead();
143  }
144}