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.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.CompatibilityFactory;
27  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
30  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
31  import org.apache.hadoop.hbase.testclassification.SmallTests;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  @Category({RegionServerTests.class, SmallTests.class})
36  public class TestMetricsTableLatencies {
37  
38    private static MetricsAssertHelper HELPER =
39      CompatibilityFactory.getInstance(MetricsAssertHelper.class);
40  
41    @Test
42    public void testTableWrapperAggregateMetrics() throws IOException {
43      TableName tn1 = TableName.valueOf("table1");
44      TableName tn2 = TableName.valueOf("table2");
45      MetricsTableLatencies latencies = CompatibilitySingletonFactory.getInstance(
46          MetricsTableLatencies.class);
47      assertTrue("'latencies' is actually " + latencies.getClass(),
48          latencies instanceof MetricsTableLatenciesImpl);
49      MetricsTableLatenciesImpl latenciesImpl = (MetricsTableLatenciesImpl) latencies;
50      RegionServerTableMetrics tableMetrics = new RegionServerTableMetrics(false);
51  
52      // Metrics to each table should be disjoint
53      // N.B. each call to assertGauge removes all previously acquired metrics so we have to
54      //   make the metrics call and then immediately verify it. Trying to do multiple metrics
55      //   updates followed by multiple verifications will fail on the 2nd verification (as the
56      //   first verification cleaned the data structures in MetricsAssertHelperImpl).
57      tableMetrics.updateGet(tn1, 500L);
58      HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
59          tn1, MetricsTableLatencies.GET_TIME + "_" + "999th_percentile"), 500L, latenciesImpl);
60      tableMetrics.updatePut(tn1, 50L);
61      HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
62          tn1, MetricsTableLatencies.PUT_TIME + "_" + "99th_percentile"), 50L, latenciesImpl);
63  
64      tableMetrics.updateGet(tn2, 300L);
65      HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
66          tn2, MetricsTableLatencies.GET_TIME + "_" + "999th_percentile"), 300L, latenciesImpl);
67      tableMetrics.updatePut(tn2, 75L);
68      HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
69          tn2, MetricsTableLatencies.PUT_TIME + "_" + "99th_percentile"), 75L, latenciesImpl);
70    }
71  
72    @Test
73    public void testTableQueryMeterSwitch() {
74      TableName tn1 = TableName.valueOf("table1");
75      MetricsTableLatencies latencies = CompatibilitySingletonFactory.getInstance(
76        MetricsTableLatencies.class);
77      assertTrue("'latencies' is actually " + latencies.getClass(),
78        latencies instanceof MetricsTableLatenciesImpl);
79      MetricsTableLatenciesImpl latenciesImpl = (MetricsTableLatenciesImpl) latencies;
80  
81      Configuration conf = new Configuration();
82      conf.setBoolean(MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY, false);
83      boolean enableTableQueryMeter = conf.getBoolean(
84        MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY,
85        MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY_DEFAULT);
86      // disable
87      assertFalse(enableTableQueryMeter);
88      RegionServerTableMetrics tableMetrics = new RegionServerTableMetrics(enableTableQueryMeter);
89      tableMetrics.updateTableReadQueryMeter(tn1, 500L);
90      assertFalse(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(
91        tn1, MetricsTableQueryMeterImpl.TABLE_READ_QUERY_PER_SECOND + "_" + "count"),
92        latenciesImpl));
93      tableMetrics.updateTableWriteQueryMeter(tn1, 500L);
94      assertFalse(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(
95        tn1, MetricsTableQueryMeterImpl.TABLE_WRITE_QUERY_PER_SECOND + "_" + "count"),
96        latenciesImpl));
97  
98      // enable
99      conf.setBoolean(MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY, true);
100     enableTableQueryMeter = conf.getBoolean(
101       MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY,
102       MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY_DEFAULT);
103     assertTrue(enableTableQueryMeter);
104     tableMetrics = new RegionServerTableMetrics(true);
105     tableMetrics.updateTableReadQueryMeter(tn1, 500L);
106     assertTrue(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(
107       tn1, MetricsTableQueryMeterImpl.TABLE_READ_QUERY_PER_SECOND + "_" + "count"),
108       latenciesImpl));
109     HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
110       tn1, MetricsTableQueryMeterImpl.TABLE_READ_QUERY_PER_SECOND + "_" + "count"),
111       500L, latenciesImpl);
112     tableMetrics.updateTableWriteQueryMeter(tn1, 500L);
113     assertTrue(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(
114       tn1, MetricsTableQueryMeterImpl.TABLE_WRITE_QUERY_PER_SECOND + "_" + "count"),
115       latenciesImpl));
116     HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
117       tn1, MetricsTableQueryMeterImpl.TABLE_WRITE_QUERY_PER_SECOND + "_" + "count"),
118       500L, latenciesImpl);
119   }
120 }