use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class RaftTestUtil method waitForLeader.
static RaftServer.Division waitForLeader(MiniRaftCluster cluster, RaftGroupId groupId, boolean expectLeader) throws InterruptedException {
final String name = "waitForLeader-" + groupId + "-(expectLeader? " + expectLeader + ")";
final int numAttempts = expectLeader ? 100 : 10;
final TimeDuration sleepTime = cluster.getTimeoutMax().apply(d -> (d * 3) >> 1);
LOG.info(cluster.printServers(groupId));
final AtomicReference<IllegalStateException> exception = new AtomicReference<>();
final Runnable handleNoLeaders = () -> {
throw cluster.newIllegalStateExceptionForNoLeaders(groupId);
};
final Consumer<List<RaftServer.Division>> handleMultipleLeaders = leaders -> {
final IllegalStateException ise = cluster.newIllegalStateExceptionForMultipleLeaders(groupId, leaders);
exception.set(ise);
};
final RaftServer.Division leader = JavaUtils.attemptRepeatedly(() -> {
final RaftServer.Division l = cluster.getLeader(groupId, handleNoLeaders, handleMultipleLeaders);
if (l != null && !l.getInfo().isLeaderReady()) {
throw new IllegalStateException("Leader: " + l.getMemberId() + " not ready");
}
return l;
}, numAttempts, sleepTime, name, LOG);
LOG.info(cluster.printServers(groupId));
if (expectLeader) {
return Optional.ofNullable(leader).orElseThrow(exception::get);
} else {
if (leader == null) {
return null;
} else {
throw new IllegalStateException("expectLeader = " + expectLeader + " but leader = " + leader);
}
}
}
Aggregations