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());
}
}
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()));
}
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();
}
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;
}
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);
}
Aggregations