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.regionserver;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.CompatibilityFactory;
026import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.test.MetricsAssertHelper;
030import org.apache.hadoop.hbase.testclassification.RegionServerTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category({ RegionServerTests.class, SmallTests.class })
037public class TestMetricsTableLatencies {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestMetricsTableLatencies.class);
042
043  public static MetricsAssertHelper HELPER =
044    CompatibilityFactory.getInstance(MetricsAssertHelper.class);
045
046  @Test
047  public void testTableWrapperAggregateMetrics() throws IOException {
048    TableName tn1 = TableName.valueOf("table1");
049    TableName tn2 = TableName.valueOf("table2");
050    MetricsTableLatencies latencies =
051      CompatibilitySingletonFactory.getInstance(MetricsTableLatencies.class);
052    assertTrue("'latencies' is actually " + latencies.getClass(),
053      latencies instanceof MetricsTableLatenciesImpl);
054    MetricsTableLatenciesImpl latenciesImpl = (MetricsTableLatenciesImpl) latencies;
055    RegionServerTableMetrics tableMetrics = new RegionServerTableMetrics(false);
056
057    // Metrics to each table should be disjoint
058    // N.B. each call to assertGauge removes all previously acquired metrics so we have to
059    // make the metrics call and then immediately verify it. Trying to do multiple metrics
060    // updates followed by multiple verifications will fail on the 2nd verification (as the
061    // first verification cleaned the data structures in MetricsAssertHelperImpl).
062    tableMetrics.updateGet(tn1, 500L);
063    HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
064      MetricsTableLatencies.GET_TIME + "_" + "999th_percentile"), 500L, latenciesImpl);
065    tableMetrics.updatePut(tn1, 50L);
066    HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
067      MetricsTableLatencies.PUT_TIME + "_" + "99th_percentile"), 50L, latenciesImpl);
068
069    tableMetrics.updateGet(tn2, 300L);
070    HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(tn2,
071      MetricsTableLatencies.GET_TIME + "_" + "999th_percentile"), 300L, latenciesImpl);
072    tableMetrics.updatePut(tn2, 75L);
073    HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(tn2,
074      MetricsTableLatencies.PUT_TIME + "_" + "99th_percentile"), 75L, latenciesImpl);
075  }
076
077  @Test
078  public void testTableQueryMeterSwitch() {
079    TableName tn1 = TableName.valueOf("table1");
080    MetricsTableLatencies latencies =
081      CompatibilitySingletonFactory.getInstance(MetricsTableLatencies.class);
082    assertTrue("'latencies' is actually " + latencies.getClass(),
083      latencies instanceof MetricsTableLatenciesImpl);
084    MetricsTableLatenciesImpl latenciesImpl = (MetricsTableLatenciesImpl) latencies;
085
086    Configuration conf = new Configuration();
087    boolean enableTableQueryMeter =
088      conf.getBoolean(MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY,
089        MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY_DEFAULT);
090    // disable
091    assertFalse(enableTableQueryMeter);
092    RegionServerTableMetrics tableMetrics = new RegionServerTableMetrics(enableTableQueryMeter);
093    tableMetrics.updateTableReadQueryMeter(tn1, 500L);
094    assertFalse(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
095      MetricsTableQueryMeterImpl.TABLE_READ_QUERY_PER_SECOND + "_" + "count"), latenciesImpl));
096    tableMetrics.updateTableWriteQueryMeter(tn1, 500L);
097    assertFalse(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
098      MetricsTableQueryMeterImpl.TABLE_WRITE_QUERY_PER_SECOND + "_" + "count"), latenciesImpl));
099
100    // enable
101    conf.setBoolean(MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY, true);
102    enableTableQueryMeter =
103      conf.getBoolean(MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY,
104        MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY_DEFAULT);
105    assertTrue(enableTableQueryMeter);
106    tableMetrics = new RegionServerTableMetrics(true);
107    tableMetrics.updateTableReadQueryMeter(tn1, 500L);
108    assertTrue(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
109      MetricsTableQueryMeterImpl.TABLE_READ_QUERY_PER_SECOND + "_" + "count"), latenciesImpl));
110    HELPER.assertGauge(
111      MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
112        MetricsTableQueryMeterImpl.TABLE_READ_QUERY_PER_SECOND + "_" + "count"),
113      500L, latenciesImpl);
114    tableMetrics.updateTableWriteQueryMeter(tn1, 500L);
115    assertTrue(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
116      MetricsTableQueryMeterImpl.TABLE_WRITE_QUERY_PER_SECOND + "_" + "count"), latenciesImpl));
117    HELPER.assertGauge(
118      MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
119        MetricsTableQueryMeterImpl.TABLE_WRITE_QUERY_PER_SECOND + "_" + "count"),
120      500L, latenciesImpl);
121  }
122}