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    conf.setBoolean(MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY, false);
088    boolean enableTableQueryMeter =
089      conf.getBoolean(MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY,
090        MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY_DEFAULT);
091    // disable
092    assertFalse(enableTableQueryMeter);
093    RegionServerTableMetrics tableMetrics = new RegionServerTableMetrics(enableTableQueryMeter);
094    tableMetrics.updateTableReadQueryMeter(tn1, 500L);
095    assertFalse(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
096      MetricsTableQueryMeterImpl.TABLE_READ_QUERY_PER_SECOND + "_" + "count"), latenciesImpl));
097    tableMetrics.updateTableWriteQueryMeter(tn1, 500L);
098    assertFalse(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
099      MetricsTableQueryMeterImpl.TABLE_WRITE_QUERY_PER_SECOND + "_" + "count"), latenciesImpl));
100
101    // enable
102    conf.setBoolean(MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY, true);
103    enableTableQueryMeter =
104      conf.getBoolean(MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY,
105        MetricsRegionServer.RS_ENABLE_TABLE_QUERY_METER_METRICS_KEY_DEFAULT);
106    assertTrue(enableTableQueryMeter);
107    tableMetrics = new RegionServerTableMetrics(true);
108    tableMetrics.updateTableReadQueryMeter(tn1, 500L);
109    assertTrue(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
110      MetricsTableQueryMeterImpl.TABLE_READ_QUERY_PER_SECOND + "_" + "count"), latenciesImpl));
111    HELPER.assertGauge(
112      MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
113        MetricsTableQueryMeterImpl.TABLE_READ_QUERY_PER_SECOND + "_" + "count"),
114      500L, latenciesImpl);
115    tableMetrics.updateTableWriteQueryMeter(tn1, 500L);
116    assertTrue(HELPER.checkGaugeExists(MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
117      MetricsTableQueryMeterImpl.TABLE_WRITE_QUERY_PER_SECOND + "_" + "count"), latenciesImpl));
118    HELPER.assertGauge(
119      MetricsTableLatenciesImpl.qualifyMetricsName(tn1,
120        MetricsTableQueryMeterImpl.TABLE_WRITE_QUERY_PER_SECOND + "_" + "count"),
121      500L, latenciesImpl);
122  }
123}