Search in sources :

Example 1 with MethodInfo

use of io.grpc.internal.ManagedChannelServiceConfig.MethodInfo in project grpc-java by grpc.

the class ClientCallImplTest method methodInfoDeadlinePropagatedToStream.

@Test
public void methodInfoDeadlinePropagatedToStream() {
    ArgumentCaptor<CallOptions> callOptionsCaptor = ArgumentCaptor.forClass(null);
    CallOptions callOptions = baseCallOptions.withDeadline(Deadline.after(2000, SECONDS));
    // Case: config Deadline expires later than CallOptions Deadline
    Map<String, ?> rawMethodConfig = ImmutableMap.of("timeout", "3000s");
    MethodInfo methodInfo = new MethodInfo(rawMethodConfig, false, 0, 0);
    callOptions = callOptions.withOption(MethodInfo.KEY, methodInfo);
    ClientCallImpl<Void, Void> call = new ClientCallImpl<>(method, MoreExecutors.directExecutor(), callOptions, clientStreamProvider, deadlineCancellationExecutor, channelCallTracer, configSelector).setDecompressorRegistry(decompressorRegistry);
    call.start(callListener, new Metadata());
    verify(clientStreamProvider).newStream(same(method), callOptionsCaptor.capture(), any(Metadata.class), any(Context.class));
    Deadline actualDeadline = callOptionsCaptor.getValue().getDeadline();
    assertThat(actualDeadline).isLessThan(Deadline.after(2001, SECONDS));
    // Case: config Deadline expires earlier than CallOptions Deadline
    rawMethodConfig = ImmutableMap.of("timeout", "1000s");
    methodInfo = new MethodInfo(rawMethodConfig, false, 0, 0);
    callOptions = callOptions.withOption(MethodInfo.KEY, methodInfo);
    call = new ClientCallImpl<>(method, MoreExecutors.directExecutor(), callOptions, clientStreamProvider, deadlineCancellationExecutor, channelCallTracer, configSelector).setDecompressorRegistry(decompressorRegistry);
    call.start(callListener, new Metadata());
    verify(clientStreamProvider, times(2)).newStream(same(method), callOptionsCaptor.capture(), any(Metadata.class), any(Context.class));
    actualDeadline = callOptionsCaptor.getValue().getDeadline();
    assertThat(actualDeadline).isLessThan(Deadline.after(1001, SECONDS));
}
Also used : Context(io.grpc.Context) Deadline(io.grpc.Deadline) Metadata(io.grpc.Metadata) MethodInfo(io.grpc.internal.ManagedChannelServiceConfig.MethodInfo) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Example 2 with MethodInfo

use of io.grpc.internal.ManagedChannelServiceConfig.MethodInfo in project grpc-java by grpc.

the class ManagedChannelServiceConfigTest method managedChannelServiceConfig_parseMethodConfig.

