Search in sources :

Example 96 with CyclicBarrier

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

the class DistributedIdGeneratorTest method concurrent.

/**
     * Use multiple threads to generate many Id. Ensure no holes and no dups in the sequence
     */
@Test
public void concurrent() throws Exception {
    int Threads = 10;
    int Iterations = 100;
    CyclicBarrier barrier = new CyclicBarrier(Threads);
    CountDownLatch counter = new CountDownLatch(Threads);
    ExecutorService executor = Executors.newCachedThreadPool();
    List<String> results = Collections.synchronizedList(Lists.newArrayList());
    for (int i = 0; i < Threads; i++) {
        executor.execute(() -> {
            try {
                DistributedIdGenerator gen = new DistributedIdGenerator(zkc, "/my/test/concurrent", "prefix");
                barrier.await();
                for (int j = 0; j < Iterations; j++) {
                    results.add(gen.getNextId());
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                counter.countDown();
            }
        });
    }
    counter.await();
    assertEquals(results.size(), Threads * Iterations);
    // Check the list contains no duplicates
    Set<String> set = Sets.newHashSet(results);
    assertEquals(set.size(), results.size());
    executor.shutdown();
}
Also used : DistributedIdGenerator(com.yahoo.pulsar.broker.service.DistributedIdGenerator) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 97 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project hazelcast by hazelcast.

the class OperationExecutorImpl_BasicTest method test_interruptAllPartitionThreads.

@Test
public void test_interruptAllPartitionThreads() throws Exception {
    initExecutor();
    int threadCount = executor.getPartitionThreadCount();
    final CyclicBarrier barrier = new CyclicBarrier(threadCount + 1);
    executor.executeOnPartitionThreads(new Runnable() {

        @Override
        public void run() {
            // current thread must be a PartitionOperationThread
            if (Thread.currentThread() instanceof PartitionOperationThread) {
                try {
                    Thread.sleep(Long.MAX_VALUE);
                } catch (InterruptedException ignored) {
                } finally {
                    try {
                        awaitBarrier(barrier);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    });
    executor.interruptPartitionThreads();
    awaitBarrier(barrier);
}
Also used : CyclicBarrier(java.util.concurrent.CyclicBarrier) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 98 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project hadoop by apache.

the class TestRPC method testRPCInterrupted.

@Test(timeout = 30000)
public void testRPCInterrupted() throws Exception {
    Server server;
    RPC.Builder builder = newServerBuilder(conf).setNumHandlers(5).setVerbose(true).setSecretManager(null);
    server = setupTestServer(builder);
    int numConcurrentRPC = 200;
    final CyclicBarrier barrier = new CyclicBarrier(numConcurrentRPC);
    final CountDownLatch latch = new CountDownLatch(numConcurrentRPC);
    final AtomicBoolean leaderRunning = new AtomicBoolean(true);
    final AtomicReference<Throwable> error = new AtomicReference<>();
    Thread leaderThread = null;
    try {
        for (int i = 0; i < numConcurrentRPC; i++) {
            final int num = i;
            final TestRpcService proxy = getClient(addr, conf);
            Thread rpcThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        barrier.await();
                        while (num == 0 || leaderRunning.get()) {
                            proxy.slowPing(null, newSlowPingRequest(false));
                        }
                        proxy.slowPing(null, newSlowPingRequest(false));
                    } catch (Exception e) {
                        if (num == 0) {
                            leaderRunning.set(false);
                        } else {
                            error.set(e);
                        }
                        LOG.error("thread " + num, e);
                    } finally {
                        latch.countDown();
                    }
                }
            });
            rpcThread.start();
            if (leaderThread == null) {
                leaderThread = rpcThread;
            }
        }
        // let threads get past the barrier
        Thread.sleep(1000);
        // stop a single thread
        while (leaderRunning.get()) {
            leaderThread.interrupt();
        }
        latch.await();
        // should not cause any other thread to get an error
        assertTrue("rpc got exception " + error.get(), error.get() == null);
    } finally {
        server.stop();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceException(com.google.protobuf.ServiceException) AuthorizationException(org.apache.hadoop.security.authorize.AuthorizationException) InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) HadoopIllegalArgumentException(org.apache.hadoop.HadoopIllegalArgumentException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AccessControlException(org.apache.hadoop.security.AccessControlException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 99 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project hadoop by apache.

the class TestRPC method testExternalCall.

@Test(timeout = 30000)
public void testExternalCall() throws Exception {
    final UserGroupInformation ugi = UserGroupInformation.createUserForTesting("user123", new String[0]);
    final IOException expectedIOE = new IOException("boom");
    // use 1 handler so the callq can be plugged
    final Server server = setupTestServer(conf, 1);
    try {
        final AtomicBoolean result = new AtomicBoolean();
        ExternalCall<String> remoteUserCall = newExtCall(ugi, new PrivilegedExceptionAction<String>() {

            @Override
            public String run() throws Exception {
                return UserGroupInformation.getCurrentUser().getUserName();
            }
        });
        ExternalCall<String> exceptionCall = newExtCall(ugi, new PrivilegedExceptionAction<String>() {

            @Override
            public String run() throws Exception {
                throw expectedIOE;
            }
        });
        final CountDownLatch latch = new CountDownLatch(1);
        final CyclicBarrier barrier = new CyclicBarrier(2);
        ExternalCall<Void> barrierCall = newExtCall(ugi, new PrivilegedExceptionAction<Void>() {

            @Override
            public Void run() throws Exception {
                // notify we are in a handler and then wait to keep the callq
                // plugged up
                latch.countDown();
                barrier.await();
                return null;
            }
        });
        server.queueCall(barrierCall);
        server.queueCall(exceptionCall);
        server.queueCall(remoteUserCall);
        // wait for barrier call to enter the handler, check that the other 2
        // calls are actually queued
        latch.await();
        assertEquals(2, server.getCallQueueLen());
        // unplug the callq
        barrier.await();
        barrierCall.get();
        // verify correct ugi is used
        String answer = remoteUserCall.get();
        assertEquals(ugi.getUserName(), answer);
        try {
            exceptionCall.get();
            fail("didn't throw");
        } catch (ExecutionException ee) {
            assertTrue((ee.getCause()) instanceof IOException);
            assertEquals(expectedIOE.getMessage(), ee.getCause().getMessage());
        }
    } finally {
        server.stop();
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceException(com.google.protobuf.ServiceException) AuthorizationException(org.apache.hadoop.security.authorize.AuthorizationException) InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) HadoopIllegalArgumentException(org.apache.hadoop.HadoopIllegalArgumentException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AccessControlException(org.apache.hadoop.security.AccessControlException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutionException(java.util.concurrent.ExecutionException) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) Test(org.junit.Test)

Example 100 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project questdb by bluestreak01.

the class HttpServerTest method assertConcurrentDownload.

private void assertConcurrentDownload(MimeTypes mimeTypes, HttpServer server, final String proto) throws InterruptedException, IOException {
    try {
        // ssl
        final File actual1 = new File(temp.getRoot(), "get.html");
        final File actual2 = new File(temp.getRoot(), "post.html");
        final File actual3 = new File(temp.getRoot(), "upload.html");
        final CyclicBarrier barrier = new CyclicBarrier(3);
        final CountDownLatch haltLatch = new CountDownLatch(3);
        final AtomicInteger counter = new AtomicInteger(0);
        new Thread(() -> {
            try {
                barrier.await();
                HttpTestUtils.download(clientBuilder("https".equals(proto)), proto + "://localhost:9000/get.html", actual1);
            } catch (Exception e) {
                counter.incrementAndGet();
                e.printStackTrace();
            } finally {
                haltLatch.countDown();
            }
        }).start();
        new Thread(() -> {
            try {
                barrier.await();
                HttpTestUtils.download(clientBuilder("https".equals(proto)), proto + "://localhost:9000/post.html", actual2);
            } catch (Exception e) {
                counter.incrementAndGet();
                e.printStackTrace();
            } finally {
                haltLatch.countDown();
            }
        }).start();
        new Thread(() -> {
            try {
                barrier.await();
                HttpTestUtils.download(clientBuilder("https".equals(proto)), proto + "://localhost:9000/upload.html", actual3);
            } catch (Exception e) {
                counter.incrementAndGet();
                e.printStackTrace();
            } finally {
                haltLatch.countDown();
            }
        }).start();
        haltLatch.await();
        Assert.assertEquals(0, counter.get());
        TestUtils.assertEquals(new File(HttpServerTest.class.getResource("/site/public/get.html").getPath()), actual1);
        TestUtils.assertEquals(new File(HttpServerTest.class.getResource("/site/public/post.html").getPath()), actual2);
        TestUtils.assertEquals(new File(HttpServerTest.class.getResource("/site/public/upload.html").getPath()), actual3);
    } finally {
        server.halt();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) ResponseContentBufferTooSmallException(com.questdb.ex.ResponseContentBufferTooSmallException) SocketException(java.net.SocketException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

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