Search in sources :

Example 21 with MethodDescriptor

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

the class HedgingPolicyTest method getRetryPolicies_hedgingDisabled.

@Test
public void getRetryPolicies_hedgingDisabled() throws Exception {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(RetryPolicyTest.class.getResourceAsStream("/io/grpc/internal/test_hedging_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= */
        3, /* maxHedgedAttemptsLimit= */
        4, /* loadBalancingConfig= */
        null);
        MethodDescriptor.Builder<Void, Void> builder = TestMethodDescriptors.voidMethod().toBuilder();
        MethodDescriptor<Void, Void> method = builder.setFullMethodName("SimpleService1/Foo1").build();
        assertThat(channelServiceConfig.getMethodConfig(method).hedgingPolicy).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 22 with MethodDescriptor

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

the class HedgingPolicyTest method getHedgingPolicies.

@Test
public void getHedgingPolicies() throws Exception {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(RetryPolicyTest.class.getResourceAsStream("/io/grpc/internal/test_hedging_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= */
        true, /* maxRetryAttemptsLimit= */
        3, /* maxHedgedAttemptsLimit= */
        4, /* loadBalancingConfig= */
        null);
        MethodDescriptor.Builder<Void, Void> builder = TestMethodDescriptors.voidMethod().toBuilder();
        MethodDescriptor<Void, Void> method = builder.setFullMethodName("not/exist").build();
        assertThat(channelServiceConfig.getMethodConfig(method)).isNull();
        method = builder.setFullMethodName("not_exist/Foo1").build();
        assertThat(channelServiceConfig.getMethodConfig(method)).isNull();
        method = builder.setFullMethodName("SimpleService1/not_exist").build();
        assertThat(channelServiceConfig.getMethodConfig(method).hedgingPolicy).isEqualTo(new HedgingPolicy(3, TimeUnit.MILLISECONDS.toNanos(2100), ImmutableSet.of(Code.UNAVAILABLE, Code.RESOURCE_EXHAUSTED)));
        method = builder.setFullMethodName("SimpleService1/Foo1").build();
        assertThat(channelServiceConfig.getMethodConfig(method).hedgingPolicy).isEqualTo(new HedgingPolicy(4, TimeUnit.MILLISECONDS.toNanos(100), ImmutableSet.of(Code.UNAVAILABLE)));
        method = builder.setFullMethodName("SimpleService2/not_exist").build();
        assertThat(channelServiceConfig.getMethodConfig(method).hedgingPolicy).isNull();
        method = builder.setFullMethodName("SimpleService2/Foo2").build();
        assertThat(channelServiceConfig.getMethodConfig(method).hedgingPolicy).isEqualTo(new HedgingPolicy(4, TimeUnit.MILLISECONDS.toNanos(100), ImmutableSet.of(Code.UNAVAILABLE)));
    } 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 23 with MethodDescriptor

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

the class FaultFilter method buildClientInterceptor.

@Nullable
@Override
public ClientInterceptor buildClientInterceptor(FilterConfig config, @Nullable FilterConfig overrideConfig, PickSubchannelArgs args, final ScheduledExecutorService scheduler) {
    checkNotNull(config, "config");
    if (overrideConfig != null) {
        config = overrideConfig;
    }
    FaultConfig faultConfig = (FaultConfig) config;
    Long delayNanos = null;
    Status abortStatus = null;
    if (faultConfig.maxActiveFaults() == null || activeFaultCounter.get() < faultConfig.maxActiveFaults()) {
        Metadata headers = args.getHeaders();
        if (faultConfig.faultDelay() != null) {
            delayNanos = determineFaultDelayNanos(faultConfig.faultDelay(), headers);
        }
        if (faultConfig.faultAbort() != null) {
            abortStatus = determineFaultAbortStatus(faultConfig.faultAbort(), headers);
        }
    }
    if (delayNanos == null && abortStatus == null) {
        return null;
    }
    final Long finalDelayNanos = delayNanos;
    final Status finalAbortStatus = getAbortStatusWithDescription(abortStatus);
    final class FaultInjectionInterceptor implements ClientInterceptor {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method, final CallOptions callOptions, final Channel next) {
            Executor callExecutor = callOptions.getExecutor();
            if (callExecutor == null) {
                // This should never happen in practice because
                // ManagedChannelImpl.ConfigSelectingClientCall always provides CallOptions with
                // a callExecutor.
                // TODO(https://github.com/grpc/grpc-java/issues/7868)
                callExecutor = MoreExecutors.directExecutor();
            }
            if (finalDelayNanos != null) {
                Supplier<? extends ClientCall<ReqT, RespT>> callSupplier;
                if (finalAbortStatus != null) {
                    callSupplier = Suppliers.ofInstance(new FailingClientCall<ReqT, RespT>(finalAbortStatus, callExecutor));
                } else {
                    callSupplier = new Supplier<ClientCall<ReqT, RespT>>() {

                        @Override
                        public ClientCall<ReqT, RespT> get() {
                            return next.newCall(method, callOptions);
                        }
                    };
                }
                final DelayInjectedCall<ReqT, RespT> delayInjectedCall = new DelayInjectedCall<>(finalDelayNanos, callExecutor, scheduler, callOptions.getDeadline(), callSupplier);
                final class DeadlineInsightForwardingCall extends ForwardingClientCall<ReqT, RespT> {

                    @Override
                    protected ClientCall<ReqT, RespT> delegate() {
                        return delayInjectedCall;
                    }

                    @Override
                    public void start(Listener<RespT> listener, Metadata headers) {
                        Listener<RespT> finalListener = new SimpleForwardingClientCallListener<RespT>(listener) {

                            @Override
                            public void onClose(Status status, Metadata trailers) {
                                if (status.getCode().equals(Code.DEADLINE_EXCEEDED)) {
                                    // TODO(zdapeng:) check effective deadline locally, and
                                    // do the following only if the local deadline is exceeded.
                                    // (If the server sends DEADLINE_EXCEEDED for its own deadline, then the
                                    // injected delay does not contribute to the error, because the request is
                                    // only sent out after the delay. There could be a race between local and
                                    // remote, but it is rather rare.)
                                    String description = String.format("Deadline exceeded after up to %d ns of fault-injected delay", finalDelayNanos);
                                    if (status.getDescription() != null) {
                                        description = description + ": " + status.getDescription();
                                    }
                                    status = Status.DEADLINE_EXCEEDED.withDescription(description).withCause(status.getCause());
                                    // Replace trailers to prevent mixing sources of status and trailers.
                                    trailers = new Metadata();
                                }
                                delegate().onClose(status, trailers);
                            }
                        };
                        delegate().start(finalListener, headers);
                    }
                }
                return new DeadlineInsightForwardingCall();
            } else {
                return new FailingClientCall<>(finalAbortStatus, callExecutor);
            }
        }
    }
    return new FaultInjectionInterceptor();
}
Also used : SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) ForwardingClientCall(io.grpc.ForwardingClientCall) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Executor(java.util.concurrent.Executor) DelayedClientCall(io.grpc.internal.DelayedClientCall) ClientCall(io.grpc.ClientCall) ForwardingClientCall(io.grpc.ForwardingClientCall) ClientInterceptor(io.grpc.ClientInterceptor) Status(io.grpc.Status) Channel(io.grpc.Channel) MethodDescriptor(io.grpc.MethodDescriptor) AtomicLong(java.util.concurrent.atomic.AtomicLong) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Nullable(javax.annotation.Nullable)

Example 24 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project tesla by linking12.

the class DynamicGrpcClient method doRemoteCall.

@Override
public String doRemoteCall(final FilterRpcDO rpcDo, final String jsonInput) {
    try {
        final String serviceName = rpcDo.getServiceName();
        final String methodName = rpcDo.getMethodName();
        final String group = rpcDo.getServiceGroup();
        final String version = rpcDo.getServiceVersion();
        Pair<Descriptor, Descriptor> inOutType = ProtobufUtil.resolveServiceInputOutputType(rpcDo);
        Descriptor inPutType = inOutType.getLeft();
        Descriptor outPutType = inOutType.getRight();
        MethodDescriptor<DynamicMessage, DynamicMessage> methodDesc = this.createGrpcMethodDescriptor(serviceName, methodName, inPutType, outPutType);
        DynamicMessage message = this.createGrpcDynamicMessage(inPutType, jsonInput);
        Message response = (Message) genricService.$invoke(serviceName, group, version, methodName, methodDesc, message);
        return JSON2PROTOBUF.printToString(response);
    } catch (IOException e) {
        throw new RpcServiceException(String.format("json covert to DynamicMessage failed! the json is :%s, the protobuf type is: %s", jsonInput), e);
    } catch (Throwable e) {
        throw new RpcFrameworkException(String.format("service definition is wrong,please check the proto file you update,service is %s, method is %s", rpcDo.getServiceName(), rpcDo.getMethodName()), e);
    }
}
Also used : RpcFrameworkException(com.quancheng.saluki.core.grpc.exception.RpcFrameworkException) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) Descriptor(com.google.protobuf.Descriptors.Descriptor) MethodDescriptor(io.grpc.MethodDescriptor) RpcServiceException(com.quancheng.saluki.core.grpc.exception.RpcServiceException) DynamicMessage(com.google.protobuf.DynamicMessage) IOException(java.io.IOException)