@Test
public void managedChannelServiceConfig_parseMethodConfig() {
    Map<String, ?> name1 = ImmutableMap.of("service", "service1", "method", "method1");
    Map<String, ?> name2 = ImmutableMap.of("service", "service2");
    Map<String, ?> methodConfig = ImmutableMap.of("name", ImmutableList.of(name1, name2), "timeout", "1.234s", "retryPolicy", ImmutableMap.builder().put("maxAttempts", 3.0D).put("initialBackoff", "1s").put("maxBackoff", "10s").put("backoffMultiplier", 1.5D).put("perAttemptRecvTimeout", "2.5s").put("retryableStatusCodes", ImmutableList.of("UNAVAILABLE")).build());
    Map<String, ?> defaultMethodConfig = ImmutableMap.of("name", ImmutableList.of(ImmutableMap.of()), "timeout", "4.321s");
    Map<String, ?> rawServiceConfig = ImmutableMap.of("methodConfig", ImmutableList.of(methodConfig, defaultMethodConfig));
    // retry disabled
    ManagedChannelServiceConfig serviceConfig = ManagedChannelServiceConfig.fromServiceConfig(rawServiceConfig, false, 0, 0, null);
    MethodInfo methodInfo = serviceConfig.getMethodConfig(methodForName("service1", "method1"));
    assertThat(methodInfo.timeoutNanos).isEqualTo(MILLISECONDS.toNanos(1234));
    assertThat(methodInfo.retryPolicy).isNull();
    methodInfo = serviceConfig.getMethodConfig(methodForName("service1", "methodX"));
    assertThat(methodInfo.timeoutNanos).isEqualTo(MILLISECONDS.toNanos(4321));
    assertThat(methodInfo.retryPolicy).isNull();
    methodInfo = serviceConfig.getMethodConfig(methodForName("service2", "methodX"));
    assertThat(methodInfo.timeoutNanos).isEqualTo(MILLISECONDS.toNanos(1234));
    assertThat(methodInfo.retryPolicy).isNull();
    // retry enabled
    serviceConfig = ManagedChannelServiceConfig.fromServiceConfig(rawServiceConfig, true, 2, 0, null);
    methodInfo = serviceConfig.getMethodConfig(methodForName("service1", "method1"));
    assertThat(methodInfo.timeoutNanos).isEqualTo(MILLISECONDS.toNanos(1234));
    assertThat(methodInfo.retryPolicy.maxAttempts).isEqualTo(2);
    assertThat(methodInfo.retryPolicy.perAttemptRecvTimeoutNanos).isEqualTo(MILLISECONDS.toNanos(2500));
    assertThat(methodInfo.retryPolicy.retryableStatusCodes).containsExactly(UNAVAILABLE);
    methodInfo = serviceConfig.getMethodConfig(methodForName("service1", "methodX"));
    assertThat(methodInfo.timeoutNanos).isEqualTo(MILLISECONDS.toNanos(4321));
    assertThat(methodInfo.retryPolicy).isNull();
}
Also used : MethodInfo(io.grpc.internal.ManagedChannelServiceConfig.MethodInfo) Test(org.junit.Test)

Example 3 with MethodInfo

use of io.grpc.internal.ManagedChannelServiceConfig.MethodInfo in project grpc-java by grpc.

the class ManagedChannelServiceConfigTest method getDefaultConfigSelectorFromConfig.

@Test
public void getDefaultConfigSelectorFromConfig() {
    Map<String, ?> name = ImmutableMap.of("service", "service1", "method", "method1");
    Map<String, ?> methodConfig = ImmutableMap.of("name", ImmutableList.of(name), "timeout", "1.234s");
    Map<String, ?> rawServiceConfig = ImmutableMap.of("methodConfig", ImmutableList.of(methodConfig));
    ManagedChannelServiceConfig serviceConfig = ManagedChannelServiceConfig.fromServiceConfig(rawServiceConfig, false, 0, 0, null);
    InternalConfigSelector configSelector = serviceConfig.getDefaultConfigSelector();
    MethodDescriptor<?, ?> method = methodForName("service1", "method1");
    Result result = configSelector.selectConfig(new PickSubchannelArgsImpl(method, new Metadata(), CallOptions.DEFAULT));
    MethodInfo methodInfoFromDefaultConfigSelector = ((ManagedChannelServiceConfig) result.getConfig()).getMethodConfig(method);
    assertThat(methodInfoFromDefaultConfigSelector).isEqualTo(serviceConfig.getMethodConfig(method));
}
Also used : InternalConfigSelector(io.grpc.InternalConfigSelector) Metadata(io.grpc.Metadata) MethodInfo(io.grpc.internal.ManagedChannelServiceConfig.MethodInfo) Result(io.grpc.InternalConfigSelector.Result) Test(org.junit.Test)

Example 4 with MethodInfo

use of io.grpc.internal.ManagedChannelServiceConfig.MethodInfo in project grpc-java by grpc.

the class ConfigSelectingClientCallTest method configSelectorInterceptsCall.

