From 8f1637a1924be8399ca66462c2122385f05e37b8 Mon Sep 17 00:00:00 2001 From: zhangduo Date: Sat, 8 Apr 2017 21:22:25 +0800 Subject: [PATCH] HBASE-17008 Examples, Doc, and Helper Classes to make AsyncClient go down easy --- .../hbase/client/example/AsyncClientExample.java | 184 +++++++++++++++++++++ .../client/example/TestAsyncClientExample.java | 57 +++++++ 2 files changed, 241 insertions(+) create mode 100644 hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/AsyncClientExample.java create mode 100644 hbase-examples/src/test/java/org/apache/hadoop/hbase/client/example/TestAsyncClientExample.java diff --git a/hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/AsyncClientExample.java b/hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/AsyncClientExample.java new file mode 100644 index 0000000..2d587bf --- /dev/null +++ b/hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/AsyncClientExample.java @@ -0,0 +1,184 @@ +/** + * 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 java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.IntStream; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configured; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.AsyncConnection; +import org.apache.hadoop.hbase.client.AsyncTable; +import org.apache.hadoop.hbase.client.ConnectionFactory; +import org.apache.hadoop.hbase.client.Get; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Threads; +import org.apache.hadoop.util.Tool; +import org.apache.hadoop.util.ToolRunner; + +/** + * A simple example shows how to use asynchronous client. + */ +public class AsyncClientExample extends Configured implements Tool { + + private static final Log LOG = LogFactory.getLog(AsyncClientExample.class); + + /** + * The size for thread pool. + */ + private static final int THREAD_POOL_SIZE = 16; + + /** + * The default number of operations. + */ + private static final int DEFAULT_NUM_OPS = 100; + + /** + * The name of the column family. d for default. + */ + private static final byte[] FAMILY = Bytes.toBytes("d"); + + /** + * For the example we're just using one qualifier. + */ + private static final byte[] QUAL = Bytes.toBytes("test"); + + private final AtomicReference> future = + new AtomicReference<>(); + + private CompletableFuture getConn() { + CompletableFuture f = future.get(); + if (f != null) { + return f; + } + for (;;) { + if (future.compareAndSet(null, new CompletableFuture<>())) { + CompletableFuture toComplete = future.get(); + ConnectionFactory.createAsyncConnection(getConf()).whenComplete((conn, error) -> { + if (error != null) { + toComplete.completeExceptionally(error); + // we need to reset the future holder so we will get a chance to recreate an async + // connection at next try. + future.set(null); + return; + } + toComplete.complete(conn); + }); + return toComplete; + } else { + f = future.get(); + if (f != null) { + return f; + } + } + } + } + + private CompletableFuture closeConn() { + CompletableFuture f = future.get(); + if (f == null) { + return CompletableFuture.completedFuture(null); + } + CompletableFuture closeFuture = new CompletableFuture<>(); + f.whenComplete((conn, error) -> { + if (error == null) { + IOUtils.closeQuietly(conn); + } + closeFuture.complete(null); + }); + return closeFuture; + } + + private byte[] getKey(int i) { + return Bytes.toBytes(String.format("%08x", i)); + } + + @Override + public int run(String[] args) throws Exception { + if (args.length < 1 || args.length > 2) { + System.out.println("Usage: " + this.getClass().getName() + " tableName [num_operations]"); + return -1; + } + TableName tableName = TableName.valueOf(args[0]); + int numOps = args.length > 1 ? Integer.parseInt(args[1]) : DEFAULT_NUM_OPS; + ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_POOL_SIZE, + Threads.newDaemonThreadFactory("AsyncClientExample")); + // We use AsyncTable here so we need to provide a separated thread pool. RawAsyncTable does not + // need a thread pool and may have a better performance if you use it correctly as it can save + // some context switches. But if you use RawAsyncTable incorrectly, you may have a very bad + // impact on performance so use it with caution. + CountDownLatch latch = new CountDownLatch(numOps); + IntStream.range(0, numOps).forEach(i -> { + CompletableFuture future = getConn(); + future.whenComplete((conn, error) -> { + if (error != null) { + LOG.warn("failed to get async connection for " + i, error); + latch.countDown(); + return; + } + AsyncTable table = conn.getTable(tableName, threadPool); + table.put(new Put(getKey(i)).addColumn(FAMILY, QUAL, Bytes.toBytes(i))) + .whenComplete((putResp, putErr) -> { + if (putErr != null) { + LOG.warn("put failed for " + i, putErr); + latch.countDown(); + return; + } + LOG.info("put for " + i + " succeeded, try getting"); + table.get(new Get(getKey(i))).whenComplete((result, getErr) -> { + if (getErr != null) { + LOG.warn("get failed for " + i, putErr); + latch.countDown(); + return; + } + if (result.isEmpty()) { + LOG.warn("get failed for " + i + ", server returns empty result"); + } else if (!result.containsColumn(FAMILY, QUAL)) { + LOG.warn("get failed for " + i + ", the result does not contain " + + Bytes.toString(FAMILY) + ":" + Bytes.toString(QUAL)); + } else { + int v = Bytes.toInt(result.getValue(FAMILY, QUAL)); + if (v != i) { + LOG.warn("get failed for " + i + ", the value of " + Bytes.toString(FAMILY) + + ":" + Bytes.toString(QUAL) + " is " + v + ", exected " + i); + } else { + LOG.info("get for " + i + " succeeded"); + } + } + latch.countDown(); + }); + }); + }); + }); + latch.await(); + closeConn().get(); + return 0; + } + + public static void main(String[] args) throws Exception { + ToolRunner.run(new AsyncClientExample(), args); + } +} diff --git a/hbase-examples/src/test/java/org/apache/hadoop/hbase/client/example/TestAsyncClientExample.java b/hbase-examples/src/test/java/org/apache/hadoop/hbase/client/example/TestAsyncClientExample.java new file mode 100644 index 0000000..b6d4f5c --- /dev/null +++ b/hbase-examples/src/test/java/org/apache/hadoop/hbase/client/example/TestAsyncClientExample.java @@ -0,0 +1,57 @@ +/** + * 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.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.TableName; +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.Test; +import org.junit.experimental.categories.Category; + +@Category({ ClientTests.class, MediumTests.class }) +public class TestAsyncClientExample { + + private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("test"); + + @BeforeClass + public static void setUp() throws Exception { + UTIL.startMiniCluster(1); + UTIL.createTable(TABLE_NAME, Bytes.toBytes("d")); + } + + @AfterClass + public static void tearDown() throws Exception { + UTIL.shutdownMiniCluster(); + } + + @Test + public void test() throws Exception { + AsyncClientExample tool = new AsyncClientExample(); + tool.setConf(UTIL.getConfiguration()); + assertEquals(0, ToolRunner.run(tool, new String[] { TABLE_NAME.getNameAsString() })); + } +} -- 2.7.4