From b0782b826721957beca49c1941683b1ca3d6473b Mon Sep 17 00:00:00 2001 From: TAK LON WU Date: Fri, 17 Aug 2018 15:55:10 -0700 Subject: [PATCH] HBASE-21318 Make RefreshHFilesClient runnable Other than when user enables hbase.coprocessor.region.classes with RefreshHFilesEndPoint, user can also run this client as tool runner class/CLI and calls refresh HFiles directly. --- .../hbase/client/example/RefreshHFilesClient.java | 26 +++++++- .../client/example/TestRefreshHFilesClient.java | 72 ++++++++++++++++++++++ 2 files changed, 97 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..4f43cfa --- /dev/null +++ b/hbase-examples/src/test/java/org/apache/hadoop/hbase/client/example/TestRefreshHFilesClient.java @@ -0,0 +1,72 @@ +/** + * 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.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)