io.grpc.ForwardingClientCall.SimpleForwardingClientCall Java Examples

The following examples show how to use io.grpc.ForwardingClientCall.SimpleForwardingClientCall. 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: CensusStatsModule.java    From grpc-nebula-java 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) {
  // New RPCs on client-side inherit the tag context from the current Context.
  TagContext parentCtx = tagger.getCurrentTagContext();
  final ClientCallTracer tracerFactory =
      newClientCallTracer(parentCtx, method.getFullMethodName(),
          recordStartedRpcs, recordFinishedRpcs);
  ClientCall<ReqT, RespT> call =
      next.newCall(method, callOptions.withStreamTracerFactory(tracerFactory));
  return new SimpleForwardingClientCall<ReqT, RespT>(call) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      delegate().start(
          new SimpleForwardingClientCallListener<RespT>(responseListener) {
            @Override
            public void onClose(Status status, Metadata trailers) {
              tracerFactory.callEnded(status);
              super.onClose(status, trailers);
            }
          },
          headers);
    }
  };
}
 
Example #2
Source File: BaseITTracingClientInterceptor.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void userInterceptor_throwsOnHalfClose() {
  closeClient(client);
  client = newClient(new ClientInterceptor() {
    @Override public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions,
        Channel channel) {
      ClientCall<ReqT, RespT> call = channel.newCall(methodDescriptor, callOptions);
      return new SimpleForwardingClientCall<ReqT, RespT>(call) {
        @Override public void halfClose() {
          throw new IllegalStateException("I'm a bad interceptor.");
        }
      };
    }
  }, grpcTracing.newClientInterceptor());

  assertThatThrownBy(() -> GreeterGrpc.newBlockingStub(client).sayHello(HELLO_REQUEST))
      .isInstanceOf(IllegalStateException.class);
  testSpanHandler.takeRemoteSpanWithErrorMessage(CLIENT, "I'm a bad interceptor.");
}
 
Example #3
Source File: BaseITTracingClientInterceptor.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void userInterceptor_throwsOnStart() {
  closeClient(client);
  client = newClient(new ClientInterceptor() {
    @Override public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions,
        Channel channel) {
      ClientCall<ReqT, RespT> call = channel.newCall(methodDescriptor, callOptions);
      return new SimpleForwardingClientCall<ReqT, RespT>(call) {
        @Override public void start(Listener<RespT> responseListener, Metadata headers) {
          throw new IllegalStateException("I'm a bad interceptor.");
        }
      };
    }
  }, grpcTracing.newClientInterceptor());

  assertThatThrownBy(() -> GreeterGrpc.newBlockingStub(client).sayHello(HELLO_REQUEST))
      .isInstanceOf(IllegalStateException.class);
  testSpanHandler.takeRemoteSpanWithErrorMessage(CLIENT, "I'm a bad interceptor.");
}
 
Example #4
Source File: ClientCallsTest.java    From grpc-java 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(ClientCall.Listener<RespT> listener, Metadata headers) {
      super.start(new SimpleForwardingClientCallListener<RespT>(listener) {
        @Override public void onClose(Status status, Metadata trailers) {
          onCloseCalled = true;
          super.onClose(status, trailers);
        }
      }, headers);
    }

    @Override public void halfClose() {
      Thread.currentThread().interrupt();
      super.halfClose();
    }
  };
}
 
Example #5
Source File: CensusStatsModule.java    From grpc-java 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) {
  // New RPCs on client-side inherit the tag context from the current Context.
  TagContext parentCtx = tagger.getCurrentTagContext();
  final ClientCallTracer tracerFactory =
      newClientCallTracer(parentCtx, method.getFullMethodName());
  ClientCall<ReqT, RespT> call =
      next.newCall(method, callOptions.withStreamTracerFactory(tracerFactory));
  return new SimpleForwardingClientCall<ReqT, RespT>(call) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      delegate().start(
          new SimpleForwardingClientCallListener<RespT>(responseListener) {
            @Override
            public void onClose(Status status, Metadata trailers) {
              tracerFactory.callEnded(status);
              super.onClose(status, trailers);
            }
          },
          headers);
    }
  };
}
 
