From 750e8ee508a621435ee3b1f4c2299c156d1ba243 Mon Sep 17 00:00:00 2001 From: Guanghao Zhang Date: Thu, 19 Jan 2017 15:13:45 +0800 Subject: [PATCH] HBASE-17498 Implement listTables and listTableNames methods --- .../org/apache/hadoop/hbase/client/AsyncAdmin.java | 61 ++++++++++++++++++++-- .../hadoop/hbase/client/AsyncHBaseAdmin.java | 60 +++++++++++++++++++-- .../apache/hadoop/hbase/client/TestAsyncAdmin.java | 59 +++++++++++++++++++++ 3 files changed, 171 insertions(+), 9 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java index fadeebe..2b6ae88 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java @@ -17,9 +17,11 @@ */ package org.apache.hadoop.hbase.client; -import java.io.IOException; import java.util.concurrent.CompletableFuture; +import java.util.regex.Pattern; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.classification.InterfaceStability; @@ -31,11 +33,60 @@ import org.apache.hadoop.hbase.classification.InterfaceStability; public interface AsyncAdmin { /** + * List all the userspace tables. + * @return - returns an array of HTableDescriptors wrapped by a {@link CompletableFuture}. + * @see #listTables(Pattern, boolean) + */ + CompletableFuture listTables(); + + /** + * List all the tables matching the given pattern. + * @param regex The regular expression to match against + * @param includeSysTables False to match only against userspace tables + * @return - returns an array of HTableDescriptors wrapped by a {@link CompletableFuture}. + * @see #listTables(Pattern, boolean) + */ + CompletableFuture listTables(String regex, boolean includeSysTables); + + /** + * List all the tables matching the given pattern. + * @param pattern The compiled regular expression to match against + * @param includeSysTables False to match only against userspace tables + * @return - returns an array of HTableDescriptors wrapped by a {@link CompletableFuture}. + */ + CompletableFuture listTables(Pattern pattern, boolean includeSysTables); + + /** + * List all of the names of userspace tables. + * @return TableName[] an array of table names wrapped by a {@link CompletableFuture}. + * @see #listTableNames(Pattern, boolean) + */ + CompletableFuture listTableNames(); + + /** + * List all of the names of userspace tables. + * @param regex The regular expression to match against + * @param includeSysTables False to match only against userspace tables + * @return TableName[] an array of table names wrapped by a {@link CompletableFuture}. + * @see #listTableNames(Pattern, boolean) + */ + CompletableFuture listTableNames(final String regex, final boolean includeSysTables); + + /** + * List all of the names of userspace tables. + * @param pattern The regular expression to match against + * @param includeSysTables False to match only against userspace tables + * @return TableName[] an array of table names wrapped by a {@link CompletableFuture}. + */ + CompletableFuture listTableNames(final Pattern pattern, + final boolean includeSysTables); + + /** * Turn the load balancer on or off. * @param on * @return Previous balancer value wrapped by a {@link CompletableFuture}. */ - CompletableFuture setBalancerRunning(final boolean on) throws IOException; + CompletableFuture setBalancerRunning(final boolean on); /** * Invoke the balancer. Will run the balancer and if regions to move, it will go ahead and do the @@ -43,7 +94,7 @@ public interface AsyncAdmin { * @return True if balancer ran, false otherwise. The return value will be wrapped by a * {@link CompletableFuture}. */ - CompletableFuture balancer() throws IOException; + CompletableFuture balancer(); /** * Invoke the balancer. Will run the balancer and if regions to move, it will go ahead and do the @@ -53,12 +104,12 @@ public interface AsyncAdmin { * @return True if balancer ran, false otherwise. The return value will be wrapped by a * {@link CompletableFuture}. */ - CompletableFuture balancer(boolean force) throws IOException; + CompletableFuture balancer(boolean force); /** * Query the current state of the balancer. * @return true if the balancer is enabled, false otherwise. * The return value will be wrapped by a {@link CompletableFuture}. */ - CompletableFuture isBalancerEnabled() throws IOException; + CompletableFuture isBalancerEnabled(); } diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java index 1dd92e5..c8fde58 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java @@ -20,15 +20,23 @@ package org.apache.hadoop.hbase.client; import java.io.IOException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.classification.InterfaceStability; import org.apache.hadoop.hbase.client.AsyncRpcRetryingCallerFactory.MasterRequestCallerBuilder; import org.apache.hadoop.hbase.ipc.HBaseRpcController; import org.apache.hadoop.hbase.shaded.com.google.protobuf.RpcCallback; +import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.BalanceRequest; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.BalanceResponse; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetTableDescriptorsRequest; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetTableDescriptorsResponse; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetTableNamesRequest; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetTableNamesResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.IsBalancerEnabledRequest; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.IsBalancerEnabledResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MasterService; @@ -105,7 +113,51 @@ public class AsyncHBaseAdmin implements AsyncAdmin { } @Override - public CompletableFuture setBalancerRunning(final boolean on) throws IOException { + public CompletableFuture listTables() { + return listTables((Pattern)null, false); + } + + @Override + public CompletableFuture listTables(String regex, boolean includeSysTables) { + return listTables(Pattern.compile(regex), false); + } + + @Override + public CompletableFuture listTables(Pattern pattern, boolean includeSysTables) { + return this + . newCaller() + .action( + (controller, stub) -> this + . call( + controller, stub, RequestConverter.buildGetTableDescriptorsRequest(pattern, + includeSysTables), (s, c, req, done) -> s.getTableDescriptors(c, req, done), ( + resp) -> ProtobufUtil.getHTableDescriptorArray(resp))).call(); + } + + @Override + public CompletableFuture listTableNames() { + return listTableNames((Pattern)null, false); + } + + @Override + public CompletableFuture listTableNames(String regex, boolean includeSysTables) { + return listTableNames(Pattern.compile(regex), false); + } + + @Override + public CompletableFuture listTableNames(Pattern pattern, boolean includeSysTables) { + return this + . newCaller() + .action( + (controller, stub) -> this + . call(controller, stub, + RequestConverter.buildGetTableNamesRequest(pattern, includeSysTables), (s, c, req, + done) -> s.getTableNames(c, req, done), (resp) -> ProtobufUtil + .getTableNameArray(resp.getTableNamesList()))).call(); + } + + @Override + public CompletableFuture setBalancerRunning(final boolean on) { return this . newCaller() .action( @@ -117,12 +169,12 @@ public class AsyncHBaseAdmin implements AsyncAdmin { } @Override - public CompletableFuture balancer() throws IOException { + public CompletableFuture balancer() { return balancer(false); } @Override - public CompletableFuture balancer(boolean force) throws IOException { + public CompletableFuture balancer(boolean force) { return this . newCaller() .action( @@ -132,7 +184,7 @@ public class AsyncHBaseAdmin implements AsyncAdmin { } @Override - public CompletableFuture isBalancerEnabled() throws IOException { + public CompletableFuture isBalancerEnabled() { return this . newCaller() .action( diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncAdmin.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncAdmin.java index 9beae1f..de14359 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncAdmin.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncAdmin.java @@ -18,13 +18,19 @@ package org.apache.hadoop.hbase.client; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.testclassification.ClientTests; import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.util.Bytes; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -39,6 +45,7 @@ public class TestAsyncAdmin { private static final Log LOG = LogFactory.getLog(TestAdmin1.class); private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + private final static byte [] FAMILY = Bytes.toBytes("testFamily"); private static AsyncConnection ASYNC_CONN; private AsyncAdmin admin; @@ -63,6 +70,58 @@ public class TestAsyncAdmin { this.admin = ASYNC_CONN.getAdmin(); } + @Test + public void testListTables() throws Exception { + TableName t1 = TableName.valueOf("testListTables1"); + TableName t2 = TableName.valueOf("testListTables2"); + TableName t3 = TableName.valueOf("testListTables3"); + TableName[] tables = new TableName[] { t1, t2, t3 }; + for (int i = 0; i < tables.length; i++) { + TEST_UTIL.createTable(tables[i], FAMILY); + } + + HTableDescriptor[] tableDescs = admin.listTables().get(); + int size = tableDescs.length; + assertTrue(size >= tables.length); + for (int i = 0; i < tables.length && i < size; i++) { + boolean found = false; + for (int j = 0; j < tableDescs.length; j++) { + if (tableDescs[j].getTableName().equals(tables[i])) { + found = true; + break; + } + } + assertTrue("Not found: " + tables[i], found); + } + + TableName[] tableNames = admin.listTableNames().get(); + size = tableNames.length; + assertTrue(size >= tables.length); + for (int i = 0; i < tables.length && i < size; i++) { + boolean found = false; + for (int j = 0; j < tableNames.length; j++) { + if (tableNames[j].equals(tables[i])) { + found = true; + break; + } + } + assertTrue("Not found: " + tables[i], found); + } + + for (int i = 0; i < tables.length; i++) { + TEST_UTIL.deleteTable(tables[i]); + } + tableDescs = admin.listTables().get(); + assertEquals(0, tableDescs.length); + tableNames = admin.listTableNames().get(); + assertEquals(0, tableNames.length); + + tableDescs = admin.listTables((Pattern) null, true).get(); + assertTrue("Not found system tables", tableDescs.length > 0); + tableNames = admin.listTableNames((Pattern) null, true).get(); + assertTrue("Not found system tables", tableNames.length > 0); + } + @Test(timeout = 30000) public void testBalancer() throws Exception { boolean initialState = admin.isBalancerEnabled().get(); -- 1.9.1