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 * http://www.apache.org/licenses/LICENSE-2.0
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.apache.hadoop.hbase.regionserver;
017
018
019import java.io.IOException;
020import org.apache.hadoop.conf.Configuration;
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.HBaseTestingUtility;
023import org.apache.hadoop.hbase.HConstants;
024import org.apache.hadoop.hbase.ServerName;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.client.Table;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.testclassification.RegionServerTests;
030import org.junit.AfterClass;
031import org.junit.Assert;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037/**
038 * Validate requestsPerSecond metric.
039 */
040@Category({ RegionServerTests.class, MediumTests.class })
041public class TestRequestsPerSecondMetric {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045      HBaseClassTestRule.forClass(TestRequestsPerSecondMetric.class);
046
047  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
048  private static final long METRICS_PERIOD = 2000L;
049  private static Configuration conf;
050
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
065  @Test
066  /**
067   * This test will confirm no negative value in requestsPerSecond metric during any region
068   * transition(close region/remove region/move region).
069   * Firstly, load 2000 random rows for 25 regions and will trigger a metric.
070   * Now, metricCache will have a current read and write requests count.
071   * Next, we disable a table and all of its 25 regions will be closed.
072   * As part of region close, his metric will also be removed from metricCache.
073   * prior to HBASE-23237, we do not remove/reset his metric so we incorrectly compute
074   * (currentRequestCount - lastRequestCount) which result into negative value.
075   *
076   * @throws IOException
077   * @throws InterruptedException
078   */
079  public void testNoNegativeSignAtRequestsPerSecond() throws IOException, InterruptedException {
080    final TableName TABLENAME = TableName.valueOf("t");
081    final String FAMILY = "f";
082    Admin admin = UTIL.getAdmin();
083    UTIL.createMultiRegionTable(TABLENAME, FAMILY.getBytes(),25);
084    Table table = admin.getConnection().getTable(TABLENAME);
085    ServerName serverName = admin.getRegionServers().iterator().next();
086    HRegionServer regionServer = UTIL.getMiniHBaseCluster().getRegionServer(serverName);
087    MetricsRegionServerWrapperImpl metricsWrapper  =
088        new MetricsRegionServerWrapperImpl(regionServer);
089    MetricsRegionServerWrapperImpl.RegionServerMetricsWrapperRunnable metricsServer
090        = metricsWrapper.new RegionServerMetricsWrapperRunnable();
091    metricsServer.run();
092    UTIL.loadRandomRows(table, FAMILY.getBytes(), 1, 2000);
093    Thread.sleep(METRICS_PERIOD);
094    metricsServer.run();
095    admin.disableTable(TABLENAME);
096    Thread.sleep(METRICS_PERIOD);
097    metricsServer.run();
098    Assert.assertTrue(metricsWrapper.getRequestsPerSecond() > -1);
099  }
100}