Example #6
Source File: AltusMetadataInterceptor.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Override
public <R, S> ClientCall<R, S> interceptCall(
        MethodDescriptor<R, S> method,
        CallOptions callOptions,
        Channel next) {
    return new SimpleForwardingClientCall<R, S>(
            next.newCall(method, callOptions)) {
        @Override
        public void start(
                Listener<S> responseListener,
                Metadata headers) {
            Metadata metadata = new Metadata();
            metadata.put(REQUEST_ID_METADATA_KEY, requestId);
            metadata.put(ACTOR_CRN_METADATA_KEY, actorCrn);
            headers.merge(metadata);
            super.start(responseListener, headers);
        }
    };
}
 
Example #7
Source File: HeaderServerInterceptorTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void serverHeaderDeliveredToClient() {
  class SpyingClientInterceptor implements ClientInterceptor {
    ClientCall.Listener<?> spyListener;

    @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) {
          spyListener = responseListener =
              mock(ClientCall.Listener.class, delegatesTo(responseListener));
          super.start(responseListener, headers);
        }
      };
    }
  }

  SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor();
  GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(channel)
      .withInterceptors(clientInterceptor);
  ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);

  blockingStub.sayHello(HelloRequest.getDefaultInstance());

  assertNotNull(clientInterceptor.spyListener);
  verify(clientInterceptor.spyListener).onHeaders(metadataCaptor.capture());
  assertEquals(
      "customRespondValue",
      metadataCaptor.getValue().get(HeaderServerInterceptor.CUSTOM_HEADER_KEY));
}
 
Example #8
Source File: SocketIdClientInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
    final ClientCall<ReqT, RespT> clientCall = next.newCall(method, callOptions);
    final ClientCall<ReqT, RespT> forwardingClientCall = new SimpleForwardingClientCall<ReqT, RespT>(clientCall) {
        @Override
        public void start(Listener<RespT> responseListener, Metadata headers) {
            final String socketId = nextSocketId();
            headers.put(Header.SOCKET_ID, socketId);
            super.start(responseListener, headers);
        }

    };
    return forwardingClientCall;
}
 
Example #9
Source File: ClientConnectionManager.java    From jetcd with Apache License 2.0 5 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) {
            String token = getToken(next);
            if (token != null) {
                headers.put(TOKEN, token);
            }
            super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
                @Override
                public void onClose(Status status, Metadata trailers) {
                    if (isInvalidTokenError(status)) {
                        try {
                            refreshToken(next);
                        } catch (Exception e) {
                            // don't throw any error here.
                            // rpc will retry on expired auth token.
                        }
                    }
                    super.onClose(status, trailers);
                }
            }, headers);
        }
    };
}
 
Example #10
Source File: HeaderServerInterceptorTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void serverHeaderDeliveredToClient() {
  class SpyingClientInterceptor implements ClientInterceptor {
    ClientCall.Listener<?> spyListener;

    @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) {
          spyListener = responseListener =
              mock(ClientCall.Listener.class, delegatesTo(responseListener));
          super.start(responseListener, headers);
        }
      };
    }
  }

  SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor();
  GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(channel)
      .withInterceptors(clientInterceptor);
  ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);

  blockingStub.sayHello(HelloRequest.getDefaultInstance());

  assertNotNull(clientInterceptor.spyListener);
  verify(clientInterceptor.spyListener).onHeaders(metadataCaptor.capture());
  assertEquals(
      "customRespondValue",
      metadataCaptor.getValue().get(HeaderServerInterceptor.CUSTOM_HEADER_KEY));
}
 
Example #11
Source File: ClientInterceptorsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void addOutboundHeaders() {
  final Metadata.Key<String> credKey = Metadata.Key.of("Cred", Metadata.ASCII_STRING_MARSHALLER);
  ClientInterceptor interceptor = new ClientInterceptor() {
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method,
        CallOptions callOptions,
        Channel next) {
      ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
      return new SimpleForwardingClientCall<ReqT, RespT>(call) {
        @Override
        public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
          headers.put(credKey, "abcd");
          super.start(responseListener, headers);
        }
      };
    }
  };
  Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
  @SuppressWarnings("unchecked")
  ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
  ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
  // start() on the intercepted call will eventually reach the call created by the real channel
  interceptedCall.start(listener, new Metadata());
  // The headers passed to the real channel call will contain the information inserted by the
  // interceptor.
  assertSame(listener, call.listener);
  assertEquals("abcd", call.headers.get(credKey));
}
 
