Search in sources :

Example 1 with SimpleOperation

use of org.apache.ratis.RaftTestUtil.SimpleOperation 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 2 with SimpleOperation

use of org.apache.ratis.RaftTestUtil.SimpleOperation 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 3 with SimpleOperation

use of org.apache.ratis.RaftTestUtil.SimpleOperation 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 4 with SimpleOperation

use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.

the class TestRaftLogSegment method testTruncate.

@Test
public void testTruncate() throws Exception {
    final long term = 1;
    final long start = 1000;
    LogSegment segment = LogSegment.newOpenSegment(null, start);
    for (int i = 0; i < 100; i++) {
        LogEntryProto entry = ProtoUtils.toLogEntryProto(new SimpleOperation("m" + i).getLogEntryContent(), term, i + start, clientId, callId);
        segment.appendToOpenSegment(entry);
    }
    // truncate an open segment (remove 1080~1099)
    long newSize = segment.getLogRecord(start + 80).getOffset();
    segment.truncate(start + 80);
    Assert.assertEquals(80, segment.numOfEntries());
    checkLogSegment(segment, start, start + 79, false, newSize, term);
    // truncate a closed segment (remove 1050~1079)
    newSize = segment.getLogRecord(start + 50).getOffset();
    segment.truncate(start + 50);
    Assert.assertEquals(50, segment.numOfEntries());
    checkLogSegment(segment, start, start + 49, false, newSize, term);
    // truncate all the remaining entries
    segment.truncate(start);
    Assert.assertEquals(0, segment.numOfEntries());
    checkLogSegment(segment, start, start - 1, false, SegmentedRaftLog.HEADER_BYTES.length, term);
}
Also used : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 5 with SimpleOperation

use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.

the class TestRaftLogSegment method testAppendWithGap.

@Test
public void testAppendWithGap() throws Exception {
    LogSegment segment = LogSegment.newOpenSegment(null, 1000);
    SimpleOperation op = new SimpleOperation("m");
    final SMLogEntryProto m = op.getLogEntryContent();
    try {
        LogEntryProto entry = ProtoUtils.toLogEntryProto(m, 0, 1001, clientId, callId);
        segment.appendToOpenSegment(entry);
        Assert.fail("should fail since the entry's index needs to be 1000");
    } catch (IllegalStateException e) {
    // the exception is expected.
    }
    LogEntryProto entry = ProtoUtils.toLogEntryProto(m, 0, 1000, clientId, callId);
    segment.appendToOpenSegment(entry);
    try {
        entry = ProtoUtils.toLogEntryProto(m, 0, 1002, clientId, callId);
        segment.appendToOpenSegment(entry);
        Assert.fail("should fail since the entry's index needs to be 1001");
    } catch (IllegalStateException e) {
    // the exception is expected.
    }
    LogEntryProto[] entries = new LogEntryProto[2];
    for (int i = 0; i < 2; i++) {
        entries[i] = ProtoUtils.toLogEntryProto(m, 0, 1001 + i * 2, clientId, callId);
    }
    try {
        segment.appendToOpenSegment(entries);
        Assert.fail("should fail since there is gap between entries");
    } catch (IllegalStateException e) {
    // the exception is expected.
    }
}
Also used : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)15 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)14 Test (org.junit.Test)9 BaseTest (org.apache.ratis.BaseTest)8 File (java.io.File)7 SMLogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto)6 ArrayList (java.util.ArrayList)5 RandomAccessFile (java.io.RandomAccessFile)3 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ChecksumException (org.apache.ratis.protocol.ChecksumException)1 SegmentRange (org.apache.ratis.server.storage.TestSegmentedRaftLog.SegmentRange)1 SizeInBytes (org.apache.ratis.util.SizeInBytes)1