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