Search in sources :

Example 11 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project grpc-java by grpc.

the class XdsTestClient method runQps.

private void runQps() throws InterruptedException, ExecutionException {
    final SettableFuture<Void> failure = SettableFuture.create();
    final class PeriodicRpc implements Runnable {

        @Override
        public void run() {
            List<RpcConfig> configs = rpcConfigs;
            for (RpcConfig cfg : configs) {
                makeRpc(cfg);
            }
        }

        private void makeRpc(final RpcConfig config) {
            final long requestId;
            final Set<XdsStatsWatcher> savedWatchers = new HashSet<>();
            synchronized (lock) {
                currentRequestId += 1;
                requestId = currentRequestId;
                savedWatchers.addAll(watchers);
            }
            ManagedChannel channel = channels.get((int) (requestId % channels.size()));
            TestServiceGrpc.TestServiceStub stub = TestServiceGrpc.newStub(channel);
            final AtomicReference<ClientCall<?, ?>> clientCallRef = new AtomicReference<>();
            final AtomicReference<String> hostnameRef = new AtomicReference<>();
            stub = stub.withDeadlineAfter(config.timeoutSec, TimeUnit.SECONDS).withInterceptors(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);
                    clientCallRef.set(call);
                    return new SimpleForwardingClientCall<ReqT, RespT>(call) {

                        @Override
                        public void start(Listener<RespT> responseListener, Metadata headers) {
                            headers.merge(config.metadata);
                            super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {

                                @Override
                                public void onHeaders(Metadata headers) {
                                    hostnameRef.set(headers.get(XdsTestServer.HOSTNAME_KEY));
                                    super.onHeaders(headers);
                                }
                            }, headers);
                        }
                    };
                }
            });
            if (config.rpcType == RpcType.EMPTY_CALL) {
                stub.emptyCall(EmptyProtos.Empty.getDefaultInstance(), new StreamObserver<EmptyProtos.Empty>() {

                    @Override
                    public void onCompleted() {
                        handleRpcCompleted(requestId, config.rpcType, hostnameRef.get(), savedWatchers);
                    }

                    @Override
                    public void onError(Throwable t) {
                        handleRpcError(requestId, config.rpcType, Status.fromThrowable(t), savedWatchers);
                    }

                    @Override
                    public void onNext(EmptyProtos.Empty response) {
                    }
                });
            } else if (config.rpcType == RpcType.UNARY_CALL) {
                SimpleRequest request = SimpleRequest.newBuilder().setFillServerId(true).build();
                stub.unaryCall(request, new StreamObserver<SimpleResponse>() {

                    @Override
                    public void onCompleted() {
                        handleRpcCompleted(requestId, config.rpcType, hostnameRef.get(), savedWatchers);
                    }

                    @Override
                    public void onError(Throwable t) {
                        if (printResponse) {
                            logger.log(Level.WARNING, "Rpc failed", t);
                        }
                        handleRpcError(requestId, config.rpcType, Status.fromThrowable(t), savedWatchers);
                    }

                    @Override
                    public void onNext(SimpleResponse response) {
                        // service and rely on parsing stdout.
                        if (printResponse) {
                            System.out.println("Greeting: Hello world, this is " + response.getHostname() + ", from " + clientCallRef.get().getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR));
                        }
                        // TODO(ericgribkoff) Delete when server is deployed that sets metadata value.
                        if (hostnameRef.get() == null) {
                            hostnameRef.set(response.getHostname());
                        }
                    }
                });
            } else {
                throw new AssertionError("Unknown RPC type: " + config.rpcType);
            }
            statsAccumulator.recordRpcStarted(config.rpcType);
        }

        private void handleRpcCompleted(long requestId, RpcType rpcType, String hostname, Set<XdsStatsWatcher> watchers) {
            statsAccumulator.recordRpcFinished(rpcType, Status.OK);
            notifyWatchers(watchers, rpcType, requestId, hostname);
        }

        private void handleRpcError(long requestId, RpcType rpcType, Status status, Set<XdsStatsWatcher> watchers) {
            statsAccumulator.recordRpcFinished(rpcType, status);
            notifyWatchers(watchers, rpcType, requestId, null);
        }
    }
    long nanosPerQuery = TimeUnit.SECONDS.toNanos(1) / qps;
    ListenableScheduledFuture<?> future = exec.scheduleAtFixedRate(new PeriodicRpc(), 0, nanosPerQuery, TimeUnit.NANOSECONDS);
    Futures.addCallback(future, new FutureCallback<Object>() {

        @Override
        public void onFailure(Throwable t) {
            failure.setException(t);
        }

        @Override
        public void onSuccess(Object o) {
        }
    }, MoreExecutors.directExecutor());
    failure.get();
}
Also used : SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Set(java.util.Set) HashSet(java.util.HashSet) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ClientCall(io.grpc.ClientCall) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) ClientInterceptor(io.grpc.ClientInterceptor) ManagedChannel(io.grpc.ManagedChannel) HashSet(java.util.HashSet) StreamObserver(io.grpc.stub.StreamObserver) Status(io.grpc.Status) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) RpcType(io.grpc.testing.integration.Messages.ClientConfigureRequest.RpcType) AtomicReference(java.util.concurrent.atomic.AtomicReference) MethodDescriptor(io.grpc.MethodDescriptor) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall)

