Search in sources :

Example 6 with MiniRaftCluster

use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.

the class WatchRequestTests method runTestWatchRequestTimeout.

static void runTestWatchRequestTimeout(TestParameters p) throws Exception {
    final Logger LOG = p.log;
    final MiniRaftCluster cluster = p.cluster;
    final int numMessages = p.numMessages;
    final RaftProperties properties = cluster.getProperties();
    final TimeDuration watchTimeout = RaftServerConfigKeys.Watch.timeout(properties);
    final TimeDuration watchTimeoutDenomination = RaftServerConfigKeys.Watch.timeoutDenomination(properties);
    // blockStartTransaction of the leader so that no transaction can be committed MAJORITY
    final RaftServer.Division leader = cluster.getLeader();
    LOG.info("block leader {}", leader.getId());
    SimpleStateMachine4Testing.get(leader).blockStartTransaction();
    // blockFlushStateMachineData a follower so that no transaction can be ALL_COMMITTED
    final List<RaftServer.Division> followers = cluster.getFollowers();
    final RaftServer.Division blockedFollower = followers.get(ThreadLocalRandom.current().nextInt(followers.size()));
    LOG.info("block follower {}", blockedFollower.getId());
    SimpleStateMachine4Testing.get(blockedFollower).blockFlushStateMachineData();
    // send a message
    final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
    final List<CompletableFuture<WatchReplies>> watches = new ArrayList<>();
    p.sendRequests(replies, watches);
    Assert.assertEquals(numMessages, replies.size());
    Assert.assertEquals(numMessages, watches.size());
    watchTimeout.sleep();
    // for roundup error
    watchTimeoutDenomination.sleep();
    assertNotDone(replies);
    assertNotDone(watches);
    // unblock leader so that the transaction can be committed.
    SimpleStateMachine4Testing.get(leader).unblockStartTransaction();
    LOG.info("unblock leader {}", leader.getId());
    checkMajority(replies, watches, LOG);
    checkTimeout(replies, watches, LOG);
    SimpleStateMachine4Testing.get(blockedFollower).unblockFlushStateMachineData();
    LOG.info("unblock follower {}", blockedFollower.getId());
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) RaftProperties(org.apache.ratis.conf.RaftProperties) ArrayList(java.util.ArrayList) Logger(org.slf4j.Logger) CompletableFuture(java.util.concurrent.CompletableFuture) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration)

Example 7 with MiniRaftCluster

use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.

the class TestRaftLogMetrics method testRaftLogMetrics.

@Test
public void testRaftLogMetrics() throws Exception {
    try (final MiniRaftCluster cluster = newCluster(NUM_SERVERS)) {
        cluster.start();
        runTestRaftLogMetrics(cluster);
    }
}
Also used : MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 8 with MiniRaftCluster

use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.

the class RaftAsyncTests method runTestNoRetryWaitOnNotLeaderException.

