Search in sources :

Example 1 with InvalidProtocolBufferException

use of org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException 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 InvalidProtocolBufferException

use of org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException in project incubator-ratis by apache.

the class FileStoreStateMachine method query.

@Override
public CompletableFuture<Message> query(Message request) {
    final ReadRequestProto proto;
    try {
        proto = ReadRequestProto.parseFrom(request.getContent());
    } catch (InvalidProtocolBufferException e) {
        return FileStoreCommon.completeExceptionally("Failed to parse " + request, e);
    }
    final String path = proto.getPath().toStringUtf8();
    return files.read(path, proto.getOffset(), proto.getLength(), true).thenApply(reply -> Message.valueOf(reply.toByteString()));
}
Also used : ReadRequestProto(org.apache.ratis.proto.ExamplesProtos.ReadRequestProto) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString)

Example 3 with InvalidProtocolBufferException

use of org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException 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 4 with InvalidProtocolBufferException

use of org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException 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 5 with InvalidProtocolBufferException

use of org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException 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)

Aggregations

InvalidProtocolBufferException (org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException)8 ByteString (org.apache.ratis.thirdparty.com.google.protobuf.ByteString)5 FileStoreRequestProto (org.apache.ratis.proto.ExamplesProtos.FileStoreRequestProto)4 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)3 DataStreamRequestByteBuf (org.apache.ratis.netty.server.DataStreamRequestByteBuf)2 WriteRequestHeaderProto (org.apache.ratis.proto.ExamplesProtos.WriteRequestHeaderProto)2 DataStreamPacketHeaderProto (org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto)2 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 StreamSupport (java.util.stream.StreamSupport)1