Example #12
Source File: ClientInterceptorsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void normalCall() {
  ClientInterceptor interceptor = new ClientInterceptor() {
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method,
        CallOptions callOptions,
        Channel next) {
      ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
      return new SimpleForwardingClientCall<ReqT, RespT>(call) { };
    }
  };
  Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
  ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
  assertNotSame(call, interceptedCall);
  @SuppressWarnings("unchecked")
  ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
  Metadata headers = new Metadata();
  interceptedCall.start(listener, headers);
  assertSame(listener, call.listener);
  assertSame(headers, call.headers);
  interceptedCall.sendMessage(null /*request*/);
  assertThat(call.messages).containsExactly((Void) null /*request*/);
  interceptedCall.halfClose();
  assertTrue(call.halfClosed);
  interceptedCall.request(1);
  assertThat(call.requests).containsExactly(1);
}
 
Example #13
Source File: ClientInterceptorsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void normalCall() {
  ClientInterceptor interceptor = new ClientInterceptor() {
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method,
        CallOptions callOptions,
        Channel next) {
      ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
      return new SimpleForwardingClientCall<ReqT, RespT>(call) { };
    }
  };
  Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
  ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
  assertNotSame(call, interceptedCall);
  @SuppressWarnings("unchecked")
  ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
  Metadata headers = new Metadata();
  interceptedCall.start(listener, headers);
  assertSame(listener, call.listener);
  assertSame(headers, call.headers);
  interceptedCall.sendMessage(null /*request*/);
  assertThat(call.messages).containsExactly((Void) null /*request*/);
  interceptedCall.halfClose();
  assertTrue(call.halfClosed);
  interceptedCall.request(1);
  assertThat(call.requests).containsExactly(1);
}
 
Example #14
Source File: CensusTracingModule.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
  // New RPCs on client-side inherit the tracing context from the current Context.
  // Safe usage of the unsafe trace API because CONTEXT_SPAN_KEY.get() returns the same value
  // as Tracer.getCurrentSpan() except when no value available when the return value is null
  // for the direct access and BlankSpan when Tracer API is used.
  final ClientCallTracer tracerFactory =
      newClientCallTracer(ContextUtils.getValue(Context.current()), method);
  ClientCall<ReqT, RespT> call =
      next.newCall(
          method,
          callOptions.withStreamTracerFactory(tracerFactory));
  return new SimpleForwardingClientCall<ReqT, RespT>(call) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      delegate().start(
          new SimpleForwardingClientCallListener<RespT>(responseListener) {
            @Override
            public void onClose(io.grpc.Status status, Metadata trailers) {
              tracerFactory.callEnded(status);
              super.onClose(status, trailers);
            }
          },
          headers);
    }
  };
}
 
Example #15
Source File: ClientInterceptorsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void addOutboundHeaders() {
  final Metadata.Key<String> credKey = Metadata.Key.of("Cred", Metadata.ASCII_STRING_MARSHALLER);
  ClientInterceptor interceptor = new ClientInterceptor() {
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method,
        CallOptions callOptions,
        Channel next) {
      ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
      return new SimpleForwardingClientCall<ReqT, RespT>(call) {
        @Override
        public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
          headers.put(credKey, "abcd");
          super.start(responseListener, headers);
        }
      };
    }
  };
  Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
  @SuppressWarnings("unchecked")
  ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
  ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
  // start() on the intercepted call will eventually reach the call created by the real channel
  interceptedCall.start(listener, new Metadata());
  // The headers passed to the real channel call will contain the information inserted by the
  // interceptor.
  assertSame(listener, call.listener);
  assertEquals("abcd", call.headers.get(credKey));
}
 
