Search in sources :

Example 1 with ThreadFactoryBuilder

use of org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder in project hbase by apache.

the class AsyncClientExample method run.

@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, new ThreadFactoryBuilder().setNameFormat("AsyncClientExample-pool-%d").setDaemon(true).setUncaughtExceptionHandler(Threads.LOGGING_EXCEPTION_HANDLER).build());
    // 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<AsyncConnection> future = getConn();
        addListener(future, (conn, error) -> {
            if (error != null) {
                LOG.warn("failed to get async connection for " + i, error);
                latch.countDown();
                return;
            }
            AsyncTable<?> table = conn.getTable(tableName, threadPool);
            addListener(table.put(new Put(getKey(i)).addColumn(FAMILY, QUAL, Bytes.toBytes(i))), (putResp, putErr) -> {
                if (putErr != null) {
                    LOG.warn("put failed for " + i, putErr);
                    latch.countDown();
                    return;
                }
                LOG.info("put for " + i + " succeeded, try getting");
                addListener(table.get(new Get(getKey(i))), (result, getErr) -> {
                    if (getErr != null) {
                        LOG.warn("get failed for " + i);
                        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;
}
Also used : TableName(org.apache.hadoop.hbase.TableName) Get(org.apache.hadoop.hbase.client.Get) ExecutorService(java.util.concurrent.ExecutorService) AsyncConnection(org.apache.hadoop.hbase.client.AsyncConnection) ThreadFactoryBuilder(org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) Put(org.apache.hadoop.hbase.client.Put)

Example 2 with ThreadFactoryBuilder

use of org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder in project hbase by apache.

the class PerformanceEvaluation method doLocalClients.

/*
   * Run all clients in this vm each to its own thread.
   */
static RunResult[] doLocalClients(final TestOptions opts, final Configuration conf) throws IOException, InterruptedException, ExecutionException {
    final Class<? extends TestBase> cmd = determineCommandClass(opts.cmdName);
    assert cmd != null;
    @SuppressWarnings("unchecked") Future<RunResult>[] threads = new Future[opts.numClientThreads];
    RunResult[] results = new RunResult[opts.numClientThreads];
    ExecutorService pool = Executors.newFixedThreadPool(opts.numClientThreads, new ThreadFactoryBuilder().setNameFormat("TestClient-%s").build());
    setupConnectionCount(opts);
    final Connection[] cons = new Connection[opts.connCount];
    final AsyncConnection[] asyncCons = new AsyncConnection[opts.connCount];
    for (int i = 0; i < opts.connCount; i++) {
        cons[i] = ConnectionFactory.createConnection(conf);
        asyncCons[i] = ConnectionFactory.createAsyncConnection(conf).get();
    }
    LOG.info("Created " + opts.connCount + " connections for " + opts.numClientThreads + " threads");
    for (int i = 0; i < threads.length; i++) {
        final int index = i;
        threads[i] = pool.submit(new Callable<RunResult>() {

            @Override
            public RunResult call() throws Exception {
                TestOptions threadOpts = new TestOptions(opts);
                final Connection con = cons[index % cons.length];
                final AsyncConnection asyncCon = asyncCons[index % asyncCons.length];
                if (threadOpts.startRow == 0)
                    threadOpts.startRow = index * threadOpts.perClientRunRows;
                RunResult run = runOneClient(cmd, conf, con, asyncCon, threadOpts, new Status() {

                    @Override
                    public void setStatus(final String msg) throws IOException {
                        LOG.info(msg);
                    }
                });
                LOG.info("Finished " + Thread.currentThread().getName() + " in " + run.duration + "ms over " + threadOpts.perClientRunRows + " rows");
                if (opts.latencyThreshold > 0) {
                    LOG.info("Number of replies over latency threshold " + opts.latencyThreshold + "(ms) is " + run.numbOfReplyOverThreshold);
                }
                return run;
            }
        });
    }
    pool.shutdown();
    for (int i = 0; i < threads.length; i++) {
        try {
            results[i] = threads[i].get();
        } catch (ExecutionException e) {
            throw new IOException(e.getCause());
        }
    }
    final String test = cmd.getSimpleName();
    LOG.info("[" + test + "] Summary of timings (ms): " + Arrays.toString(results));
    Arrays.sort(results);
    long total = 0;
    float avgLatency = 0;
    float avgTPS = 0;
    long replicaWins = 0;
    for (RunResult result : results) {
        total += result.duration;
        avgLatency += result.hist.getSnapshot().getMean();
        avgTPS += opts.perClientRunRows * 1.0f / result.duration;
        replicaWins += result.numOfReplyFromReplica;
    }
    // ms to second
    avgTPS *= 1000;
    avgLatency = avgLatency / results.length;
    LOG.info("[" + test + " duration ]" + "\tMin: " + results[0] + "ms" + "\tMax: " + results[results.length - 1] + "ms" + "\tAvg: " + (total / results.length) + "ms");
    LOG.info("[ Avg latency (us)]\t" + Math.round(avgLatency));
    LOG.info("[ Avg TPS/QPS]\t" + Math.round(avgTPS) + "\t row per second");
    if (opts.replicas > 1) {
        LOG.info("[results from replica regions] " + replicaWins);
    }
    for (int i = 0; i < opts.connCount; i++) {
        cons[i].close();
        asyncCons[i].close();
    }
    return results;
}
Also used : AsyncConnection(org.apache.hadoop.hbase.client.AsyncConnection) Connection(org.apache.hadoop.hbase.client.Connection) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) AsyncConnection(org.apache.hadoop.hbase.client.AsyncConnection) Future(java.util.concurrent.Future) ThreadFactoryBuilder(org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with ThreadFactoryBuilder

use of org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder in project hbase by apache.

the class MemStoreFlusher method start.

synchronized void start(UncaughtExceptionHandler eh) {
    ThreadFactory flusherThreadFactory = new ThreadFactoryBuilder().setNameFormat(server.getServerName().toShortString() + "-MemStoreFlusher-pool-%d").setDaemon(true).setUncaughtExceptionHandler(eh).build();
    for (int i = 0; i < flushHandlers.length; i++) {
        flushHandlers[i] = new FlushHandler("MemStoreFlusher." + i);
        flusherThreadFactory.newThread(flushHandlers[i]);
        flushHandlers[i].start();
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ThreadFactoryBuilder(org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder)

Example 4 with ThreadFactoryBuilder

use of org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder in project hbase by apache.

the class ConnectionOverAsyncConnection method createThreadPool.

// only used for executing coprocessor calls, as users may reference the methods in the
// BlockingInterface of the protobuf stub so we have to execute the call in a separated thread...
// Will be removed in 4.0.0 along with the deprecated coprocessor methods in Table and Admin
// interface.
private ThreadPoolExecutor createThreadPool() {
    Configuration conf = conn.getConfiguration();
    int threads = conf.getInt("hbase.hconnection.threads.max", 256);
    long keepAliveTime = conf.getLong("hbase.hconnection.threads.keepalivetime", 60);
    BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(threads * conf.getInt(HConstants.HBASE_CLIENT_MAX_TOTAL_TASKS, HConstants.DEFAULT_HBASE_CLIENT_MAX_TOTAL_TASKS));
    ThreadPoolExecutor tpe = new ThreadPoolExecutor(threads, threads, keepAliveTime, TimeUnit.SECONDS, workQueue, new ThreadFactoryBuilder().setDaemon(true).setNameFormat(toString() + "-shared-%d").build());
    tpe.allowCoreThreadTimeOut(true);
    return tpe;
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) ThreadFactoryBuilder(org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 5 with ThreadFactoryBuilder

use of org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder in project hbase by apache.

the class AcidGuaranteesTestTool method createThreadPool.

private ExecutorService createThreadPool() {
    int maxThreads = 256;
    int coreThreads = 128;
    long keepAliveTime = 60;
    BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(maxThreads * HConstants.DEFAULT_HBASE_CLIENT_MAX_TOTAL_TASKS);
    ThreadPoolExecutor tpe = new ThreadPoolExecutor(coreThreads, maxThreads, keepAliveTime, TimeUnit.SECONDS, workQueue, new ThreadFactoryBuilder().setNameFormat(toString() + "-shared-pool-%d").setDaemon(true).setUncaughtExceptionHandler(Threads.LOGGING_EXCEPTION_HANDLER).build());
    tpe.allowCoreThreadTimeOut(true);
    return tpe;
}
Also used : ThreadFactoryBuilder(org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Aggregations

ThreadFactoryBuilder (org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder)25 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)9 ExecutorService (java.util.concurrent.ExecutorService)7 IOException (java.io.IOException)6 Configuration (org.apache.hadoop.conf.Configuration)6 Future (java.util.concurrent.Future)5 TableName (org.apache.hadoop.hbase.TableName)5 BeforeClass (org.junit.BeforeClass)5 ExecutionException (java.util.concurrent.ExecutionException)4 Executors (java.util.concurrent.Executors)4 TimeUnit (java.util.concurrent.TimeUnit)4 InterruptedIOException (java.io.InterruptedIOException)3 Arrays (java.util.Arrays)3 Path (org.apache.hadoop.fs.Path)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 NavigableMap (java.util.NavigableMap)2 Random (java.util.Random)2 Set (java.util.Set)2 SortedSet (java.util.SortedSet)2