private void runTestNoRetryWaitOnNotLeaderException(MiniRaftCluster cluster) throws Exception {
    final RaftServer.Division leader = waitForLeader(cluster);
    final List<RaftServer.Division> followers = cluster.getFollowers();
    Assert.assertNotNull(followers);
    Assert.assertEquals(2, followers.size());
    Assert.assertNotSame(leader, followers.get(0));
    Assert.assertNotSame(leader, followers.get(1));
    // send a message to make sure that the leader is ready
    try (final RaftClient client = cluster.createClient(leader.getId())) {
        final CompletableFuture<RaftClientReply> f = client.async().send(new SimpleMessage("first"));
        FIVE_SECONDS.apply(f::get);
    }
    // if sleep interval defined by retry policy is used the test will timeout
    final RetryPolicy r = event -> () -> TimeDuration.valueOf(60, TimeUnit.SECONDS);
    try (final RaftClient client = cluster.createClient(followers.get(0).getId(), cluster.getGroup(), r)) {
        final CompletableFuture<RaftClientReply> f = client.async().send(new SimpleMessage("abc"));
        FIVE_SECONDS.apply(f::get);
    } catch (TimeoutException e) {
        throw new AssertionError("Failed to get async result", e);
    }
}
Also used : RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) RetryPolicies(org.apache.ratis.retry.RetryPolicies) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) ArrayList(java.util.ArrayList) Log4jUtils(org.apache.ratis.util.Log4jUtils) Message(org.apache.ratis.protocol.Message) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) CheckedRunnable(org.apache.ratis.util.function.CheckedRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) Level(org.apache.log4j.Level) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) StreamSupport(java.util.stream.StreamSupport) JavaUtils(org.apache.ratis.util.JavaUtils) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) StateMachine(org.apache.ratis.statemachine.StateMachine) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RetryPolicy(org.apache.ratis.retry.RetryPolicy) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Collection(java.util.Collection) RaftClientTestUtil(org.apache.ratis.client.impl.RaftClientTestUtil) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) RetryLimited(org.apache.ratis.retry.RetryPolicies.RetryLimited) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) Comparator(java.util.Comparator) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) DelayLocalExecutionInjection(org.apache.ratis.server.impl.DelayLocalExecutionInjection) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RetryPolicy(org.apache.ratis.retry.RetryPolicy) RaftClient(org.apache.ratis.client.RaftClient) TimeoutException(java.util.concurrent.TimeoutException)

Example 9 with MiniRaftCluster

use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.

the class WatchRequestTests method runTestWatchRequestAsyncChangeLeader.

static void runTestWatchRequestAsyncChangeLeader(TestParameters p) throws Exception {
    final Logger LOG = p.log;
    final MiniRaftCluster cluster = p.cluster;
    final int numMessages = p.numMessages;
    // blockFlushStateMachineData a follower so that no transaction can be ALL_COMMITTED
    final List<RaftServer.Division> followers = cluster.getFollowers();
    final RaftServer.Division blockedFollower = followers.get(ThreadLocalRandom.current().nextInt(followers.size()));
    LOG.info("block follower {}", blockedFollower.getId());
    SimpleStateMachine4Testing.get(blockedFollower).blockFlushStateMachineData();
    final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
    final List<CompletableFuture<WatchReplies>> watches = new ArrayList<>();
    p.sendRequests(replies, watches);
    Assert.assertEquals(numMessages, replies.size());
    Assert.assertEquals(numMessages, watches.size());
    // since only one follower is blocked commit, requests can be committed MAJORITY and ALL but not ALL_COMMITTED.
    checkMajority(replies, watches, LOG);
    TimeUnit.SECONDS.sleep(1);
    assertNotDone(watches.stream().map(CompletableFuture::join).map(w -> w.allCommitted));
    // Now change leader
    RaftTestUtil.changeLeader(cluster, cluster.getLeader().getId());
    // unblock follower so that the transaction can be replicated and committed to all.
    SimpleStateMachine4Testing.get(blockedFollower).unblockFlushStateMachineData();
    LOG.info("unblock follower {}", blockedFollower.getId());
    checkAll(watches, LOG);
}
Also used : RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) NotReplicatedException(org.apache.ratis.protocol.exceptions.NotReplicatedException) CheckedSupplier(org.apache.ratis.util.function.CheckedSupplier) CompletableFuture(java.util.concurrent.CompletableFuture) RetryPolicies(org.apache.ratis.retry.RetryPolicies) ArrayList(java.util.ArrayList) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) Log4jUtils(org.apache.ratis.util.Log4jUtils) ReplicationLevel(org.apache.ratis.proto.RaftProtos.ReplicationLevel) ProtoUtils(org.apache.ratis.util.ProtoUtils) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) CheckedConsumer(org.apache.ratis.util.function.CheckedConsumer) Level(org.apache.log4j.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) StateMachine(org.apache.ratis.statemachine.StateMachine) Logger(org.slf4j.Logger) RetryPolicy(org.apache.ratis.retry.RetryPolicy) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Collection(java.util.Collection) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) Stream(java.util.stream.Stream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftServer(org.apache.ratis.server.RaftServer) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) CompletableFuture(java.util.concurrent.CompletableFuture) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RaftServer(org.apache.ratis.server.RaftServer) ArrayList(java.util.ArrayList) Logger(org.slf4j.Logger)

