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.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.util.Optional;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.metrics.Metric;
029import org.apache.hadoop.hbase.metrics.MetricRegistries;
030import org.apache.hadoop.hbase.metrics.MetricRegistry;
031import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
032import org.apache.hadoop.hbase.metrics.Snapshot;
033import org.apache.hadoop.hbase.metrics.impl.DropwizardMeter;
034import org.apache.hadoop.hbase.metrics.impl.HistogramImpl;
035import org.apache.hadoop.hbase.regionserver.metrics.MetricsTableRequests;
036import org.apache.hadoop.hbase.testclassification.RegionServerTests;
037import org.apache.hadoop.hbase.testclassification.SmallTests;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042@Category({ RegionServerTests.class, SmallTests.class })
043public class TestMetricsTableRequests {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestMetricsTableRequests.class);
048
049  @Test
050  public void testMetricsTableLatencies() {
051    TableName tn1 = TableName.valueOf("table1");
052    TableName tn2 = TableName.valueOf("table2");
053    MetricsTableRequests requests1 = new MetricsTableRequests(tn1, new Configuration());
054    MetricsTableRequests requests2 = new MetricsTableRequests(tn2, new Configuration());
055    assertTrue("'requests' is actually " + requests1.getClass(),
056      requests1 instanceof MetricsTableRequests);
057    assertTrue("'requests' is actually " + requests2.getClass(),
058      requests2 instanceof MetricsTableRequests);
059
060    MetricRegistryInfo info1 = requests1.getMetricRegistryInfo();
061    MetricRegistryInfo info2 = requests2.getMetricRegistryInfo();
062    Optional<MetricRegistry> registry1 = MetricRegistries.global().get(info1);
063    assertTrue(registry1.isPresent());
064    Optional<MetricRegistry> registry2 = MetricRegistries.global().get(info2);
065    assertTrue(registry2.isPresent());
066
067    requests1.updateGet(500L, 5000L);
068    Snapshot latencies1SnapshotGet =
069      ((HistogramImpl) registry1.get().get("getTime").get()).snapshot();
070    assertEquals(500, latencies1SnapshotGet.get999thPercentile());
071    Snapshot blockBytesScanned1SnapshotGet =
072      ((HistogramImpl) registry1.get().get("getBlockBytesScanned").get()).snapshot();
073    assertEquals(5000, blockBytesScanned1SnapshotGet.get999thPercentile());
074
075    requests1.updatePut(50L);
076    Snapshot latencies1SnapshotPut =
077      ((HistogramImpl) registry1.get().get("putTime").get()).snapshot();
078    assertEquals(50, latencies1SnapshotPut.get99thPercentile());
079
080    requests2.updateGet(300L, 3000L);
081    Snapshot latencies2SnapshotGet =
082      ((HistogramImpl) registry2.get().get("getTime").get()).snapshot();
083    assertEquals(300, latencies2SnapshotGet.get999thPercentile());
084    Snapshot blockBytesScanned2SnapshotGet =
085      ((HistogramImpl) registry2.get().get("getBlockBytesScanned").get()).snapshot();
086    assertEquals(3000, blockBytesScanned2SnapshotGet.get999thPercentile());
087
088    requests2.updatePut(75L);
089    Snapshot latencies2SnapshotPut =
090      ((HistogramImpl) registry2.get().get("putTime").get()).snapshot();
091    assertEquals(75, latencies2SnapshotPut.get99thPercentile());
092  }
093
094  @Test
095  public void testTableQueryMeterSwitch() {
096    TableName tn1 = TableName.valueOf("table1");
097    Configuration conf = new Configuration();
098    boolean enableTableQueryMeter =
099      conf.getBoolean(MetricsTableRequests.ENABLE_TABLE_QUERY_METER_METRICS_KEY,
100        MetricsTableRequests.ENABLE_TABLE_QUERY_METER_METRICS_KEY_DEFAULT);
101    // disable
102    assertFalse(enableTableQueryMeter);
103    MetricsTableRequests requests = new MetricsTableRequests(tn1, conf);
104    assertTrue("'requests' is actually " + requests.getClass(),
105      requests instanceof MetricsTableRequests);
106
107    MetricRegistryInfo info = requests.getMetricRegistryInfo();
108    Optional<MetricRegistry> registry = MetricRegistries.global().get(info);
109    assertTrue(registry.isPresent());
110    requests.updateTableReadQueryMeter(500L);
111    Optional<Metric> read = registry.get().get("tableReadQueryPerSecond");
112    assertFalse(read.isPresent());
113
114    // enable
115    conf.setBoolean(MetricsTableRequests.ENABLE_TABLE_QUERY_METER_METRICS_KEY, true);
116    enableTableQueryMeter =
117      conf.getBoolean(MetricsTableRequests.ENABLE_TABLE_QUERY_METER_METRICS_KEY,
118        MetricsTableRequests.ENABLE_TABLE_QUERY_METER_METRICS_KEY_DEFAULT);
119    assertTrue(enableTableQueryMeter);
120    requests = new MetricsTableRequests(tn1, conf);
121    assertTrue("'requests' is actually " + requests.getClass(),
122      requests instanceof MetricsTableRequests);
123
124    info = requests.getMetricRegistryInfo();
125    registry = MetricRegistries.global().get(info);
126    assertTrue(registry.isPresent());
127    requests.updateTableReadQueryMeter(500L);
128    read = registry.get().get("tableReadQueryPerSecond");
129    assertTrue(read.isPresent());
130    assertEquals(((DropwizardMeter) read.get()).getCount(), 500);
131  }
132}