Search in sources :

Example 91 with ThreadFactoryBuilder

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

the class Replication method initialize.

public void initialize(final Server server, final FileSystem fs, final Path logDir, final Path oldLogDir) throws IOException {
    this.server = server;
    this.conf = this.server.getConfiguration();
    this.replicationForBulkLoadData = isReplicationForBulkLoadDataEnabled(this.conf);
    this.scheduleThreadPool = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder().setNameFormat(server.getServerName().toShortString() + "Replication Statistics #%d").setDaemon(true).build());
    if (this.replicationForBulkLoadData) {
        if (conf.get(HConstants.REPLICATION_CLUSTER_ID) == null || conf.get(HConstants.REPLICATION_CLUSTER_ID).isEmpty()) {
            throw new IllegalArgumentException(HConstants.REPLICATION_CLUSTER_ID + " cannot be null/empty when " + HConstants.REPLICATION_BULKLOAD_ENABLE_KEY + " is set to true.");
        }
    }
    try {
        this.replicationQueues = ReplicationFactory.getReplicationQueues(new ReplicationQueuesArguments(conf, this.server, server.getZooKeeper()));
        this.replicationQueues.init(this.server.getServerName().toString());
        this.replicationPeers = ReplicationFactory.getReplicationPeers(server.getZooKeeper(), this.conf, this.server);
        this.replicationPeers.init();
        this.replicationTracker = ReplicationFactory.getReplicationTracker(server.getZooKeeper(), this.replicationPeers, this.conf, this.server, this.server);
    } catch (Exception e) {
        throw new IOException("Failed replication handler create", e);
    }
    UUID clusterId = null;
    try {
        clusterId = ZKClusterId.getUUIDForCluster(this.server.getZooKeeper());
    } catch (KeeperException ke) {
        throw new IOException("Could not read cluster id", ke);
    }
    this.replicationManager = new ReplicationSourceManager(replicationQueues, replicationPeers, replicationTracker, conf, this.server, fs, logDir, oldLogDir, clusterId);
    this.statsThreadPeriod = this.conf.getInt("replication.stats.thread.period.seconds", 5 * 60);
    LOG.debug("ReplicationStatisticsThread " + this.statsThreadPeriod);
    this.replicationLoad = new ReplicationLoad();
}
Also used : ReplicationQueuesArguments(org.apache.hadoop.hbase.replication.ReplicationQueuesArguments) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) IOException(java.io.IOException) UUID(java.util.UUID) ReplicationException(org.apache.hadoop.hbase.replication.ReplicationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException)

Example 92 with ThreadFactoryBuilder

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

the class ThriftServerRunner method createExecutor.