Example 12 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project grpc-java by grpc.

the class RetryPolicyTest method getRetryPolicies_retryDisabled.

@Test
public void getRetryPolicies_retryDisabled() throws Exception {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(RetryPolicyTest.class.getResourceAsStream("/io/grpc/internal/test_retry_service_config.json"), "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
        Object serviceConfigObj = JsonParser.parse(sb.toString());
        assertTrue(serviceConfigObj instanceof Map);
        @SuppressWarnings("unchecked") Map<String, ?> serviceConfig = (Map<String, ?>) serviceConfigObj;
        ManagedChannelServiceConfig channelServiceConfig = ManagedChannelServiceConfig.fromServiceConfig(serviceConfig, /* retryEnabled= */
        false, /* maxRetryAttemptsLimit= */
        4, /* maxHedgedAttemptsLimit= */
        3, /* loadBalancingConfig= */
        null);
        MethodDescriptor.Builder<Void, Void> builder = TestMethodDescriptors.voidMethod().toBuilder();
        MethodDescriptor<Void, Void> method = builder.setFullMethodName("SimpleService1/Foo1").build();
        assertThat(channelServiceConfig.getMethodConfig(method).retryPolicy).isNull();
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) MethodDescriptor(io.grpc.MethodDescriptor) BufferedReader(java.io.BufferedReader) Map(java.util.Map) Test(org.junit.Test)

Example 13 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project grpc-java by grpc.

the class CensusModulesTest method testClientInterceptors.

// Test that Census ClientInterceptors uses the TagContext and Span out of the current Context
// to create the ClientCallTracer, and that it intercepts ClientCall.Listener.onClose() to call
// ClientCallTracer.callEnded().
private void testClientInterceptors(boolean nonDefaultContext) {
    grpcServerRule.getServiceRegistry().addService(ServerServiceDefinition.builder("package1.service2").addMethod(method, new ServerCallHandler<String, String>() {

        @Override
        public ServerCall.Listener<String> startCall(ServerCall<String, String> call, Metadata headers) {
            call.sendHeaders(new Metadata());
            call.sendMessage("Hello");
            call.close(Status.PERMISSION_DENIED.withDescription("No you don't"), new Metadata());
            return mockServerCallListener;
        }
    }).build());
    final AtomicReference<CallOptions> capturedCallOptions = new AtomicReference<>();
    ClientInterceptor callOptionsCaptureInterceptor = new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            capturedCallOptions.set(callOptions);
            return next.newCall(method, callOptions);
        }
    };
    Channel interceptedChannel = ClientInterceptors.intercept(grpcServerRule.getChannel(), callOptionsCaptureInterceptor, censusStats.getClientInterceptor(), censusTracing.getClientInterceptor());
    ClientCall<String, String> call;
    if (nonDefaultContext) {
        Context ctx = io.opencensus.tags.unsafe.ContextUtils.withValue(Context.ROOT, tagger.emptyBuilder().putLocal(StatsTestUtils.EXTRA_TAG, TagValue.create("extra value")).build());
        ctx = ContextUtils.withValue(ctx, fakeClientParentSpan);
        Context origCtx = ctx.attach();
        try {
            call = interceptedChannel.newCall(method, CALL_OPTIONS);
        } finally {
            ctx.detach(origCtx);
        }
    } else {
        assertEquals(io.opencensus.tags.unsafe.ContextUtils.getValue(Context.ROOT), io.opencensus.tags.unsafe.ContextUtils.getValue(Context.current()));
        assertEquals(ContextUtils.getValue(Context.current()), BlankSpan.INSTANCE);
        call = interceptedChannel.newCall(method, CALL_OPTIONS);
    }
    // The interceptor adds tracer factory to CallOptions
    assertEquals("customvalue", capturedCallOptions.get().getOption(CUSTOM_OPTION));
    assertEquals(2, capturedCallOptions.get().getStreamTracerFactories().size());
    assertTrue(capturedCallOptions.get().getStreamTracerFactories().get(0) instanceof CallAttemptsTracerFactory);
    assertTrue(capturedCallOptions.get().getStreamTracerFactories().get(1) instanceof CensusStatsModule.CallAttemptsTracerFactory);
    // Make the call
    Metadata headers = new Metadata();
    call.start(mockClientCallListener, headers);
    StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
    assertNotNull(record);
    TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    if (nonDefaultContext) {
        TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
        assertEquals("extra value", extraTag.asString());
        assertEquals(2, record.tags.size());
    } else {
        assertNull(record.tags.get(StatsTestUtils.EXTRA_TAG));
        assertEquals(1, record.tags.size());
    }
    if (nonDefaultContext) {
        verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), same(fakeClientParentSpan));
        verify(spyClientSpanBuilder).setRecordEvents(eq(true));
    } else {
        verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), ArgumentMatchers.<Span>isNotNull());
        verify(spyClientSpanBuilder).setRecordEvents(eq(true));
    }
    verify(spyClientSpan, never()).end(any(EndSpanOptions.class));
    // End the call
    call.halfClose();
    call.request(1);
    verify(mockClientCallListener).onClose(statusCaptor.capture(), any(Metadata.class));
    Status status = statusCaptor.getValue();
    assertEquals(Status.Code.PERMISSION_DENIED, status.getCode());
    assertEquals("No you don't", status.getDescription());
    // The intercepting listener calls callEnded() on ClientCallTracer, which records to Census.
    record = statsRecorder.pollRecord();
    assertNotNull(record);
    methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
    assertEquals(Status.Code.PERMISSION_DENIED.toString(), statusTag.asString());
    if (nonDefaultContext) {
        TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
        assertEquals("extra value", extraTag.asString());
    } else {
        assertNull(record.tags.get(StatsTestUtils.EXTRA_TAG));
    }
    verify(spyClientSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.PERMISSION_DENIED.withDescription("No you don't")).setSampleToLocalSpanStore(false).build());
    verify(spyClientSpan, never()).end();
    assertZeroRetryRecorded();
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) Status(io.grpc.Status) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) EndSpanOptions(io.opencensus.trace.EndSpanOptions) ClientInterceptor(io.grpc.ClientInterceptor) TagValue(io.opencensus.tags.TagValue) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)

