Search in sources :

Example 21 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project druid by druid-io.

the class NamedIntrospectionHandler method testConcurrencyStartStoooooooooop.

@Test
public void testConcurrencyStartStoooooooooop() throws Exception {
    lookupReferencesManager.stop();
    lookupReferencesManager.start();
    final CyclicBarrier cyclicBarrier = new CyclicBarrier(CONCURRENT_THREADS);
    final Runnable start = new Runnable() {

        @Override
        public void run() {
            try {
                cyclicBarrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                throw Throwables.propagate(e);
            }
            lookupReferencesManager.stop();
        }
    };
    final Collection<ListenableFuture<?>> futures = new ArrayList<>(CONCURRENT_THREADS);
    for (int i = 0; i < CONCURRENT_THREADS; ++i) {
        futures.add(executorService.submit(start));
    }
    Futures.allAsList(futures).get(100, TimeUnit.MILLISECONDS);
    for (ListenableFuture future : futures) {
        Assert.assertNull(future.get());
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 22 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project elastic-job by dangdangdotcom.

the class ExecutorServiceHandlerRegistryTest method assertGetExecutorServiceHandlerForConcurrent.

@Test
public void assertGetExecutorServiceHandlerForConcurrent() throws InterruptedException {
    int threadCount = 100;
    CyclicBarrier barrier = new CyclicBarrier(threadCount);
    ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
    CountDownLatch latch = new CountDownLatch(threadCount);
    Set<ExecutorService> set = new CopyOnWriteArraySet<>();
    for (int i = 0; i < threadCount; i++) {
        executorService.submit(new GetExecutorServiceHandlerTask(barrier, latch, set));
    }
    latch.await();
    assertThat(set.size(), is(1));
    assertThat(ExecutorServiceHandlerRegistry.getExecutorServiceHandler("test_job", new DefaultExecutorServiceHandler()), is(set.iterator().next()));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) DefaultExecutorServiceHandler(com.dangdang.ddframe.job.executor.handler.impl.DefaultExecutorServiceHandler) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 23 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project druid by druid-io.

the class HdfsClasspathSetupTest method testConcurrentUpload.

@Test
public void testConcurrentUpload() throws IOException, InterruptedException, ExecutionException, TimeoutException {
    final int concurrency = 10;
    ListeningExecutorService pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(concurrency));
    // barrier ensures that all jobs try to add files to classpath at same time.
    final CyclicBarrier barrier = new CyclicBarrier(concurrency);
    final DistributedFileSystem fs = miniCluster.getFileSystem();
    final Path expectedJarPath = new Path(finalClasspath, dummyJarFile.getName());
    List<ListenableFuture<Boolean>> futures = new ArrayList<>();
    for (int i = 0; i < concurrency; i++) {
        futures.add(pool.submit(new Callable() {

            @Override
            public Boolean call() throws Exception {
                int id = barrier.await();
                Job job = Job.getInstance(conf, "test-job-" + id);
                Path intermediatePathForJob = new Path(intermediatePath, "job-" + id);
                JobHelper.addJarToClassPath(dummyJarFile, finalClasspath, intermediatePathForJob, fs, job);
                // check file gets uploaded to final HDFS path
                Assert.assertTrue(fs.exists(expectedJarPath));
                // check that the intermediate file is not present
                Assert.assertFalse(fs.exists(new Path(intermediatePathForJob, dummyJarFile.getName())));
                // check file gets added to the classpath
                Assert.assertEquals(expectedJarPath.toString(), job.getConfiguration().get(MRJobConfig.CLASSPATH_FILES));
                return true;
            }
        }));
    }
    Futures.allAsList(futures).get(30, TimeUnit.SECONDS);
    pool.shutdownNow();
}
Also used : Path(org.apache.hadoop.fs.Path) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem) Job(org.apache.hadoop.mapreduce.Job) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 24 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project elasticsearch by elastic.

the class RetryTests method blockExecutor.

/**
     * Blocks the named executor by getting its only thread running a task blocked on a CyclicBarrier and fills the queue with a noop task.
     * So requests to use this queue should get {@link EsRejectedExecutionException}s.
     */
