Search in sources :

Example 1 with CountDownLatch

use of com.twitter.util.CountDownLatch in project distributedlog by twitter.

the class TestDefaultSpeculativeRequestExecutionPolicy method testSpeculativeRequests.

@Test(timeout = 20000)
public void testSpeculativeRequests() throws Exception {
    DefaultSpeculativeRequestExecutionPolicy policy = new DefaultSpeculativeRequestExecutionPolicy(10, 10000, 2);
    SpeculativeRequestExecutor executor = mock(SpeculativeRequestExecutor.class);
    final AtomicInteger callCount = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(3);
    Mockito.doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            try {
                return Future.value(callCount.incrementAndGet() < 3);
            } finally {
                latch.countDown();
            }
        }
    }).when(executor).issueSpeculativeRequest();
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    policy.initiateSpeculativeRequest(executorService, executor);
    latch.await();
    assertEquals(40, policy.getNextSpeculativeRequestTimeout());
}
Also used : Answer(org.mockito.stubbing.Answer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CountDownLatch(com.twitter.util.CountDownLatch) Test(org.junit.Test)

Example 2 with CountDownLatch

use of com.twitter.util.CountDownLatch in project distributedlog by twitter.

the class TestDefaultSpeculativeRequestExecutionPolicy method testSpeculativeRequestsWithMaxTimeout.

@Test(timeout = 20000)
public void testSpeculativeRequestsWithMaxTimeout() throws Exception {
    DefaultSpeculativeRequestExecutionPolicy policy = new DefaultSpeculativeRequestExecutionPolicy(10, 15, 2);
    SpeculativeRequestExecutor executor = mock(SpeculativeRequestExecutor.class);
    final AtomicInteger callCount = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(3);
    Mockito.doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            try {
                return Future.value(callCount.incrementAndGet() < 3);
            } finally {
                latch.countDown();
            }
        }
    }).when(executor).issueSpeculativeRequest();
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    policy.initiateSpeculativeRequest(executorService, executor);
    latch.await();
    assertEquals(15, policy.getNextSpeculativeRequestTimeout());
}
Also used : Answer(org.mockito.stubbing.Answer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CountDownLatch(com.twitter.util.CountDownLatch) Test(org.junit.Test)

Example 3 with CountDownLatch

use of com.twitter.util.CountDownLatch in project distributedlog by twitter.

the class StreamRewinder method readLoop.

private static void readLoop(final DistributedLogManager dlm, final int rewindSeconds) throws Exception {
    final CountDownLatch keepAliveLatch = new CountDownLatch(1);
    long rewindToTxId = System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(rewindSeconds, TimeUnit.SECONDS);
    System.out.println("Record records starting from " + rewindToTxId + " which is " + rewindSeconds + " seconds ago");
    final AsyncLogReader reader = FutureUtils.result(dlm.openAsyncLogReader(rewindToTxId));
    final AtomicBoolean caughtup = new AtomicBoolean(false);
    final FutureEventListener<LogRecordWithDLSN> readListener = new FutureEventListener<LogRecordWithDLSN>() {

        @Override
        public void onFailure(Throwable cause) {
            System.err.println("Encountered error on reading records from stream " + dlm.getStreamName());
            cause.printStackTrace(System.err);
            keepAliveLatch.countDown();
        }

        @Override
        public void onSuccess(LogRecordWithDLSN record) {
            System.out.println("Received record " + record.getDlsn());
            System.out.println("\"\"\"");
            System.out.println(new String(record.getPayload(), UTF_8));
            System.out.println("\"\"\"");
            long diffInMilliseconds = System.currentTimeMillis() - record.getTransactionId();
            if (!caughtup.get() && diffInMilliseconds < 2000) {
                System.out.println("Reader caught with latest data");
                caughtup.set(true);
            }
            reader.readNext().addEventListener(this);
        }
    };
    reader.readNext().addEventListener(readListener);
    keepAliveLatch.await();
    FutureUtils.result(reader.asyncClose(), Duration.apply(5, TimeUnit.SECONDS));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FutureEventListener(com.twitter.util.FutureEventListener) CountDownLatch(com.twitter.util.CountDownLatch)

Aggregations

CountDownLatch (com.twitter.util.CountDownLatch)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 FutureEventListener (com.twitter.util.FutureEventListener)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1