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 java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hbase.testclassification.RegionServerTests;
031import org.junit.AfterClass;
032import org.junit.Assert;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038/**
039 * Validate requestsPerSecond metric.
040 */
041@Category({ RegionServerTests.class, MediumTests.class })
042public class TestRequestsPerSecondMetric {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestRequestsPerSecondMetric.class);
047
048  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
049  private static final long METRICS_PERIOD = 2000L;
050  private static Configuration conf;
051
052  @BeforeClass
053  public static void setup() throws Exception {
054    conf = UTIL.getConfiguration();
055    conf.setLong(HConstants.REGIONSERVER_METRICS_PERIOD, METRICS_PERIOD);
056    UTIL.startMiniCluster(1);
057  }
058
059  @AfterClass
060  public static void teardown() throws Exception {
061    UTIL.shutdownMiniCluster();
062  }
063
064  @Test
065  /**
066   * This test will confirm no negative value in requestsPerSecond metric during any region
067   * transition(close region/remove region/move region). Firstly, load 2000 random rows for 25
068   * regions and will trigger a metric. Now, metricCache will have a current read and write requests
069   * count. Next, we disable a table and all of its 25 regions will be closed. As part of region
070   * close, his metric will also be removed from metricCache. prior to HBASE-23237, we do not
071   * remove/reset his metric so we incorrectly compute (currentRequestCount - lastRequestCount)
072   * which result into negative value. nn
073   */
074  public void testNoNegativeSignAtRequestsPerSecond() throws IOException, InterruptedException {
075    final TableName TABLENAME = TableName.valueOf("t");
076    final String FAMILY = "f";
077    Admin admin = UTIL.getAdmin();
078    UTIL.createMultiRegionTable(TABLENAME, FAMILY.getBytes(), 25);
079    Table table = admin.getConnection().getTable(TABLENAME);
080    ServerName serverName = admin.getRegionServers().iterator().next();
081    HRegionServer regionServer = UTIL.getMiniHBaseCluster().getRegionServer(serverName);
082    MetricsRegionServerWrapperImpl metricsWrapper =
083      new MetricsRegionServerWrapperImpl(regionServer);
084    MetricsRegionServerWrapperImpl.RegionServerMetricsWrapperRunnable metricsServer =
085      metricsWrapper.new RegionServerMetricsWrapperRunnable();
086    metricsServer.run();
087    UTIL.loadRandomRows(table, FAMILY.getBytes(), 1, 2000);
088    Thread.sleep(METRICS_PERIOD);
089    metricsServer.run();
090    admin.disableTable(TABLENAME);
091    Thread.sleep(METRICS_PERIOD);
092    metricsServer.run();
093    Assert.assertTrue(metricsWrapper.getRequestsPerSecond() > -1);
094  }
095}