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;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.lang.reflect.InvocationTargetException;
024import java.lang.reflect.Method;
025import java.util.ArrayList;
026import java.util.Collection;
027import java.util.EnumSet;
028import java.util.List;
029import java.util.Map;
030import java.util.concurrent.TimeUnit;
031import java.util.stream.Collectors;
032import org.apache.hadoop.hbase.ClusterMetrics.Option;
033import org.apache.hadoop.hbase.client.Admin;
034import org.apache.hadoop.hbase.client.RegionInfo;
035import org.apache.hadoop.hbase.client.Table;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.testclassification.MiscTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.junit.AfterClass;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047import org.apache.hbase.thirdparty.com.google.common.collect.Maps;
048
049@Category({ MiscTests.class, MediumTests.class })
050public class TestRegionMetrics {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestRegionMetrics.class);
055
056  private static final Logger LOG = LoggerFactory.getLogger(TestRegionMetrics.class);
057  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
058  private static Admin admin;
059
060  private static final TableName TABLE_1 = TableName.valueOf("table_1");
061  private static final TableName TABLE_2 = TableName.valueOf("table_2");
062  private static final TableName TABLE_3 = TableName.valueOf("table_3");
063  private static final TableName[] tables = new TableName[] { TABLE_1, TABLE_2, TABLE_3 };
064  private static final int MSG_INTERVAL = 500; // ms
065
066  @BeforeClass
067  public static void beforeClass() throws Exception {
068    // Make servers report eagerly. This test is about looking at the cluster status reported.
069    // Make it so we don't have to wait around too long to see change.
070    UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", MSG_INTERVAL);
071    UTIL.startMiniCluster(4);
072    admin = UTIL.getAdmin();
073    admin.balancerSwitch(false, true);
074    byte[] FAMILY = Bytes.toBytes("f");
075    for (TableName tableName : tables) {
076      Table table = UTIL.createMultiRegionTable(tableName, FAMILY, 16);
077      UTIL.waitTableAvailable(tableName);
078      UTIL.loadTable(table, FAMILY);
079    }
080  }
081
082  @AfterClass
083  public static void afterClass() throws Exception {
084    for (TableName table : tables) {
085      UTIL.deleteTableIfAny(table);
086    }
087    UTIL.shutdownMiniCluster();
088  }
089
090  @Test
091  public void testRegionMetrics() throws Exception {
092
093    // Check if regions match with the RegionMetrics from the server
094    for (ServerName serverName : admin.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS))
095      .getLiveServerMetrics().keySet()) {
096      List<RegionInfo> regions = admin.getRegions(serverName);
097      Collection<RegionMetrics> regionMetricsList = admin.getRegionMetrics(serverName);
098      checkRegionsAndRegionMetrics(regions, regionMetricsList);
099    }
100
101    // Check if regionMetrics matches the table's regions and nothing is missed
102    for (TableName table : new TableName[] { TABLE_1, TABLE_2, TABLE_3 }) {
103      List<RegionInfo> tableRegions = admin.getRegions(table);
104
105      List<RegionMetrics> regionMetrics = new ArrayList<>();
106      for (ServerName serverName : admin.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS))
107        .getLiveServerMetrics().keySet()) {
108        regionMetrics.addAll(admin.getRegionMetrics(serverName, table));
109      }
110      checkRegionsAndRegionMetrics(tableRegions, regionMetrics);
111    }
112
113    // Just wait here. If this fixes the test, come back and do a better job.
114    // Would have to redo the below so can wait on cluster status changing.
115    // Admin#getClusterMetrics retrieves data from HMaster. Admin#getRegionMetrics, by contrast,
116    // get the data from RS. Hence, it will fail if we do the assert check before RS has done
117    // the report.
118    TimeUnit.MILLISECONDS.sleep(3 * MSG_INTERVAL);
119
120    // Check RegionMetrics matches the RegionMetrics from ClusterMetrics
121    for (Map.Entry<ServerName, ServerMetrics> entry : admin
122      .getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)).getLiveServerMetrics().entrySet()) {
123      ServerName serverName = entry.getKey();
124      ServerMetrics serverMetrics = entry.getValue();
125      List<RegionMetrics> regionMetrics = admin.getRegionMetrics(serverName);
126      LOG.debug("serverName=" + serverName + ", getRegionLoads=" + serverMetrics.getRegionMetrics()
127        .keySet().stream().map(r -> Bytes.toString(r)).collect(Collectors.toList()));
128      LOG.debug("serverName=" + serverName + ", regionLoads=" + regionMetrics.stream()
129        .map(r -> Bytes.toString(r.getRegionName())).collect(Collectors.toList()));
130      assertEquals(serverMetrics.getRegionMetrics().size(), regionMetrics.size());
131      checkMetricsValue(regionMetrics, serverMetrics);
132    }
133  }
134
135  private void checkMetricsValue(List<RegionMetrics> regionMetrics, ServerMetrics serverMetrics)
136    throws InvocationTargetException, IllegalAccessException {
137    for (RegionMetrics fromRM : regionMetrics) {
138      RegionMetrics fromSM = serverMetrics.getRegionMetrics().get(fromRM.getRegionName());
139      Class clazz = RegionMetrics.class;
140      for (Method method : clazz.getMethods()) {
141        // check numeric values only
142        if (
143          method.getReturnType().equals(Size.class) || method.getReturnType().equals(int.class)
144            || method.getReturnType().equals(long.class)
145            || method.getReturnType().equals(float.class)
146        ) {
147          Object valueRm = method.invoke(fromRM);
148          Object valueSM = method.invoke(fromSM);
149          assertEquals("Return values of method " + method.getName() + " are different",
150            valueRm.toString(), valueSM.toString());
151        }
152      }
153    }
154  }
155
156  private void checkRegionsAndRegionMetrics(Collection<RegionInfo> regions,
157    Collection<RegionMetrics> regionMetrics) {
158
159    assertEquals("No of regions and regionMetrics doesn't match", regions.size(),
160      regionMetrics.size());
161
162    Map<byte[], RegionMetrics> regionMetricsMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
163    for (RegionMetrics r : regionMetrics) {
164      regionMetricsMap.put(r.getRegionName(), r);
165    }
166    for (RegionInfo info : regions) {
167      assertTrue("Region not in RegionMetricsMap region:" + info.getRegionNameAsString()
168        + " regionMap: " + regionMetricsMap, regionMetricsMap.containsKey(info.getRegionName()));
169    }
170  }
171}