Search in sources :

Example 61 with RaftPeerId

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

the class RaftClientImpl method getHighestPriorityPeerId.

private RaftPeerId getHighestPriorityPeerId() {
    int maxPriority = Integer.MIN_VALUE;
    RaftPeerId highestPriorityPeerId = null;
    for (RaftPeer peer : peers) {
        if (maxPriority < peer.getPriority()) {
            maxPriority = peer.getPriority();
            highestPriorityPeerId = peer.getId();
        }
    }
    return highestPriorityPeerId;
}
Also used : RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer)

Example 62 with RaftPeerId

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

the class ServerRestartTests method runTestRestartFollower.

void runTestRestartFollower(MiniRaftCluster cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeader().getId();
    // write some messages
    final AtomicInteger messageCount = new AtomicInteger();
    final Supplier<Message> newMessage = () -> new SimpleMessage("m" + messageCount.getAndIncrement());
    writeSomething(newMessage, cluster);
    // restart a follower
    RaftPeerId followerId = cluster.getFollowers().get(0).getId();
    LOG.info("Restart follower {}", followerId);
    cluster.restartServer(followerId, false);
    // write some more messages
    writeSomething(newMessage, cluster);
    final int truncatedMessageIndex = messageCount.get() - 1;
    final long leaderLastIndex = cluster.getLeader().getRaftLog().getLastEntryTermIndex().getIndex();
    // make sure the restarted follower can catchup
    final RaftServer.Division followerState = cluster.getDivision(followerId);
    JavaUtils.attemptRepeatedly(() -> {
        Assert.assertTrue(followerState.getInfo().getLastAppliedIndex() >= leaderLastIndex);
        return null;
    }, 10, ONE_SECOND, "follower catchup", LOG);
    // make sure the restarted peer's log segments is correct
    final RaftServer.Division follower = cluster.restartServer(followerId, false);
    final RaftLog followerLog = follower.getRaftLog();
    final long followerLastIndex = followerLog.getLastEntryTermIndex().getIndex();
    Assert.assertTrue(followerLastIndex >= leaderLastIndex);
    final File followerOpenLogFile = getOpenLogFile(follower);
    final File leaderOpenLogFile = getOpenLogFile(cluster.getDivision(leaderId));
    // shutdown all servers
    for (RaftServer s : cluster.getServers()) {
        s.close();
    }
    // truncate log and
    assertTruncatedLog(followerId, followerOpenLogFile, followerLastIndex, cluster);
    assertTruncatedLog(leaderId, leaderOpenLogFile, leaderLastIndex, cluster);
    // restart and write something.
    cluster.restart(false);
    writeSomething(newMessage, cluster);
    // restart again and check messages.
    cluster.restart(false);
    try (final RaftClient client = cluster.createClient()) {
        for (int i = 0; i < messageCount.get(); i++) {
            if (i != truncatedMessageIndex) {
                final Message m = new SimpleMessage("m" + i);
                final RaftClientReply reply = client.io().sendReadOnly(m);
                Assert.assertTrue(reply.isSuccess());
                LOG.info("query {}: {} {}", m, reply, LogEntryProto.parseFrom(reply.getMessage().getContent()));
            }
        }
    }
}
Also used : Message(org.apache.ratis.protocol.Message) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog) TestSegmentedRaftLog(org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog)

Example 63 with RaftPeerId

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

the class MemoryRaftLogTest method testEntryDoNotPerformTruncation.

