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.metrics;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertSame;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.util.Optional;
026import org.apache.hadoop.conf.Configuration;
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.testclassification.RegionServerTests;
036import org.apache.hadoop.hbase.testclassification.SmallTests;
037import org.junit.jupiter.api.AfterEach;
038import org.junit.jupiter.api.Tag;
039import org.junit.jupiter.api.Test;
040import org.junit.jupiter.api.TestInfo;
041
042@Tag(RegionServerTests.TAG)
043@Tag(SmallTests.TAG)
044public class TestMetricsTableRequests {
045
046  @AfterEach
047  public void tearDown() {
048    MetricRegistries.global().clear();
049  }
050
051  @Test
052  public void testMetricsTableLatencies() {
053    TableName tn1 = TableName.valueOf("table1");
054    TableName tn2 = TableName.valueOf("table2");
055    MetricsTableRequests requests1 = new MetricsTableRequests(tn1, new Configuration());
056    MetricsTableRequests requests2 = new MetricsTableRequests(tn2, new Configuration());
057
058    MetricRegistryInfo info1 = requests1.getMetricRegistryInfo();
059    MetricRegistryInfo info2 = requests2.getMetricRegistryInfo();
060    Optional<MetricRegistry> registry1 = MetricRegistries.global().get(info1);
061    assertTrue(registry1.isPresent());
062    Optional<MetricRegistry> registry2 = MetricRegistries.global().get(info2);
063    assertTrue(registry2.isPresent());
064
065    requests1.updateGet(500L, 5000L);
066    Snapshot latencies1SnapshotGet =
067      ((HistogramImpl) registry1.get().get("getTime").get()).snapshot();
068    assertEquals(500, latencies1SnapshotGet.get999thPercentile());
069    Snapshot blockBytesScanned1SnapshotGet =
070      ((HistogramImpl) registry1.get().get("getBlockBytesScanned").get()).snapshot();
071    assertEquals(5000, blockBytesScanned1SnapshotGet.get999thPercentile());
072
073    requests1.updatePut(50L);
074    Snapshot latencies1SnapshotPut =
075      ((HistogramImpl) registry1.get().get("putTime").get()).snapshot();
076    assertEquals(50, latencies1SnapshotPut.get99thPercentile());
077
078    requests2.updateGet(300L, 3000L);
079    Snapshot latencies2SnapshotGet =
080      ((HistogramImpl) registry2.get().get("getTime").get()).snapshot();
081    assertEquals(300, latencies2SnapshotGet.get999thPercentile());
082    Snapshot blockBytesScanned2SnapshotGet =
083      ((HistogramImpl) registry2.get().get("getBlockBytesScanned").get()).snapshot();
084    assertEquals(3000, blockBytesScanned2SnapshotGet.get999thPercentile());
085
086    requests2.updatePut(75L);
087    Snapshot latencies2SnapshotPut =
088      ((HistogramImpl) registry2.get().get("putTime").get()).snapshot();
089    assertEquals(75, latencies2SnapshotPut.get99thPercentile());
090  }
091
092  @Test
093  public void testTableQueryMeterSwitch() {
094    TableName tn1 = TableName.valueOf("table1");
095    Configuration conf = new Configuration();
096    boolean enableTableQueryMeter =
097      conf.getBoolean(MetricsTableRequests.ENABLE_TABLE_QUERY_METER_METRICS_KEY,
098        MetricsTableRequests.ENABLE_TABLE_QUERY_METER_METRICS_KEY_DEFAULT);
099    // disable
100    assertFalse(enableTableQueryMeter);
101    MetricsTableRequests requests = new MetricsTableRequests(tn1, conf);
102
103    MetricRegistryInfo info = requests.getMetricRegistryInfo();
104    Optional<MetricRegistry> registry = MetricRegistries.global().get(info);
105    assertTrue(registry.isPresent());
106    requests.updateTableReadQueryMeter(500L);
107    Optional<Metric> read = registry.get().get("tableReadQueryPerSecond");
108    assertFalse(read.isPresent());
109
110    // enable
111    conf.setBoolean(MetricsTableRequests.ENABLE_TABLE_QUERY_METER_METRICS_KEY, true);
112    enableTableQueryMeter =
113      conf.getBoolean(MetricsTableRequests.ENABLE_TABLE_QUERY_METER_METRICS_KEY,
114        MetricsTableRequests.ENABLE_TABLE_QUERY_METER_METRICS_KEY_DEFAULT);
115    assertTrue(enableTableQueryMeter);
116    requests = new MetricsTableRequests(tn1, conf);
117
118    info = requests.getMetricRegistryInfo();
119    registry = MetricRegistries.global().get(info);
120    assertTrue(registry.isPresent());
121    requests.updateTableReadQueryMeter(500L);
122    read = registry.get().get("tableReadQueryPerSecond");
123    assertTrue(read.isPresent());
124    assertEquals(500, ((DropwizardMeter) read.get()).getCount());
125  }
126
127  @Test
128  public void testSameRegistryInstanceRefCounting(TestInfo testInfo) {
129    TableName tn1 = TableName.valueOf(testInfo.getTestMethod().get().getName());
130
131    // create registry twice, should return same instance due to ref-counting
132    MetricsTableRequests requests1 = new MetricsTableRequests(tn1, new Configuration());
133    MetricsTableRequests requests2 = new MetricsTableRequests(tn1, new Configuration());
134
135    MetricRegistry metricRegistry1 = requests1.getMetricRegistry();
136    MetricRegistry metricRegistry2 = requests2.getMetricRegistry();
137    assertSame(metricRegistry1, metricRegistry2);
138
139    MetricRegistryInfo registryInfo = metricRegistry1.getMetricRegistryInfo();
140    MetricRegistries.global().remove(registryInfo);
141    assertTrue(MetricRegistries.global().get(registryInfo).isPresent());
142    MetricRegistries.global().remove(registryInfo);
143    assertFalse(MetricRegistries.global().get(registryInfo).isPresent());
144  }
145}