use of org.apache.ratis.thirdparty.io.grpc.Metadata in project incubator-ratis by apache.
the class GrpcUtil method getCallId.
static long getCallId(Throwable t) {
if (t instanceof StatusRuntimeException) {
final Metadata trailers = ((StatusRuntimeException) t).getTrailers();
String callId = trailers.get(CALL_ID);
return callId != null ? Integer.parseInt(callId) : -1;
}
return -1;
}
use of org.apache.ratis.thirdparty.io.grpc.Metadata in project incubator-ratis by apache.
the class GrpcUtil method wrapException.
static StatusRuntimeException wrapException(Throwable t, long callId) {
t = JavaUtils.unwrapCompletionException(t);
Metadata trailers = new StatusRuntimeExceptionMetadataBuilder(t).addCallId(callId).build();
return wrapException(t, trailers);
}
use of org.apache.ratis.thirdparty.io.grpc.Metadata 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;
}
use of org.apache.ratis.thirdparty.io.grpc.Metadata in project incubator-ratis by apache.
the class GrpcUtil method wrapException.
static StatusRuntimeException wrapException(Throwable t, long callId, boolean isHeartbeat) {
t = JavaUtils.unwrapCompletionException(t);
Metadata trailers = new StatusRuntimeExceptionMetadataBuilder(t).addCallId(callId).addIsHeartbeat(isHeartbeat).build();
return wrapException(t, trailers);
}
use of org.apache.ratis.thirdparty.io.grpc.Metadata in project incubator-ratis by apache.
the class GrpcUtil method isHeartbeat.
static boolean isHeartbeat(Throwable t) {
if (t instanceof StatusRuntimeException) {
final Metadata trailers = ((StatusRuntimeException) t).getTrailers();
String isHeartbeat = trailers != null ? trailers.get(HEARTBEAT) : null;
return isHeartbeat != null && Boolean.valueOf(isHeartbeat);
}
return false;
}
Aggregations