@Test
public void configSelectorInterceptsCall() {
    Map<String, ?> rawMethodConfig = ImmutableMap.of("retryPolicy", ImmutableMap.of("maxAttempts", 3.0D, "initialBackoff", "1s", "maxBackoff", "10s", "backoffMultiplier", 1.5D, "retryableStatusCodes", ImmutableList.of("UNAVAILABLE")));
    final MethodInfo methodInfo = new MethodInfo(rawMethodConfig, true, 4, 4);
    final Metadata.Key<String> metadataKey = Metadata.Key.of("test", Metadata.ASCII_STRING_MARSHALLER);
    final CallOptions.Key<String> callOptionsKey = CallOptions.Key.create("test");
    InternalConfigSelector configSelector = new InternalConfigSelector() {

        @Override
        public Result selectConfig(final PickSubchannelArgs args) {
            ManagedChannelServiceConfig config = new ManagedChannelServiceConfig(methodInfo, ImmutableMap.<String, MethodInfo>of(), ImmutableMap.<String, MethodInfo>of(), null, null, null);
            return Result.newBuilder().setConfig(config).setInterceptor(// An interceptor that mutates CallOptions based on headers value.
            new ClientInterceptor() {

                String value = args.getHeaders().get(metadataKey);

                @Override
                public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
                    callOptions = callOptions.withOption(callOptionsKey, value);
                    return next.newCall(method, callOptions);
                }
            }).build();
        }
    };
    ClientCall<Void, Void> configSelectingClientCall = new ConfigSelectingClientCall<>(configSelector, channel, MoreExecutors.directExecutor(), method, CallOptions.DEFAULT.withAuthority("bar.authority"));
    Metadata metadata = new Metadata();
    metadata.put(metadataKey, "fooValue");
    configSelectingClientCall.start(callListener, metadata);
    assertThat(call.callOptions.getAuthority()).isEqualTo("bar.authority");
    assertThat(call.callOptions.getOption(MethodInfo.KEY)).isEqualTo(methodInfo);
    assertThat(call.callOptions.getOption(callOptionsKey)).isEqualTo("fooValue");
}
Also used : Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) InternalConfigSelector(io.grpc.InternalConfigSelector) ClientInterceptor(io.grpc.ClientInterceptor) ConfigSelectingClientCall(io.grpc.internal.ManagedChannelImpl.ConfigSelectingClientCall) MethodInfo(io.grpc.internal.ManagedChannelServiceConfig.MethodInfo) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 5 with MethodInfo

use of io.grpc.internal.ManagedChannelServiceConfig.MethodInfo in project grpc-java by grpc.

the class ClientCallImpl method applyMethodConfig.

private void applyMethodConfig() {
    MethodInfo info = callOptions.getOption(MethodInfo.KEY);
    if (info == null) {
        return;
    }
    if (info.timeoutNanos != null) {
        Deadline newDeadline = Deadline.after(info.timeoutNanos, TimeUnit.NANOSECONDS);
        Deadline existingDeadline = callOptions.getDeadline();
        // If the new deadline is sooner than the existing deadline, swap them.
        if (existingDeadline == null || newDeadline.compareTo(existingDeadline) < 0) {
            callOptions = callOptions.withDeadline(newDeadline);
        }
    }
    if (info.waitForReady != null) {
        callOptions = info.waitForReady ? callOptions.withWaitForReady() : callOptions.withoutWaitForReady();
    }
    if (info.maxInboundMessageSize != null) {
        Integer existingLimit = callOptions.getMaxInboundMessageSize();
        if (existingLimit != null) {
            callOptions = callOptions.withMaxInboundMessageSize(Math.min(existingLimit, info.maxInboundMessageSize));
        } else {
            callOptions = callOptions.withMaxInboundMessageSize(info.maxInboundMessageSize);
        }
    }
    if (info.maxOutboundMessageSize != null) {
        Integer existingLimit = callOptions.getMaxOutboundMessageSize();
        if (existingLimit != null) {
            callOptions = callOptions.withMaxOutboundMessageSize(Math.min(existingLimit, info.maxOutboundMessageSize));
        } else {
            callOptions = callOptions.withMaxOutboundMessageSize(info.maxOutboundMessageSize);
        }
    }
}
Also used : Deadline(io.grpc.Deadline) MethodInfo(io.grpc.internal.ManagedChannelServiceConfig.MethodInfo)

Aggregations

MethodInfo (io.grpc.internal.ManagedChannelServiceConfig.MethodInfo)5 Test (org.junit.Test)4 Metadata (io.grpc.Metadata)3 CallOptions (io.grpc.CallOptions)2 Deadline (io.grpc.Deadline)2 InternalConfigSelector (io.grpc.InternalConfigSelector)2 Channel (io.grpc.Channel)1 ClientInterceptor (io.grpc.ClientInterceptor)1 Context (io.grpc.Context)1 Result (io.grpc.InternalConfigSelector.Result)1 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)1 MethodDescriptor (io.grpc.MethodDescriptor)1 ConfigSelectingClientCall (io.grpc.internal.ManagedChannelImpl.ConfigSelectingClientCall)1