Search in sources :

Example 11 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project sling by apache.

the class ThreadPoolExecutorCleaningThreadLocalsTest method setUp.

@Before
public void setUp() {
    final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(20);
    final RejectedExecutionHandler rejectionHandler = new ThreadPoolExecutor.AbortPolicy();
    pool = new ThreadPoolExecutorCleaningThreadLocals(1, 1, 100, TimeUnit.MILLISECONDS, queue, Executors.defaultThreadFactory(), rejectionHandler, listener);
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) Before(org.junit.Before)

Example 12 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project voldemort by voldemort.

the class ConsistencyFix method execute.

public String execute(int parallelism, String badKeyFileIn, boolean orphanFormat, String badKeyFileOut) {
    ExecutorService badKeyReaderService;
    ExecutorService badKeyWriterService;
    ExecutorService consistencyFixWorkers;
    // Create BadKeyWriter thread
    BlockingQueue<BadKeyStatus> badKeyQOut = new ArrayBlockingQueue<BadKeyStatus>(parallelism * 10);
    badKeyWriterService = Executors.newSingleThreadExecutor();
    badKeyWriterService.submit(new BadKeyWriter(badKeyFileOut, badKeyQOut));
    logger.info("Created badKeyWriter.");
    // Create ConsistencyFixWorker thread pool
    BlockingQueue<Runnable> blockingQ = new ArrayBlockingQueue<Runnable>(parallelism);
    RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
    consistencyFixWorkers = new ThreadPoolExecutor(parallelism, parallelism, 0L, TimeUnit.MILLISECONDS, blockingQ, rejectedExecutionHandler);
    logger.info("Created ConsistencyFixWorker pool.");
    // Create BadKeyReader thread
    CountDownLatch allBadKeysReadLatch = new CountDownLatch(1);
    badKeyReaderService = Executors.newSingleThreadExecutor();
    BadKeyReader badKeyReader = null;
    if (!orphanFormat) {
        badKeyReader = new BadKeyReader(allBadKeysReadLatch, badKeyFileIn, this, consistencyFixWorkers, badKeyQOut);
    } else {
        badKeyReader = new BadKeyOrphanReader(allBadKeysReadLatch, badKeyFileIn, this, consistencyFixWorkers, badKeyQOut);
    }
    badKeyReaderService.submit(badKeyReader);
    logger.info("Created badKeyReader.");
    try {
        allBadKeysReadLatch.await();
        badKeyReaderService.shutdown();
        badKeyReaderService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        logger.info("Bad key reader service has shutdown.");
        consistencyFixWorkers.shutdown();
        consistencyFixWorkers.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        logger.info("All workers have shutdown.");
        // Poison the bad key writer to have it exit.
        badKeyQOut.put(new BadKeyStatus());
        badKeyWriterService.shutdown();
        badKeyWriterService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        logger.info("Bad key writer service has shutdown.");
    } catch (InterruptedException e) {
        logger.error("InterruptedException caught.");
        if (logger.isDebugEnabled()) {
            e.printStackTrace();
        }
    } finally {
        adminClient.close();
    }
    // Cobble together a status string for overall execution.
    StringBuilder sb = new StringBuilder();
    sb.append("\n\n");
    sb.append("Exit statuses of various threads:\n");
    sb.append("\tBadKeyReader: ");
    if (badKeyReader.hasException()) {
        sb.append("Had exception!\n");
    } else {
        sb.append("OK.\n");
    }
    sb.append("\tBadKeyWriter: ");
    if (badKeyReader.hasException()) {
        sb.append("Had exception!\n");
    } else {
        sb.append("OK.\n");
    }
    sb.append("\n\n");
    sb.append(stats.summary());
    return sb.toString();
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) CountDownLatch(java.util.concurrent.CountDownLatch) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 13 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project tomee by apache.

the class Pool method createExecutor.

private Executor createExecutor() {
    final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 10, 60L, SECONDS, new LinkedBlockingQueue<Runnable>(2), new DaemonThreadFactory("org.apache.openejb.util.Pool", hashCode()));
    threadPoolExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(final Runnable r, final ThreadPoolExecutor tpe) {
            if (null == r || null == tpe || tpe.isShutdown() || tpe.isTerminated() || tpe.isTerminating()) {
                return;
            }
            try {
                if (!tpe.getQueue().offer(r, 20, SECONDS)) {
                    org.apache.openejb.util.Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources").warning("Default pool executor failed to run asynchronous process: " + r);
                }
            } catch (final InterruptedException e) {
            //Ignore
            }
        }
    });
    return threadPoolExecutor;
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 14 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project ddf by codice.

the class IngestCommand method executeWithSubject.

