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
091  @Test
092  public void testRegionMetrics() throws Exception {
093
094    // Check if regions match with the RegionMetrics from the server
095    for (ServerName serverName : admin.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS))
096        .getLiveServerMetrics().keySet()) {
097      List<RegionInfo> regions = admin.getRegions(serverName);
098      Collection<RegionMetrics> regionMetricsList =
099          admin.getRegionMetrics(serverName);
100      checkRegionsAndRegionMetrics(regions, regionMetricsList);
101    }
102
103    // Check if regionMetrics matches the table's regions and nothing is missed
104    for (TableName table : new TableName[] { TABLE_1, TABLE_2, TABLE_3 }) {
105      List<RegionInfo> tableRegions = admin.getRegions(table);
106
107      List<RegionMetrics> regionMetrics = new ArrayList<>();
108      for (ServerName serverName : admin.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS))
109          .getLiveServerMetrics().keySet()) {
110        regionMetrics.addAll(admin.getRegionMetrics(serverName, table));
111      }
112      checkRegionsAndRegionMetrics(tableRegions, regionMetrics);
113    }
114
115    // Just wait here. If this fixes the test, come back and do a better job.
116    // Would have to redo the below so can wait on cluster status changing.
117    // Admin#getClusterMetrics retrieves data from HMaster. Admin#getRegionMetrics, by contrast,
118    // get the data from RS. Hence, it will fail if we do the assert check before RS has done
119    // the report.
120    TimeUnit.MILLISECONDS.sleep(3 * MSG_INTERVAL);
121
122    // Check RegionMetrics matches the RegionMetrics from ClusterMetrics
123    for (Map.Entry<ServerName, ServerMetrics> entry : admin
124      .getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)).getLiveServerMetrics().entrySet()) {
125      ServerName serverName = entry.getKey();
126      ServerMetrics serverMetrics = entry.getValue();
127      List<RegionMetrics> regionMetrics = admin.getRegionMetrics(serverName);
128      LOG.debug("serverName=" + serverName + ", getRegionLoads=" +
129        serverMetrics.getRegionMetrics().keySet().stream().map(r -> Bytes.toString(r)).
130          collect(Collectors.toList()));
131      LOG.debug("serverName=" + serverName + ", regionLoads=" +
132        regionMetrics.stream().map(r -> Bytes.toString(r.getRegionName())).
133          collect(Collectors.toList()));
134      assertEquals(serverMetrics.getRegionMetrics().size(), regionMetrics.size());
135      checkMetricsValue(regionMetrics, serverMetrics);
136    }
137  }
138
139  private void checkMetricsValue(List<RegionMetrics> regionMetrics, ServerMetrics serverMetrics)
140    throws InvocationTargetException, IllegalAccessException {
141    for (RegionMetrics fromRM : regionMetrics) {
142      RegionMetrics fromSM = serverMetrics.getRegionMetrics().get(fromRM.getRegionName());
143      Class clazz = RegionMetrics.class;
144      for (Method method : clazz.getMethods()) {
145        // check numeric values only
146        if (method.getReturnType().equals(Size.class)
147          || method.getReturnType().equals(int.class)
148          || method.getReturnType().equals(long.class)
149          || method.getReturnType().equals(float.class)) {
150          Object valueRm = method.invoke(fromRM);
151          Object valueSM = method.invoke(fromSM);
152          assertEquals("Return values of method " + method.getName() + " are different",
153              valueRm.toString(), valueSM.toString());
154        }
155      }
156    }
157  }
158
159  private void checkRegionsAndRegionMetrics(Collection<RegionInfo> regions,
160      Collection<RegionMetrics> regionMetrics) {
161
162    assertEquals("No of regions and regionMetrics doesn't match", regions.size(),
163        regionMetrics.size());
164
165    Map<byte[], RegionMetrics> regionMetricsMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
166    for (RegionMetrics r : regionMetrics) {
167      regionMetricsMap.put(r.getRegionName(), r);
168    }
169    for (RegionInfo info : regions) {
170      assertTrue("Region not in RegionMetricsMap region:"
171          + info.getRegionNameAsString() + " regionMap: "
172          + regionMetricsMap, regionMetricsMap.containsKey(info.getRegionName()));
173    }
174  }
175}