Search in sources :

Example 16 with RaftServerImpl

use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.

the class RaftTestUtil method waitForLeader.

static RaftServerImpl waitForLeader(MiniRaftCluster cluster, boolean tolerateMultipleLeaders, RaftGroupId groupId) throws InterruptedException {
    final long sleepTime = (cluster.getMaxTimeout() * 3) >> 1;
    LOG.info(cluster.printServers(groupId));
    RaftServerImpl leader = null;
    for (int i = 0; leader == null && i < 10; i++) {
        Thread.sleep(sleepTime);
        try {
            leader = cluster.getLeader(groupId);
        } catch (IllegalStateException e) {
            if (!tolerateMultipleLeaders) {
                throw e;
            }
        }
    }
    LOG.info(cluster.printServers(groupId));
    return leader;
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl)

Example 17 with RaftServerImpl

use of org.apache.ratis.server.impl.RaftServerImpl 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)

Example 18 with RaftServerImpl

use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.

the class TestRaftStream method testWriteAndFlush.

@Test
public void testWriteAndFlush() throws Exception {
    LOG.info("Running testWriteAndFlush");
    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());
    int[] lengths = new int[] { 1, 500, 1023, 1024, 1025, 2048, 3000, 3072 };
    ByteValue[] values = new ByteValue[lengths.length];
    for (int i = 0; i < values.length; i++) {
        values[i] = new ByteValue(lengths[i], (byte) 9);
    }
    List<byte[]> expectedTxs = new ArrayList<>();
    for (ByteValue v : values) {
        byte[] data = v.genData();
        expectedTxs.addAll(v.getTransactions());
        out.write(data);
        out.flush();
        // make sure after the flush the data has been committed
        Assert.assertEquals(expectedTxs.size(), leader.getState().getLastAppliedIndex());
    }
    out.close();
    try {
        out.write(0);
        fail("The OutputStream has been closed");
    } catch (IOException ignored) {
    }
    LOG.info("Start to check leader's log");
    final AtomicInteger index = new AtomicInteger(0);
    checkLog(leader.getState().getLog(), expectedTxs.size(), () -> expectedTxs.get(index.getAndIncrement()));
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) RaftOutputStream(org.apache.ratis.grpc.client.RaftOutputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 19 with RaftServerImpl

use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.

the class TestSegmentedRaftLog method testAppendEntriesWithInconsistency.

/**
 * Test append with inconsistent entries
 */
@Test
public void testAppendEntriesWithInconsistency() throws Exception {
    // prepare the log for truncation
    List<SegmentRange> ranges = prepareRanges(5, 200, 0);
    List<LogEntryProto> entries = prepareLogEntries(ranges, null);
    RaftServerImpl server = mock(RaftServerImpl.class);
    RetryCache retryCache = RetryCacheTestUtil.createRetryCache();
    when(server.getRetryCache()).thenReturn(retryCache);
    doCallRealMethod().when(server).failClientRequest(any(LogEntryProto.class));
    try (SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, server, storage, -1, properties)) {
        raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
        entries.stream().forEach(entry -> RetryCacheTestUtil.createEntry(retryCache, entry));
        // append entries to the raftlog
        entries.stream().map(raftLog::appendEntry).forEach(CompletableFuture::join);
    }
    // append entries whose first 100 entries are the same with existing log,
    // and the next 100 are with different term
    SegmentRange r1 = new SegmentRange(550, 599, 2, false);
    SegmentRange r2 = new SegmentRange(600, 649, 3, false);
    SegmentRange r3 = new SegmentRange(650, 749, 10, false);
    List<LogEntryProto> newEntries = prepareLogEntries(Arrays.asList(r1, r2, r3), null);
    try (SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, server, storage, -1, properties)) {
        raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
        raftLog.append(newEntries.toArray(new LogEntryProto[newEntries.size()])).forEach(CompletableFuture::join);
        checkFailedEntries(entries, 650, retryCache);
        checkEntries(raftLog, entries, 0, 650);
        checkEntries(raftLog, newEntries, 100, 100);
        Assert.assertEquals(newEntries.get(newEntries.size() - 1), getLastEntry(raftLog));
        Assert.assertEquals(newEntries.get(newEntries.size() - 1).getIndex(), raftLog.getLatestFlushedIndex());
    }
    // load the raftlog again and check
    try (SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, server, storage, -1, properties)) {
        raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
        checkEntries(raftLog, entries, 0, 650);
        checkEntries(raftLog, newEntries, 100, 100);
        Assert.assertEquals(newEntries.get(newEntries.size() - 1), getLastEntry(raftLog));
        Assert.assertEquals(newEntries.get(newEntries.size() - 1).getIndex(), raftLog.getLatestFlushedIndex());
        RaftLogCache cache = raftLog.getRaftLogCache();
        Assert.assertEquals(5, cache.getNumOfSegments());
    }
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) RetryCache(org.apache.ratis.server.impl.RetryCache) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 20 with RaftServerImpl

use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.

the class RaftSnapshotBaseTest method getSnapshotFile.

static File getSnapshotFile(MiniRaftCluster cluster, int i) {
    final RaftServerImpl leader = cluster.getLeader();
    final SimpleStateMachine4Testing sm = SimpleStateMachine4Testing.get(leader.getProxy());
    return sm.getStateMachineStorage().getSnapshotFile(leader.getState().getCurrentTerm(), i);
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl)

Aggregations

RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)20 Test (org.junit.Test)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 BaseTest (org.apache.ratis.BaseTest)7 RaftClient (org.apache.ratis.client.RaftClient)7 RaftLog (org.apache.ratis.server.storage.RaftLog)7 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)7 IOException (java.io.IOException)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 RaftTestUtil (org.apache.ratis.RaftTestUtil)5 RaftProperties (org.apache.ratis.conf.RaftProperties)5 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)5 List (java.util.List)4 Timer (java.util.Timer)4 TimerTask (java.util.TimerTask)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeUnit (java.util.concurrent.TimeUnit)4