@Override
protected Object executeWithSubject() throws Exception {
    if (batchSize * multithreaded > MAX_QUEUE_SIZE) {
        throw new IngestException(String.format("batchsize * multithreaded cannot be larger than %d.", MAX_QUEUE_SIZE));
    }
    final File inputFile = getInputFile();
    if (inputFile == null) {
        return null;
    }
    int totalFiles = totalFileCount(inputFile);
    fileCount.set(totalFiles);
    final ArrayBlockingQueue<Metacard> metacardQueue = new ArrayBlockingQueue<>(batchSize * multithreaded);
    ExecutorService queueExecutor = Executors.newSingleThreadExecutor();
    final long start = System.currentTimeMillis();
    printProgressAndFlush(start, fileCount.get(), 0);
    // Registering for the main thread and on behalf of the buildQueue thread;
    // the buildQueue thread will unregister itself when the files have all
    // been added to the blocking queue and the final registration will
    // be held for the await.
    phaser.register();
    phaser.register();
    queueExecutor.submit(() -> buildQueue(inputFile, metacardQueue, start));
    final ScheduledExecutorService batchScheduler = Executors.newSingleThreadScheduledExecutor();
    BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(multithreaded);
    RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
    ExecutorService executorService = new ThreadPoolExecutor(multithreaded, multithreaded, 0L, TimeUnit.MILLISECONDS, blockingQueue, rejectedExecutionHandler);
    final CatalogFacade catalog = getCatalog();
    submitToCatalog(batchScheduler, executorService, metacardQueue, catalog, start);
    // await on catalog processing threads to complete emptying queue
    phaser.awaitAdvance(phaser.arrive());
    try {
        queueExecutor.shutdown();
        executorService.shutdown();
        batchScheduler.shutdown();
    } catch (SecurityException e) {
        LOGGER.info("Executor service shutdown was not permitted: {}", e);
    }
    printProgressAndFlush(start, fileCount.get(), ingestCount.get() + ignoreCount.get());
    long end = System.currentTimeMillis();
    console.println();
    String elapsedTime = timeFormatter.print(new Period(start, end).withMillis(0));
    console.println();
    console.printf(" %d file(s) ingested in %s %n", ingestCount.get(), elapsedTime);
    LOGGER.debug("{} file(s) ingested in {} [{} records/sec]", ingestCount.get(), elapsedTime, calculateRecordsPerSecond(ingestCount.get(), start, end));
    INGEST_LOGGER.info("{} file(s) ingested in {} [{} records/sec]", ingestCount.get(), elapsedTime, calculateRecordsPerSecond(ingestCount.get(), start, end));
    if (fileCount.get() != ingestCount.get()) {
        console.println();
        if ((fileCount.get() - ingestCount.get() - ignoreCount.get()) >= 1) {
            String failedAmount = Integer.toString(fileCount.get() - ingestCount.get() - ignoreCount.get());
            printErrorMessage(failedAmount + " file(s) failed to be ingested.  See the ingest log for more details.");
            INGEST_LOGGER.warn("{} files(s) failed to be ingested.", failedAmount);
        }
        if (ignoreList != null) {
            String ignoredAmount = Integer.toString(ignoreCount.get());
            printColor(Ansi.Color.YELLOW, ignoredAmount + " file(s) ignored.  See the ingest log for more details.");
            INGEST_LOGGER.warn("{} files(s) were ignored.", ignoredAmount);
        }
    }
    console.println();
    SecurityLogger.audit("Ingested {} files from {}", ingestCount.get(), filePath);
    return null;
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) Period(org.joda.time.Period) Metacard(ddf.catalog.data.Metacard) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade) IngestException(ddf.catalog.source.IngestException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) File(java.io.File)

Example 15 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project hbase by apache.

the class MobUtils method createMobCompactorThreadPool.

/**
   * Creates a thread pool.
   * @param conf the Configuration
   * @return A thread pool.
   */
public static ExecutorService createMobCompactorThreadPool(Configuration conf) {
    int maxThreads = conf.getInt(MobConstants.MOB_COMPACTION_THREADS_MAX, MobConstants.DEFAULT_MOB_COMPACTION_THREADS_MAX);
    if (maxThreads == 0) {
        maxThreads = 1;
    }
    final SynchronousQueue<Runnable> queue = new SynchronousQueue<>();
    ThreadPoolExecutor pool = new ThreadPoolExecutor(1, maxThreads, 60, TimeUnit.SECONDS, queue, Threads.newDaemonThreadFactory("MobCompactor"), new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                // waiting for a thread to pick up instead of throwing exceptions.
                queue.put(r);
            } catch (InterruptedException e) {
                throw new RejectedExecutionException(e);
            }
        }
    });
    ((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
    return pool;
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)27 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)23 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)6 ThreadFactory (java.util.concurrent.ThreadFactory)6 ExecutorService (java.util.concurrent.ExecutorService)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 SynchronousQueue (java.util.concurrent.SynchronousQueue)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)3 Metacard (ddf.catalog.data.Metacard)2 SourceResponse (ddf.catalog.operation.SourceResponse)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 XRejectedExecutionHandler (org.elasticsearch.common.util.concurrent.XRejectedExecutionHandler)2 Test (org.junit.Test)2 NamedThreadFactory (com.alibaba.otter.shared.common.utils.thread.NamedThreadFactory)1