Example 10 with MiniRaftCluster

use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.

the class WatchRequestTests method runTestWatchRequestAsync.

static void runTestWatchRequestAsync(TestParameters p) throws Exception {
    final Logger LOG = p.log;
    final MiniRaftCluster cluster = p.cluster;
    final int numMessages = p.numMessages;
    // blockStartTransaction of the leader so that no transaction can be committed MAJORITY
    final RaftServer.Division leader = cluster.getLeader();
    LOG.info("block leader {}", leader.getId());
    SimpleStateMachine4Testing.get(leader).blockStartTransaction();
    // blockFlushStateMachineData a follower so that no transaction can be ALL_COMMITTED
    final List<RaftServer.Division> followers = cluster.getFollowers();
    final RaftServer.Division blockedFollower = followers.get(ThreadLocalRandom.current().nextInt(followers.size()));
    LOG.info("block follower {}", blockedFollower.getId());
    SimpleStateMachine4Testing.get(blockedFollower).blockFlushStateMachineData();
    // send a message
    final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
    final List<CompletableFuture<WatchReplies>> watches = new ArrayList<>();
    p.sendRequests(replies, watches);
    Assert.assertEquals(numMessages, replies.size());
    Assert.assertEquals(numMessages, watches.size());
    // since leader is blocked, nothing can be done.
    TimeUnit.SECONDS.sleep(1);
    assertNotDone(replies);
    assertNotDone(watches);
    // unblock leader so that the transaction can be committed.
    SimpleStateMachine4Testing.get(leader).unblockStartTransaction();
    LOG.info("unblock leader {}", leader.getId());
    checkMajority(replies, watches, LOG);
    Assert.assertEquals(numMessages, watches.size());
    // but not replicated/committed to all.
    TimeUnit.SECONDS.sleep(1);
    assertNotDone(watches.stream().map(CompletableFuture::join).map(w -> w.all));
    assertNotDone(watches.stream().map(CompletableFuture::join).map(w -> w.allCommitted));
    // unblock follower so that the transaction can be replicated and committed to all.
    LOG.info("unblock follower {}", blockedFollower.getId());
    SimpleStateMachine4Testing.get(blockedFollower).unblockFlushStateMachineData();
    checkAll(watches, LOG);
}
Also used : RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) NotReplicatedException(org.apache.ratis.protocol.exceptions.NotReplicatedException) CheckedSupplier(org.apache.ratis.util.function.CheckedSupplier) CompletableFuture(java.util.concurrent.CompletableFuture) RetryPolicies(org.apache.ratis.retry.RetryPolicies) ArrayList(java.util.ArrayList) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) Log4jUtils(org.apache.ratis.util.Log4jUtils) ReplicationLevel(org.apache.ratis.proto.RaftProtos.ReplicationLevel) ProtoUtils(org.apache.ratis.util.ProtoUtils) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) CheckedConsumer(org.apache.ratis.util.function.CheckedConsumer) Level(org.apache.log4j.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) StateMachine(org.apache.ratis.statemachine.StateMachine) Logger(org.slf4j.Logger) RetryPolicy(org.apache.ratis.retry.RetryPolicy) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Collection(java.util.Collection) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) Stream(java.util.stream.Stream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftServer(org.apache.ratis.server.RaftServer) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) CompletableFuture(java.util.concurrent.CompletableFuture) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RaftServer(org.apache.ratis.server.RaftServer) ArrayList(java.util.ArrayList) Logger(org.slf4j.Logger)

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