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