Search in sources :

Example 26 with LogEntryProto

use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestRaftLogCache method checkCache.

private void checkCache(long start, long end, int segmentSize) throws IOException {
    Assert.assertEquals(start, cache.getStartIndex());
    Assert.assertEquals(end, cache.getEndIndex());
    for (long index = start; index <= end; index++) {
        LogEntryProto entry = cache.getSegment(index).getEntryWithoutLoading(index).getEntry();
        Assert.assertEquals(index, entry.getIndex());
    }
    long[] offsets = new long[] { start, start + 1, start + (end - start) / 2, end - 1, end };
    for (long offset : offsets) {
        checkCacheEntries(offset, (int) (end - offset + 1), end);
        checkCacheEntries(offset, 1, end);
        checkCacheEntries(offset, 20, end);
        checkCacheEntries(offset, segmentSize, end);
        checkCacheEntries(offset, segmentSize - 1, end);
    }
}
Also used : LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)

Example 27 with LogEntryProto

use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestRaftLogReadWrite method testReadWithPadding.

/**
 * Simulate the scenario that the peer is shutdown without truncating
 * log segment file padding. Make sure the reader can correctly handle this.
 */
@Test
public void testReadWithPadding() throws IOException {
    final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR);
    File openSegment = storage.getStorageDir().getOpenLogFile(0);
    long size = SegmentedRaftLog.HEADER_BYTES.length;
    LogEntryProto[] entries = new LogEntryProto[100];
    LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize);
    size += writeMessages(entries, out);
    out.flush();
    // make sure the file contains padding
    Assert.assertEquals(RaftServerConfigKeys.Log.PREALLOCATED_SIZE_DEFAULT.getSize(), openSegment.length());
    // check if the reader can correctly read the log file
    LogEntryProto[] readEntries = readLog(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true);
    Assert.assertArrayEquals(entries, readEntries);
    out.close();
    Assert.assertEquals(size, openSegment.length());
}
Also used : LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 28 with LogEntryProto

use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto 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 29 with LogEntryProto

use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto 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 30 with LogEntryProto

use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto 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

LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)36 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)16 Test (org.junit.Test)16 BaseTest (org.apache.ratis.BaseTest)14 File (java.io.File)12 ArrayList (java.util.ArrayList)8 SMLogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto)8 IOException (java.io.IOException)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 RandomAccessFile (java.io.RandomAccessFile)5 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)5 List (java.util.List)4 RaftProperties (org.apache.ratis.conf.RaftProperties)4 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)4 TermIndex (org.apache.ratis.server.protocol.TermIndex)4 AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)4 Level (org.apache.log4j.Level)3 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)3 RetryCache (org.apache.ratis.server.impl.RetryCache)3 RetryCacheTestUtil (org.apache.ratis.server.impl.RetryCacheTestUtil)3