From d6e1d18be901bd969be976ad8d20359e876e6cd0 Mon Sep 17 00:00:00 2001 From: Duo Zhang Date: Tue, 27 Nov 2018 16:14:44 +0800 Subject: [PATCH] HBASE-21517 Move the getTableRegionForRow method from HMaster to TestMaster --- .../apache/hadoop/hbase/master/HMaster.java | 35 ------------------- .../hadoop/hbase/master/TestMaster.java | 34 ++++++++++++++++-- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java index 428030d4f93..132e2714e1d 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java @@ -47,7 +47,6 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -84,7 +83,6 @@ import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; import org.apache.hadoop.hbase.client.MasterSwitchType; import org.apache.hadoop.hbase.client.RegionInfo; import org.apache.hadoop.hbase.client.RegionInfoBuilder; -import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.TableDescriptor; import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.client.TableState; @@ -2626,39 +2624,6 @@ public class HMaster extends HRegionServer implements MasterServices { }); } - /** - * Return the region and current deployment for the region containing - * the given row. If the region cannot be found, returns null. If it - * is found, but not currently deployed, the second element of the pair - * may be null. - */ - @VisibleForTesting // Used by TestMaster. - Pair getTableRegionForRow( - final TableName tableName, final byte [] rowKey) - throws IOException { - final AtomicReference> result = new AtomicReference<>(null); - - MetaTableAccessor.Visitor visitor = new MetaTableAccessor.Visitor() { - @Override - public boolean visit(Result data) throws IOException { - if (data == null || data.size() <= 0) { - return true; - } - Pair pair = - new Pair<>(MetaTableAccessor.getRegionInfo(data), - MetaTableAccessor.getServerName(data,0)); - if (!pair.getFirst().getTable().equals(tableName)) { - return false; - } - result.set(pair); - return true; - } - }; - - MetaTableAccessor.scanMeta(clusterConnection, visitor, tableName, rowKey, 1); - return result.get(); - } - private long modifyTable(final TableName tableName, final TableDescriptorGetter newDescriptorGetter, final long nonceGroup, final long nonce) throws IOException { diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMaster.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMaster.java index af89cbcf4dd..7fdf60101c7 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMaster.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMaster.java @@ -26,6 +26,7 @@ import static org.junit.Assert.fail; import java.io.IOException; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; @@ -43,6 +44,7 @@ import org.apache.hadoop.hbase.UnknownRegionException; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.RegionInfo; import org.apache.hadoop.hbase.client.RegionInfoBuilder; +import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.client.TableState; import org.apache.hadoop.hbase.testclassification.MasterTests; @@ -94,6 +96,35 @@ public class TestMaster { TEST_UTIL.shutdownMiniCluster(); } + /** + * Return the region and current deployment for the region containing the given row. If the region + * cannot be found, returns null. If it is found, but not currently deployed, the second element + * of the pair may be null. + */ + private Pair getTableRegionForRow(HMaster master, TableName tableName, + byte[] rowKey) throws IOException { + final AtomicReference> result = new AtomicReference<>(null); + + MetaTableAccessor.Visitor visitor = new MetaTableAccessor.Visitor() { + @Override + public boolean visit(Result data) throws IOException { + if (data == null || data.size() <= 0) { + return true; + } + Pair pair = new Pair<>(MetaTableAccessor.getRegionInfo(data), + MetaTableAccessor.getServerName(data, 0)); + if (!pair.getFirst().getTable().equals(tableName)) { + return false; + } + result.set(pair); + return true; + } + }; + + MetaTableAccessor.scanMeta(master.getConnection(), visitor, tableName, rowKey, 1); + return result.get(); + } + @Test @SuppressWarnings("deprecation") public void testMasterOpsWhileSplitting() throws Exception { @@ -128,8 +159,7 @@ public class TestMaster { // We have three regions because one is split-in-progress assertEquals(3, tableRegions.size()); LOG.info("Making sure we can call getTableRegionClosest while opening"); - Pair pair = - m.getTableRegionForRow(TABLENAME, Bytes.toBytes("cde")); + Pair pair = getTableRegionForRow(m, TABLENAME, Bytes.toBytes("cde")); LOG.info("Result is: " + pair); Pair tableRegionFromName = MetaTableAccessor.getRegion(m.getConnection(),