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