com.google.common.util.concurrent.ThreadFactoryBuilder

Here are the examples of the java api class com.google.common.util.concurrent.ThreadFactoryBuilder taken from open source projects.

1. BulkAssigner#bulkAssign()

Project: hindex
Source File: BulkAssigner.java
View license
/**
   * 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;
}

2. RpcChannelFactory#createServerChannelFactory()

View license
// Client must release the external resources
public static synchronized ServerSocketChannelFactory createServerChannelFactory(String name, int workerNum) {
    name = name + "-" + serverCount.incrementAndGet();
    if (LOG.isInfoEnabled()) {
        LOG.info("Create " + name + " ServerSocketChannelFactory. Worker:" + workerNum);
    }
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    ThreadFactory bossFactory = builder.setNameFormat(name + " Server Boss #%d").build();
    ThreadFactory workerFactory = builder.setNameFormat(name + " Server Worker #%d").build();
    NioServerBossPool bossPool = new NioServerBossPool(Executors.newCachedThreadPool(bossFactory), 1, ThreadNameDeterminer.CURRENT);
    NioWorkerPool workerPool = new NioWorkerPool(Executors.newCachedThreadPool(workerFactory), workerNum, ThreadNameDeterminer.CURRENT);
    return new NioServerSocketChannelFactory(bossPool, workerPool);
}

3. RpcChannelFactory#createClientChannelFactory()

View license
// Client must release the external resources
public static synchronized ClientSocketChannelFactory createClientChannelFactory(String name, int workerNum) {
    name = name + "-" + clientCount.incrementAndGet();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Create " + name + " ClientSocketChannelFactory. Worker:" + workerNum);
    }
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    ThreadFactory bossFactory = builder.setNameFormat(name + " Boss #%d").build();
    ThreadFactory workerFactory = builder.setNameFormat(name + " Worker #%d").build();
    NioClientBossPool bossPool = new NioClientBossPool(Executors.newCachedThreadPool(bossFactory), 1, new HashedWheelTimer(), ThreadNameDeterminer.CURRENT);
    NioWorkerPool workerPool = new NioWorkerPool(Executors.newCachedThreadPool(workerFactory), workerNum, ThreadNameDeterminer.CURRENT);
    return new NioClientSocketChannelFactory(bossPool, workerPool);
}

4. ThriftServer#createExecutor()

Project: hindex
Source File: ThriftServer.java
View license
private static ExecutorService createExecutor(int workerThreads, ThriftMetrics metrics) {
    CallQueue callQueue = new CallQueue(new LinkedBlockingQueue<Call>(), metrics);
    ThreadFactoryBuilder tfb = new ThreadFactoryBuilder();
    tfb.setDaemon(true);
    tfb.setNameFormat("thrift2-worker-%d");
    return new ThreadPoolExecutor(workerThreads, workerThreads, Long.MAX_VALUE, TimeUnit.SECONDS, callQueue, tfb.build());
}

5. ThriftServerRunner#createExecutor()

Project: hindex
Source File: ThriftServerRunner.java
View license
ExecutorService createExecutor(BlockingQueue<Runnable> callQueue, int workerThreads) {
    ThreadFactoryBuilder tfb = new ThreadFactoryBuilder();
    tfb.setDaemon(true);
    tfb.setNameFormat("thrift-worker-%d");
    return new ThreadPoolExecutor(workerThreads, workerThreads, Long.MAX_VALUE, TimeUnit.SECONDS, callQueue, tfb.build());
}

6. SplitTransaction#splitStoreFiles()

Project: hindex
Source File: SplitTransaction.java
View license
protected void splitStoreFiles(final Path splitdir, final List<StoreFile> hstoreFilesToSplit) throws IOException {
    if (hstoreFilesToSplit == null) {
        // Could be null because close didn't succeed -- for now consider it fatal
        throw new IOException("Close returned empty list of StoreFiles");
    }
    // The following code sets up a thread pool executor with as many slots as
    // there's files to split. It then fires up everything, waits for
    // completion and finally checks for any exception
    int nbFiles = hstoreFilesToSplit.size();
    boolean secondaryIndex = this.parent.getConf().getBoolean("hbase.use.secondary.index", false);
    if (secondaryIndex) {
        String idxTableName = this.parent.getTableDesc().getNameAsString();
        if (isIndexTable(idxTableName))
            if (nbFiles == 0) {
                LOG.debug("Setting number of threads for ThreadPoolExecutor to 1 since IndexTable " + idxTableName + " doesn't have any store files ");
                nbFiles = 1;
            }
    }
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setNameFormat("StoreFileSplitter-%1$d");
    ThreadFactory factory = builder.build();
    ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(nbFiles, factory);
    List<Future<Void>> futures = new ArrayList<Future<Void>>(nbFiles);
    // Split each store file.
    for (StoreFile sf : hstoreFilesToSplit) {
        //splitStoreFile(sf, splitdir);
        StoreFileSplitter sfs = new StoreFileSplitter(sf, splitdir);
        futures.add(threadPool.submit(sfs));
    }
    // Shutdown the pool
    threadPool.shutdown();
    // Wait for all the tasks to finish
    try {
        boolean stillRunning = !threadPool.awaitTermination(this.fileSplitTimeout, TimeUnit.MILLISECONDS);
        if (stillRunning) {
            threadPool.shutdownNow();
            // wait for the thread to shutdown completely.
            while (!threadPool.isTerminated()) {
                Thread.sleep(50);
            }
            throw new IOException("Took too long to split the" + " files and create the references, aborting split");
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IOException("Interrupted while waiting for file splitters", e);
    }
    // Look for any exception
    for (Future<Void> future : futures) {
        try {
            future.get();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IOException("Interrupted while trying to get the results of file splitters", e);
        } catch (ExecutionException e) {
            throw new IOException(e);
        }
    }
}

7. LoadIncrementalHFiles#doBulkLoad()

Project: hindex
Source File: LoadIncrementalHFiles.java
View license
/**
   * Perform a bulk load of the given directory into the given
   * pre-existing table.  This method is not threadsafe.
   * 
   * @param hfofDir the directory that was provided as the output path
   * of a job using HFileOutputFormat
   * @param table the table to load into
   * @throws TableNotFoundException if table does not yet exist
   */
