Search in sources :

Example 6 with RaftLog

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

the class TestRaftStream method testWriteWithOffset.

@Test
public void testWriteWithOffset() throws Exception {
    LOG.info("Running testWriteWithOffset");
    GrpcConfigKeys.OutputStream.setBufferSize(prop, SizeInBytes.valueOf(ByteValue.BUFFERSIZE));
    cluster = MiniRaftClusterWithGRpc.FACTORY.newCluster(NUM_SERVERS, prop);
    cluster.start();
    RaftServerImpl leader = waitForLeader(cluster);
    RaftOutputStream out = new RaftOutputStream(prop, ClientId.randomId(), cluster.getGroup(), leader.getId());
    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();
    final RaftLog log = leader.getState().getLog();
    // 0.5 + 1 + 2.5 + 4 = 8
    Assert.assertEquals(8, leader.getState().getLastAppliedIndex());
    Assert.assertEquals(8, log.getLastCommittedIndex());
    TermIndex[] entries = log.getEntries(1, 9);
    byte[] actual = new byte[ByteValue.BUFFERSIZE * 8];
    totalSize = 0;
    for (TermIndex e : entries) {
        byte[] eValue = log.get(e.getIndex()).getSmLogEntry().getData().toByteArray();
        Assert.assertEquals(ByteValue.BUFFERSIZE, eValue.length);
        System.arraycopy(eValue, 0, actual, totalSize, eValue.length);
        totalSize += eValue.length;
    }
    Assert.assertArrayEquals(expected, actual);
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) RaftOutputStream(org.apache.ratis.grpc.client.RaftOutputStream) RaftLog(org.apache.ratis.server.storage.RaftLog) TermIndex(org.apache.ratis.server.protocol.TermIndex) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

RaftLog (org.apache.ratis.server.storage.RaftLog)6 Test (org.junit.Test)5 BaseTest (org.apache.ratis.BaseTest)4 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)4 IOException (java.io.IOException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 RaftClient (org.apache.ratis.client.RaftClient)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 MiniRaftCluster (org.apache.ratis.MiniRaftCluster)2 PeerChanges (org.apache.ratis.MiniRaftCluster.PeerChanges)2 RaftOutputStream (org.apache.ratis.grpc.client.RaftOutputStream)2 List (java.util.List)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1