Skip to content

Commit

Permalink
Revert "HBASE-22744 Removed deprecated status and load classes in cli…
Browse files Browse the repository at this point in the history
…ent module"

This reverts commit 4a61c8b.
  • Loading branch information
HorizonNet committed Jul 31, 2019
1 parent caa0535 commit 24b970e
Show file tree
Hide file tree
Showing 11 changed files with 1,784 additions and 49 deletions.
400 changes: 400 additions & 0 deletions hbase-client/src/main/java/org/apache/hadoop/hbase/ClusterStatus.java

Large diffs are not rendered by default.

421 changes: 421 additions & 0 deletions hbase-client/src/main/java/org/apache/hadoop/hbase/RegionLoad.java

Large diffs are not rendered by default.

596 changes: 596 additions & 0 deletions hbase-client/src/main/java/org/apache/hadoop/hbase/ServerLoad.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetOnlineRegionResponse;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetRegionInfoRequest;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetRegionInfoResponse;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetRegionLoadResponse;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetServerInfoRequest;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetServerInfoResponse;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetStoreFileRequest;
Expand Down Expand Up @@ -1733,6 +1734,16 @@ public static org.apache.hadoop.hbase.client.RegionInfo getRegionInfo(final RpcC
}
}

public static List<org.apache.hadoop.hbase.RegionLoad> getRegionLoadInfo(
GetRegionLoadResponse regionLoadResponse) {
List<org.apache.hadoop.hbase.RegionLoad> regionLoadList =
new ArrayList<>(regionLoadResponse.getRegionLoadsCount());
for (RegionLoad regionLoad : regionLoadResponse.getRegionLoadsList()) {
regionLoadList.add(new org.apache.hadoop.hbase.RegionLoad(regionLoad));
}
return regionLoadList;
}

/**
* A helper to close a region given a region name
* using admin protocol.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static void setUpBeforeClass() throws Exception {
TEST_UTIL.waitFor(6000, new Waiter.Predicate<IOException>() {
@Override
public boolean evaluate() throws IOException {
return TEST_UTIL.getMiniHBaseCluster().getClusterMetrics().getAverageLoad() > 0;
return TEST_UTIL.getMiniHBaseCluster().getClusterStatus().getAverageLoad() > 0;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public MiniHBaseCluster(Configuration conf, int numMasters, int numRegionServers
CompatibilityFactory.getInstance(MetricsAssertHelper.class).init();

init(numMasters, numRegionServers, rsPorts, masterClass, regionserverClass);
this.initialClusterStatus = getClusterMetrics();
this.initialClusterStatus = getClusterStatus();
}

public Configuration getConfiguration() {
Expand Down Expand Up @@ -435,9 +435,9 @@ public JVMClusterUtil.RegionServerThread startRegionServerAndWait(long timeout)
ServerName rsServerName = t.getRegionServer().getServerName();

long start = System.currentTimeMillis();
ClusterMetrics clusterStatus = getClusterMetrics();
ClusterStatus clusterStatus = getClusterStatus();
while ((System.currentTimeMillis() - start) < timeout) {
if (clusterStatus != null && clusterStatus.getLiveServerMetrics().containsKey(rsServerName)) {
if (clusterStatus != null && clusterStatus.getServers().contains(rsServerName)) {
return t;
}
Threads.sleep(100);
Expand Down Expand Up @@ -659,6 +659,16 @@ public void shutdown() throws IOException {
public void close() throws IOException {
}

/**
* @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
* Use {@link #getClusterMetrics()} instead.
*/
@Deprecated
public ClusterStatus getClusterStatus() throws IOException {
HMaster master = getMaster();
return master == null ? null : new ClusterStatus(master.getClusterMetrics());
}

