Search in sources :

Example 6 with LogEntryHeader

use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.

the class MemoryRaftLog method getEntries.

@Override
public LogEntryHeader[] getEntries(long startIndex, long endIndex) {
    checkLogState();
    try (AutoCloseableLock readLock = readLock()) {
        if (startIndex >= entries.size()) {
            return null;
        }
        final int from = Math.toIntExact(startIndex);
        final int to = Math.toIntExact(Math.min(entries.size(), endIndex));
        final LogEntryHeader[] headers = new LogEntryHeader[to - from];
        for (int i = 0; i < headers.length; i++) {
            headers[i] = entries.getLogEntryHeader(i);
        }
        return headers;
    }
}
Also used : LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 7 with LogEntryHeader

use of org.apache.ratis.server.raftlog.LogEntryHeader 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 8 with LogEntryHeader

use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.

the class TestRaftWithGrpc method runTestUpdateViaHeartbeat.

void runTestUpdateViaHeartbeat(MiniRaftClusterWithGrpc cluster) throws Exception {
    waitForLeader(cluster);
    try (final RaftClient client = cluster.createClient()) {
        // block append requests
        cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::blockWriteStateMachineData);
        CompletableFuture<RaftClientReply> replyFuture = client.async().send(new RaftTestUtil.SimpleMessage("abc"));
        TimeDuration.valueOf(5, TimeUnit.SECONDS).sleep();
        // replyFuture should not be completed until append request is unblocked.
        Assert.assertFalse(replyFuture.isDone());
        // unblock append request.
        cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::unblockWriteStateMachineData);
        final RaftLog leaderLog = cluster.getLeader().getRaftLog();
        // The entries have been appended in the followers
        // although the append entry timed out at the leader
        cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).forEach(raftServer -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> {
            final long leaderNextIndex = leaderLog.getNextIndex();
            final LogEntryHeader[] leaderEntries = leaderLog.getEntries(0, Long.MAX_VALUE);
            final RaftLog followerLog = raftServer.getRaftLog();
            Assert.assertEquals(leaderNextIndex, followerLog.getNextIndex());
            final LogEntryHeader[] serverEntries = followerLog.getEntries(0, Long.MAX_VALUE);
            Assert.assertArrayEquals(serverEntries, leaderEntries);
        }, 10, HUNDRED_MILLIS, "assertRaftLog-" + raftServer.getId(), LOG)));
        // Wait for heartbeats from leader to be received by followers
        Thread.sleep(500);
        RaftServerTestUtil.getLogAppenders(cluster.getLeader()).forEach(logAppender -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> {
            final long leaderNextIndex = leaderLog.getNextIndex();
            // FollowerInfo in the leader state should have updated next and match index.
            final long followerMatchIndex = logAppender.getFollower().getMatchIndex();
            Assert.assertTrue(followerMatchIndex >= leaderNextIndex - 1);
            Assert.assertEquals(followerMatchIndex + 1, logAppender.getFollower().getNextIndex());
        }, 10, HUNDRED_MILLIS, "assertRaftLog-" + logAppender.getFollower(), LOG)));
    }
}
Also used : RaftBasicTests(org.apache.ratis.RaftBasicTests) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) StateMachine(org.apache.ratis.statemachine.StateMachine) RaftLog(org.apache.ratis.server.raftlog.RaftLog) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) RaftTestUtil(org.apache.ratis.RaftTestUtil) TimeUnit(java.util.concurrent.TimeUnit) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftClient(org.apache.ratis.client.RaftClient) JavaUtils(org.apache.ratis.util.JavaUtils) Assert(org.junit.Assert) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 9 with LogEntryHeader

use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.

the class OutputStreamBaseTest method runTestWriteWithOffset.

private void runTestWriteWithOffset(CLUSTER cluster) throws Exception {
    final int bufferSize = ByteValue.BUFFERSIZE;
    final RaftServer.Division leader = waitForLeader(cluster);
    final OutputStream out = newOutputStream(cluster, bufferSize);
    byte[] b1 = new byte[ByteValue.BUFFERSIZE / 2];
    Arrays.fill(b1, (byte) 1);
    byte[] b2 = new byte[ByteValue.BUFFERSIZE];
    Arrays.fill(b2, (byte) 2);
    byte[] b3 = new byte[ByteValue.BUFFERSIZE * 2 + ByteValue.BUFFERSIZE / 2];
    Arrays.fill(b3, (byte) 3);
    byte[] b4 = new byte[ByteValue.BUFFERSIZE * 4];
    Arrays.fill(b3, (byte) 4);
    byte[] expected = new byte[ByteValue.BUFFERSIZE * 8];
    byte[][] data = new byte[][] { b1, b2, b3, b4 };
    final Random random = new Random();
    int totalSize = 0;
    for (byte[] b : data) {
        System.arraycopy(b, 0, expected, totalSize, b.length);
        totalSize += b.length;
        int written = 0;
        while (written < b.length) {
            int toWrite = random.nextInt(b.length - written) + 1;
            LOG.info("write {} bytes", toWrite);
            out.write(b, written, toWrite);
            written += toWrite;
        }
    }
    out.close();
    // 0.5 + 1 + 2.5 + 4 = 8
    final int expectedEntries = 8;
    final RaftLog raftLog = assertRaftLog(expectedEntries, leader);
    final LogEntryHeader[] entries = raftLog.getEntries(1, Long.MAX_VALUE);
    final byte[] actual = new byte[ByteValue.BUFFERSIZE * expectedEntries];
    totalSize = 0;
    for (LogEntryHeader ti : entries) {
        final LogEntryProto e = raftLog.get(ti.getIndex());
        if (e.hasStateMachineLogEntry()) {
            final byte[] eValue = e.getStateMachineLogEntry().getLogData().toByteArray();
            Assert.assertEquals(ByteValue.BUFFERSIZE, eValue.length);
            System.arraycopy(eValue, 0, actual, totalSize, eValue.length);
            totalSize += eValue.length;
        }
    }
    Assert.assertArrayEquals(expected, actual);
}
Also used : LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Random(java.util.Random) RaftServer(org.apache.ratis.server.RaftServer) RaftOutputStream(org.apache.ratis.client.impl.RaftOutputStream) OutputStream(java.io.OutputStream) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Aggregations

LogEntryHeader (org.apache.ratis.server.raftlog.LogEntryHeader)9 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)6 CompletableFuture (java.util.concurrent.CompletableFuture)5 Test (org.junit.Test)5 BaseTest (org.apache.ratis.BaseTest)4 RaftProperties (org.apache.ratis.conf.RaftProperties)4 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)4 RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)4 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)4 RaftLog (org.apache.ratis.server.raftlog.RaftLog)4 TimeUnit (java.util.concurrent.TimeUnit)3 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)3 StateMachine (org.apache.ratis.statemachine.StateMachine)3 JavaUtils (org.apache.ratis.util.JavaUtils)3 Timer (com.codahale.metrics.Timer)2 File (java.io.File)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2