List of usage examples for java.util.concurrent CyclicBarrier CyclicBarrier
public CyclicBarrier(int parties)
From source file:de.hybris.platform.test.ThreadPoolTest.java
@Test public void testTransactionCleanUpSimple() throws InterruptedException, BrokenBarrierException, TimeoutException { final boolean flagBefore = Config.getBoolean("transaction.monitor.begin", false); Config.setParameter("transaction.monitor.begin", "true"); ThreadPool pool = null;// w ww . j av a 2s .c o m try { pool = new ThreadPool(Registry.getCurrentTenantNoFallback().getTenantID(), 1); final GenericObjectPool.Config config = new GenericObjectPool.Config(); config.maxActive = 1; config.maxIdle = 1; config.maxWait = -1; config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK; config.testOnBorrow = true; config.testOnReturn = true; config.timeBetweenEvictionRunsMillis = 30 * 1000; // keep idle threads for at most 30 sec pool.setConfig(config); final CyclicBarrier gate = new CyclicBarrier(2); final AtomicReference<Throwable> threadError = new AtomicReference<Throwable>(); final AtomicReference<RecordingTransaction> threadTransaction = new AtomicReference<ThreadPoolTest.RecordingTransaction>(); final PoolableThread thread1 = pool.borrowThread(); thread1.execute(new Runnable() { @Override public void run() { try { final Transaction tx = new RecordingTransaction(); tx.activateAsCurrentTransaction(); tx.begin(); tx.begin(); // second time tx.begin(); // third time assertTrue(tx.isRunning()); assertEquals(3, tx.getOpenTransactionCount()); threadTransaction.set((RecordingTransaction) tx); gate.await(10, TimeUnit.SECONDS); } catch (final Throwable t) { threadError.set(t); } } }); gate.await(10, TimeUnit.SECONDS); assertNull(threadError.get()); // busy waiting for correct number of rollbacks - at the moment there is no hook for a better solution final RecordingTransaction tx = threadTransaction.get(); final long maxWait = System.currentTimeMillis() + (15 * 1000); while (tx.rollbackCounter.get() < 3 && System.currentTimeMillis() < maxWait) { Thread.sleep(100); } assertEquals(3, tx.rollbackCounter.get()); final PoolableThread thread2 = pool.borrowThread(); assertNotSame(thread1, thread2); } finally { if (pool != null) { try { pool.close(); } catch (final Exception e) { // can't help it } } Config.setParameter("transaction.monitor.begin", BooleanUtils.toStringTrueFalse(flagBefore)); } }
From source file:uk.ac.gla.terrier.probos.cli.pbsdsh.java
@Override public int run(String[] args) throws Exception { Credentials credentials = UserGroupInformation.getCurrentUser().getCredentials(); Iterator<Token<?>> iter = credentials.getAllTokens().iterator(); LOG.debug("Executing with tokens:"); while (iter.hasNext()) { Token<?> token = iter.next(); LOG.debug(token.toString());//from ww w. ja v a 2s. c o m if (token.getKind().equals(AMRMTokenIdentifier.KIND_NAME)) { iter.remove(); } } Options options = new Options(); options.addOption("h", true, "Specify hostname."); CommandLineParser parser = new GnuParser(); CommandLine cmd = parser.parse(options, args); final String[] dibcommand = cmd.getArgs(); int jobId = getJobId(); if (jobId == -1) { System.err.println("PBS: PBS_JOBID not set"); return 1; } String[] hosts; PBSJobStatusDistributed d = (PBSJobStatusDistributed) c.getJobStatus(jobId, 5); if (d.getState() == '?') { System.err.println("PBS: Job " + jobId + " was lost"); return -1; } if (d.getState() != 'R') { System.err.println("PBS: Job " + jobId + " was not running"); return -1; } int[] ports; if (cmd.hasOption('h')) { hosts = cmd.getOptionValues('h'); ports = new int[hosts.length]; String[] tmpH = d.getHostnames(); int[] tmpP = d.getPorts(); TObjectIntHashMap<String> host2port = new TObjectIntHashMap<String>(tmpH.length); for (int i = 0; i < tmpH.length; i++) { host2port.put(tmpH[i], tmpP[i]); } int i = 0; for (String h : hosts) { if (!host2port.contains(h)) { throw new IllegalArgumentException("Host " + h + " is not a member of this distributed job"); } ports[i++] = host2port.get(h); } } else { hosts = d.getHostnames(); ports = d.getPorts(); } final String secret = d.getSecret(); if (secret == null) throw new IllegalArgumentException( "No secret found - pbsdsh called too early? " + Arrays.toString(d.getHostnames())); LOG.debug("To run on " + Arrays.toString(hosts)); final CyclicBarrier barrier = new CyclicBarrier(1 + hosts.length); int i = 0; for (final String h : hosts) { final int port = ports[i++]; new Thread() { @Override public void run() { try { if (connectToSister(h, port, secret, dibcommand) != 0) LOG.error("Could not connect"); } catch (Exception e) { LOG.error("Could not connect", e); } finally { try { barrier.await(); } catch (Exception e) { LOG.error("Barrier problem?"); } } } }.start(); } barrier.await(); return 0; }
From source file:ch.cyberduck.core.PathTest.java
@Test public void testTransferInterrupt() throws Exception { final Path p = new NullPath("/t", Path.FILE_TYPE); final TransferStatus status = new TransferStatus(); final CyclicBarrier lock = new CyclicBarrier(2); final CyclicBarrier exit = new CyclicBarrier(2); status.setLength(432768L);// w w w. j a v a2 s . co m new Thread(new Runnable() { @Override public void run() { try { p.transfer(new NullInputStream(status.getLength()), new NullOutputStream(), new StreamListener() { @Override public void bytesSent(long bytes) { // } @Override public void bytesReceived(long bytes) { try { lock.await(); exit.await(); } catch (InterruptedException e) { fail(e.getMessage()); } catch (BrokenBarrierException e) { fail(e.getMessage()); } } }, -1, status); } catch (IOException e) { assertTrue(e instanceof ConnectionCanceledException); } } }).start(); lock.await(); status.setCanceled(); exit.await(); assertFalse(status.isComplete()); assertTrue(status.isCanceled()); assertEquals(32768L, status.getCurrent()); }
From source file:com.netflix.dyno.connectionpool.impl.lb.CircularListTest.java
@Test public void testMultipleThreads() throws Exception { final AtomicBoolean stop = new AtomicBoolean(false); final CyclicBarrier barrier = new CyclicBarrier(5); final List<Future<Map<Integer, Integer>>> futures = new ArrayList<Future<Map<Integer, Integer>>>(); for (int i = 0; i < 5; i++) { futures.add(threadPool.submit(new Callable<Map<Integer, Integer>>() { @Override/*from w w w . j ava 2s. c o m*/ public Map<Integer, Integer> call() throws Exception { barrier.await(); TestWorker worker = new TestWorker(); while (!stop.get()) { worker.process(); } return worker.map; } })); } Thread.sleep(200); stop.set(true); Map<Integer, Integer> totalMap = getTotalMap(futures); checkValues(new ArrayList<Integer>(totalMap.values())); }
From source file:com.linkedin.pinot.core.query.scheduler.PrioritySchedulerTest.java
@Test public void testOneQuery() throws InterruptedException, ExecutionException, IOException, BrokenBarrierException { PropertiesConfiguration conf = new PropertiesConfiguration(); conf.setProperty(ResourceLimitPolicy.THREADS_PER_QUERY_PCT, 50); conf.setProperty(ResourceLimitPolicy.TABLE_THREADS_HARD_LIMIT, 40); conf.setProperty(ResourceLimitPolicy.TABLE_THREADS_SOFT_LIMIT, 20); useBarrier = true;//from w w w. j a va 2s .c o m startupBarrier = new CyclicBarrier(2); validationBarrier = new CyclicBarrier(2); TestPriorityScheduler scheduler = TestPriorityScheduler.create(conf); int totalPermits = scheduler.getRunningQueriesSemaphore().availablePermits(); scheduler.start(); ListenableFuture<byte[]> result = scheduler.submit(createServerQueryRequest("1", metrics)); startupBarrier.await(); TestSchedulerGroup group = TestPriorityScheduler.groupFactory.groupMap.get("1"); assertEquals(group.numRunning(), 1); assertEquals(group.getThreadsInUse(), 1); // The total number of threads allocated for query execution will be dependent on the underlying // platform (number of cores). Scheduler will assign total threads up to but not exceeding total // number of segments. On servers with less cores, this can assign only 1 thread (less than total segments) assertTrue(group.totalReservedThreads() <= 2 /* 2: numSegments in request*/); validationBarrier.await(); byte[] resultData = result.get(); DataTable table = DataTableFactory.getDataTable(resultData); assertEquals(table.getMetadata().get("table"), "1"); // verify that accounting is handled right assertEquals(group.numPending(), 0); assertEquals(group.getThreadsInUse(), 0); assertEquals(group.totalReservedThreads(), 0); // -1 because we expect that 1 permit is blocked by the scheduler main thread assertEquals(scheduler.getRunningQueriesSemaphore().availablePermits(), totalPermits - 1); scheduler.stop(); }
From source file:com.netflix.dyno.connectionpool.impl.lb.CircularListTest.java
@Test public void testMultipleThreadsWithElementAdd() throws Exception { final AtomicBoolean stop = new AtomicBoolean(false); final CyclicBarrier barrier = new CyclicBarrier(5); final List<Future<Map<Integer, Integer>>> futures = new ArrayList<Future<Map<Integer, Integer>>>(); for (int i = 0; i < 5; i++) { futures.add(threadPool.submit(new Callable<Map<Integer, Integer>>() { @Override//from ww w .j a v a 2s . co m public Map<Integer, Integer> call() throws Exception { barrier.await(); TestWorker worker = new TestWorker(); while (!stop.get()) { worker.process(); } return worker.map; } })); } Thread.sleep(200); List<Integer> newList = new ArrayList<Integer>(iList); for (int i = 10; i < 15; i++) { newList.add(i); } cList.swapWithList(newList); Thread.sleep(200); stop.set(true); Map<Integer, Integer> result = getTotalMap(futures); Map<Integer, Integer> subMap = CollectionUtils.filterKeys(result, new Predicate<Integer>() { @Override public boolean apply(Integer input) { return input < 10; } }); checkValues(new ArrayList<Integer>(subMap.values())); subMap = CollectionUtils.difference(result, subMap).entriesOnlyOnLeft(); checkValues(new ArrayList<Integer>(subMap.values())); }
From source file:org.apache.hadoop.yarn.server.nodemanager.TestNodeManagerResync.java
@Before public void setup() throws UnsupportedFileSystemException { localFS = FileContext.getLocalFSFileContext(); tmpDir.mkdirs();//from w ww . java 2s . c om logsDir.mkdirs(); remoteLogsDir.mkdirs(); nmLocalDir.mkdirs(); syncBarrier = new CyclicBarrier(2); updateBarrier = new CyclicBarrier(2); }
From source file:io.druid.indexer.HdfsClasspathSetupTest.java
@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//from w ww.jav a 2s. co m 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(); }
From source file:com.adaptris.core.interceptor.ThrottlingInterceptor.java
private boolean throttle(final long delay) { final CyclicBarrier barrier = new CyclicBarrier(2); // This is to make sure we don't break the barrier before the real delay is up. // /*from w w w. ja va 2s .c o m*/ long barrierDelay = delay + TimeUnit.SECONDS.toMillis(1L); boolean result = true; try { executor.execute(new Runnable() { @Override public void run() { try { Thread.sleep(delay); barrier.await(TimeUnit.SECONDS.toMillis(1L), TimeUnit.MILLISECONDS); } catch (InterruptedException | BrokenBarrierException | TimeoutException e) { log.trace("Interrupted during sleep; breaking barrier"); barrier.reset(); } } }); barrier.await(barrierDelay, TimeUnit.MILLISECONDS); } catch (InterruptedException | BrokenBarrierException | TimeoutException e) { result = false; } return result; }
From source file:org.opendatakit.persistence.TaskLockTest.java
@Test public void verifyLock() throws ODKDatastoreException, InterruptedException { TaskLockImpl.TaskLockTable.assertRelation(callingContext.getDatastore(), callingContext.getCurrentUser()); Thread.sleep(1000);/*from w w w .ja v a 2 s. c o m*/ int MAX_THREADS = 8; CyclicBarrier launchBarrier = new CyclicBarrier(MAX_THREADS); List<TaskLockThread> lockTesters = new ArrayList<TaskLockThread>(); for (int i = 0; i < MAX_THREADS; ++i) { TaskLockThread t = new TaskLockThread(launchBarrier, callingContext); t.start(); lockTesters.add(t); } boolean failure = false; for (int i = 0; i < MAX_THREADS; ++i) { TaskLockThread t = lockTesters.get(i); try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); failure = true; } } int entryTally = 0; int declinedEntryTally = 0; for (int i = 0; i < MAX_THREADS; ++i) { TaskLockThread t = lockTesters.get(i); if (t.failed) { System.out.println("FAILED Thread " + t.getId()); failure = true; } entryTally += t.entryCount; declinedEntryTally += t.declinedEntryCount; } System.out.println("entryCount " + entryTally + " of " + TaskLockThread.ENTRY_ATTEMPTS * MAX_THREADS); System.out.println("declinedEntryCount " + declinedEntryTally); assertEquals(failure, false); }