Search in sources :

Example 26 with InternalConfigSelector

use of io.grpc.InternalConfigSelector 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 27 with InternalConfigSelector

use of io.grpc.InternalConfigSelector in project grpc-java by grpc.

the class ConfigSelectingClientCallTest method selectionErrorPropagatedToListener.

@Test
public void selectionErrorPropagatedToListener() {
    InternalConfigSelector configSelector = new InternalConfigSelector() {

        @Override
        public Result selectConfig(PickSubchannelArgs args) {
            return Result.forError(Status.FAILED_PRECONDITION);
        }
    };
    ClientCall<Void, Void> configSelectingClientCall = new ConfigSelectingClientCall<>(configSelector, channel, MoreExecutors.directExecutor(), method, CallOptions.DEFAULT);
    configSelectingClientCall.start(callListener, new Metadata());
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(null);
    verify(callListener).onClose(statusCaptor.capture(), any(Metadata.class));
    assertThat(statusCaptor.getValue().getCode()).isEqualTo(Status.Code.FAILED_PRECONDITION);
    // The call should not delegate to null and fail methods with NPE.
    configSelectingClientCall.request(1);
}
Also used : Status(io.grpc.Status) InternalConfigSelector(io.grpc.InternalConfigSelector) ConfigSelectingClientCall(io.grpc.internal.ManagedChannelImpl.ConfigSelectingClientCall) Metadata(io.grpc.Metadata) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 28 with InternalConfigSelector

use of io.grpc.InternalConfigSelector in project grpc-java by grpc.

the class ServiceConfigErrorHandlingTest method invalidConfig_2ndResolution.

@Test
public void invalidConfig_2ndResolution() throws Exception {
    FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(ImmutableList.of(addressGroup)).build();
    channelBuilder.nameResolverFactory(nameResolverFactory);
    Map<String, Object> rawServiceConfig = parseJson("{\"loadBalancingConfig\": [{\"mock_lb\": {\"check\": \"1st raw config\"}}]}");
    nameResolverFactory.nextRawServiceConfig.set(rawServiceConfig);
    InternalConfigSelector configSelector = mock(InternalConfigSelector.class);
    nameResolverFactory.nextAttributes.set(Attributes.newBuilder().set(InternalConfigSelector.KEY, configSelector).build());
    createChannel();
    ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
    verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
    ResolvedAddresses resolvedAddresses = resultCaptor.getValue();
    assertThat(resolvedAddresses.getAddresses()).containsExactly(addressGroup);
    assertThat(resolvedAddresses.getLoadBalancingPolicyConfig()).isEqualTo("1st raw config");
    assertThat(channel.getConfigSelector()).isSameInstanceAs(configSelector);
    verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
    assertThat(channel.getState(false)).isNotEqualTo(ConnectivityState.TRANSIENT_FAILURE);
    reset(mockLoadBalancer);
    // 2nd resolution lbConfig is error
    nextLbPolicyConfigError.set(Status.UNKNOWN);
    nameResolverFactory.allResolved();
    verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
    ResolvedAddresses newResolvedAddress = resultCaptor.getValue();
    // should use previous service config because new service config is invalid.
    assertThat(newResolvedAddress.getLoadBalancingPolicyConfig()).isEqualTo("1st raw config");
    assertThat(channel.getConfigSelector()).isSameInstanceAs(configSelector);
    verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
    assertThat(channel.getState(false)).isEqualTo(ConnectivityState.IDLE);
}
Also used : Status(io.grpc.Status) InternalConfigSelector(io.grpc.InternalConfigSelector) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Test(org.junit.Test)

Aggregations

InternalConfigSelector (io.grpc.InternalConfigSelector)28 Test (org.junit.Test)28 ResolutionResult (io.grpc.NameResolver.ResolutionResult)17 HashPolicy (io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy)10 ClientCall (io.grpc.ClientCall)7 Metadata (io.grpc.Metadata)7 NoopClientCall (io.grpc.internal.NoopClientCall)7 Status (io.grpc.Status)5 Result (io.grpc.InternalConfigSelector.Result)4 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)3 UnsupportedClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder)3 PickSubchannelArgsImpl (io.grpc.internal.PickSubchannelArgsImpl)3 CallOptions (io.grpc.CallOptions)2 Channel (io.grpc.Channel)2 ClientInterceptor (io.grpc.ClientInterceptor)2 ResolvedAddresses (io.grpc.LoadBalancer.ResolvedAddresses)2 MethodDescriptor (io.grpc.MethodDescriptor)2 ConfigSelectingClientCall (io.grpc.internal.ManagedChannelImpl.ConfigSelectingClientCall)2 MethodInfo (io.grpc.internal.ManagedChannelServiceConfig.MethodInfo)2 Route (io.grpc.xds.VirtualHost.Route)2