Example #16
Source File: BaseITTracingServerInterceptor.java    From brave with Apache License 2.0 5 votes vote down vote up
Channel clientWithB3SingleHeader(TraceContext parent) {
  return ClientInterceptors.intercept(client, new ClientInterceptor() {
    @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) {
          headers.put(Key.of("b3", ASCII_STRING_MARSHALLER),
              B3SingleFormat.writeB3SingleFormat(parent));
          super.start(responseListener, headers);
        }
      };
    }
  });
}
 
Example #17
Source File: BaseITTracingClientInterceptor.java    From brave with Apache License 2.0 5 votes vote down vote up
/**
 * NOTE: for this to work, the tracing interceptor must be last (so that it executes first)
 *
 * <p>Also notice that we are only making the current context available in the request side.
 */
@Test public void currentSpanVisibleToUserInterceptors() {
  closeClient(client);

  client = newClient(
      new ClientInterceptor() {
        @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) {
              tracing.tracer().currentSpanCustomizer().annotate("start");
              super.start(responseListener, headers);
            }

            @Override public void sendMessage(ReqT message) {
              tracing.tracer().currentSpanCustomizer().annotate("sendMessage");
              super.sendMessage(message);
            }
          };
        }
      },
      grpcTracing.newClientInterceptor()
  );

  GreeterGrpc.newBlockingStub(client).sayHello(HELLO_REQUEST);

  assertThat(testSpanHandler.takeRemoteSpan(CLIENT).annotations())
      .extracting(Entry::getValue)
      .containsOnly("start", "sendMessage");
}
 
Example #18
Source File: CensusTracingModule.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
  // New RPCs on client-side inherit the tracing context from the current Context.
  // Safe usage of the unsafe trace API because CONTEXT_SPAN_KEY.get() returns the same value
  // as Tracer.getCurrentSpan() except when no value available when the return value is null
  // for the direct access and BlankSpan when Tracer API is used.
  final ClientCallTracer tracerFactory = newClientCallTracer(CONTEXT_SPAN_KEY.get(), method);
  ClientCall<ReqT, RespT> call =
      next.newCall(
          method,
          callOptions.withStreamTracerFactory(tracerFactory));
  return new SimpleForwardingClientCall<ReqT, RespT>(call) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      delegate().start(
          new SimpleForwardingClientCallListener<RespT>(responseListener) {
            @Override
            public void onClose(io.grpc.Status status, Metadata trailers) {
              tracerFactory.callEnded(status);
              super.onClose(status, trailers);
            }
          },
          headers);
    }
  };
}
 
Example #19
Source File: StorageStubProvider.java    From hadoop-connectors with Apache License 2.0 4 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions, Channel channel) {
  ClientCall<ReqT, RespT> newCall = channel.newCall(methodDescriptor, callOptions);
  final AtomicBoolean countedCancel = new AtomicBoolean(false);

  // A streaming call might be terminated in one of several possible ways:
  // * The call completes normally -> onClose() will be invoked.
  // * The context is cancelled -> CancellationListener.cancelled() will be called.
  // * The call itself is cancelled (doesn't currently happen) -> ClientCall.cancel() called.
  //
  // It's possible more than one of these could happen, so we use countedCancel to make sure we
  // don't double count a decrement.
  Context.current()
      .addListener(
          context -> {
            if (countedCancel.compareAndSet(false, true)) {
              ongoingRequestCount.decrementAndGet();
            }
          },
          backgroundTasksThreadPool);

  return new SimpleForwardingClientCall(newCall) {
    @Override
    public void cancel(@Nullable String message, @Nullable Throwable cause) {
      if (countedCancel.compareAndSet(false, true)) {
        ongoingRequestCount.decrementAndGet();
      }
      super.cancel(message, cause);
    }

    @Override
    public void start(Listener responseListener, Metadata headers) {
      ongoingRequestCount.incrementAndGet();
      this.delegate()
          .start(
              new SimpleForwardingClientCallListener(responseListener) {
                @Override
                public void onClose(Status status, Metadata trailers) {
                  if (countedCancel.compareAndSet(false, true)) {
                    ongoingRequestCount.decrementAndGet();
                  }
                  super.onClose(status, trailers);
                }
              },
              headers);
    }
  };
}