View Javadoc

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  package org.apache.hadoop.hbase.metrics.impl;
19  
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertNull;
22  
23  import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
24  import org.apache.hadoop.hbase.testclassification.MetricsTests;
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  import org.apache.hadoop.metrics2.MetricsSource;
27  import org.apache.hadoop.metrics2.MetricsSystem;
28  import org.apache.hadoop.metrics2.annotation.Metric;
29  import org.apache.hadoop.metrics2.annotation.Metrics;
30  import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
31  import org.apache.hadoop.metrics2.lib.MetricsRegistry;
32  import org.apache.hadoop.metrics2.lib.MutableCounterLong;
33  import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
34  import org.apache.hadoop.metrics2.lib.MutableRate;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  import org.mockito.Mockito;
38  
39  @Category({ MetricsTests.class, SmallTests.class })
40  public class TestGlobalMetricRegistriesAdapter {
41  
42    /**
43     * Tests that using reflection to unregister the Hadoop metrics source works properly
44     */
45    @Test
46    public void testUnregisterSource() {
47      GlobalMetricRegistriesAdapter adapter = GlobalMetricRegistriesAdapter.init();
48      // we'll configure the sources manually, so disable the executor
49      adapter.stop();
50      TestSource ts1 = new TestSource("ts1");
51      TestSource ts2 = new TestSource("ts2");
52      MetricsSystem metricsSystem = DefaultMetricsSystem.instance();
53      metricsSystem.register("ts1", "", ts1);
54      metricsSystem.register("ts2", "", ts2);
55      MetricsSource s1 = metricsSystem.getSource("ts1");
56      assertNotNull(s1);
57      MetricRegistryInfo mockRegistryInfo = Mockito.mock(MetricRegistryInfo.class);
58      Mockito.when(mockRegistryInfo.getMetricsJmxContext()).thenReturn("ts1");
59      adapter.unregisterSource(mockRegistryInfo);
60      s1 = metricsSystem.getSource("ts1");
61      assertNull(s1);
62      MetricsSource s2 = metricsSystem.getSource("ts2");
63      assertNotNull(s2);
64    }
65  
66    @Metrics(context = "test")
67    private static class TestSource {
68      @Metric("C1 desc")
69      MutableCounterLong c1;
70      @Metric("XXX desc")
71      MutableCounterLong xxx;
72      @Metric("G1 desc")
73      MutableGaugeLong g1;
74      @Metric("YYY desc")
75      MutableGaugeLong yyy;
76      @Metric
77      MutableRate s1;
78      @SuppressWarnings("unused")
79      final MetricsRegistry registry;
80  
81      TestSource(String recName) {
82        registry = new MetricsRegistry(recName);
83      }
84    }
85  
86  }