use of java.util.concurrent.CyclicBarrier in project ACS by ACS-Community.
the class ManagerImplTest method testConcurrentContainersLogin.
public void testConcurrentContainersLogin() {
final Object sync = new Object();
final AtomicBoolean done = new AtomicBoolean(false);
final AtomicInteger counter = new AtomicInteger(0);
final AtomicInteger loginCounter = new AtomicInteger(0);
final int CONCURRENT_LOGINS = 100;
final CyclicBarrier barrier = new CyclicBarrier(CONCURRENT_LOGINS);
class LoginWorker implements Runnable {
public void run() {
final String containerName = "Container" + counter.incrementAndGet();
Container container = new TestContainer(containerName);
try {
barrier.await();
} catch (Throwable th) {
return;
}
ClientInfo info = null;
try {
info = manager.login(container);
} catch (AcsJNoPermissionEx e) {
fail("No permission");
}
// note that if this fails, tearDown will be called (and manager shutdown)
assertTrue(containerName + " failed to login.", info != null && info.getHandle() != 0);
if (loginCounter.incrementAndGet() == CONCURRENT_LOGINS) {
synchronized (sync) {
sync.notifyAll();
}
}
}
}
synchronized (sync) {
// spawn threads
for (int i = 0; i < CONCURRENT_LOGINS; ++i) new Thread(new LoginWorker()).start();
try {
sync.wait(CONCURRENT_LOGINS * 500);
} catch (InterruptedException e) {
}
}
assertTrue("All containers failed to login successfully in time.", loginCounter.get() == CONCURRENT_LOGINS);
}
use of java.util.concurrent.CyclicBarrier in project alluxio by Alluxio.
the class IndexedSetConcurrencyTest method nonUniqueConcurrentUpdateTest.
/**
* Use the mSizeIndex as primary index, test the correctness of using non-unique index as primary
* index.
*/
@Test
public void nonUniqueConcurrentUpdateTest() throws Exception {
mIndexedSet = new IndexedSet<>(mSizeIndex, mIdIndex);
List<Future<?>> futures = new ArrayList<>();
int[] tasksNumbers = new int[5];
int totalTasksNumber = 0;
// Try to balance adds and removes
tasksNumbers[0] = 4 * ThreadLocalRandom.current().nextInt(MIN_TASKS, MAX_TASKS + 1);
totalTasksNumber += tasksNumbers[0];
// Add random number of each task type.
for (int i = 1; i < 5; i++) {
tasksNumbers[i] = ThreadLocalRandom.current().nextInt(MIN_TASKS, MAX_TASKS + 1);
totalTasksNumber += tasksNumbers[i];
}
CyclicBarrier barrier = new CyclicBarrier(totalTasksNumber);
for (int i = 0; i < tasksNumbers[0]; i++) {
futures.add(mThreadPool.submit(new ConcurrentAdd(barrier)));
}
for (int i = 0; i < tasksNumbers[1]; i++) {
futures.add(mThreadPool.submit(new ConcurrentRemove(barrier)));
}
for (int i = 0; i < tasksNumbers[2]; i++) {
futures.add(mThreadPool.submit(new ConcurrentRemoveByField(barrier)));
}
for (int i = 0; i < tasksNumbers[3]; i++) {
futures.add(mThreadPool.submit(new ConcurrentRemoveByIterator(barrier)));
}
for (int i = 0; i < tasksNumbers[4]; i++) {
futures.add(mThreadPool.submit(new ConcurrentClear(barrier)));
}
CommonUtils.sleepMs(TEST_CASE_DURATION_MS);
mStopThreads.set(true);
for (Future<?> future : futures) {
future.get();
}
verifySet();
}
use of java.util.concurrent.CyclicBarrier in project alluxio by Alluxio.
the class ConcurrentFileSystemMasterTest method concurrentRename.
/**
* Helper for renaming a list of paths concurrently. Assumes the srcs are already created and
* dsts do not exist. Enforces that the run time of this method is not greater than twice the
* sleep time (to infer concurrent operations). Injects an artificial sleep time to the
* sleeping under file system and resets it after the renames are complete.
*
* @param src list of source paths
* @param dst list of destination paths
* @return how many errors occurred
*/
private int concurrentRename(final AlluxioURI[] src, final AlluxioURI[] dst) throws Exception {
final int numFiles = src.length;
final CyclicBarrier barrier = new CyclicBarrier(numFiles);
List<Thread> threads = new ArrayList<>(numFiles);
// If there are exceptions, we will store them here.
final ConcurrentHashSet<Throwable> errors = new ConcurrentHashSet<>();
Thread.UncaughtExceptionHandler exceptionHandler = new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread th, Throwable ex) {
errors.add(ex);
}
};
for (int i = 0; i < numFiles; i++) {
final int iteration = i;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
AuthenticatedClientUser.set(TEST_USER);
barrier.await();
mFileSystem.rename(src[iteration], dst[iteration]);
} catch (Exception e) {
Throwables.propagate(e);
}
}
});
t.setUncaughtExceptionHandler(exceptionHandler);
threads.add(t);
}
Collections.shuffle(threads);
long startMs = CommonUtils.getCurrentMs();
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
}
long durationMs = CommonUtils.getCurrentMs() - startMs;
Assert.assertTrue("Execution duration " + durationMs + " took longer than expected " + LIMIT_MS, durationMs < LIMIT_MS);
return errors.size();
}
use of java.util.concurrent.CyclicBarrier in project alluxio by Alluxio.
the class FileSystemMasterIntegrationTest method concurrentCreateDelete.
@Test
public void concurrentCreateDelete() throws Exception {
List<Future<?>> futures = new ArrayList<>();
AlluxioURI directory = new AlluxioURI("/dir");
AlluxioURI[] files = new AlluxioURI[10];
final int numThreads = 8;
final int testDurationMs = 3000;
for (int i = 0; i < 10; i++) {
files[i] = directory.join("file_" + i);
}
mFsMaster.createDirectory(directory, CreateDirectoryOptions.defaults());
AtomicBoolean stopThreads = new AtomicBoolean(false);
CyclicBarrier barrier = new CyclicBarrier(numThreads);
ExecutorService threadPool = Executors.newCachedThreadPool();
try {
for (int i = 0; i < numThreads; i++) {
futures.add(threadPool.submit(new ConcurrentCreateDelete(barrier, stopThreads, files)));
}
CommonUtils.sleepMs(testDurationMs);
stopThreads.set(true);
for (Future<?> future : futures) {
future.get();
}
// Stop Alluxio.
mLocalAlluxioClusterResource.get().stopFS();
// Create the master using the existing journal.
createFileSystemMasterFromJournal();
} finally {
threadPool.shutdownNow();
threadPool.awaitTermination(SHUTDOWN_TIME_MS, TimeUnit.MILLISECONDS);
}
}
use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.
the class ManagedLedgerBkTest method testConcurrentMarkDelete.
@Test
public void testConcurrentMarkDelete() throws Exception {
ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, zkc);
ManagedLedgerConfig mlConfig = new ManagedLedgerConfig();
mlConfig.setEnsembleSize(1).setAckQuorumSize(1).setMetadataEnsembleSize(1).setMetadataAckQuorumSize(1);
// set the data ledger size
mlConfig.setMaxEntriesPerLedger(100);
// set the metadata ledger size to 1 to kick off many ledger switching cases
mlConfig.setMetadataMaxEntriesPerLedger(10);
ManagedLedger ledger = factory.open("ml-markdelete-ledger", mlConfig);
final List<Position> addedEntries = Lists.newArrayList();
int numCursors = 10;
final CyclicBarrier barrier = new CyclicBarrier(numCursors);
List<ManagedCursor> cursors = Lists.newArrayList();
for (int i = 0; i < numCursors; i++) {
cursors.add(ledger.openCursor(String.format("c%d", i)));
}
for (int i = 0; i < 50; i++) {
Position pos = ledger.addEntry("entry".getBytes());
addedEntries.add(pos);
}
List<Future<?>> futures = Lists.newArrayList();
for (ManagedCursor cursor : cursors) {
futures.add(executor.submit(() -> {
barrier.await();
for (Position position : addedEntries) {
cursor.markDelete(position);
}
return null;
}));
}
for (Future<?> future : futures) {
future.get();
}
// Since in this test we roll-over the cursor ledger every 10 entries acknowledged, the background roll back
// might still be happening when the futures are completed.
Thread.sleep(1000);
factory.shutdown();
}
Aggregations