Search in sources :

Example 61 with CyclicBarrier

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);
}
Also used : CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Container(com.cosylab.acs.maci.Container) AcsJNoPermissionEx(alma.maciErrType.wrappers.AcsJNoPermissionEx) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientInfo(com.cosylab.acs.maci.ClientInfo)

Example 62 with CyclicBarrier

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();
}
Also used : ArrayList(java.util.ArrayList) CyclicBarrier(java.util.concurrent.CyclicBarrier) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 63 with CyclicBarrier

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();
}
Also used : ArrayList(java.util.ArrayList) InvalidPathException(alluxio.exception.InvalidPathException) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ConcurrentHashSet(alluxio.collections.ConcurrentHashSet)

Example 64 with CyclicBarrier

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);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) AlluxioURI(alluxio.AlluxioURI) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 65 with CyclicBarrier

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();
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) Future(java.util.concurrent.Future) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Aggregations

CyclicBarrier (java.util.concurrent.CyclicBarrier)650 Test (org.junit.Test)315 CountDownLatch (java.util.concurrent.CountDownLatch)169 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)142 ArrayList (java.util.ArrayList)126 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)124 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)121 IOException (java.io.IOException)97 ExecutorService (java.util.concurrent.ExecutorService)84 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)67 AtomicReference (java.util.concurrent.atomic.AtomicReference)66 Ignite (org.apache.ignite.Ignite)64 List (java.util.List)53 Test (org.testng.annotations.Test)52 TimeoutException (java.util.concurrent.TimeoutException)48 Transaction (org.apache.ignite.transactions.Transaction)48 IgniteException (org.apache.ignite.IgniteException)47 ExecutionException (java.util.concurrent.ExecutionException)40 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)40 IgniteCache (org.apache.ignite.IgniteCache)37