@Override
public ClusterMetrics getClusterMetrics() throws IOException {
HMaster master = getMaster();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public void testNone() throws Exception {
// or more requests than expected.
Assert.assertEquals(status0.getLiveServerMetrics().size(),
status1.getLiveServerMetrics().size());
checkPbObjectNotNull(new ClusterStatus(status0));
checkPbObjectNotNull(new ClusterStatus(status1));
}

@Test
Expand All @@ -107,26 +109,28 @@ public void testLiveAndDeadServersStatus() throws Exception {
Waiter.waitFor(CLUSTER.getConfiguration(), 10 * 1000, 100, new Predicate<Exception>() {
@Override
public boolean evaluate() throws Exception {
ClusterMetrics status = ADMIN.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS));
ClusterStatus status
= new ClusterStatus(ADMIN.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)));
Assert.assertNotNull(status);
return status.getRegionCount() > 0;
return status.getRegionsCount() > 0;
}
});
// Retrieve live servers and dead servers info.
EnumSet<Option> options =
EnumSet.of(Option.LIVE_SERVERS, Option.DEAD_SERVERS, Option.SERVERS_NAME);
ClusterMetrics status = ADMIN.getClusterMetrics(options);
ClusterStatus status = new ClusterStatus(ADMIN.getClusterMetrics(options));
checkPbObjectNotNull(status);
Assert.assertNotNull(status);
Assert.assertNotNull(status.getLiveServerMetrics().keySet());
Assert.assertNotNull(status.getServers());
// exclude a dead region server
Assert.assertEquals(SLAVES -1, numRs);
// live servers = nums of regionservers
// By default, HMaster don't carry any regions so it won't report its load.
// Hence, it won't be in the server list.
Assert.assertEquals(status.getLiveServerMetrics().keySet().size(), numRs);
Assert.assertTrue(status.getRegionCount() > 0);
Assert.assertEquals(status.getServers().size(), numRs);
Assert.assertTrue(status.getRegionsCount() > 0);
Assert.assertNotNull(status.getDeadServerNames());
Assert.assertEquals(1, status.getDeadServerNames().size());
Assert.assertEquals(1, status.getDeadServersSize());
ServerName deadServerName = status.getDeadServerNames().iterator().next();
Assert.assertEquals(DEAD.getServerName(), deadServerName);
Assert.assertNotNull(status.getServersName());
Expand Down Expand Up @@ -154,18 +158,18 @@ public void testMasterAndBackupMastersStatus() throws Exception {
Assert.assertEquals(MASTERS, masterThreads.size());
// Retrieve master and backup masters infos only.
EnumSet<Option> options = EnumSet.of(Option.MASTER, Option.BACKUP_MASTERS);
ClusterMetrics status = ADMIN.getClusterMetrics(options);
Assert.assertTrue(status.getMasterName().equals(activeName));
Assert.assertEquals(MASTERS - 1, status.getBackupMasterNames().size());
ClusterStatus status = new ClusterStatus(ADMIN.getClusterMetrics(options));
Assert.assertTrue(status.getMaster().equals(activeName));
Assert.assertEquals(MASTERS - 1, status.getBackupMastersSize());
}

@Test
public void testOtherStatusInfos() throws Exception {
EnumSet<Option> options =
EnumSet.of(Option.MASTER_COPROCESSORS, Option.HBASE_VERSION,
Option.CLUSTER_ID, Option.BALANCER_ON);
ClusterMetrics status = ADMIN.getClusterMetrics(options);
Assert.assertTrue(status.getMasterCoprocessorNames().size() == 1);
ClusterStatus status = new ClusterStatus(ADMIN.getClusterMetrics(options));
Assert.assertTrue(status.getMasterCoprocessors().length == 1);
Assert.assertNotNull(status.getHBaseVersion());
Assert.assertNotNull(status.getClusterId());
Assert.assertTrue(status.getAverageLoad() == 0.0);
Expand All @@ -188,6 +192,21 @@ public void testObserver() throws IOException {
Assert.assertEquals(postCount + 1, MyObserver.POST_COUNT.get());
}

/**
* HBASE-19496 do the refactor for ServerLoad and RegionLoad so the inner pb object is useless
* now. However, they are Public classes, and consequently we must make sure the all pb objects
* have initialized.
*/
private static void checkPbObjectNotNull(ClusterStatus status) {
for (ServerName name : status.getLiveServerMetrics().keySet()) {
ServerLoad load = status.getLoad(name);
Assert.assertNotNull(load.obtainServerLoadPB());
for (RegionLoad rl : load.getRegionsLoad().values()) {
Assert.assertNotNull(rl.regionLoadPB);
}
}
}

public static class MyObserver implements MasterCoprocessor, MasterObserver {
private static final AtomicInteger PRE_COUNT = new AtomicInteger(0);
private static final AtomicInteger POST_COUNT = new AtomicInteger(0);
Expand Down
180 changes: 180 additions & 0 deletions hbase-server/src/test/java/org/apache/hadoop/hbase/TestRegionLoad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.hadoop.hbase.ClusterMetrics.Option;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.RegionInfo;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
import org.apache.hbase.thirdparty.com.google.common.collect.Maps;

@Category({MiscTests.class, MediumTests.class})
public class TestRegionLoad {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestRegionLoad.class);

private static final Logger LOG = LoggerFactory.getLogger(TestRegionLoad.class);
private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
private static Admin admin;

private static final TableName TABLE_1 = TableName.valueOf("table_1");
private static final TableName TABLE_2 = TableName.valueOf("table_2");
private static final TableName TABLE_3 = TableName.valueOf("table_3");
private static final TableName[] tables = new TableName[]{TABLE_1, TABLE_2, TABLE_3};
private static final int MSG_INTERVAL = 500; // ms

@BeforeClass
public static void beforeClass() throws Exception {
// Make servers report eagerly. This test is about looking at the cluster status reported.
// Make it so we don't have to wait around too long to see change.
UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", MSG_INTERVAL);
UTIL.startMiniCluster(4);
admin = UTIL.getAdmin();
admin.balancerSwitch(false, true);
createTables();
}

@AfterClass
public static void afterClass() throws Exception {
UTIL.shutdownMiniCluster();
}

private static void createTables() throws IOException, InterruptedException {
byte[][] FAMILIES = new byte [][] {Bytes.toBytes("f")};
for (TableName tableName : tables) {
Table table =
UTIL.createTable(tableName, FAMILIES, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
UTIL.waitTableAvailable(tableName);
UTIL.loadTable(table, FAMILIES[0]);
}
}

@Test
public void testRegionLoad() throws Exception {

// Check if regions match with the regionLoad from the server
for (ServerName serverName : admin
.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)).getLiveServerMetrics().keySet()) {
List<RegionInfo> regions = admin.getRegions(serverName);
LOG.info("serverName=" + serverName + ", regions=" +
regions.stream().map(r -> r.getRegionNameAsString()).collect(Collectors.toList()));
Collection<RegionLoad> regionLoads = admin.getRegionMetrics(serverName)
.stream().map(r -> new RegionLoad(r)).collect(Collectors.toList());
LOG.info("serverName=" + serverName + ", regionLoads=" +
regionLoads.stream().map(r -> Bytes.toString(r.getRegionName())).
collect(Collectors.toList()));
checkRegionsAndRegionLoads(regions, regionLoads);
}

// Check if regionLoad matches the table's regions and nothing is missed
for (TableName table : new TableName[]{TABLE_1, TABLE_2, TABLE_3}) {
List<RegionInfo> tableRegions = admin.getRegions(table);

List<RegionLoad> regionLoads = Lists.newArrayList();
for (ServerName serverName : admin
.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)).getLiveServerMetrics().keySet()) {
regionLoads.addAll(admin.getRegionMetrics(serverName, table)
.stream().map(r -> new RegionLoad(r)).collect(Collectors.toList()));
}
checkRegionsAndRegionLoads(tableRegions, regionLoads);
}

