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 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.apache
.hadoop
.hbase
.regionserver
;
19 import java
.io
.IOException
;
20 import org
.apache
.hadoop
.conf
.Configuration
;
21 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
22 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
23 import org
.apache
.hadoop
.hbase
.HConstants
;
24 import org
.apache
.hadoop
.hbase
.ServerName
;
25 import org
.apache
.hadoop
.hbase
.TableName
;
26 import org
.apache
.hadoop
.hbase
.client
.Admin
;
27 import org
.apache
.hadoop
.hbase
.client
.Table
;
28 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
29 import org
.apache
.hadoop
.hbase
.testclassification
.RegionServerTests
;
30 import org
.junit
.AfterClass
;
31 import org
.junit
.Assert
;
32 import org
.junit
.BeforeClass
;
33 import org
.junit
.ClassRule
;
34 import org
.junit
.Test
;
35 import org
.junit
.experimental
.categories
.Category
;
38 * Validate requestsPerSecond metric.
40 @Category({ RegionServerTests
.class, MediumTests
.class })
41 public class TestRequestsPerSecondMetric
{
44 public static final HBaseClassTestRule CLASS_RULE
=
45 HBaseClassTestRule
.forClass(TestRequestsPerSecondMetric
.class);
47 private static final HBaseTestingUtil UTIL
= new HBaseTestingUtil();
48 private static final long METRICS_PERIOD
= 2000L;
49 private static Configuration conf
;
53 public static void setup() throws Exception
{
54 conf
= UTIL
.getConfiguration();
55 conf
.setLong(HConstants
.REGIONSERVER_METRICS_PERIOD
, METRICS_PERIOD
);
56 UTIL
.startMiniCluster(1);
60 public static void teardown() throws Exception
{
61 UTIL
.shutdownMiniCluster();
67 * This test will confirm no negative value in requestsPerSecond metric during any region
68 * transition(close region/remove region/move region).
69 * Firstly, load 2000 random rows for 25 regions and will trigger a metric.
70 * Now, metricCache will have a current read and write requests count.
71 * Next, we disable a table and all of its 25 regions will be closed.
72 * As part of region close, his metric will also be removed from metricCache.
73 * prior to HBASE-23237, we do not remove/reset his metric so we incorrectly compute
74 * (currentRequestCount - lastRequestCount) which result into negative value.
77 * @throws InterruptedException
79 public void testNoNegativeSignAtRequestsPerSecond() throws IOException
, InterruptedException
{
80 final TableName TABLENAME
= TableName
.valueOf("t");
81 final String FAMILY
= "f";
82 Admin admin
= UTIL
.getAdmin();
83 UTIL
.createMultiRegionTable(TABLENAME
, FAMILY
.getBytes(),25);
84 Table table
= admin
.getConnection().getTable(TABLENAME
);
85 ServerName serverName
= admin
.getRegionServers().iterator().next();
86 HRegionServer regionServer
= UTIL
.getMiniHBaseCluster().getRegionServer(serverName
);
87 MetricsRegionServerWrapperImpl metricsWrapper
=
88 new MetricsRegionServerWrapperImpl(regionServer
);
89 MetricsRegionServerWrapperImpl
.RegionServerMetricsWrapperRunnable metricsServer
90 = metricsWrapper
.new RegionServerMetricsWrapperRunnable();
92 UTIL
.loadRandomRows(table
, FAMILY
.getBytes(), 1, 2000);
93 Thread
.sleep(METRICS_PERIOD
);
95 admin
.disableTable(TABLENAME
);
96 Thread
.sleep(METRICS_PERIOD
);
98 Assert
.assertTrue(metricsWrapper
.getRequestsPerSecond() > -1);