Search in sources :

Example 1 with ChecksumException

use of org.apache.ratis.protocol.ChecksumException 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 = new RaftStorage(storageDir, StartupOption.REGULAR);
    File openSegment = storage.getStorageDir().getOpenLogFile(0);
    try (LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize)) {
        for (int i = 0; i < 100; i++) {
            LogEntryProto entry = ProtoUtils.toLogEntryProto(new SimpleOperation("m" + i).getLogEntryContent(), 0, i, clientId, callId);
            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, RaftServerConstants.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.shaded.proto.RaftProtos.LogEntryProto) RandomAccessFile(java.io.RandomAccessFile) ChecksumException(org.apache.ratis.protocol.ChecksumException) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 BaseTest (org.apache.ratis.BaseTest)1 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)1 ChecksumException (org.apache.ratis.protocol.ChecksumException)1 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)1 Test (org.junit.Test)1