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());
}
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());
}
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));
}
Aggregations