Example 14 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project grpc-java by grpc.

the class GrpcUtil method getTransportFromPickResult.

/**
 * Returns a transport out of a PickResult, or {@code null} if the result is "buffer".
 */
@Nullable
static ClientTransport getTransportFromPickResult(PickResult result, boolean isWaitForReady) {
    final ClientTransport transport;
    Subchannel subchannel = result.getSubchannel();
    if (subchannel != null) {
        transport = ((TransportProvider) subchannel.getInternalSubchannel()).obtainActiveTransport();
    } else {
        transport = null;
    }
    if (transport != null) {
        final ClientStreamTracer.Factory streamTracerFactory = result.getStreamTracerFactory();
        if (streamTracerFactory == null) {
            return transport;
        }
        return new ClientTransport() {

            @Override
            public ClientStream newStream(MethodDescriptor<?, ?> method, Metadata headers, CallOptions callOptions, ClientStreamTracer[] tracers) {
                StreamInfo info = StreamInfo.newBuilder().setCallOptions(callOptions).build();
                ClientStreamTracer streamTracer = streamTracerFactory.newClientStreamTracer(info, headers);
                checkState(tracers[tracers.length - 1] == NOOP_TRACER, "lb tracer already assigned");
                tracers[tracers.length - 1] = streamTracer;
                return transport.newStream(method, headers, callOptions, tracers);
            }

            @Override
            public void ping(PingCallback callback, Executor executor) {
                transport.ping(callback, executor);
            }

            @Override
            public InternalLogId getLogId() {
                return transport.getLogId();
            }

            @Override
            public ListenableFuture<SocketStats> getStats() {
                return transport.getStats();
            }
        };
    }
    if (!result.getStatus().isOk()) {
        if (result.isDrop()) {
            return new FailingClientTransport(result.getStatus(), RpcProgress.DROPPED);
        }
        if (!isWaitForReady) {
            return new FailingClientTransport(result.getStatus(), RpcProgress.PROCESSED);
        }
    }
    return null;
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) Executor(java.util.concurrent.Executor) Subchannel(io.grpc.LoadBalancer.Subchannel) Metadata(io.grpc.Metadata) InternalMetadata(io.grpc.InternalMetadata) StreamInfo(io.grpc.ClientStreamTracer.StreamInfo) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) SocketStats(io.grpc.InternalChannelz.SocketStats) Nullable(javax.annotation.Nullable)

