View Javadoc

1   /**
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.
15   */
16  package org.apache.hadoop.hbase.regionserver;
17  
18  
19  import java.io.IOException;
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.HBaseTestingUtility;
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.TableName;
24  import org.apache.hadoop.hbase.client.Admin;
25  import org.apache.hadoop.hbase.client.Table;
26  import org.apache.hadoop.hbase.testclassification.MediumTests;
27  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
28  import org.junit.AfterClass;
29  import org.junit.Assert;
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  /**
35   * Validate requestsPerSecond metric.
36   */
37  @Category({ RegionServerTests.class, MediumTests.class })
38  public class TestRequestsPerSecondMetric {
39  
40    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
41    private static final long METRICS_PERIOD = 2000L;
42    private static Configuration conf;
43  
44  
45    @BeforeClass
46    public static void setup() throws Exception {
47      conf = UTIL.getConfiguration();
48      conf.setLong(HConstants.REGIONSERVER_METRICS_PERIOD, METRICS_PERIOD);
49      UTIL.startMiniCluster(1);
50    }
51  
52    @AfterClass
53    public static void teardown() throws Exception {
54      UTIL.shutdownMiniCluster();
55    }
56  
57  
58    @Test
59    /**
60     * This test will confirm no negative value in requestsPerSecond metric during any region
61     * transition(close region/remove region/move region).
62     * Firstly, load 2000 random rows for 25 regions and will trigger a metric.
63     * Now, metricCache will have a current read and write requests count.
64     * Next, we disable a table and all of its 25 regions will be closed.
65     * As part of region close, his metric will also be removed from metricCache.
66     * prior to HBASE-23237, we do not remove/reset his metric so we incorrectly compute
67     * (currentRequestCount - lastRequestCount) which result into negative value.
68     *
69     * @throws IOException
70     * @throws InterruptedException
71     */
72    public void testNoNegativeSignAtRequestsPerSecond() throws IOException, InterruptedException {
73      final TableName TABLENAME = TableName.valueOf("t");
74      final String FAMILY = "f";
75      Admin admin = UTIL.getHBaseAdmin();
76      UTIL.createMultiRegionTable(TABLENAME, FAMILY.getBytes(),25);
77      Table table = admin.getConnection().getTable(TABLENAME);
78      HRegionServer regionServer = UTIL.getMiniHBaseCluster().getRegionServer(0);
79      MetricsRegionServerWrapperImpl metricsWrapper  =
80          new MetricsRegionServerWrapperImpl(regionServer);
81      MetricsRegionServerWrapperImpl.RegionServerMetricsWrapperRunnable metricsServer
82          = metricsWrapper.new RegionServerMetricsWrapperRunnable();
83      metricsServer.run();
84      UTIL.loadNumericRows(table, FAMILY.getBytes(), 1, 2000);
85      Thread.sleep(METRICS_PERIOD);
86      metricsServer.run();
87      admin.disableTable(TABLENAME);
88      Thread.sleep(METRICS_PERIOD);
89      metricsServer.run();
90      Assert.assertTrue(metricsWrapper.getRequestsPerSecond() > -1);
91    }
92  }