public void doBulkLoad(Path hfofDir, final HTable table) throws TableNotFoundException, IOException {
    final HConnection conn = table.getConnection();
    if (!conn.isTableAvailable(table.getTableName())) {
        throw new TableNotFoundException("Table " + Bytes.toStringBinary(table.getTableName()) + "is not currently available.");
    }
    // initialize thread pools
    int nrThreads = cfg.getInt("hbase.loadincremental.threads.max", Runtime.getRuntime().availableProcessors());
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setNameFormat("LoadIncrementalHFiles-%1$d");
    ExecutorService pool = new ThreadPoolExecutor(nrThreads, nrThreads, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), builder.build());
    ((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
    // LQI queue does not need to be threadsafe -- all operations on this queue
    // happen in this thread
    Deque<LoadQueueItem> queue = new LinkedList<LoadQueueItem>();
    try {
        discoverLoadQueue(queue, hfofDir);
        int count = 0;
        if (queue.isEmpty()) {
            LOG.warn("Bulk load operation did not find any files to load in " + "directory " + hfofDir.toUri() + ".  Does it contain files in " + "subdirectories that correspond to column family names?");
            return;
        }
        //prepare staging directory and token
        if (useSecure) {
            //Since delegation token doesn't work in mini cluster
            if (User.isSecurityEnabled()) {
                FileSystem fs = FileSystem.get(cfg);
                userToken = fs.getDelegationToken("renewer");
            }
            bulkToken = new SecureBulkLoadClient(table).prepareBulkLoad(table.getTableName());
        }
        // Assumes that region splits can happen while this occurs.
        while (!queue.isEmpty()) {
            // need to reload split keys each iteration.
            final Pair<byte[][], byte[][]> startEndKeys = table.getStartEndKeys();
            if (count != 0) {
                LOG.info("Split occured while grouping HFiles, retry attempt " + +count + " with " + queue.size() + " files remaining to group or split");
            }
            int maxRetries = cfg.getInt("hbase.bulkload.retries.number", 0);
            if (maxRetries != 0 && count >= maxRetries) {
                LOG.error("Retry attempted " + count + " times without completing, bailing out");
                return;
            }
            count++;
            // Using ByteBuffer for byte[] equality semantics
            Multimap<ByteBuffer, LoadQueueItem> regionGroups = groupOrSplitPhase(table, pool, queue, startEndKeys);
            bulkLoadPhase(table, conn, pool, queue, regionGroups);
        // NOTE: The next iteration's split / group could happen in parallel to
        // atomic bulkloads assuming that there are splits and no merges, and
        // that we can atomically pull out the groups we want to retry.
        }
    } finally {
        if (useSecure) {
            if (userToken != null) {
                try {
                    userToken.cancel(cfg);
                } catch (Exception e) {
                    LOG.warn("Failed to cancel HDFS delegation token.", e);
                }
            }
            if (bulkToken != null) {
                new SecureBulkLoadClient(table).cleanupBulkLoad(bulkToken);
            }
        }
        pool.shutdown();
        if (queue != null && !queue.isEmpty()) {
            StringBuilder err = new StringBuilder();
            err.append("-------------------------------------------------\n");
            err.append("Bulk load aborted with some files not yet loaded:\n");
            err.append("-------------------------------------------------\n");
            for (LoadQueueItem q : queue) {
                err.append("  ").append(q.hfilePath).append('\n');
            }
            LOG.error(err);
        }
    }
    if (queue != null && !queue.isEmpty()) {
        throw new RuntimeException("Bulk load aborted with some files not yet loaded." + "Please check log for more details.");
    }
}

8. IndexLoadIncrementalHFile#doBulkLoad()

View license
/**
   * Perform a bulk load of the given directory into the given pre-existing table. This method is
   * not threadsafe.
   * @param hfofDir the directory that was provided as the output path of a job using
   *          HFileOutputFormat
   * @param table the table to load into
   * @throws TableNotFoundException if table does not yet exist
   */
public void doBulkLoad(Path hfofDir, final HTable table) throws TableNotFoundException, IOException {
    final HConnection conn = table.getConnection();
    if (!conn.isTableAvailable(table.getTableName())) {
        throw new TableNotFoundException("Table " + Bytes.toStringBinary(table.getTableName()) + "is not currently available.");
    }
    // initialize thread pools
    int nrThreads = cfg.getInt("hbase.loadincremental.threads.max", Runtime.getRuntime().availableProcessors());
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setNameFormat("LoadIncrementalHFiles-%1$d");
    ExecutorService pool = new ThreadPoolExecutor(nrThreads, nrThreads, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), builder.build());
    ((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
    // LQI queue does not need to be threadsafe -- all operations on this queue
    // happen in this thread
    Deque<LoadQueueItem> queue = new LinkedList<LoadQueueItem>();
    try {
        discoverLoadQueue(queue, hfofDir);
        int count = 0;
        if (queue.isEmpty()) {
            LOG.warn("Bulk load operation did not find any files to load in " + "directory " + hfofDir.toUri() + ".  Does it contain files in " + "subdirectories that correspond to column family names?");
            return;
        }
        if (queue.isEmpty()) {
            LOG.warn("Bulk load operation did not find any files to load in " + "directory " + hfofDir.toUri() + ".  Does it contain files in " + "subdirectories that correspond to column family names?");
        }
        // Assumes that region splits can happen while this occurs.
        while (!queue.isEmpty()) {
            // need to reload split keys each iteration.
            final Pair<byte[][], byte[][]> startEndKeys = table.getStartEndKeys();
            if (count != 0) {
                LOG.info("Split occured while grouping HFiles, retry attempt " + +count + " with " + queue.size() + " files remaining to group or split");
            }
            int maxRetries = cfg.getInt("hbase.bulkload.retries.number", 0);
            if (maxRetries != 0 && count >= maxRetries) {
                LOG.error("Retry attempted " + count + " times without completing, bailing out");
                return;
            }
            count++;
            // Using ByteBuffer for byte[] equality semantics
            Multimap<ByteBuffer, LoadQueueItem> regionGroups = groupOrSplitPhase(table, pool, queue, startEndKeys);
            bulkLoadPhase(table, conn, pool, queue, regionGroups);
        // NOTE: The next iteration's split / group could happen in parallel to
        // atomic bulkloads assuming that there are splits and no merges, and
        // that we can atomically pull out the groups we want to retry.
        }
    } finally {
        pool.shutdown();
        if (queue != null && !queue.isEmpty()) {
            StringBuilder err = new StringBuilder();
            err.append("-------------------------------------------------\n");
            err.append("Bulk load aborted with some files not yet loaded:\n");
            err.append("-------------------------------------------------\n");
            for (LoadQueueItem q : queue) {
                err.append("  ").append(q.hfilePath).append('\n');
            }
            LOG.error(err);
        }
    }
}

9. Execs#makeThreadFactory()

Project: druid
Source File: Execs.java
View license
public static ThreadFactory makeThreadFactory(@NotNull String nameFormat, @Nullable Integer priority) {
    final ThreadFactoryBuilder builder = new ThreadFactoryBuilder().setDaemon(true).setNameFormat(nameFormat);
    if (priority != null) {
        builder.setPriority(priority);
    }
    return builder.build();
}

10. BookieClient#main()

Project: bookkeeper
Source File: BookieClient.java
View license
/**
     * @param args
     * @throws IOException
     * @throws NumberFormatException
     * @throws InterruptedException
     */
public static void main(String[] args) throws NumberFormatException, IOException, InterruptedException {
    if (args.length != 3) {
        System.err.println("USAGE: BookieClient bookieHost port ledger#");
        return;
    }
    WriteCallback cb = new WriteCallback() {

        public void writeComplete(int rc, long ledger, long entry, BookieSocketAddress addr, Object ctx) {
            Counter counter = (Counter) ctx;
            counter.dec();
            if (rc != 0) {
                System.out.println("rc = " + rc + " for " + entry + "@" + ledger);
            }
        }
    };
    Counter counter = new Counter();
    byte hello[] = "hello".getBytes(UTF_8);
    long ledger = Long.parseLong(args[2]);
    ThreadFactoryBuilder tfb = new ThreadFactoryBuilder();
    ClientSocketChannelFactory channelFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(tfb.setNameFormat("BookKeeper-NIOBoss-%d").build()), Executors.newCachedThreadPool(tfb.setNameFormat("BookKeeper-NIOWorker-%d").build()));
    OrderedSafeExecutor executor = OrderedSafeExecutor.newBuilder().name("BookieClientWorker").numThreads(1).build();
    BookieClient bc = new BookieClient(new ClientConfiguration(), channelFactory, executor);
    BookieSocketAddress addr = new BookieSocketAddress(args[0], Integer.parseInt(args[1]));
    for (int i = 0; i < 100000; i++) {
        counter.inc();
        bc.addEntry(addr, ledger, new byte[0], i, ChannelBuffers.wrappedBuffer(hello), cb, counter, 0);
    }
    counter.wait(0);
    System.out.println("Total = " + counter.total());
    channelFactory.releaseExternalResources();
    executor.shutdown();
}

11. UpdateLedgerOp#updateBookieIdInLedgers()

Project: bookkeeper
Source File: UpdateLedgerOp.java
View license
/**
     * Update the bookie id present in the ledger metadata.
     *
     * @param oldBookieId
     *            current bookie id
     * @param newBookieId
     *            new bookie id
     * @param rate
     *            number of ledgers updating per second (default 5 per sec)
     * @param limit
     *            maximum number of ledgers to update (default: no limit). Stop
     *            update if reaching limit
     * @param progressable
     *            report progress of the ledger updates
     * @throws IOException
     *             if there is an error when updating bookie id in ledger
     *             metadata
     * @throws InterruptedException
     *             interrupted exception when update ledger meta
     */
public void updateBookieIdInLedgers(final BookieSocketAddress oldBookieId, final BookieSocketAddress newBookieId, final int rate, final int limit, final UpdateLedgerNotifier progressable) throws BKException, IOException {
    final ThreadFactoryBuilder tfb = new ThreadFactoryBuilder().setNameFormat("UpdateLedgerThread").setDaemon(true);
    final ExecutorService executor = Executors.newSingleThreadExecutor(tfb.build());
    final AtomicInteger issuedLedgerCnt = new AtomicInteger();
    final AtomicInteger updatedLedgerCnt = new AtomicInteger();
    final Future<?> updateBookieCb = executor.submit(new Runnable() {

        @Override
        public void run() {
            updateLedgers(oldBookieId, newBookieId, rate, limit, progressable);
        }

        private void updateLedgers(final BookieSocketAddress oldBookieId, final BookieSocketAddress newBookieId, final int rate, final int limit, final UpdateLedgerNotifier progressable) {
            try {
                final AtomicBoolean stop = new AtomicBoolean(false);
                final Set<Long> outstandings = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>());
                final RateLimiter throttler = RateLimiter.create(rate);
                final Iterator<Long> ledgerItr = admin.listLedgers().iterator();
                final CountDownLatch syncObj = new CountDownLatch(1);
                // iterate through all the ledgers
                while (ledgerItr.hasNext() && !stop.get()) {
                    // throttler to control updates per second
                    throttler.acquire();
                    final Long lId = ledgerItr.next();
                    final ReadLedgerMetadataCb readCb = new ReadLedgerMetadataCb(bkc, lId, oldBookieId, newBookieId);
                    outstandings.add(lId);
                    FutureCallback<Void> updateLedgerCb = new UpdateLedgerCb(lId, stop, issuedLedgerCnt, updatedLedgerCnt, outstandings, syncObj, progressable);
                    Futures.addCallback(readCb.getFutureListener(), updateLedgerCb);
                    issuedLedgerCnt.incrementAndGet();
                    if (limit != Integer.MIN_VALUE && issuedLedgerCnt.get() >= limit || !ledgerItr.hasNext()) {
                        stop.set(true);
                    }
                    bkc.getLedgerManager().readLedgerMetadata(lId, readCb);
                }
                // waiting till all the issued ledgers are finished
                syncObj.await();
            } catch (IOException ioe) {
                LOG.error("Exception while updating ledger", ioe);
                throw new RuntimeException("Exception while updating ledger", ioe.getCause());
            } catch (InterruptedException ie) {
                LOG.error("Exception while updating ledger metadata", ie);
                Thread.currentThread().interrupt();
                throw new RuntimeException("Exception while updating ledger", ie.getCause());
            }
        }
    });
    try {
        // Wait to finish the issued ledgers.
        updateBookieCb.get();
    } catch (ExecutionException ee) {
        throw new IOException("Exception while updating ledger", ee);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new IOException("Exception while updating ledger", ie);
    } finally {
        executor.shutdown();
    }
}