private CyclicBarrier blockExecutor(String name) throws Exception {
    ThreadPool threadPool = getInstanceFromNode(ThreadPool.class);
    CyclicBarrier barrier = new CyclicBarrier(2);
    logger.info("Blocking the [{}] executor", name);
    threadPool.executor(name).execute(() -> {
        try {
            threadPool.executor(name).execute(() -> {
            });
            barrier.await();
            logger.info("Blocked the [{}] executor", name);
            barrier.await();
            logger.info("Unblocking the [{}] executor", name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    barrier.await();
    blockedExecutors.add(barrier);
    return barrier;
}
Also used : ThreadPool(org.elasticsearch.threadpool.ThreadPool) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 25 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.

the class BlockingArrayQueueTest method testConcurrentAccess.

@Test
@Slow
public void testConcurrentAccess() throws Exception {
    final int THREADS = 50;
    final int LOOPS = 1000;
    final BlockingArrayQueue<Integer> queue = new BlockingArrayQueue<>(1 + THREADS * LOOPS);
    final ConcurrentLinkedQueue<Integer> produced = new ConcurrentLinkedQueue<>();
    final ConcurrentLinkedQueue<Integer> consumed = new ConcurrentLinkedQueue<>();
    final AtomicBoolean running = new AtomicBoolean(true);
    // start consumers
    final CyclicBarrier barrier0 = new CyclicBarrier(THREADS + 1);
    for (int i = 0; i < THREADS; i++) {
        new Thread() {

            @Override
            public void run() {
                final Random random = new Random();
                setPriority(getPriority() - 1);
                try {
                    while (running.get()) {
                        int r = 1 + random.nextInt(10);
                        if (r % 2 == 0) {
                            Integer msg = queue.poll();
                            if (msg == null) {
                                Thread.sleep(1 + random.nextInt(10));
                                continue;
                            }
                            consumed.add(msg);
                        } else {
                            Integer msg = queue.poll(r, TimeUnit.MILLISECONDS);
                            if (msg != null)
                                consumed.add(msg);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        barrier0.await();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    // start producers
    final CyclicBarrier barrier1 = new CyclicBarrier(THREADS + 1);
    for (int i = 0; i < THREADS; i++) {
        final int id = i;
        new Thread() {

            @Override
            public void run() {
                final Random random = new Random();
                try {
                    for (int j = 0; j < LOOPS; j++) {
                        Integer msg = random.nextInt();
                        produced.add(msg);
                        if (!queue.offer(msg))
                            throw new Exception(id + " FULL! " + queue.size());
                        Thread.sleep(1 + random.nextInt(10));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        barrier1.await();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    barrier1.await();
    int size = queue.size();
    int last = size - 1;
    while (size > 0 && size != last) {
        last = size;
        Thread.sleep(500);
        size = queue.size();
    }
    running.set(false);
    barrier0.await();
    HashSet<Integer> prodSet = new HashSet<>(produced);
    HashSet<Integer> consSet = new HashSet<>(consumed);
    Assert.assertEquals(prodSet, consSet);
}
Also used : CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) HashSet(java.util.HashSet) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Aggregations

CyclicBarrier (java.util.concurrent.CyclicBarrier)322 Test (org.junit.Test)112 CountDownLatch (java.util.concurrent.CountDownLatch)89 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)77 IOException (java.io.IOException)65 ArrayList (java.util.ArrayList)65 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)64 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)63 ExecutorService (java.util.concurrent.ExecutorService)39 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 Ignite (org.apache.ignite.Ignite)33 IgniteException (org.apache.ignite.IgniteException)32 Test (org.testng.annotations.Test)26 IgniteCache (org.apache.ignite.IgniteCache)25 List (java.util.List)24 TimeoutException (java.util.concurrent.TimeoutException)22 ExecutionException (java.util.concurrent.ExecutionException)21 Transaction (org.apache.ignite.transactions.Transaction)21 Callable (java.util.concurrent.Callable)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19