// Just wait here. If this fixes the test, come back and do a better job.
// Would have to redo the below so can wait on cluster status changing.
// Admin#getClusterMetrics retrieves data from HMaster. Admin#getRegionMetrics, by contrast,
// get the data from RS. Hence, it will fail if we do the assert check before RS has done
// the report.
TimeUnit.MILLISECONDS.sleep(3 * MSG_INTERVAL);

// Check RegionLoad matches the regionLoad from ClusterStatus
ClusterStatus clusterStatus
= new ClusterStatus(admin.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)));
for (ServerName serverName : clusterStatus.getServers()) {
ServerLoad serverLoad = clusterStatus.getLoad(serverName);
Map<byte[], RegionLoad> regionLoads = admin.getRegionMetrics(serverName).stream()
.collect(Collectors.toMap(e -> e.getRegionName(), e -> new RegionLoad(e),
(v1, v2) -> {
throw new RuntimeException("impossible!!");
}, () -> new TreeMap<>(Bytes.BYTES_COMPARATOR)));
LOG.debug("serverName=" + serverName + ", getRegionLoads=" +
serverLoad.getRegionsLoad().keySet().stream().map(r -> Bytes.toString(r)).
collect(Collectors.toList()));
LOG.debug("serverName=" + serverName + ", regionLoads=" +
regionLoads.keySet().stream().map(r -> Bytes.toString(r)).
collect(Collectors.toList()));
compareRegionLoads(serverLoad.getRegionsLoad(), regionLoads);
}
}

private void compareRegionLoads(Map<byte[], RegionLoad> regionLoadCluster,
Map<byte[], RegionLoad> regionLoads) {

assertEquals("No of regionLoads from clusterStatus and regionloads from RS doesn't match",
regionLoadCluster.size(), regionLoads.size());

// The contents of region load from cluster and server should match
for (byte[] regionName : regionLoadCluster.keySet()) {
regionLoads.remove(regionName);
}
assertEquals("regionLoads from SN should be empty", 0, regionLoads.size());
}

private void checkRegionsAndRegionLoads(Collection<RegionInfo> regions,
Collection<RegionLoad> regionLoads) {
for (RegionLoad load : regionLoads) {
assertNotNull(load.regionLoadPB);
}

assertEquals("No of regions and regionloads doesn't match", regions.size(), regionLoads.size());

Map<byte[], RegionLoad> regionLoadMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
for (RegionLoad regionLoad : regionLoads) {
regionLoadMap.put(regionLoad.getName(), regionLoad);
}
for (RegionInfo info : regions) {
assertTrue("Region not in regionLoadMap region:" + info.getRegionNameAsString() +
" regionMap: " + regionLoadMap, regionLoadMap.containsKey(info.getRegionName()));
}
}
}
Loading

0 comments on commit 24b970e

Please sign in to comment.