Search in sources :

Example 1 with FileStoreRequestProto

use of org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto 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 FileStoreRequestProto

use of org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto 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 FileStoreRequestProto

use of org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto in project incubator-ratis by apache.

the class FileStoreStateMachine method stream.

@Override
public CompletableFuture<DataStream> stream(RaftClientRequest request) {
    final ByteString reqByteString = request.getMessage().getContent();
    final FileStoreRequestProto proto;
    try {
        proto = FileStoreRequestProto.parseFrom(reqByteString);
    } catch (InvalidProtocolBufferException e) {
        return FileStoreCommon.completeExceptionally("Failed to parse stream header", e);
    }
    return files.createDataChannel(proto.getStream().getPath().toStringUtf8()).thenApply(LocalStream::new);
}
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)

Example 4 with FileStoreRequestProto

use of org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto 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 5 with FileStoreRequestProto

use of org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto in project incubator-ratis by apache.

the class FileStoreClient method deleteImpl.

private static <OUTPUT, THROWABLE extends Throwable> OUTPUT deleteImpl(CheckedFunction<ByteString, OUTPUT, THROWABLE> sendFunction, String path) throws THROWABLE {
    final DeleteRequestProto.Builder delete = DeleteRequestProto.newBuilder().setPath(ProtoUtils.toByteString(path));
    final FileStoreRequestProto request = FileStoreRequestProto.newBuilder().setDelete(delete).build();
    return sendFunction.apply(request.toByteString());
}
Also used : DeleteRequestProto(org.apache.ratis.proto.ExamplesProtos.DeleteRequestProto) FileStoreRequestProto(org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto)

Aggregations

FileStoreRequestProto (org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto)8 ByteString (org.apache.ratis.thirdparty.com.google.protobuf.ByteString)4 InvalidProtocolBufferException (org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException)4 StreamWriteRequestProto (org.apache.ratis.proto.ExamplesProtos.StreamWriteRequestProto)3 WriteRequestHeaderProto (org.apache.ratis.proto.ExamplesProtos.WriteRequestHeaderProto)3 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)3 WriteRequestProto (org.apache.ratis.proto.ExamplesProtos.WriteRequestProto)2 DeleteRequestProto (org.apache.ratis.proto.ExamplesProtos.DeleteRequestProto)1 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)1 TransactionContext (org.apache.ratis.statemachine.TransactionContext)1