Search in sources :

Example 1 with Status

use of org.apache.ratis.thirdparty.io.grpc.Status in project incubator-ratis by apache.

the class ClientProto method execProto.

public void execProto(int reps) throws Exception {
    System.out.println("Starting streaming with Protobuffers");
    StreamObserver<TransferMsgProto> requestObserver = asyncStubProto.sendData(new StreamObserver<TransferReplyProto>() {

        @Override
        public void onNext(TransferReplyProto msg) {
            available.release();
            recv[0]++;
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            System.out.println(status);
            System.out.println("Finished streaming with errors");
        }

        @Override
        public void onCompleted() {
            System.out.println("Finished streaming");
        }
    });
    try {
        int i = 0;
        // allocate a byte buffer containing message data.
        ByteBuffer bf = ByteBuffer.allocate(1024 * 1024);
        if (bf.hasArray()) {
            Arrays.fill(bf.array(), (byte) 'a');
        }
        while (i < reps) {
            partId++;
            available.acquire();
            // using unsafewrap operations
            // creates a ByteString refrencing buffer data. Avoids Copying.
            // Something similar is missing in flatbuffers.
            TransferMsgProto msg = TransferMsgProto.newBuilder().setPartId(partId).setData(UnsafeByteOperations.unsafeWrap(bf)).build();
            requestObserver.onNext(msg);
            i++;
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    requestObserver.onCompleted();
    Thread.sleep(1000 * 100);
    if (recv[0] == partId) {
        System.out.println("Transfer Successfull....");
    } else {
        System.out.println("Some error occurred...");
    }
}
Also used : Status(org.apache.ratis.thirdparty.io.grpc.Status) TransferReplyProto(org.apache.ratis.proto.ExperimentsProtos.TransferReplyProto) TransferMsgProto(org.apache.ratis.proto.ExperimentsProtos.TransferMsgProto) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Status

use of org.apache.ratis.thirdparty.io.grpc.Status in project incubator-ratis by apache.

the class ClientFlat method execFlatClient.

public void execFlatClient(int reps) throws Exception {
    System.out.println("Starting streaming with Flatbuffers");
    StreamObserver<TransferMsg> requestObserver = asyncStubFlat.sendData(new StreamObserver<TransferReply>() {

        @Override
        public void onNext(TransferReply msg) {
            recv[0]++;
            available.release();
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            System.out.println(status);
            System.out.println("Finished streaming with errors");
        }

        @Override
        public void onCompleted() {
            System.out.println("Finished streaming");
        }
    });
    try {
        int i = 0;
        // allocate a byte buffer containing message data.
        ByteBuffer bf = ByteBuffer.allocateDirect(1024 * 1024);
        if (bf.hasArray()) {
            Arrays.fill(bf.array(), (byte) 'a');
        }
        while (i < reps) {
            partId++;
            available.acquire();
            // start a builder and reset position of databuffer to 0.
            FlatBufferBuilder builder = new FlatBufferBuilder();
            bf.position(0).limit(bf.capacity());
            // create string of the message data
            // the datacopy happens here, significant CPU time spent.
            int dataOff = builder.createString(bf);
            // Use flatbuffers generated message builder.
            int off = TransferMsg.createTransferMsg(builder, partId, dataOff);
            builder.finish(off);
            TransferMsg msg = TransferMsg.getRootAsTransferMsg(builder.dataBuffer());
            requestObserver.onNext(msg);
            i++;
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    requestObserver.onCompleted();
    Thread.sleep(1000 * 100);
    if (recv[0] == partId) {
        System.out.println("Transfer Successfull....");
    } else {
        System.out.println("Some error occurred...");
    }
}
Also used : Status(org.apache.ratis.thirdparty.io.grpc.Status) TransferReply(org.apache.ratis.flatbufs.TransferReply) FlatBufferBuilder(org.apache.ratis.thirdparty.com.google.flatbuffers.FlatBufferBuilder) TransferMsg(org.apache.ratis.flatbufs.TransferMsg) ByteBuffer(java.nio.ByteBuffer)

Example 3 with Status

use of org.apache.ratis.thirdparty.io.grpc.Status in project incubator-ratis by apache.

the class FileTransferProtobufs method sendData.

@Override
public StreamObserver<TransferMsgProto> sendData(final StreamObserver<TransferReplyProto> responseObserver) {
    return new StreamObserver<TransferMsgProto>() {

        private long rcvdDataSize = 0;

        public long getRcvdDataSize() {
            return rcvdDataSize;
        }

        @Override
        public void onNext(TransferMsgProto msg) {
            rcvdDataSize += msg.getData().size();
            TransferReplyProto rep = TransferReplyProto.newBuilder().setPartId(msg.getPartId()).setMessage("OK").build();
            responseObserver.onNext(rep);
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            System.out.println(status);
            System.out.println("Finished streaming with errors");
        }

        @Override
        public void onCompleted() {
            responseObserver.onCompleted();
        }
    };
}
Also used : StreamObserver(org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver) Status(org.apache.ratis.thirdparty.io.grpc.Status) TransferReplyProto(org.apache.ratis.proto.ExperimentsProtos.TransferReplyProto) TransferMsgProto(org.apache.ratis.proto.ExperimentsProtos.TransferMsgProto)

Example 4 with Status

use of org.apache.ratis.thirdparty.io.grpc.Status in project incubator-ratis by apache.

the class FileTransferFlatbufs method sendData.

@Override
public StreamObserver<TransferMsg> sendData(final StreamObserver<TransferReply> responseObserver) {
    return new StreamObserver<TransferMsg>() {

        private long rcvdDataSize = 0;

        public long getRcvdDataSize() {
            return rcvdDataSize;
        }

        @Override
        public void onNext(TransferMsg msg) {
            rcvdDataSize += msg.dataLength();
            FlatBufferBuilder builder = new FlatBufferBuilder();
            int off = TransferReply.createTransferReply(builder, msg.partId(), builder.createString("OK"));
            builder.finish(off);
            TransferReply rep = TransferReply.getRootAsTransferReply(builder.dataBuffer());
            responseObserver.onNext(rep);
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            System.out.println(status);
            System.out.println("Finished streaming with errors");
        }

        @Override
        public void onCompleted() {
            System.out.println(rcvdDataSize);
            responseObserver.onCompleted();
        }
    };
}
Also used : StreamObserver(org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver) Status(org.apache.ratis.thirdparty.io.grpc.Status) FlatBufferBuilder(org.apache.ratis.thirdparty.com.google.flatbuffers.FlatBufferBuilder) TransferReply(org.apache.ratis.flatbufs.TransferReply) TransferMsg(org.apache.ratis.flatbufs.TransferMsg)

Example 5 with Status

use of org.apache.ratis.thirdparty.io.grpc.Status in project incubator-ratis by apache.

the class GrpcUtil method tryUnwrapException.

static IOException tryUnwrapException(StatusRuntimeException se) {
    final Status status = se.getStatus();
    if (status != null && status.getCode() == Status.Code.DEADLINE_EXCEEDED) {
        return new TimeoutIOException(status.getDescription(), se);
    }
    final Metadata trailers = se.getTrailers();
    if (trailers == null) {
        return null;
    }
    final byte[] bytes = trailers.get(EXCEPTION_OBJECT_KEY);
    if (bytes != null) {
        try {
            return IOUtils.bytes2Object(bytes, IOException.class);
        } catch (Exception e) {
            se.addSuppressed(e);
        }
    }
    if (status != null) {
        final String className = trailers.get(EXCEPTION_TYPE_KEY);
        if (className != null) {
            try {
                final Class<? extends Throwable> clazz = Class.forName(className).asSubclass(Throwable.class);
                final Throwable unwrapped = ReflectionUtils.instantiateException(clazz, status.getDescription());
                return IOUtils.asIOException(unwrapped.initCause(se));
            } catch (Throwable e) {
                se.addSuppressed(e);
                return new IOException(se);
            }
        }
    }
    return null;
}
Also used : Status(org.apache.ratis.thirdparty.io.grpc.Status) Metadata(org.apache.ratis.thirdparty.io.grpc.Metadata) IOException(java.io.IOException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) ServerNotReadyException(org.apache.ratis.protocol.exceptions.ServerNotReadyException) IOException(java.io.IOException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) StatusRuntimeException(org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException)

Aggregations

Status (org.apache.ratis.thirdparty.io.grpc.Status)5 ByteBuffer (java.nio.ByteBuffer)2 TransferMsg (org.apache.ratis.flatbufs.TransferMsg)2 TransferReply (org.apache.ratis.flatbufs.TransferReply)2 TransferMsgProto (org.apache.ratis.proto.ExperimentsProtos.TransferMsgProto)2 TransferReplyProto (org.apache.ratis.proto.ExperimentsProtos.TransferReplyProto)2 FlatBufferBuilder (org.apache.ratis.thirdparty.com.google.flatbuffers.FlatBufferBuilder)2 StreamObserver (org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver)2 IOException (java.io.IOException)1 ServerNotReadyException (org.apache.ratis.protocol.exceptions.ServerNotReadyException)1 TimeoutIOException (org.apache.ratis.protocol.exceptions.TimeoutIOException)1 Metadata (org.apache.ratis.thirdparty.io.grpc.Metadata)1 StatusRuntimeException (org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException)1