Search in sources :

Example 6 with LogEntryProto

use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto 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 int maxCachedNum = RaftServerConfigKeys.Log.maxCachedSegmentNum(prop);
    File storageDir = getTestDir();
    RaftServerConfigKeys.setStorageDir(prop, storageDir);
    RaftStorage storage = new RaftStorage(storageDir, RaftServerConstants.StartupOption.REGULAR);
    RaftServerImpl server = Mockito.mock(RaftServerImpl.class);
    ServerState state = Mockito.mock(ServerState.class);
    Mockito.when(server.getState()).thenReturn(state);
    Mockito.when(server.getFollowerNextIndices()).thenReturn(new long[] {});
    Mockito.when(state.getLastAppliedIndex()).thenReturn(0L);
    SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, server, storage, -1, prop);
    raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
    List<SegmentRange> slist = TestSegmentedRaftLog.prepareRanges(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(server.getFollowerNextIndices()).thenReturn(new long[] { 21, 40, 40 });
    Mockito.when(state.getLastAppliedIndex()).thenReturn(35L);
    slist = TestSegmentedRaftLog.prepareRanges(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.storage.TestSegmentedRaftLog.SegmentRange) ServerState(org.apache.ratis.server.impl.ServerState) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 7 with LogEntryProto

use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestRaftLogCache method prepareLogSegment.

private LogSegment prepareLogSegment(long start, long end, boolean isOpen) {
    LogSegment s = LogSegment.newOpenSegment(null, start);
    for (long i = start; i <= end; i++) {
        SimpleOperation m = new SimpleOperation("m" + i);
        LogEntryProto entry = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId);
        s.appendToOpenSegment(entry);
    }
    if (!isOpen) {
        s.close();
    }
    return s;
}
Also used : LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation)

Example 8 with LogEntryProto

use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestRaftLogCache method testAppendEntry.

@Test
public void testAppendEntry() throws Exception {
    LogSegment closedSegment = prepareLogSegment(0, 99, false);
    cache.addSegment(closedSegment);
    final SimpleOperation m = new SimpleOperation("m");
    try {
        LogEntryProto entry = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, 0, clientId, callId);
        cache.appendEntry(entry);
        Assert.fail("the open segment is null");
    } catch (IllegalStateException ignored) {
    }
    LogSegment openSegment = prepareLogSegment(100, 100, true);
    cache.addSegment(openSegment);
    for (long index = 101; index < 200; index++) {
        LogEntryProto entry = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, index, clientId, callId);
        cache.appendEntry(entry);
    }
    Assert.assertNotNull(cache.getOpenSegment());
    checkCache(0, 199, 100);
}
Also used : LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Test(org.junit.Test)

Example 9 with LogEntryProto

use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestRaftLogReadWrite method testAppendLog.

@Test
public void testAppendLog() throws IOException {
    final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR);
    File openSegment = storage.getStorageDir().getOpenLogFile(0);
    LogEntryProto[] entries = new LogEntryProto[200];
    try (LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize)) {
        for (int i = 0; i < 100; i++) {
            SimpleOperation m = new SimpleOperation("m" + i);
            entries[i] = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId);
            out.write(entries[i]);
        }
    }
    try (LogOutputStream out = new LogOutputStream(openSegment, true, segmentMaxSize, preallocatedSize, bufferSize)) {
        for (int i = 100; i < 200; i++) {
            SimpleOperation m = new SimpleOperation("m" + i);
            entries[i] = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId);
            out.write(entries[i]);
        }
    }
    LogEntryProto[] readEntries = readLog(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true);
    Assert.assertArrayEquals(entries, readEntries);
    storage.close();
}
Also used : LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 10 with LogEntryProto

use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestRaftLogReadWrite method testReadWriteLog.

/**
 * Test basic functionality: write several log entries, then read
 */
@Test
public void testReadWriteLog() throws IOException {
    final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR);
    File openSegment = storage.getStorageDir().getOpenLogFile(0);
    long size = SegmentedRaftLog.HEADER_BYTES.length;
    final LogEntryProto[] entries = new LogEntryProto[100];
    try (LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize)) {
        size += writeMessages(entries, out);
    } finally {
        storage.close();
    }
    Assert.assertEquals(size, openSegment.length());
    LogEntryProto[] readEntries = readLog(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true);
    Assert.assertArrayEquals(entries, readEntries);
}
Also used : LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)36 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)16 Test (org.junit.Test)16 BaseTest (org.apache.ratis.BaseTest)14 File (java.io.File)12 ArrayList (java.util.ArrayList)8 SMLogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto)8 IOException (java.io.IOException)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 RandomAccessFile (java.io.RandomAccessFile)5 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)5 List (java.util.List)4 RaftProperties (org.apache.ratis.conf.RaftProperties)4 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)4 TermIndex (org.apache.ratis.server.protocol.TermIndex)4 AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)4 Level (org.apache.log4j.Level)3 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)3 RetryCache (org.apache.ratis.server.impl.RetryCache)3 RetryCacheTestUtil (org.apache.ratis.server.impl.RetryCacheTestUtil)3