Search in sources :

Example 41 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class RaftBasicTests method runTestOldLeaderNotCommit.

void runTestOldLeaderNotCommit(CLUSTER cluster) throws Exception {
    final RaftPeerId leaderId = waitForLeader(cluster).getId();
    final List<RaftServer.Division> followers = cluster.getFollowers();
    final RaftServer.Division followerToCommit = followers.get(0);
    try {
        for (int i = 1; i < NUM_SERVERS - 1; i++) {
            cluster.killServer(followers.get(i).getId());
        }
    } catch (IndexOutOfBoundsException e) {
        throw new org.junit.AssumptionViolatedException("The assumption is follower.size() = NUM_SERVERS - 1, " + "actual NUM_SERVERS is " + NUM_SERVERS + ", and actual follower.size() is " + followers.size(), e);
    }
    SimpleMessage[] messages = SimpleMessage.create(1);
    RaftTestUtil.sendMessageInNewThread(cluster, leaderId, messages);
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) + 100);
    RaftTestUtil.logEntriesContains(followerToCommit.getRaftLog(), messages);
    cluster.killServer(leaderId);
    cluster.killServer(followerToCommit.getId());
    for (int i = 1; i < NUM_SERVERS - 1; i++) {
        cluster.restartServer(followers.get(i).getId(), false);
    }
    waitForLeader(cluster);
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) + 100);
    final Predicate<LogEntryProto> predicate = l -> l.getTerm() != 1;
    cluster.getServerAliveStream().map(RaftServer.Division::getRaftLog).forEach(log -> RaftTestUtil.checkLogEntries(log, messages, predicate));
}
Also used : RetryCacheTestUtil(org.apache.ratis.server.impl.RetryCacheTestUtil) Timer(java.util.Timer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Level(org.apache.log4j.Level) JavaUtils(org.apache.ratis.util.JavaUtils) TimerTask(java.util.TimerTask) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) STATEMACHINE_APPLIED_INDEX_GAUGE(org.apache.ratis.server.impl.StateMachineMetrics.STATEMACHINE_APPLIED_INDEX_GAUGE) Timestamp(org.apache.ratis.util.Timestamp) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Predicate(java.util.function.Predicate) Set(java.util.Set) RATIS_STATEMACHINE_METRICS(org.apache.ratis.server.impl.StateMachineMetrics.RATIS_STATEMACHINE_METRICS) Collectors(java.util.stream.Collectors) RATIS_STATEMACHINE_METRICS_DESC(org.apache.ratis.server.impl.StateMachineMetrics.RATIS_STATEMACHINE_METRICS_DESC) MetricRegistries(org.apache.ratis.metrics.MetricRegistries) MetricRegistryInfo(org.apache.ratis.metrics.MetricRegistryInfo) List(java.util.List) Stream(java.util.stream.Stream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Optional(java.util.Optional) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId) Gauge(com.codahale.metrics.Gauge) SortedMap(java.util.SortedMap) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) RaftLog(org.apache.ratis.server.raftlog.RaftLog) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) RATIS_APPLICATION_NAME_METRICS(org.apache.ratis.metrics.RatisMetrics.RATIS_APPLICATION_NAME_METRICS) Log4jUtils(org.apache.ratis.util.Log4jUtils) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl) Logger(org.slf4j.Logger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) RaftClientTestUtil(org.apache.ratis.client.impl.RaftClientTestUtil) STATEMACHINE_APPLY_COMPLETED_GAUGE(org.apache.ratis.server.impl.StateMachineMetrics.STATEMACHINE_APPLY_COMPLETED_GAUGE) Test(org.junit.Test) IOException(java.io.IOException) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) TimeUnit(java.util.concurrent.TimeUnit) ExitUtils(org.apache.ratis.util.ExitUtils) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Example 42 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class RaftTestUtil method changeLeader.

static RaftPeerId changeLeader(MiniRaftCluster cluster, RaftPeerId oldLeader, Function<String, Exception> constructor) throws Exception {
    final String name = JavaUtils.getCallerStackTraceElement().getMethodName() + "-changeLeader";
    cluster.setBlockRequestsFrom(oldLeader.toString(), true);
    try {
        return JavaUtils.attemptRepeatedly(() -> {
            final RaftPeerId newLeader = waitForLeader(cluster).getId();
            if (newLeader.equals(oldLeader)) {
                throw constructor.apply("Failed to change leader: newLeader == oldLeader == " + oldLeader);
            }
            LOG.info("Changed leader from " + oldLeader + " to " + newLeader);
            return newLeader;
        }, 20, BaseTest.HUNDRED_MILLIS, name, LOG);
    } finally {
        cluster.setBlockRequestsFrom(oldLeader.toString(), false);
    }
}
Also used : ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Example 43 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class RetryCacheTests method runTestRetryOnNewLeader.

