Search in sources :

Example 11 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(ProtoUtils.toLogEntryProto(m.getLogEntryContent(), range.term, index, clientId, callId));
        }
    }
    return eList.toArray(new LogEntryProto[eList.size()]);
}
Also used : SegmentRange(org.apache.ratis.server.storage.TestSegmentedRaftLog.SegmentRange) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) ArrayList(java.util.ArrayList) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation)

Example 12 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 = 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)

Example 13 with SimpleOperation

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

the class TestRaftLogReadWrite method writeMessages.

private long writeMessages(LogEntryProto[] entries, LogOutputStream out) throws IOException {
    long size = 0;
    for (int i = 0; i < entries.length; i++) {
        SimpleOperation m = new SimpleOperation("m" + i);
        entries[i] = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId);
        final int s = entries[i].getSerializedSize();
        size += CodedOutputStream.computeUInt32SizeNoTag(s) + s + 4;
        out.write(entries[i]);
    }
    return size;
}
Also used : SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation)

Example 14 with SimpleOperation

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

the class TestRaftLogReadWrite method testReadWithCorruptPadding.

/**
 * corrupt the padding by inserting non-zero bytes. Make sure the reader
 * throws exception.
 */
@Test
public void testReadWithCorruptPadding() throws IOException {
    final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR);
    File openSegment = storage.getStorageDir().getOpenLogFile(0);
    LogEntryProto[] entries = new LogEntryProto[10];
    LogOutputStream out = new LogOutputStream(openSegment, false, 16 * 1024 * 1024, 4 * 1024 * 1024, bufferSize);
    for (int i = 0; i < 10; i++) {
        SimpleOperation m = new SimpleOperation("m" + i);
        entries[i] = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId);
        out.write(entries[i]);
    }
    out.flush();
    // make sure the file contains padding
    Assert.assertEquals(4 * 1024 * 1024, openSegment.length());
    try (FileOutputStream fout = new FileOutputStream(openSegment, true)) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[] { -1, 1 });
        fout.getChannel().write(byteBuffer, 16 * 1024 * 1024 - 10);
    }
    List<LogEntryProto> list = new ArrayList<>();
    try (LogInputStream in = new LogInputStream(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true)) {
        LogEntryProto entry;
        while ((entry = in.nextEntry()) != null) {
            list.add(entry);
        }
        Assert.fail("should fail since we corrupt the padding");
    } catch (IOException e) {
        boolean findVerifyTerminator = false;
        for (StackTraceElement s : e.getStackTrace()) {
            if (s.getMethodName().equals("verifyTerminator")) {
                findVerifyTerminator = true;
                break;
            }
        }
        Assert.assertTrue(findVerifyTerminator);
    }
    Assert.assertArrayEquals(entries, list.toArray(new LogEntryProto[list.size()]));
}
Also used : ArrayList(java.util.ArrayList) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 15 with SimpleOperation

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

the class TestRaftLogSegment method testAppendEntries.

@Test
public void testAppendEntries() throws Exception {
    final long start = 1000;
    LogSegment segment = LogSegment.newOpenSegment(null, start);
    long size = SegmentedRaftLog.HEADER_BYTES.length;
    final long max = 8 * 1024 * 1024;
    checkLogSegment(segment, start, start - 1, true, size, 0);
    // append till full
    long term = 0;
    int i = 0;
    List<LogEntryProto> list = new ArrayList<>();
    while (size < max) {
        SimpleOperation op = new SimpleOperation("m" + i);
        LogEntryProto entry = ProtoUtils.toLogEntryProto(op.getLogEntryContent(), term, i++ + start, clientId, callId);
        size += getEntrySize(entry);
        list.add(entry);
    }
    segment.appendToOpenSegment(list.toArray(new LogEntryProto[list.size()]));
    Assert.assertTrue(segment.getTotalSize() >= max);
    checkLogSegment(segment, start, i - 1 + start, true, size, term);
}
Also used : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) ArrayList(java.util.ArrayList) 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