From be0a6cea402ebe59ff910ec56c92c1c5109ff9e8 Mon Sep 17 00:00:00 2001 From: Duo Zhang Date: Mon, 26 Jul 2021 16:57:49 +0800 Subject: [PATCH] HBASE-26119 Polish TestAsyncNonMetaRegionLocator (#3526) Signed-off-by: Xin Sun --- .../client/TestAsyncNonMetaRegionLocator.java | 106 ++++++++---------- 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncNonMetaRegionLocator.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncNonMetaRegionLocator.java index 0ebaf428532..97b18d324b1 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncNonMetaRegionLocator.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncNonMetaRegionLocator.java @@ -31,7 +31,6 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.List; -import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ThreadLocalRandom; @@ -54,14 +53,14 @@ import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil; import org.junit.After; import org.junit.AfterClass; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.junit.runners.Parameterized.Parameter; import org.apache.hbase.thirdparty.com.google.common.io.Closeables; @@ -73,24 +72,21 @@ public class TestAsyncNonMetaRegionLocator { public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestAsyncNonMetaRegionLocator.class); - private static final Logger LOG = LoggerFactory.getLogger(TestAsyncNonMetaRegionLocator.class); - private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); - private static TableName TABLE_NAME = TableName.valueOf("async"); + private static final TableName TABLE_NAME = TableName.valueOf("async"); private static byte[] FAMILY = Bytes.toBytes("cf"); - private static final int META_STOREFILE_REFRESH_PERIOD = 100; private static final int NB_SERVERS = 4; - private static int numOfMetaReplica = NB_SERVERS - 1; - - private static AsyncConnectionImpl CONN; - - private static AsyncNonMetaRegionLocator LOCATOR; - private static ConnectionRegistry registry; + private static final int NUM_OF_META_REPLICA = NB_SERVERS - 1; private static byte[][] SPLIT_KEYS; - private CatalogReplicaMode metaReplicaMode; + + private AsyncConnectionImpl conn; + private AsyncNonMetaRegionLocator locator; + + @Parameter + public CatalogReplicaMode metaReplicaMode; @BeforeClass public static void setUp() throws Exception { @@ -98,16 +94,16 @@ public class TestAsyncNonMetaRegionLocator { // Enable hbase:meta replication. conf.setBoolean(ServerRegionReplicaUtil.REGION_REPLICA_REPLICATION_CATALOG_CONF_KEY, true); - conf.setLong("replication.source.sleepforretries", 10); // 10 ms + conf.setLong("replication.source.sleepforretries", 10); // 10 ms + TEST_UTIL.startMiniCluster(NB_SERVERS); Admin admin = TEST_UTIL.getAdmin(); admin.balancerSwitch(false, true); // Enable hbase:meta replication. - HBaseTestingUtility.setReplicas(admin, TableName.META_TABLE_NAME, numOfMetaReplica); - TEST_UTIL.waitFor(30000, () -> TEST_UTIL.getMiniHBaseCluster().getRegions( - TableName.META_TABLE_NAME).size() >= numOfMetaReplica); + HBaseTestingUtility.setReplicas(admin, TableName.META_TABLE_NAME, NUM_OF_META_REPLICA); + TEST_UTIL.waitFor(30000, () -> TEST_UTIL.getMiniHBaseCluster() + .getRegions(TableName.META_TABLE_NAME).size() >= NUM_OF_META_REPLICA); - registry = ConnectionRegistryFactory.getRegistry(TEST_UTIL.getConfiguration()); SPLIT_KEYS = new byte[8][]; for (int i = 111; i < 999; i += 111) { SPLIT_KEYS[i / 111 - 1] = Bytes.toBytes(String.format("%03d", i)); @@ -116,40 +112,36 @@ public class TestAsyncNonMetaRegionLocator { @AfterClass public static void tearDown() throws Exception { - Closeables.close(CONN, true); TEST_UTIL.shutdownMiniCluster(); } + @Before + public void setUpBeforeTest() throws InterruptedException, ExecutionException, IOException { + Configuration c = new Configuration(TEST_UTIL.getConfiguration()); + // Enable meta replica LoadBalance mode for this connection. + c.set(RegionLocator.LOCATOR_META_REPLICAS_MODE, metaReplicaMode.toString()); + ConnectionRegistry registry = + ConnectionRegistryFactory.getRegistry(TEST_UTIL.getConfiguration()); + conn = new AsyncConnectionImpl(c, registry, null, User.getCurrent()); + locator = new AsyncNonMetaRegionLocator(conn); + } + @After public void tearDownAfterTest() throws IOException { Admin admin = TEST_UTIL.getAdmin(); if (admin.tableExists(TABLE_NAME)) { if (admin.isTableEnabled(TABLE_NAME)) { - TEST_UTIL.getAdmin().disableTable(TABLE_NAME); + admin.disableTable(TABLE_NAME); } - TEST_UTIL.getAdmin().deleteTable(TABLE_NAME); + admin.deleteTable(TABLE_NAME); } - LOCATOR.clearCache(TABLE_NAME); + Closeables.close(conn, true); } @Parameterized.Parameters public static Collection parameters() { - return Arrays.asList(new Object[][] { - { null }, - { CatalogReplicaMode.LOAD_BALANCE.toString() } - }); - } - - public TestAsyncNonMetaRegionLocator(String clientMetaReplicaMode) throws Exception { - Configuration c = new Configuration(TEST_UTIL.getConfiguration()); - // Enable meta replica LoadBalance mode for this connection. - if (clientMetaReplicaMode != null) { - c.set(RegionLocator.LOCATOR_META_REPLICAS_MODE, clientMetaReplicaMode); - metaReplicaMode = CatalogReplicaMode.fromString(clientMetaReplicaMode); - } - - CONN = new AsyncConnectionImpl(c, registry, registry.getClusterId().get(), User.getCurrent()); - LOCATOR = new AsyncNonMetaRegionLocator(CONN); + return Arrays + .asList(new Object[][] { { CatalogReplicaMode.NONE }, { CatalogReplicaMode.LOAD_BALANCE } }); } private void createSingleRegionTable() throws IOException, InterruptedException { @@ -158,8 +150,8 @@ public class TestAsyncNonMetaRegionLocator { } private CompletableFuture getDefaultRegionLocation(TableName tableName, - byte[] row, RegionLocateType locateType, boolean reload) { - return LOCATOR + byte[] row, RegionLocateType locateType, boolean reload) { + return locator .getRegionLocations(tableName, row, RegionReplicaUtil.DEFAULT_REPLICA_ID, locateType, reload) .thenApply(RegionLocations::getDefaultRegionLocation); } @@ -189,7 +181,7 @@ public class TestAsyncNonMetaRegionLocator { } private void assertLocEquals(byte[] startKey, byte[] endKey, ServerName serverName, - HRegionLocation loc) { + HRegionLocation loc) { RegionInfo info = loc.getRegion(); assertEquals(TABLE_NAME, info.getTable()); assertArrayEquals(startKey, info.getStartKey()); @@ -259,7 +251,7 @@ public class TestAsyncNonMetaRegionLocator { } })); - LOCATOR.clearCache(TABLE_NAME); + locator.clearCache(TABLE_NAME); IntStream.range(0, 2).forEach(n -> IntStream.range(0, startKeys.length).forEach(i -> { try { assertLocEquals(startKeys[i], i == startKeys.length - 1 ? EMPTY_END_ROW : startKeys[i + 1], @@ -270,7 +262,7 @@ public class TestAsyncNonMetaRegionLocator { } })); - LOCATOR.clearCache(TABLE_NAME); + locator.clearCache(TABLE_NAME); byte[][] endKeys = getEndKeys(); IntStream.range(0, 2).forEach( n -> IntStream.range(0, endKeys.length).map(i -> endKeys.length - 1 - i).forEach(i -> { @@ -302,11 +294,11 @@ public class TestAsyncNonMetaRegionLocator { // Should be same as it is in cache assertSame(loc, getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get()); - LOCATOR.updateCachedLocationOnError(loc, null); + locator.updateCachedLocationOnError(loc, null); // null error will not trigger a cache cleanup assertSame(loc, getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get()); - LOCATOR.updateCachedLocationOnError(loc, new NotServingRegionException()); + locator.updateCachedLocationOnError(loc, new NotServingRegionException()); assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, newServerName, getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get()); } @@ -345,7 +337,7 @@ public class TestAsyncNonMetaRegionLocator { byte[][] endKeys = getEndKeys(); ServerName[] serverNames = getLocations(startKeys); for (int i = 0; i < 100; i++) { - LOCATOR.clearCache(TABLE_NAME); + locator.clearCache(TABLE_NAME); List> futures = IntStream.range(0, 1000).mapToObj(n -> String.format("%03d", n)).map(s -> Bytes.toBytes(s)) .map(r -> getDefaultRegionLocation(TABLE_NAME, r, RegionLocateType.CURRENT, false)) @@ -395,8 +387,9 @@ public class TestAsyncNonMetaRegionLocator { TEST_UTIL.waitFor(3000, new ExplainingPredicate() { @Override public boolean evaluate() throws Exception { - HRegionLocation loc = getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, - RegionLocateType.CURRENT, true).get(); + HRegionLocation loc = + getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, true) + .get(); return newServerName.equals(loc.getServerName()); } @@ -416,7 +409,7 @@ public class TestAsyncNonMetaRegionLocator { // Testcase for HBASE-20822 @Test public void testLocateBeforeLastRegion() - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException { createMultiRegionTable(); getDefaultRegionLocation(TABLE_NAME, SPLIT_KEYS[0], RegionLocateType.CURRENT, false).join(); HRegionLocation loc = @@ -434,14 +427,14 @@ public class TestAsyncNonMetaRegionLocator { @Override public void updateCachedLocationOnError(HRegionLocation loc, Throwable error) - throws Exception { - LOCATOR.updateCachedLocationOnError(loc, error); + throws Exception { + locator.updateCachedLocationOnError(loc, error); } @Override public RegionLocations getRegionLocations(TableName tableName, int replicaId, boolean reload) - throws Exception { - return LOCATOR.getRegionLocations(tableName, EMPTY_START_ROW, replicaId, + throws Exception { + return locator.getRegionLocations(tableName, EMPTY_START_ROW, replicaId, RegionLocateType.CURRENT, reload).get(); } }); @@ -462,9 +455,8 @@ public class TestAsyncNonMetaRegionLocator { public void testConcurrentUpdateCachedLocationOnError() throws Exception { createSingleRegionTable(); HRegionLocation loc = - getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false) - .get(); + getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get(); IntStream.range(0, 100).parallel() - .forEach(i -> LOCATOR.updateCachedLocationOnError(loc, new NotServingRegionException())); + .forEach(i -> locator.updateCachedLocationOnError(loc, new NotServingRegionException())); } }