Search in sources :

Example 6 with SimpleOperation

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

the class TestRaftLogSegment method prepareLog.

private File prepareLog(boolean isOpen, long start, int size, long term) throws IOException {
    RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR);
    File file = isOpen ? storage.getStorageDir().getOpenLogFile(start) : storage.getStorageDir().getClosedLogFile(start, start + size - 1);
    LogEntryProto[] entries = new LogEntryProto[size];
    try (LogOutputStream out = new LogOutputStream(file, false, segmentMaxSize, preallocatedSize, bufferSize)) {
        for (int i = 0; i < size; i++) {
            SimpleOperation op = new SimpleOperation("m" + i);
            entries[i] = ProtoUtils.toLogEntryProto(op.getLogEntryContent(), term, i + start, clientId, callId);
            out.write(entries[i]);
        }
    }
    storage.close();
    return file;
}
Also used : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) File(java.io.File)

Example 7 with SimpleOperation

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

the class TestSegmentedRaftLog method prepareLogEntries.

List<LogEntryProto> prepareLogEntries(List<SegmentRange> slist, Supplier<String> stringSupplier) {
    List<LogEntryProto> eList = new ArrayList<>();
    for (SegmentRange range : slist) {
        for (long index = range.start; index <= range.end; index++) {
            SimpleOperation m = stringSupplier == null ? new SimpleOperation("m" + index) : new SimpleOperation(stringSupplier.get());
            eList.add(ProtoUtils.toLogEntryProto(m.getLogEntryContent(), range.term, index, clientId, index));
        }
    }
    return eList;
}
Also used : LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) ArrayList(java.util.ArrayList) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation)

Example 8 with SimpleOperation

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

the class TestCacheEviction method generateEntries.

private LogEntryProto[] generateEntries(List<SegmentRange> slist) {
    List<LogEntryProto> eList = new ArrayList<>();
    for (SegmentRange range : slist) {
        for (long index = range.start; index <= range.end; index++) {
            SimpleOperation m = new SimpleOperation(new String(new byte[1024]));
            eList.add(LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), range.term, index));
        }
    }
    return eList.toArray(new LogEntryProto[eList.size()]);
}
Also used : SegmentRange(org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog.SegmentRange) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) ArrayList(java.util.ArrayList) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation)

Example 9 with SimpleOperation

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

the class TestRaftLogReadWrite method testReadWithEntryCorruption.

/**
 * Test the log reader to make sure it can detect the checksum mismatch.
 */
@Test
public void testReadWithEntryCorruption() throws IOException {
    RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
    final File openSegment = LogSegmentStartEnd.valueOf(0).getFile(storage);
    try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
        for (int i = 0; i < 100; i++) {
            LogEntryProto entry = LogProtoUtils.toLogEntryProto(new SimpleOperation("m" + i).getLogEntryContent(), 0, i);
            out.write(entry);
        }
    } finally {
        storage.close();
    }
    // corrupt the log file
    try (RandomAccessFile raf = new RandomAccessFile(openSegment.getCanonicalFile(), "rw")) {
        raf.seek(100);
        int correctValue = raf.read();
        raf.seek(100);
        raf.write(correctValue + 1);
    }
    try {
        readLog(openSegment, 0, RaftLog.INVALID_LOG_INDEX, true);
        Assert.fail("The read of corrupted log file should fail");
    } catch (ChecksumException e) {
        LOG.info("Caught ChecksumException as expected", e);
    }
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftStorage(org.apache.ratis.server.storage.RaftStorage) RandomAccessFile(java.io.RandomAccessFile) ChecksumException(org.apache.ratis.protocol.exceptions.ChecksumException) 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 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 = RaftStorageTestUtils.newRaftStorage(storageDir);
    final File openSegment = LogSegmentStartEnd.valueOf(0).getFile(storage);
    LogEntryProto[] entries = new LogEntryProto[200];
    try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
        for (int i = 0; i < 100; i++) {
            SimpleOperation m = new SimpleOperation("m" + i);
            entries[i] = LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i);
            out.write(entries[i]);
        }
    }
    try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(openSegment, true, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
        for (int i = 100; i < 200; i++) {
            SimpleOperation m = new SimpleOperation("m" + i);
            entries[i] = LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i);
            out.write(entries[i]);
        }
    }
    final LogEntryProto[] readEntries = readLog(openSegment, 0, RaftLog.INVALID_LOG_INDEX, true);
    Assert.assertArrayEquals(entries, readEntries);
    storage.close();
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftStorage(org.apache.ratis.server.storage.RaftStorage) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) 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