void runTestRetryOnNewLeader(CLUSTER cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(false).getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        RaftClientRpc rpc = client.getClientRpc();
        final long callId = 999;
        RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, new SimpleMessage("message"));
        assertReply(rpc.sendRequest(r), client, callId);
        final long oldLastApplied = cluster.getLeader().getInfo().getLastAppliedIndex();
        // trigger the reconfiguration, make sure the original leader is kicked out
        PeerChanges change = cluster.addNewPeers(2, true);
        RaftPeer[] allPeers = cluster.removePeers(2, true, asList(change.newPeers)).allPeersInNewConf;
        // trigger setConfiguration
        cluster.setConfiguration(allPeers);
        final RaftPeerId newLeaderId = JavaUtils.attemptRepeatedly(() -> {
            final RaftPeerId id = RaftTestUtil.waitForLeader(cluster).getId();
            Assert.assertNotEquals(leaderId, id);
            return id;
        }, 10, TimeDuration.valueOf(100, TimeUnit.MILLISECONDS), "wait for a leader different than " + leaderId, LOG);
        Assert.assertNotEquals(leaderId, newLeaderId);
        // same clientId and callId in the request
        r = cluster.newRaftClientRequest(client.getId(), newLeaderId, callId, new SimpleMessage("message"));
        rpc.addRaftPeers(Arrays.asList(change.newPeers));
        for (int i = 0; i < 10; i++) {
            try {
                assertReply(rpc.sendRequest(r), client, callId);
                LOG.info("successfully sent out the retry request_" + i);
            } catch (Exception e) {
                LOG.info("hit exception while retrying the same request: " + r, e);
            }
            Thread.sleep(100);
        }
        // check the new leader and make sure the retry did not get committed
        Assert.assertEquals(0, count(cluster.getLeader().getRaftLog(), oldLastApplied + 1));
    }
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) PeerChanges(org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) RaftClientRpc(org.apache.ratis.client.RaftClientRpc)

Example 44 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class TestLeaderElectionMetrics method setUp.

@BeforeClass
public static void setUp() {
    RaftGroupId raftGroupId = RaftGroupId.randomId();
    RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
    RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId.valueOf(raftPeerId, raftGroupId);
    leaderElectionMetrics = LeaderElectionMetrics.getLeaderElectionMetrics(raftGroupMemberId, () -> 1000L);
    ratisMetricRegistry = leaderElectionMetrics.getRegistry();
}
Also used : RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) BeforeClass(org.junit.BeforeClass)

Example 45 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class LeaderElectionTests method testChangeLeader.

@Test
public void testChangeLeader() throws Exception {
    SegmentedRaftLogTestUtils.setRaftLogWorkerLogLevel(Level.TRACE);
    LOG.info("Running testChangeLeader");
    final MiniRaftCluster cluster = newCluster(3);
    cluster.start();
    RaftPeerId leader = RaftTestUtil.waitForLeader(cluster).getId();
    for (int i = 0; i < 10; i++) {
        leader = RaftTestUtil.changeLeader(cluster, leader, IllegalStateException::new);
        ExitUtils.assertNotTerminated();
    }
    SegmentedRaftLogTestUtils.setRaftLogWorkerLogLevel(Level.INFO);
    cluster.shutdown();
}
Also used : RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

RaftPeerId (org.apache.ratis.protocol.RaftPeerId)101 RaftClient (org.apache.ratis.client.RaftClient)58 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)45 IOException (java.io.IOException)36 Test (org.junit.Test)32 RaftPeer (org.apache.ratis.protocol.RaftPeer)31 RaftServer (org.apache.ratis.server.RaftServer)31 BaseTest (org.apache.ratis.BaseTest)21 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)20 CompletableFuture (java.util.concurrent.CompletableFuture)18 RaftProperties (org.apache.ratis.conf.RaftProperties)18 File (java.io.File)17 ArrayList (java.util.ArrayList)15 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)15 RaftGroup (org.apache.ratis.protocol.RaftGroup)14 TimeDuration (org.apache.ratis.util.TimeDuration)14 List (java.util.List)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Collectors (java.util.stream.Collectors)11 RaftLog (org.apache.ratis.server.raftlog.RaftLog)11