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());
}
}
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);
}
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);
}
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;
}
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());
}
Aggregations