Example 25 with MethodDescriptor

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

the class EtcdClient method refreshCredentials.

private CallCredentials refreshCredentials() {
    return new CallCredentials() {

        // TODO volatile TBD
        private Metadata tokenHeader;

        private final long authTime = System.currentTimeMillis();

        private final ListenableFuture<Metadata> futureTokenHeader = Futures.transform(authenticate(), (Function<AuthenticateResponse, Metadata>) ar -> tokenHeader = tokenHeader(ar));

        @Override
        public void applyRequestMetadata(MethodDescriptor<?, ?> method, Attributes attrs, Executor appExecutor, MetadataApplier applier) {
            Metadata tokHeader = tokenHeader;
            if (tokHeader != null)
                applier.apply(tokHeader);
            else
                futureTokenHeader.addListener(() -> {
                    try {
                        applier.apply(futureTokenHeader.get());
                    } catch (ExecutionException | InterruptedException ee) {
                        // (IE won't be thrown)
                        Status failStatus = Status.fromThrowable(ee.getCause());
                        Code code = failStatus != null ? failStatus.getCode() : null;
                        if (code != Code.INVALID_ARGUMENT && (System.currentTimeMillis() - authTime) > 15_000L) {
                            // this will force another auth attempt
                            failStatus = Status.UNAUTHENTICATED.withDescription("re-attempt re-auth");
                        }
                        applier.fail(failStatus);
                    }
                }, directExecutor());
        }

        // @Override
        public void thisUsesUnstableApi() {
        }
    };
}
Also used : Status(io.grpc.Status) Function(com.google.common.base.Function) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) Executor(java.util.concurrent.Executor) Metadata(io.grpc.Metadata) Attributes(io.grpc.Attributes) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) MethodDescriptor(io.grpc.MethodDescriptor) Code(io.grpc.Status.Code) CallCredentials(io.grpc.CallCredentials)

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