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.metrics.impl;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotSame;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.lang.reflect.Method;
025import java.util.Collection;
026import org.apache.hadoop.hbase.metrics.MetricRegistries;
027import org.apache.hadoop.hbase.metrics.MetricRegistry;
028import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
029import org.apache.hadoop.hbase.testclassification.MetricsTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.metrics2.AbstractMetric;
032import org.apache.hadoop.metrics2.MetricsRecord;
033import org.apache.hadoop.metrics2.impl.MetricsExportHelper;
034import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
035import org.junit.jupiter.api.AfterEach;
036import org.junit.jupiter.api.BeforeEach;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039
040@Tag(MetricsTests.TAG)
041@Tag(SmallTests.TAG)
042public class TestGlobalMetricRegistriesAdapter {
043
044  private static final String METRICS_NAME = "TestGlobalMetricRegistriesAdapter";
045  private static final String COUNTER_NAME = "requests";
046  private static final MetricRegistryInfo INFO =
047    new MetricRegistryInfo(METRICS_NAME, "Metrics for GlobalMetricRegistriesAdapter tests",
048      "RegionServer,sub=" + METRICS_NAME, "regionserver", false);
049
050  private GlobalMetricRegistriesAdapter adapter;
051
052  @BeforeEach
053  public void setUp() {
054    MetricRegistries.global().clear();
055    DefaultMetricsSystem.shutdown();
056    DefaultMetricsSystem.initialize("globalMetricRegistriesAdapterTest");
057    DefaultMetricsSystem.instance().start();
058    adapter = GlobalMetricRegistriesAdapter.init();
059  }
060
061  @AfterEach
062  public void tearDown() {
063    MetricRegistries.global().clear();
064    if (adapter != null) {
065      // First unregister sources for the cleared registries, then stop the adapter's executor.
066      runAdapter();
067      adapter.stop();
068      runAdapter();
069    }
070    DefaultMetricsSystem.shutdown();
071  }
072
073  @Test
074  public void testExportUsesRecreatedMetricRegistry() {
075    MetricRegistry registryBefore = MetricRegistries.global().create(INFO);
076    registryBefore.counter(COUNTER_NAME).increment(1);
077    runAdapter();
078
079    assertEquals(1, getExportedCounterValue());
080
081    assertTrue(MetricRegistries.global().remove(INFO));
082    MetricRegistry registryAfter = MetricRegistries.global().create(INFO);
083    assertNotSame(registryBefore, registryAfter);
084    registryAfter.counter(COUNTER_NAME).increment(2);
085    runAdapter();
086
087    assertEquals(2, getExportedCounterValue());
088  }
089
090  private void runAdapter() {
091    try {
092      // Trigger the adapter synchronously so this small test does not wait for the 10s scheduler.
093      Method doRun = GlobalMetricRegistriesAdapter.class.getDeclaredMethod("doRun");
094      doRun.setAccessible(true);
095      doRun.invoke(adapter);
096    } catch (ReflectiveOperationException e) {
097      throw new AssertionError("Failed to run GlobalMetricRegistriesAdapter", e);
098    }
099  }
100
101  private long getExportedCounterValue() {
102    Collection<MetricsRecord> exportedMetrics = MetricsExportHelper.export();
103    for (MetricsRecord exported : exportedMetrics) {
104      if (exported.name().equals(INFO.getMetricsName())) {
105        for (AbstractMetric metric : exported.metrics()) {
106          if (metric.name().equals("Requests")) {
107            return metric.value().longValue();
108          }
109        }
110      }
111    }
112    return -1;
113  }
114}