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