Search in sources :

Example 66 with CyclicBarrier

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

the class ManagedLedgerBkTest method verifyConcurrentUsage.

@Test
public void verifyConcurrentUsage() throws Exception {
    ManagedLedgerFactoryConfig config = new ManagedLedgerFactoryConfig();
    config.setMaxCacheSize(100 * 1024 * 1024);
    ManagedLedgerFactoryImpl factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle(), config);
    EntryCacheManager cacheManager = factory.getEntryCacheManager();
    ManagedLedgerConfig conf = new ManagedLedgerConfig();
    conf.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
    final ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my-ledger", conf);
    int NumProducers = 1;
    int NumConsumers = 1;
    final AtomicBoolean done = new AtomicBoolean();
    final CyclicBarrier barrier = new CyclicBarrier(NumProducers + NumConsumers + 1);
    List<Future<?>> futures = Lists.newArrayList();
    for (int i = 0; i < NumProducers; i++) {
        futures.add(executor.submit(() -> {
            try {
                barrier.await();
                while (!done.get()) {
                    ledger.addEntry("entry".getBytes());
                    Thread.sleep(1);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }));
    }
    for (int i = 0; i < NumConsumers; i++) {
        final int idx = i;
        futures.add(executor.submit(() -> {
            try {
                barrier.await();
                ManagedCursor cursor = ledger.openCursor("my-cursor-" + idx);
                while (!done.get()) {
                    List<Entry> entries = cursor.readEntries(1);
                    if (!entries.isEmpty()) {
                        cursor.markDelete(entries.get(0).getPosition());
                    }
                    entries.forEach(e -> e.release());
                    Thread.sleep(2);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }));
    }
    barrier.await();
    Thread.sleep(1 * 1000);
    done.set(true);
    for (Future<?> future : futures) {
        future.get();
    }
    cacheManager.mlFactoryMBean.refreshStats(1, TimeUnit.SECONDS);
    assertTrue(cacheManager.mlFactoryMBean.getCacheHitsRate() > 0.0);
    assertEquals(cacheManager.mlFactoryMBean.getCacheMissesRate(), 0.0);
    assertTrue(cacheManager.mlFactoryMBean.getCacheHitsThroughput() > 0.0);
    assertEquals(cacheManager.mlFactoryMBean.getNumberOfCacheEvictions(), 0);
    factory.shutdown();
}
Also used : BookKeeperClusterTestCase(org.apache.bookkeeper.test.BookKeeperClusterTestCase) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) Entry(org.apache.bookkeeper.mledger.Entry) Assert.assertEquals(org.testng.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) DeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback) Future(java.util.concurrent.Future) PersistentOfflineTopicStats(com.yahoo.pulsar.common.policies.data.PersistentOfflineTopicStats) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Lists(com.google.common.collect.Lists) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Assert.assertFalse(org.testng.Assert.assertFalse) Charsets(com.google.common.base.Charsets) CyclicBarrier(java.util.concurrent.CyclicBarrier) DigestType(org.apache.bookkeeper.client.BookKeeper.DigestType) Assert.fail(org.testng.Assert.fail) BookKeeperTestClient(org.apache.bookkeeper.client.BookKeeperTestClient) Assert.assertNotNull(org.testng.Assert.assertNotNull) Position(org.apache.bookkeeper.mledger.Position) TimeUnit(java.util.concurrent.TimeUnit) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertTrue(org.testng.Assert.assertTrue) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Future(java.util.concurrent.Future) List(java.util.List) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 67 with CyclicBarrier

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

the class ManagedLedgerTest method testConcurrentOpenCursor.

@Test
public void testConcurrentOpenCursor() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("testConcurrentOpenCursor");
    final AtomicReference<ManagedCursor> cursor1 = new AtomicReference<>(null);
    final AtomicReference<ManagedCursor> cursor2 = new AtomicReference<>(null);
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch latch = new CountDownLatch(2);
    cachedExecutor.execute(() -> {
        try {
            barrier.await();
        } catch (Exception e) {
        }
        ledger.asyncOpenCursor("c1", new OpenCursorCallback() {

            @Override
            public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
                latch.countDown();
            }

            @Override
            public void openCursorComplete(ManagedCursor cursor, Object ctx) {
                cursor1.set(cursor);
                latch.countDown();
            }
        }, null);
    });
    cachedExecutor.execute(() -> {
        try {
            barrier.await();
        } catch (Exception e) {
        }
        ledger.asyncOpenCursor("c1", new OpenCursorCallback() {

            @Override
            public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
                latch.countDown();
            }

            @Override
            public void openCursorComplete(ManagedCursor cursor, Object ctx) {
                cursor2.set(cursor);
                latch.countDown();
            }
        }, null);
    });
    latch.await();
    assertNotNull(cursor1.get());
    assertNotNull(cursor2.get());
    assertEquals(cursor1.get(), cursor2.get());
    ledger.close();
}
Also used : OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) IOException(java.io.IOException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 68 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project jdk8u_jdk by JetBrains.

the class SiblingIOEHandle method performA.

private static void performA(boolean fileOut) {
    try {
        stopC.delete();
        ProcessBuilder builderB = new ProcessBuilder(getCommandArray(APP.B.name()));
        File outB = null;
        if (fileOut) {
            outB = new File("outB.txt");
            builderB.redirectOutput(outB);
        }
        builderB.redirectErrorStream(true);
        final CyclicBarrier barrier = new CyclicBarrier(2);
        //Create process C in a new thread
        ProcessC processC = new ProcessC(barrier);
        Thread procCRunner = new Thread(processC);
        procCRunner.start();
        if (!waitBarrier(barrier)) {
            throw new RuntimeException("Catastrophe in process A! Synchronization failed.");
        }
        // Run process B first.
        Process processB = builderB.start();
        while (true) try {
            procCRunner.join();
            break;
        } catch (InterruptedException ex) {
            continue;
        }
        if (!procClaunched) {
            throw new RuntimeException("Catastrophe in process A! C was not launched.");
        }
        processB.getOutputStream().close();
        processB.getErrorStream().close();
        if (fileOut) {
            try {
                processB.waitFor();
            } catch (InterruptedException ex) {
                throw new RuntimeException("Catastrophe in process B! B hung up.");
            }
            System.err.println("Trying to delete [outB.txt].");
            if (!outB.delete()) {
                throw new RuntimeException("Greedy brother C deadlock! File share.");
            }
            System.err.println("Succeeded in delete [outB.txt].");
        } else {
            System.err.println("Read stream start.");
            boolean isSignalReceived = false;
            try (BufferedReader in = new BufferedReader(new InputStreamReader(processB.getInputStream(), "utf-8"))) {
                String result;
                while ((result = in.readLine()) != null) {
                    if (SIGNAL.equals(result)) {
                        isSignalReceived = true;
                    } else {
                        throw new RuntimeException("Catastrophe in process B! Bad output.");
                    }
                }
            }
            if (!isSignalReceived) {
                throw new RuntimeException("Signal from B was not received");
            }
            System.err.println("Read stream finished.");
        }
        // If JDK-6921885 is not fixed that point is unreachable.
        // Test timeout exception.
        // write signal file to stop C process.
        stopC.createNewFile();
        processC.waitFor();
    } catch (IOException ex) {
        throw new RuntimeException("Catastrophe in process A!", ex);
    } catch (InterruptedException ex) {
        throw new RuntimeException("Process A was interrupted while waiting for C", ex);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 69 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project jdk8u_jdk by JetBrains.

the class ChatTest method performTestUseName.

private static void performTestUseName() throws Exception {
    final CyclicBarrier barrier1 = new CyclicBarrier(2);
    final CyclicBarrier barrier2 = new CyclicBarrier(2);
    final CyclicBarrier barrier3 = new CyclicBarrier(2);
    final List<Exception> exceptions = new ArrayList<Exception>();
    ChatConnection chatConnection = new ChatConnection() {

        @Override
        public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception {
            String string = readAvailableString(reader);
            if (!"Name: ".equals(string)) {
                exceptions.add(new RuntimeException("Expected Name: "));
            }
            writer.write("testClient1\n");
            waitForJoin(reader, "testClient1");
            barrier1.await();
            barrier2.await();
            string = readAvailableString(reader);
            if (!"testClient2: Hello world!\n".equals(string)) {
                exceptions.add(new RuntimeException("testClient2: Hello world!\n"));
            }
            barrier3.await();
        }
    };
    Thread client2 = new Thread(new ChatConnection() {

        @Override
        public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception {
            String string = readAvailableString(reader);
            if (!"Name: ".equals(string)) {
                exceptions.add(new RuntimeException("Expected Name: "));
            }
            writer.write("testClient2\n");
            waitForJoin(reader, "testClient2");
            barrier1.await();
            writer.write("Hello world!\n");
            barrier2.await();
            barrier3.await();
        }
    });
    client2.start();
    chatConnection.run();
    if (!exceptions.isEmpty()) {
        throw exceptions.get(0);
    }
}
Also used : ArrayList(java.util.ArrayList) Socket(java.net.Socket) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 70 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project android by JetBrains.

the class RenderSecurityManagerTest method testThread2.

@Test
public void testThread2() throws Exception {
    // Check that when a new thread is created simultaneously from an unrelated
    // thread during rendering, that new thread does not pick up the security manager.
    //
    final CyclicBarrier barrier1 = new CyclicBarrier(2);
    final CyclicBarrier barrier2 = new CyclicBarrier(2);
    final CyclicBarrier barrier3 = new CyclicBarrier(4);
    final CyclicBarrier barrier4 = new CyclicBarrier(4);
    final CyclicBarrier barrier5 = new CyclicBarrier(4);
    final CyclicBarrier barrier6 = new CyclicBarrier(4);
    // First the threads reach barrier1. Then from barrier1 to barrier2, thread1
    // installs the security manager. Then from barrier2 to barrier3, thread2
    // checks that it does not have any security restrictions, and creates thread3.
    // Thread1 will ensure that the security manager is working there, and it will
    // create thread4. Then after barrier3 (where thread3 and thread4 are now also
    // participating) thread3 will ensure that it too has no security restrictions,
    // and thread4 will ensure that it does. At barrier4 the security manager gets
    // uninstalled, and at barrier5 all threads will check that there are no more
    // restrictions. At barrier6 all threads are done.
    final Thread thread1 = new Thread("render") {

        @Override
        public void run() {
            try {
                barrier1.await();
                assertNull(RenderSecurityManager.getCurrent());
                RenderSecurityManager manager = new RenderSecurityManager(null, null);
                manager.setActive(true, myCredential);
                barrier2.await();
                Thread thread4 = new Thread() {

                    @Override
                    public void run() {
                        try {
                            barrier3.await();
                            try {
                                System.getProperties();
                                fail("Should have thrown security exception");
                            } catch (SecurityException e) {
                            // pass
                            }
                            barrier4.await();
                            barrier5.await();
                            assertNull(RenderSecurityManager.getCurrent());
                            assertNull(System.getSecurityManager());
                            barrier6.await();
                        } catch (InterruptedException e) {
                            fail(e.toString());
                        } catch (BrokenBarrierException e) {
                            fail(e.toString());
                        }
                    }
                };
                thread4.start();
                try {
                    System.getProperties();
                    fail("Should have thrown security exception");
                } catch (SecurityException e) {
                // expected
                }
                barrier3.await();
                barrier4.await();
                manager.dispose(myCredential);
                assertNull(RenderSecurityManager.getCurrent());
                assertNull(System.getSecurityManager());
                barrier5.await();
                barrier6.await();
            } catch (InterruptedException e) {
                fail(e.toString());
            } catch (BrokenBarrierException e) {
                fail(e.toString());
            }
        }
    };
    final Thread thread2 = new Thread("unrelated") {

        @Override
        public void run() {
            try {
                barrier1.await();
                assertNull(RenderSecurityManager.getCurrent());
                barrier2.await();
                assertNull(RenderSecurityManager.getCurrent());
                assertNotNull(System.getSecurityManager());
                try {
                    System.getProperties();
                } catch (SecurityException e) {
                    fail("Should not have been affected by security manager");
                }
                Thread thread3 = new Thread() {

                    @Override
                    public void run() {
                        try {
                            barrier3.await();
                            try {
                                System.getProperties();
                            } catch (SecurityException e) {
                                fail("Should not have been affected by security manager");
                            }
                            barrier4.await();
                            barrier5.await();
                            assertNull(RenderSecurityManager.getCurrent());
                            assertNull(System.getSecurityManager());
                            barrier6.await();
                        } catch (InterruptedException e) {
                            fail(e.toString());
                        } catch (BrokenBarrierException e) {
                            fail(e.toString());
                        }
                    }
                };
                thread3.start();
                barrier3.await();
                barrier4.await();
                barrier5.await();
                assertNull(RenderSecurityManager.getCurrent());
                assertNull(System.getSecurityManager());
                barrier6.await();
            } catch (InterruptedException e) {
                fail(e.toString());
            } catch (BrokenBarrierException e) {
                fail(e.toString());
            }
        }
    };
    thread1.start();
    thread2.start();
    thread1.join();
    thread2.join();
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.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