Search in sources :

Example 11 with LogEntryProto

use of org.apache.ratis.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 = 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 12 with LogEntryProto

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

Example 13 with LogEntryProto

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

the class TestSegmentedRaftLogCache method checkCache.

private void checkCache(long start, long end, int segmentSize) {
    Assert.assertEquals(start, cache.getStartIndex());
    Assert.assertEquals(end, cache.getEndIndex());
    for (long index = start; index <= end; index++) {
        final LogSegment segment = cache.getSegment(index);
        final LogRecord record = segment.getLogRecord(index);
        final LogEntryProto entry = segment.getEntryFromCache(record.getTermIndex());
        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.proto.RaftProtos.LogEntryProto) LogRecord(org.apache.ratis.server.raftlog.segmented.LogSegment.LogRecord)

Example 14 with LogEntryProto

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

the class FileStoreStateMachine method applyTransaction.

@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
    final LogEntryProto entry = trx.getLogEntry();
    final long index = entry.getIndex();
    updateLastAppliedTermIndex(entry.getTerm(), index);
    final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry();
    final FileStoreRequestProto request;
    try {
        request = FileStoreRequestProto.parseFrom(smLog.getLogData());
    } catch (InvalidProtocolBufferException e) {
        return FileStoreCommon.completeExceptionally(index, "Failed to parse logData in" + smLog, e);
    }
    switch(request.getRequestCase()) {
        case DELETE:
            return delete(index, request.getDelete());
        case WRITEHEADER:
            return writeCommit(index, request.getWriteHeader(), smLog.getStateMachineEntry().getStateMachineData().size());
        case STREAM:
            return streamCommit(request.getStream());
        case WRITE:
        // startTransaction converts WRITE requests to WRITEHEADER requests.
        default:
            LOG.error(getId() + ": Unexpected request case " + request.getRequestCase());
            return FileStoreCommon.completeExceptionally(index, "Unexpected request case " + request.getRequestCase());
    }
}
Also used : FileStoreRequestProto(org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)

Example 15 with LogEntryProto

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

the class RaftSnapshotBaseTest method assertLeaderContent.

public static void assertLeaderContent(MiniRaftCluster cluster) throws Exception {
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final RaftLog leaderLog = leader.getRaftLog();
    final long lastIndex = leaderLog.getLastEntryTermIndex().getIndex();
    final LogEntryProto e = leaderLog.get(lastIndex);
    Assert.assertTrue(e.hasMetadataEntry());
    JavaUtils.attemptRepeatedly(() -> {
        Assert.assertEquals(leaderLog.getLastCommittedIndex() - 1, e.getMetadataEntry().getCommitIndex());
        return null;
    }, 50, BaseTest.HUNDRED_MILLIS, "CheckMetadataEntry", LOG);
    SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing.get(leader);
    Assert.assertTrue("Is not notified as a leader", simpleStateMachine.isNotifiedAsLeader());
    final LogEntryProto[] entries = simpleStateMachine.getContent();
    long message = 0;
    for (int i = 0; i < entries.length; i++) {
        LOG.info("{}) {} {}", i, message, entries[i]);
        if (entries[i].hasStateMachineLogEntry()) {
            final SimpleMessage m = new SimpleMessage("m" + message++);
            Assert.assertArrayEquals(m.getContent().toByteArray(), entries[i].getStateMachineLogEntry().getLogData().toByteArray());
        }
    }
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Aggregations

LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)61 Test (org.junit.Test)22 BaseTest (org.apache.ratis.BaseTest)20 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)15 CompletableFuture (java.util.concurrent.CompletableFuture)14 File (java.io.File)13 IOException (java.io.IOException)13 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)12 RaftLog (org.apache.ratis.server.raftlog.RaftLog)12 RaftStorage (org.apache.ratis.server.storage.RaftStorage)11 ArrayList (java.util.ArrayList)10 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)10 TermIndex (org.apache.ratis.server.protocol.TermIndex)9 LogEntryHeader (org.apache.ratis.server.raftlog.LogEntryHeader)7 RaftClient (org.apache.ratis.client.RaftClient)6 RaftProperties (org.apache.ratis.conf.RaftProperties)6 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)6 RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)6 RaftServer (org.apache.ratis.server.RaftServer)6 RandomAccessFile (java.io.RandomAccessFile)5