Search in sources :

Example 51 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 52 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project openhab1-addons by openhab.

the class LightwaveRfBindingFunctionalTest method receiveACommand.

private void receiveACommand(List<ItemConfigAndExpectedState> itemConfigAndExpectedStates, String messageToReceive) throws Exception {
    // Set up sockets for testing
    CyclicBarrier sendMessageBarrier = new CyclicBarrier(2);
    CountDownLatch messageProcessedCountdownLatch = new CountDownLatch(itemConfigAndExpectedStates.size());
    doAnswer(sendMessageOnceUnlatched(messageToReceive, sendMessageBarrier)).when(mockReceiveSocket).receive(any(DatagramPacket.class));
    doAnswer(waitIndefinitely()).when(mockReceiveSocket2).receive(any(DatagramPacket.class));
    doAnswer(waitIndefinitely()).when(mockTransmitSocket).send(any(DatagramPacket.class));
    for (ItemConfigAndExpectedState t : itemConfigAndExpectedStates) {
        doAnswer(unlatchWhenEventReceivedOrTimeout(messageProcessedCountdownLatch)).when(mockEventPublisher).postUpdate(t.getItem().getName(), t.getExpectedState());
    }
    // Setup Item config
    for (ItemConfigAndExpectedState t : itemConfigAndExpectedStates) {
        bindingProvider.processBindingConfiguration(CONTEXT, t.getItem(), t.getItemConfig());
    }
    binding.addBindingProvider(bindingProvider);
    // Activate the binding ready for the test
    binding.activateForTesting();
    // Receive the command
    sendMessageBarrier.await();
    // Wait till the receive has been processes we use a timeout in case we don't receive anything
    messageProcessedCountdownLatch.await(1000, TimeUnit.MILLISECONDS);
}
Also used : DatagramPacket(java.net.DatagramPacket) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 53 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.

the class ManagedCursorConcurrencyTest method testAckAndClose.

@Test(timeOut = 30000)
public void testAckAndClose() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger_test_ack_and_close", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    final ManagedCursor cursor = ledger.openCursor("c1");
    final List<Position> addedEntries = Lists.newArrayList();
    for (int i = 0; i < 1000; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch counter = new CountDownLatch(2);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    // Deleter thread
    cachedExecutor.execute(() -> {
        try {
            barrier.await();
            for (Position position : addedEntries) {
                cursor.asyncDelete(position, deleteCallback, position);
            }
        } catch (Exception e) {
            e.printStackTrace();
            gotException.set(true);
        } finally {
            counter.countDown();
        }
    });
    // Reader thread
    cachedExecutor.execute(() -> {
        try {
            barrier.await();
            for (int i = 0; i < 1000; i++) {
                cursor.readEntries(1).forEach(e -> e.release());
            }
        } catch (Exception e) {
            e.printStackTrace();
            gotException.set(true);
        } finally {
            counter.countDown();
        }
    });
    counter.await();
    assertEquals(gotException.get(), false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 54 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.

the class ManagedCursorConcurrencyTest method testMarkDeleteAndRead.

@Test
public void testMarkDeleteAndRead() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    final ManagedCursor cursor = ledger.openCursor("c1");
    final List<Position> addedEntries = Lists.newArrayList();
    for (int i = 0; i < 1000; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch counter = new CountDownLatch(2);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    Thread deleter = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (Position position : addedEntries) {
                    cursor.markDelete(position);
                }
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    Thread reader = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (int i = 0; i < 1000; i++) {
                    cursor.readEntries(1).forEach(e -> e.release());
                }
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    reader.start();
    counter.await();
    assertEquals(gotException.get(), false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 55 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.

the class ManagedCursorConcurrencyTest method testConcurrentReadOfSameEntry.

@Test(timeOut = 30000)
public void testConcurrentReadOfSameEntry() throws Exception {
    ManagedLedger ledger = factory.open("testConcurrentReadOfSameEntry", new ManagedLedgerConfig());
    final int numCursors = 5;
    final List<ManagedCursor> cursors = Lists.newArrayList();
    for (int i = 0; i < numCursors; i++) {
        final ManagedCursor cursor = ledger.openCursor("c" + i);
        cursors.add(cursor);
    }
    final int N = 100;
    for (int i = 0; i < N; i++) {
        ledger.addEntry(("entry" + i).getBytes());
    }
    long currentLedger = ((PositionImpl) cursors.get(0).getMarkDeletedPosition()).getLedgerId();
    // empty the cache
    ((ManagedLedgerImpl) ledger).entryCache.invalidateAllEntries(currentLedger);
    final CyclicBarrier barrier = new CyclicBarrier(numCursors);
    final CountDownLatch counter = new CountDownLatch(numCursors);
    AtomicReference<String> result = new AtomicReference<String>();
    for (int i = 0; i < numCursors; i++) {
        final int cursorIndex = i;
        final ManagedCursor cursor = cursors.get(cursorIndex);
        cachedExecutor.execute(() -> {
            try {
                barrier.await();
                for (int j = 0; j < N; j++) {
                    String expected = "entry" + j;
                    String data = new String(cursor.readEntries(1).get(0).getDataAndRelease());
                    if ((!expected.equals(data)) && result.get() == null) {
                        result.set("Mismatched entry in cursor " + (cursorIndex + 1) + " at position " + (j + 1) + "--- Expected: " + expected + ", Actual: " + data);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                counter.countDown();
            }
        });
    }
    counter.await();
    assertNull(result.get());
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) 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