Example 15 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project grpc-java by grpc.

the class BinlogHelperTest method clientInterceptor_trailersOnlyResponseLogsPeerAddress.

@Test
public void clientInterceptor_trailersOnlyResponseLogsPeerAddress() throws Exception {
    final AtomicReference<ClientCall.Listener<byte[]>> interceptedListener = new AtomicReference<>();
    // capture these manually because ClientCall can not be mocked
    final AtomicReference<Metadata> actualClientInitial = new AtomicReference<>();
    final AtomicReference<Object> actualRequest = new AtomicReference<>();
    Channel channel = new Channel() {

        @Override
        public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
            return new NoopClientCall<RequestT, ResponseT>() {

                @Override
                @SuppressWarnings("unchecked")
                public void start(Listener<ResponseT> responseListener, Metadata headers) {
                    interceptedListener.set((Listener<byte[]>) responseListener);
                    actualClientInitial.set(headers);
                }

                @Override
                public void sendMessage(RequestT message) {
                    actualRequest.set(message);
                }

                @Override
                public Attributes getAttributes() {
                    return Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, peer).build();
                }
            };
        }

        @Override
        public String authority() {
            return "the-authority";
        }
    };
    @SuppressWarnings("unchecked") ClientCall.Listener<byte[]> mockListener = mock(ClientCall.Listener.class);
    MethodDescriptor<byte[], byte[]> method = MethodDescriptor.<byte[], byte[]>newBuilder().setType(MethodType.UNKNOWN).setFullMethodName("service/method").setRequestMarshaller(BYTEARRAY_MARSHALLER).setResponseMarshaller(BYTEARRAY_MARSHALLER).build();
    ClientCall<byte[], byte[]> interceptedCall = new BinlogHelper(mockSinkWriter).getClientInterceptor(CALL_ID).interceptCall(method, CallOptions.DEFAULT.withDeadlineAfter(1, TimeUnit.SECONDS), channel);
    Metadata clientInitial = new Metadata();
    interceptedCall.start(mockListener, clientInitial);
    verify(mockSinkWriter).logClientHeader(/*seq=*/
    eq(1L), anyString(), anyString(), any(Duration.class), any(Metadata.class), eq(Logger.LOGGER_CLIENT), eq(CALL_ID), ArgumentMatchers.<SocketAddress>isNull());
    verifyNoMoreInteractions(mockSinkWriter);
    // trailer only response
    {
        Status status = Status.INTERNAL.withDescription("some description");
        Metadata trailers = new Metadata();
        interceptedListener.get().onClose(status, trailers);
        verify(mockSinkWriter).logTrailer(/*seq=*/
        eq(2L), same(status), same(trailers), eq(Logger.LOGGER_CLIENT), eq(CALL_ID), same(peer));
        verifyNoMoreInteractions(mockSinkWriter);
        verify(mockListener).onClose(same(status), same(trailers));
    }
}
Also used : Status(io.grpc.Status) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(com.google.protobuf.Duration) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) NoopClientCall(io.grpc.internal.NoopClientCall) ClientCall(io.grpc.ClientCall) NoopClientCall(io.grpc.internal.NoopClientCall) Test(org.junit.Test)

Aggregations

MethodDescriptor (io.grpc.MethodDescriptor)35 CallOptions (io.grpc.CallOptions)20 Metadata (io.grpc.Metadata)19 Channel (io.grpc.Channel)18 Test (org.junit.Test)17 ClientInterceptor (io.grpc.ClientInterceptor)13 ClientCall (io.grpc.ClientCall)9 Status (io.grpc.Status)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 ByteString (com.google.protobuf.ByteString)6 ManagedChannel (io.grpc.ManagedChannel)6 Duration (com.google.protobuf.Duration)5 NoopClientCall (io.grpc.internal.NoopClientCall)5 SocketAddress (java.net.SocketAddress)5 SimpleForwardingClientCall (io.grpc.ForwardingClientCall.SimpleForwardingClientCall)4 SimpleForwardingClientCallListener (io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener)4 InetSocketAddress (java.net.InetSocketAddress)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 ClientStreamTracer (io.grpc.ClientStreamTracer)3 ForwardingClientCall (io.grpc.ForwardingClientCall)3