Search in sources :

Example 61 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project cdap by caskdata.

the class InMemoryElectionTest method testElection.

@Test(timeout = 5000)
public void testElection() throws ExecutionException, InterruptedException, BrokenBarrierException {
    final InMemoryElectionRegistry electionRegistry = new InMemoryElectionRegistry();
    ExecutorService executor = Executors.newCachedThreadPool();
    // Create 5 participants to join leader election process simultaneously
    int participantCount = 5;
    final CyclicBarrier barrier = new CyclicBarrier(participantCount + 1);
    final Semaphore leaderSem = new Semaphore(0);
    final Semaphore followerSem = new Semaphore(0);
    final CountDownLatch[] stopLatch = new CountDownLatch[participantCount];
    try {
        final AtomicInteger currentLeader = new AtomicInteger(-1);
        for (int i = 0; i < participantCount; i++) {
            stopLatch[i] = new CountDownLatch(1);
            final int idx = i;
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        barrier.await();
                        Cancellable cancel = electionRegistry.join("test", new ElectionHandler() {

                            @Override
                            public void leader() {
                                currentLeader.set(idx);
                                leaderSem.release();
                            }

                            @Override
                            public void follower() {
                                followerSem.release();
                            }
                        });
                        stopLatch[idx].await(10, TimeUnit.SECONDS);
                        cancel.cancel();
                    } catch (Exception e) {
                        LOG.error(e.getMessage(), e);
                    }
                }
            });
        }
        // Sync the joining
        barrier.await();
        // There should be 1 leader and 4 followers
        leaderSem.tryAcquire(10, TimeUnit.SECONDS);
        followerSem.tryAcquire(participantCount - 1, 10, TimeUnit.SECONDS);
        // Continuously stopping leader until there is one left.
        for (int i = 0; i < participantCount - 1; i++) {
            stopLatch[currentLeader.get()].countDown();
            // Each time when the leader is unregistered from the leader election, a new leader would rise and
            // the old leader would become a follower.
            leaderSem.tryAcquire(10, TimeUnit.SECONDS);
            followerSem.tryAcquire(10, TimeUnit.SECONDS);
        }
        // Withdraw the last leader, it'd become follower as well.
        stopLatch[currentLeader.get()].countDown();
        followerSem.tryAcquire(10, TimeUnit.SECONDS);
    } finally {
        executor.shutdown();
        executor.awaitTermination(5L, TimeUnit.SECONDS);
    }
}
Also used : Cancellable(org.apache.twill.common.Cancellable) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ElectionHandler(org.apache.twill.api.ElectionHandler) Test(org.junit.Test)

Example 62 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project cdap by caskdata.

the class ConcurrentMessageWriterTest method testConcurrentWrites.

@Test
public void testConcurrentWrites() throws InterruptedException, BrokenBarrierException {
    int payloadsPerRequest = 200;
    int threadCount = 20;
    final int requestPerThread = 20;
    long writeLatencyMillis = 50L;
    final TopicId topicId = NamespaceId.DEFAULT.topic("t");
    final TopicMetadata metadata = new TopicMetadata(topicId, new HashMap<String, String>(), 1);
    TestStoreRequestWriter testWriter = new TestStoreRequestWriter(new TimeProvider.IncrementalTimeProvider(), writeLatencyMillis);
    final ConcurrentMessageWriter writer = new ConcurrentMessageWriter(testWriter);
    final List<String> payload = new ArrayList<>(payloadsPerRequest);
    for (int i = 0; i < payloadsPerRequest; i++) {
        payload.add(Integer.toString(i));
    }
    ExecutorService executor = Executors.newFixedThreadPool(threadCount);
    final CyclicBarrier barrier = new CyclicBarrier(threadCount + 1);
    for (int i = 0; i < threadCount; i++) {
        final int threadId = i;
        executor.submit(new Runnable() {

            @Override
            public void run() {
                Stopwatch stopwatch = new Stopwatch();
                try {
                    barrier.await();
                    stopwatch.start();
                    for (int i = 0; i < requestPerThread; i++) {
                        writer.persist(new TestStoreRequest(topicId, payload), metadata);
                    }
                    LOG.info("Complete time for thread {} is {} ms", threadId, stopwatch.elapsedMillis());
                } catch (Exception e) {
                    LOG.error("Exception raised when persisting.", e);
                }
            }
        });
    }
    Stopwatch stopwatch = new Stopwatch();
    barrier.await();
    stopwatch.start();
    executor.shutdown();
    Assert.assertTrue(executor.awaitTermination(1, TimeUnit.MINUTES));
    LOG.info("Total time passed: {} ms", stopwatch.elapsedMillis());
    // Validate that the total number of messages written is correct
    List<RawMessage> messages = testWriter.getMessages().get(topicId);
    Assert.assertEquals(payloadsPerRequest * threadCount * requestPerThread, messages.size());
    // The message id must be sorted
    RawMessage lastMessage = null;
    for (RawMessage message : messages) {
        if (lastMessage != null) {
            Assert.assertTrue(Bytes.compareTo(lastMessage.getId(), message.getId()) < 0);
        }
        lastMessage = message;
    }
}
Also used : TimeProvider(co.cask.cdap.common.utils.TimeProvider) ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) CyclicBarrier(java.util.concurrent.CyclicBarrier) ExecutorService(java.util.concurrent.ExecutorService) TopicId(co.cask.cdap.proto.id.TopicId) RawMessage(co.cask.cdap.messaging.data.RawMessage) Test(org.junit.Test)

Aggregations

BrokenBarrierException (java.util.concurrent.BrokenBarrierException)62 CyclicBarrier (java.util.concurrent.CyclicBarrier)53 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 IOException (java.io.IOException)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 ExecutionException (java.util.concurrent.ExecutionException)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 List (java.util.List)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 TimeoutException (java.util.concurrent.TimeoutException)8 Random (java.util.Random)7 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)7 Set (java.util.Set)6 TimeUnit (java.util.concurrent.TimeUnit)6