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.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.testclassification.RegionServerTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.jupiter.api.BeforeEach;
027import org.junit.jupiter.api.Tag;
028import org.junit.jupiter.api.Test;
029
030@Tag(RegionServerTests.TAG)
031@Tag(SmallTests.TAG)
032public class TestMetricsTableMetricsMap {
033
034  private String tableName = "testTableMetricsMap";
035
036  private MetricsTableWrapperStub tableWrapper;
037  private MetricsTable mt;
038  private MetricsRegionServerWrapper rsWrapper;
039  private MetricsRegionServer rsm;
040  private MetricsTableAggregateSourceImpl agg;
041
042  @BeforeEach
043  public void setUp() {
044    Configuration conf = new Configuration();
045
046    tableWrapper = new MetricsTableWrapperStub(tableName);
047    mt = new MetricsTable(tableWrapper);
048    rsWrapper = new MetricsRegionServerWrapperStub();
049
050    rsm = new MetricsRegionServer(rsWrapper, conf, mt);
051    MetricsTableAggregateSource tableSourceAgg = mt.getTableSourceAgg();
052    if (tableSourceAgg instanceof MetricsTableAggregateSourceImpl) {
053      agg = (MetricsTableAggregateSourceImpl) tableSourceAgg;
054    } else {
055      throw new RuntimeException(
056        "tableSourceAgg should be the instance of MetricsTableAggregateSourceImpl");
057    }
058  }
059
060  @Test
061  public void testMetricsMap() throws InterruptedException {
062    // do major compaction
063    rsm.updateCompaction(tableName, true, 100, 200, 300, 400, 500);
064
065    int metricsMapSize = agg.getMetricsRegistry().getMetricsMap().size();
066    assertTrue(metricsMapSize > 0, "table metrics added then metricsMapSize should larger than 0");
067
068    // just for metrics update
069    Thread.sleep(1000);
070    // delete table all metrics
071    agg.deleteTableSource(tableName);
072
073    metricsMapSize = agg.getMetricsRegistry().getMetricsMap().size();
074    assertEquals(0, metricsMapSize, "table metrics all deleted then metricsSize should be 0");
075  }
076}