Search in sources :

Example 21 with RaftLog

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

the class OutputStreamBaseTest method runTestSimpleWrite.

private void runTestSimpleWrite(CLUSTER cluster) throws Exception {
    final int numRequests = 5000;
    final int bufferSize = 4;
    RaftTestUtil.waitForLeader(cluster);
    try (OutputStream out = newOutputStream(cluster, bufferSize)) {
        for (int i = 0; i < numRequests; i++) {
            // generate requests
            out.write(toBytes(i));
        }
    }
    // check the leader's raft log
    final RaftLog raftLog = cluster.getLeader().getRaftLog();
    final AtomicInteger i = new AtomicInteger();
    checkLog(raftLog, numRequests, () -> toBytes(i.getAndIncrement()));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftOutputStream(org.apache.ratis.client.impl.RaftOutputStream) OutputStream(java.io.OutputStream) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 22 with RaftLog

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

the class OutputStreamBaseTest method assertRaftLog.

private RaftLog assertRaftLog(int expectedEntries, RaftServer.Division server) throws Exception {
    final RaftLog raftLog = server.getRaftLog();
    final EnumMap<LogEntryBodyCase, AtomicLong> counts = RaftTestUtil.countEntries(raftLog);
    Assert.assertEquals(expectedEntries, counts.get(LogEntryBodyCase.STATEMACHINELOGENTRY).get());
    final LogEntryProto last = RaftTestUtil.getLastEntry(LogEntryBodyCase.STATEMACHINELOGENTRY, raftLog);
    Assert.assertNotNull(last);
    Assert.assertTrue(raftLog.getLastCommittedIndex() >= last.getIndex());
    Assert.assertTrue(server.getInfo().getLastAppliedIndex() >= last.getIndex());
    return raftLog;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftLog(org.apache.ratis.server.raftlog.RaftLog) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase)

Example 23 with RaftLog

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

RaftLog (org.apache.ratis.server.raftlog.RaftLog)23 RaftClient (org.apache.ratis.client.RaftClient)12 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)12 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)12 RaftServer (org.apache.ratis.server.RaftServer)11 IOException (java.io.IOException)9 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)9 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 TimeDuration (org.apache.ratis.util.TimeDuration)6 ArrayList (java.util.ArrayList)5 TimeUnit (java.util.concurrent.TimeUnit)5 JavaUtils (org.apache.ratis.util.JavaUtils)5 Assert (org.junit.Assert)5 File (java.io.File)4 List (java.util.List)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 TermIndex (org.apache.ratis.server.protocol.TermIndex)4 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)4 Arrays (java.util.Arrays)3