From 775fbf7c67c5ab4119871a5b4bf9fe473cc8456f Mon Sep 17 00:00:00 2001 From: TAK LON WU Date: Fri, 17 Aug 2018 15:55:10 -0700 Subject: [PATCH] Make RefreshHFilesClient runnable Other than when user enables hbase.coprocessor.region.classes with RefreshHFilesEndPoint, use can also run this client as tool runner classes/CLI and calls refresh HFiles directly. --- .../hbase/client/example/RefreshHFilesClient.java | 26 +++++++++- .../client/example/TestRefreshHFilesClient.java | 55 ++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 hbase-examples/src/test/java/org/apache/hadoop/hbase/client/example/TestRefreshHFilesClient.java diff --git a/hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/RefreshHFilesClient.java b/hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/RefreshHFilesClient.java index ead0af0..82e935a 100644 --- a/hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/RefreshHFilesClient.java +++ b/hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/RefreshHFilesClient.java @@ -22,6 +22,8 @@ package org.apache.hadoop.hbase.client.example; import java.io.Closeable; import java.io.IOException; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.conf.Configured; +import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; @@ -31,6 +33,8 @@ import org.apache.hadoop.hbase.client.coprocessor.Batch; import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils.BlockingRpcCallback; import org.apache.hadoop.hbase.ipc.ServerRpcController; import org.apache.hadoop.hbase.protobuf.generated.RefreshHFilesProtos; +import org.apache.hadoop.util.Tool; +import org.apache.hadoop.util.ToolRunner; import org.apache.yetus.audience.InterfaceAudience; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +44,7 @@ import org.slf4j.LoggerFactory; * Region Server side via the RefreshHFilesService. */ @InterfaceAudience.Private -public class RefreshHFilesClient implements Closeable { +public class RefreshHFilesClient extends Configured implements Tool, Closeable { private static final Logger LOG = LoggerFactory.getLogger(RefreshHFilesClient.class); private final Connection connection; @@ -93,4 +97,24 @@ public class RefreshHFilesClient implements Closeable { }); LOG.debug("Done refreshing HFiles"); } + + @Override + public int run(String[] args) throws Exception { + if (args.length != 1) { + System.out.println("Usage: " + this.getClass().getName() + " tableName"); + return -1; + } + final TableName tableName = TableName.valueOf(args[0]); + try { + refreshHFiles(tableName); + } catch (Throwable t) { + LOG.error("Refresh HFiles failed: ", t); + return -1; + } + return 0; + } + + public static void main(String[] args) throws Exception { + ToolRunner.run(new RefreshHFilesClient(HBaseConfiguration.create()), args); + } } diff --git a/hbase-examples/src/test/java/org/apache/hadoop/hbase/client/example/TestRefreshHFilesClient.java b/hbase-examples/src/test/java/org/apache/hadoop/hbase/client/example/TestRefreshHFilesClient.java new file mode 100644 index 0000000..6c648c1 --- /dev/null +++ b/hbase-examples/src/test/java/org/apache/hadoop/hbase/client/example/TestRefreshHFilesClient.java @@ -0,0 +1,55 @@ +package org.apache.hadoop.hbase.client.example; + +import static org.junit.Assert.assertEquals; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.testclassification.ClientTests; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.util.ToolRunner; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({ ClientTests.class, MediumTests.class }) +public class TestRefreshHFilesClient { + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRefreshHFilesClient.class); + + private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("test"); + private static final byte[] FAMILY_NAME = Bytes.toBytes("d"); + private static Configuration conf; + + @BeforeClass + public static void setUp() throws Exception { + conf = UTIL.getConfiguration(); + conf.set("hbase.coprocessor.region.classes", + "org.apache.hadoop.hbase.coprocessor.example.RefreshHFilesEndpoint"); + + UTIL.startMiniCluster(1); + UTIL.createTable(TABLE_NAME, FAMILY_NAME); + Table table = UTIL.getConnection().getTable(TABLE_NAME); + UTIL.loadRandomRows(table, FAMILY_NAME, 50, 100); + UTIL.flush(TABLE_NAME); + } + + @AfterClass + public static void tearDown() throws Exception { + UTIL.shutdownMiniCluster(); + } + + @Test + public void test() throws Exception { + RefreshHFilesClient tool = new RefreshHFilesClient(conf); + assertEquals(0, ToolRunner.run(tool, new String[] { TABLE_NAME.getNameAsString() })); + } +} -- 2.10.1 (Apple Git-78)