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.regionserver;
19  
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.IOException;
23  import org.apache.hadoop.hbase.CompatibilityFactory;
24  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
25  import org.apache.hadoop.hbase.TableName;
26  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category({SmallTests.class})
32  public class TestMetricsTableLatencies {
33  
34    public static MetricsAssertHelper HELPER =
35        CompatibilityFactory.getInstance(MetricsAssertHelper.class);
36  
37    @Test
38    public void testTableWrapperAggregateMetrics() throws IOException {
39      TableName tn1 = TableName.valueOf("table1");
40      TableName tn2 = TableName.valueOf("table2");
41      MetricsTableLatencies latencies = CompatibilitySingletonFactory.getInstance(
42          MetricsTableLatencies.class);
43      assertTrue("'latencies' is actually " + latencies.getClass(),
44          latencies instanceof MetricsTableLatenciesImpl);
45      MetricsTableLatenciesImpl latenciesImpl = (MetricsTableLatenciesImpl) latencies;
46      RegionServerTableMetrics tableMetrics = new RegionServerTableMetrics();
47  
48      // Metrics to each table should be disjoint
49      // N.B. each call to assertGauge removes all previously acquired metrics so we have to
50      //   make the metrics call and then immediately verify it. Trying to do multiple metrics
51      //   updates followed by multiple verifications will fail on the 2nd verification (as the
52      //   first verification cleaned the data structures in MetricsAssertHelperImpl).
53      tableMetrics.updateGet(tn1, 500L);
54      HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
55          tn1, MetricsTableLatencies.GET_TIME + "_" + "999th_percentile"), 500L, latenciesImpl);
56      tableMetrics.updatePut(tn1, 50L);
57      HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
58          tn1, MetricsTableLatencies.PUT_TIME + "_" + "99th_percentile"), 50L, latenciesImpl);
59  
60      tableMetrics.updateGet(tn2, 300L);
61      HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
62          tn2, MetricsTableLatencies.GET_TIME + "_" + "999th_percentile"), 300L, latenciesImpl);
63      tableMetrics.updatePut(tn2, 75L);
64      HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
65          tn2, MetricsTableLatencies.PUT_TIME + "_" + "99th_percentile"), 75L, latenciesImpl);
66    }
67  }