Search in sources :

Example 1 with StateMachineLogEntryProto

use of org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto 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 2 with StateMachineLogEntryProto

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

the class FileStoreStateMachine method read.

@Override
public CompletableFuture<ByteString> read(LogEntryProto entry) {
    final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry();
    final ByteString data = smLog.getLogData();
    final FileStoreRequestProto proto;
    try {
        proto = FileStoreRequestProto.parseFrom(data);
    } catch (InvalidProtocolBufferException e) {
        return FileStoreCommon.completeExceptionally(entry.getIndex(), "Failed to parse data, entry=" + entry, e);
    }
    if (proto.getRequestCase() != FileStoreRequestProto.RequestCase.WRITEHEADER) {
        return null;
    }
    final WriteRequestHeaderProto h = proto.getWriteHeader();
    CompletableFuture<ExamplesProtos.ReadReplyProto> reply = files.read(h.getPath().toStringUtf8(), h.getOffset(), h.getLength(), false);
    return reply.thenApply(ExamplesProtos.ReadReplyProto::getData);
}
Also used : FileStoreRequestProto(org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) WriteRequestHeaderProto(org.apache.ratis.proto.ExamplesProtos.WriteRequestHeaderProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)

Example 3 with StateMachineLogEntryProto

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

the class FileStoreStateMachine method write.

@Override
public CompletableFuture<Integer> write(LogEntryProto entry) {
    final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry();
    final ByteString data = smLog.getLogData();
    final FileStoreRequestProto proto;
    try {
        proto = FileStoreRequestProto.parseFrom(data);
    } catch (InvalidProtocolBufferException e) {
        return FileStoreCommon.completeExceptionally(entry.getIndex(), "Failed to parse data, entry=" + entry, e);
    }
    if (proto.getRequestCase() != FileStoreRequestProto.RequestCase.WRITEHEADER) {
        return null;
    }
    final WriteRequestHeaderProto h = proto.getWriteHeader();
    final CompletableFuture<Integer> f = files.write(entry.getIndex(), h.getPath().toStringUtf8(), h.getClose(), h.getSync(), h.getOffset(), smLog.getStateMachineEntry().getStateMachineData());
    // sync only if closing the file
    return h.getClose() ? f : null;
}
Also used : FileStoreRequestProto(org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) WriteRequestHeaderProto(org.apache.ratis.proto.ExamplesProtos.WriteRequestHeaderProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)

Example 4 with StateMachineLogEntryProto

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

the class TestLogSegment method testAppendWithGap.

@Test
public void testAppendWithGap() throws Exception {
    LogSegment segment = LogSegment.newOpenSegment(null, 1000, null);
    SimpleOperation op = new SimpleOperation("m");
    final StateMachineLogEntryProto m = op.getLogEntryContent();
    try {
        LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1001);
        segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
        Assert.fail("should fail since the entry's index needs to be 1000");
    } catch (IllegalStateException e) {
    // the exception is expected.
    }
    LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1000);
    segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
    try {
        entry = LogProtoUtils.toLogEntryProto(m, 0, 1002);
        segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
        Assert.fail("should fail since the entry's index needs to be 1001");
    } catch (IllegalStateException e) {
    // the exception is expected.
    }
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 5 with StateMachineLogEntryProto

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

the class DataStreamTestUtils method assertLogEntry.

static void assertLogEntry(LogEntryProto logEntry, RaftClientRequest request) {
    Assert.assertNotNull(logEntry);
    Assert.assertTrue(logEntry.hasStateMachineLogEntry());
    final StateMachineLogEntryProto s = logEntry.getStateMachineLogEntry();
    Assert.assertEquals(StateMachineLogEntryProto.Type.DATASTREAM, s.getType());
    Assert.assertEquals(request.getCallId(), s.getCallId());
    Assert.assertEquals(request.getClientId().toByteString(), s.getClientId());
}
Also used : StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)

Aggregations

StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)5 FileStoreRequestProto (org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto)3 InvalidProtocolBufferException (org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException)3 WriteRequestHeaderProto (org.apache.ratis.proto.ExamplesProtos.WriteRequestHeaderProto)2 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)2 ByteString (org.apache.ratis.thirdparty.com.google.protobuf.ByteString)2 BaseTest (org.apache.ratis.BaseTest)1 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)1 Test (org.junit.Test)1