org.apache.ratis.thirdparty.io.grpc.Metadata Java Examples

The following examples show how to use org.apache.ratis.thirdparty.io.grpc.Metadata. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClientCredentialInterceptor.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> method,
    CallOptions callOptions,
    Channel next) {

  return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
      next.newCall(method, callOptions)) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      if (token != null) {
        headers.put(OBT_METADATA_KEY, token);
      }
      if (user != null) {
        headers.put(USER_METADATA_KEY, user);
      }
      super.start(responseListener, headers);
    }
  };
}
 
Example #2
Source File: GrpcClientInterceptor.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> method, CallOptions callOptions,
    Channel next) {

  return new SimpleForwardingClientCall<ReqT, RespT>(
      next.newCall(method, callOptions)) {

    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {

      Metadata tracingHeaders = new Metadata();
      tracingHeaders.put(TRACING_HEADER, TracingUtil.exportCurrentSpan());

      headers.merge(tracingHeaders);

      super.start(responseListener, headers);
    }
  };
}
 
Example #3
Source File: GrpcServerInterceptor.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(
    ServerCall<ReqT, RespT> call, Metadata headers,
    ServerCallHandler<ReqT, RespT> next) {

  return new SimpleForwardingServerCallListener<ReqT>(
      next.startCall(call, headers)) {
    @Override
    public void onMessage(ReqT message) {
      Span span = TracingUtil
          .importAndCreateSpan(
              call.getMethodDescriptor().getFullMethodName(),
              headers.get(GrpcClientInterceptor.TRACING_HEADER));
      try (Scope scope = GlobalTracer.get().activateSpan(span)) {
        super.onMessage(message);
      } finally {
        span.finish();
      }
    }
  };
}
 
Example #4
Source File: GrpcUtil.java    From ratis with Apache License 2.0 6 votes vote down vote up
static IOException tryUnwrapException(StatusRuntimeException se) {
  final Metadata trailers = se.getTrailers();
  final Status status = se.getStatus();
  if (trailers != null && status != null) {
    final String className = trailers.get(EXCEPTION_TYPE_KEY);
    if (className != null) {
      try {
        Class<?> clazz = Class.forName(className);
        final Exception unwrapped = ReflectionUtils.instantiateException(
            clazz.asSubclass(Exception.class), status.getDescription(), se);
        return IOUtils.asIOException(unwrapped);
      } catch (Exception e) {
        se.addSuppressed(e);
        return new IOException(se);
      }
    }
  }
  return null;
}
 
Example #5
Source File: GrpcUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static StatusRuntimeException wrapException(Throwable t, long callId) {
  t = JavaUtils.unwrapCompletionException(t);
  Metadata trailers = new StatusRuntimeExceptionMetadataBuilder(t)
      .addCallId(callId)
      .build();
  return wrapException(t, trailers);
}
 
Example #6
Source File: GrpcUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #7
Source File: GrpcUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #8
Source File: GrpcUtil.java    From ratis with Apache License 2.0 5 votes vote down vote up
static StatusRuntimeException wrapException(Throwable t, long callId) {
  t = JavaUtils.unwrapCompletionException(t);

  Metadata trailers = new Metadata();
  trailers.put(EXCEPTION_TYPE_KEY, t.getClass().getCanonicalName());
  if (callId > 0) {
    trailers.put(CALL_ID, String.valueOf(callId));
  }
  return new StatusRuntimeException(
      Status.INTERNAL.withCause(t).withDescription(t.getMessage()), trailers);
}
 
Example #9
Source File: GrpcUtil.java    From ratis with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #10
Source File: GrpcUtil.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static StatusRuntimeException wrapException(Throwable t, Metadata trailers) {
  return new StatusRuntimeException(
      Status.INTERNAL.withCause(t).withDescription(t.getMessage()), trailers);
}
 
Example #11
Source File: GrpcUtil.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
Metadata build() {
  return trailers;
}