Search in sources :

Example 11 with MiniRaftCluster

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);
        }
    }
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) BooleanSupplier(java.util.function.BooleanSupplier) ProtoUtils(org.apache.ratis.util.ProtoUtils) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) JavaUtils(org.apache.ratis.util.JavaUtils) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) EnumMap(java.util.EnumMap) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Predicate(java.util.function.Predicate) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) Objects(java.util.Objects) List(java.util.List) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase) ClientId(org.apache.ratis.protocol.ClientId) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Preconditions(org.apache.ratis.thirdparty.com.google.common.base.Preconditions) Optional(java.util.Optional) DelayLocalExecutionInjection(org.apache.ratis.server.impl.DelayLocalExecutionInjection) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) LogProtoUtils(org.apache.ratis.server.raftlog.LogProtoUtils) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftLog(org.apache.ratis.server.raftlog.RaftLog) CollectionUtils(org.apache.ratis.util.CollectionUtils) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) Message(org.apache.ratis.protocol.Message) StreamSupport(java.util.stream.StreamSupport) IntSupplier(java.util.function.IntSupplier) AssumptionViolatedException(org.junit.AssumptionViolatedException) Logger(org.slf4j.Logger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) IOException(java.io.IOException) Field(java.lang.reflect.Field) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) RaftServer(org.apache.ratis.server.RaftServer) RaftLogBase(org.apache.ratis.server.raftlog.RaftLogBase) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) RaftServer(org.apache.ratis.server.RaftServer) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) TimeDuration(org.apache.ratis.util.TimeDuration) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)11 RaftServer (org.apache.ratis.server.RaftServer)8 Test (org.junit.Test)8 List (java.util.List)7 RaftClient (org.apache.ratis.client.RaftClient)7 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)7 Assert (org.junit.Assert)7 ArrayList (java.util.ArrayList)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Level (org.apache.log4j.Level)6 RaftProperties (org.apache.ratis.conf.RaftProperties)6 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)6 Log4jUtils (org.apache.ratis.util.Log4jUtils)6 TimeDuration (org.apache.ratis.util.TimeDuration)6 Logger (org.slf4j.Logger)6 IOException (java.io.IOException)5 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)5 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)5 RaftServerTestUtil (org.apache.ratis.server.impl.RaftServerTestUtil)5 TimeUnit (java.util.concurrent.TimeUnit)4