@Test
public void testEntryDoNotPerformTruncation() throws Exception {
    final RaftProperties prop = new RaftProperties();
    prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
    final RaftPeerId peerId = RaftPeerId.valueOf("s0");
    final RaftGroupId groupId = RaftGroupId.randomId();
    final RaftGroupMemberId memberId = RaftGroupMemberId.valueOf(peerId, groupId);
    MemoryRaftLog raftLog = new MemoryRaftLog(memberId, () -> -1, prop);
    raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
    LogEntryProto[] entries1 = new LogEntryProto[2];
    entries1[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(0).build();
    entries1[1] = LogEntryProto.newBuilder().setIndex(1).setTerm(0).build();
    raftLog.append(entries1).forEach(CompletableFuture::join);
    LogEntryProto[] entries2 = new LogEntryProto[1];
    entries2[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(0).build();
    raftLog.append(entries2).forEach(CompletableFuture::join);
    final LogEntryHeader[] termIndices = raftLog.getEntries(0, 10);
    assertEquals(2, termIndices.length);
    for (int i = 0; i < 2; i++) {
        assertEquals(entries1[i].getIndex(), termIndices[i].getIndex());
        assertEquals(entries1[i].getTerm(), termIndices[i].getTerm());
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 64 with RaftPeerId

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

the class TestCacheEviction method testEvictionInSegmentedLog.

@Test
public void testEvictionInSegmentedLog() throws Exception {
    final RaftProperties prop = new RaftProperties();
    prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
    RaftServerConfigKeys.Log.setSegmentSizeMax(prop, SizeInBytes.valueOf("8KB"));
    RaftServerConfigKeys.Log.setPreallocatedSize(prop, SizeInBytes.valueOf("8KB"));
    final RaftPeerId peerId = RaftPeerId.valueOf("s0");
    final RaftGroupId groupId = RaftGroupId.randomId();
    final RaftGroupMemberId memberId = RaftGroupMemberId.valueOf(peerId, groupId);
    final int maxCachedNum = RaftServerConfigKeys.Log.segmentCacheNumMax(prop);
    File storageDir = getTestDir();
    RaftServerConfigKeys.setStorageDir(prop, Collections.singletonList(storageDir));
    RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
    final DivisionInfo info = Mockito.mock(DivisionInfo.class);
    Mockito.when(info.getLastAppliedIndex()).thenReturn(0L);
    Mockito.when(info.getFollowerNextIndices()).thenReturn(new long[] {});
    final SegmentedRaftLog raftLog = RaftServerTestUtil.newSegmentedRaftLog(memberId, info, storage, prop);
    raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
    List<SegmentRange> slist = TestSegmentedRaftLog.prepareRanges(0, maxCachedNum, 7, 0);
    LogEntryProto[] entries = generateEntries(slist);
    raftLog.append(entries).forEach(CompletableFuture::join);
    // check the current cached segment number: the last segment is still open
    Assert.assertEquals(maxCachedNum - 1, raftLog.getRaftLogCache().getCachedSegmentNum());
    Mockito.when(info.getLastAppliedIndex()).thenReturn(35L);
    Mockito.when(info.getFollowerNextIndices()).thenReturn(new long[] { 21, 40, 40 });
    slist = TestSegmentedRaftLog.prepareRanges(maxCachedNum, maxCachedNum + 2, 7, 7 * maxCachedNum);
    entries = generateEntries(slist);
    raftLog.append(entries).forEach(CompletableFuture::join);
    // check the cached segment number again. since the slowest follower is on
    // index 21, the eviction should happen and evict 3 segments
    Assert.assertEquals(maxCachedNum + 1 - 3, raftLog.getRaftLogCache().getCachedSegmentNum());
}
Also used : SegmentRange(org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog.SegmentRange) DivisionInfo(org.apache.ratis.server.DivisionInfo) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftStorage(org.apache.ratis.server.storage.RaftStorage) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 65 with RaftPeerId

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

the class TestStateMachine method testStateMachineRegistry.

@Test
public void testStateMachineRegistry() throws Throwable {
    final Map<RaftGroupId, StateMachine> registry = new ConcurrentHashMap<>();
    registry.put(RaftGroupId.randomId(), new SimpleStateMachine4Testing());
    registry.put(RaftGroupId.randomId(), new SMTransactionContext());
    try (MiniRaftClusterWithSimulatedRpc cluster = newCluster(0)) {
        cluster.setStateMachineRegistry(registry::get);
        final RaftPeerId id = RaftPeerId.valueOf("s0");
        cluster.putNewServer(id, null, true);
        cluster.start();
        for (RaftGroupId gid : registry.keySet()) {
            final RaftGroup newGroup = RaftGroup.valueOf(gid, cluster.getPeers());
            LOG.info("add new group: " + newGroup);
            try (final RaftClient client = cluster.createClient(newGroup)) {
                for (RaftPeer p : newGroup.getPeers()) {
                    client.getGroupManagementApi(p.getId()).add(newGroup);
                }
            }
        }
        final RaftServer server = cluster.getServer(id);
        for (Map.Entry<RaftGroupId, StateMachine> e : registry.entrySet()) {
            Assert.assertSame(e.getValue(), server.getDivision(e.getKey()).getStateMachine());
        }
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) MiniRaftClusterWithSimulatedRpc(org.apache.ratis.server.simulation.MiniRaftClusterWithSimulatedRpc) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RaftClient(org.apache.ratis.client.RaftClient) 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