Search in sources :

Example 21 with SimpleOperation

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

the class TestSegmentedRaftLogCache method prepareLogSegment.

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

Example 22 with SimpleOperation

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

the class TestSegmentedRaftLogCache 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 = LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, 0);
        cache.appendEntry(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
        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 = LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, index);
        cache.appendEntry(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
    }
    Assert.assertNotNull(cache.getOpenSegment());
    checkCache(0, 199, 100);
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Test(org.junit.Test)

Example 23 with SimpleOperation

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

the class TestLogSegment method prepareLog.

File prepareLog(boolean isOpen, long startIndex, int numEntries, long term, boolean isLastEntryPartiallyWritten) throws IOException {
    if (!isOpen) {
        Preconditions.assertTrue(!isLastEntryPartiallyWritten, "For closed log, the last entry cannot be partially written.");
    }
    RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
    final File file = LogSegmentStartEnd.valueOf(startIndex, startIndex + numEntries - 1, isOpen).getFile(storage);
    final LogEntryProto[] entries = new LogEntryProto[numEntries];
    try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(file, false, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
        for (int i = 0; i < entries.length; i++) {
            SimpleOperation op = new SimpleOperation("m" + i);
            entries[i] = LogProtoUtils.toLogEntryProto(op.getLogEntryContent(), term, i + startIndex);
            out.write(entries[i]);
        }
    }
    if (isLastEntryPartiallyWritten) {
        final int entrySize = size(entries[entries.length - 1]);
        final int truncatedEntrySize = ThreadLocalRandom.current().nextInt(entrySize - 1) + 1;
        // 0 < truncatedEntrySize < entrySize
        final long fileLength = file.length();
        final long truncatedFileLength = fileLength - (entrySize - truncatedEntrySize);
        LOG.info("truncate last entry: entry(size={}, truncated={}), file(length={}, truncated={})", entrySize, truncatedEntrySize, fileLength, truncatedFileLength);
        FileUtils.truncateFile(file, truncatedFileLength);
    }
    storage.close();
    return file;
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) RaftStorage(org.apache.ratis.server.storage.RaftStorage) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) File(java.io.File)

Example 24 with SimpleOperation

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

the class TestLogSegment method testAppendWithGap.

@Test
public void testAppendWithGap() throws Exception {
    LogSegment segment = LogSegment.newOpenSegment(null, 1000, null);
    SimpleOperation op = new SimpleOperation("m");
    final StateMachineLogEntryProto m = op.getLogEntryContent();
    try {
        LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1001);
        segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
        Assert.fail("should fail since the entry's index needs to be 1000");
    } catch (IllegalStateException e) {
    // the exception is expected.
    }
    LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1000);
    segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
    try {
        entry = LogProtoUtils.toLogEntryProto(m, 0, 1002);
        segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
        Assert.fail("should fail since the entry's index needs to be 1001");
    } catch (IllegalStateException e) {
    // the exception is expected.
    }
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 25 with SimpleOperation

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

the class TestLogSegment method testAppendEntries.

@Test
public void testAppendEntries() throws Exception {
    final long start = 1000;
    LogSegment segment = LogSegment.newOpenSegment(null, start, null);
    long size = SegmentedRaftLogFormat.getHeaderLength();
    final long max = 8 * 1024 * 1024;
    checkLogSegment(segment, start, start - 1, true, size, 0);
    // append till full
    long term = 0;
    int i = 0;
    while (size < max) {
        SimpleOperation op = new SimpleOperation("m" + i);
        LogEntryProto entry = LogProtoUtils.toLogEntryProto(op.getLogEntryContent(), term, i++ + start);
        size += getEntrySize(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
        segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
    }
    Assert.assertTrue(segment.getTotalFileSize() >= max);
    checkLogSegment(segment, start, i - 1 + start, true, size, term);
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)29 Test (org.junit.Test)18 BaseTest (org.apache.ratis.BaseTest)16 File (java.io.File)14 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)14 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)13 ArrayList (java.util.ArrayList)8 RandomAccessFile (java.io.RandomAccessFile)6 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)6 RaftStorage (org.apache.ratis.server.storage.RaftStorage)6 SMLogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto)6 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 SizeInBytes (org.apache.ratis.util.SizeInBytes)2 ChecksumException (org.apache.ratis.protocol.ChecksumException)1 ChecksumException (org.apache.ratis.protocol.exceptions.ChecksumException)1 SegmentRange (org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog.SegmentRange)1 SegmentRange (org.apache.ratis.server.storage.TestSegmentedRaftLog.SegmentRange)1