ExecutorService createExecutor(BlockingQueue<Runnable> callQueue, int minWorkers, int maxWorkers) {
    ThreadFactoryBuilder tfb = new ThreadFactoryBuilder();
    tfb.setDaemon(true);
    tfb.setNameFormat("thrift-worker-%d");
    ThreadPoolExecutor threadPool = new THBaseThreadPoolExecutor(minWorkers, maxWorkers, Long.MAX_VALUE, TimeUnit.SECONDS, callQueue, tfb.build(), metrics);
    threadPool.allowCoreThreadTimeOut(true);
    return threadPool;
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 93 with ThreadFactoryBuilder

use of com.google.common.util.concurrent.ThreadFactoryBuilder in project druid by druid-io.

the class ExecsTest method runTest.

private static void runTest(final int capacity) throws Exception {
    final int nTasks = (capacity + 1) * 3;
    final ExecutorService blockingExecutor = Execs.newBlockingSingleThreaded("ExecsTest-Blocking-%d", capacity);
    final CountDownLatch queueShouldBeFullSignal = new CountDownLatch(capacity + 1);
    final CountDownLatch taskCompletedSignal = new CountDownLatch(nTasks);
    final CountDownLatch taskStartSignal = new CountDownLatch(1);
    final AtomicInteger producedCount = new AtomicInteger();
    final AtomicInteger consumedCount = new AtomicInteger();
    final ExecutorService producer = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("ExecsTest-Producer-%d").build());
    producer.submit(new Runnable() {

        public void run() {
            for (int i = 0; i < nTasks; i++) {
                final int taskID = i;
                System.out.println("Produced task" + taskID);
                blockingExecutor.submit(new Runnable() {

                    @Override
                    public void run() {
                        log.info("Starting task: %s", taskID);
                        try {
                            taskStartSignal.await();
                            consumedCount.incrementAndGet();
                            taskCompletedSignal.countDown();
                        } catch (Exception e) {
                            throw Throwables.propagate(e);
                        }
                        log.info("Completed task: %s", taskID);
                    }
                });
                producedCount.incrementAndGet();
                queueShouldBeFullSignal.countDown();
            }
        }
    });
    queueShouldBeFullSignal.await();
    // Verify that the producer blocks. I don't think it's possible to be sure that the producer is blocking (since
    // it could be doing nothing for any reason). But waiting a short period of time and checking that it hasn't done
    // anything should hopefully be sufficient.
    Thread.sleep(500);
    Assert.assertEquals(capacity + 1, producedCount.get());
    // let the tasks run
    taskStartSignal.countDown();
    // wait until all tasks complete
    taskCompletedSignal.await();
    // verify all tasks consumed
    Assert.assertEquals(nTasks, consumedCount.get());
    // cleanup
    blockingExecutor.shutdown();
    producer.shutdown();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 94 with ThreadFactoryBuilder

use of com.google.common.util.concurrent.ThreadFactoryBuilder in project druid by druid-io.

the class PrioritizedListenableFutureTask method create.

public static PrioritizedExecutorService create(Lifecycle lifecycle, DruidProcessingConfig config) {
    final PrioritizedExecutorService service = new PrioritizedExecutorService(new ThreadPoolExecutor(config.getNumThreads(), config.getNumThreads(), 0L, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>(), new ThreadFactoryBuilder().setDaemon(true).setNameFormat(config.getFormatString()).build()), config);
    lifecycle.addHandler(new Lifecycle.Handler() {

        @Override
        public void start() throws Exception {
        }

        @Override
        public void stop() {
            service.shutdownNow();
        }
    });
    return service;
}
Also used : Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 95 with ThreadFactoryBuilder

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

the class BulkAssigner method bulkAssign.

/**
   * Run the bulk assign.
   * 
   * @param sync
   *          Whether to assign synchronously.
   * @throws InterruptedException
   * @return True if done.
   * @throws IOException
   */
public boolean bulkAssign(boolean sync) throws InterruptedException, IOException {
    boolean result = false;
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setDaemon(true);
    builder.setNameFormat(getThreadNamePrefix() + "-%1$d");
    builder.setUncaughtExceptionHandler(getUncaughtExceptionHandler());
    int threadCount = getThreadCount();
    java.util.concurrent.ExecutorService pool = Executors.newFixedThreadPool(threadCount, builder.build());
    try {
        populatePool(pool);
        // RIT monitor should do fixup.
        if (sync)
            result = waitUntilDone(getTimeoutOnRIT());
    } finally {
        // We're done with the pool.  It'll exit when its done all in queue.
        pool.shutdown();
    }
    return result;
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Aggregations

ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)143 ExecutorService (java.util.concurrent.ExecutorService)49 ThreadFactory (java.util.concurrent.ThreadFactory)46 IOException (java.io.IOException)23 Future (java.util.concurrent.Future)19 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)19 ExecutionException (java.util.concurrent.ExecutionException)17 ArrayList (java.util.ArrayList)15 Callable (java.util.concurrent.Callable)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 HashMap (java.util.HashMap)11 Path (org.apache.hadoop.fs.Path)11 LinkedList (java.util.LinkedList)10 Map (java.util.Map)10 HashSet (java.util.HashSet)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)9 Test (org.junit.Test)9 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)8 Before (org.junit.Before)8