Search in sources :

Example 21 with ResolutionResult

use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.

the class XdsNameResolverTest method resolved_faultDelayInLdsUpdate.

@Test
public void resolved_faultDelayInLdsUpdate() {
    resolver.start(mockListener);
    FakeXdsClient xdsClient = (FakeXdsClient) resolver.getXdsClient();
    // 50%
    when(mockRandom.nextInt(1000_000)).thenReturn(500_000);
    // header delay, header delay rate = 60 %
    FaultConfig httpFilterFaultConfig = FaultConfig.create(FaultDelay.forHeader(FaultConfig.FractionalPercent.perHundred(70)), null, null);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    ResolutionResult result = resolutionResultCaptor.getValue();
    InternalConfigSelector configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    // no header delay key provided in metadata, rpc should succeed immediately
    ClientCall.Listener<Void> observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, Collections.<String, String>emptyMap(), CallOptions.DEFAULT);
    verifyRpcSucceeded(observer);
    // header delay key provided, rpc should be delayed
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, ImmutableMap.of(HEADER_DELAY_KEY.name(), "1000", HEADER_DELAY_PERCENTAGE_KEY.name(), "60"), CallOptions.DEFAULT);
    verifyRpcDelayed(observer, TimeUnit.MILLISECONDS.toNanos(1000));
    // header delay, no header rate, fix rate = 60 %
    httpFilterFaultConfig = FaultConfig.create(FaultDelay.forHeader(FaultConfig.FractionalPercent.perMillion(600_000)), null, null);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    result = resolutionResultCaptor.getValue();
    configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, ImmutableMap.of(HEADER_DELAY_KEY.name(), "1000"), CallOptions.DEFAULT);
    verifyRpcDelayed(observer, TimeUnit.MILLISECONDS.toNanos(1000));
    // header delay, no header rate, fix rate = 0
    httpFilterFaultConfig = FaultConfig.create(FaultDelay.forHeader(FaultConfig.FractionalPercent.perMillion(0)), null, null);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    result = resolutionResultCaptor.getValue();
    configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, ImmutableMap.of(HEADER_DELAY_KEY.name(), "1000"), CallOptions.DEFAULT);
    verifyRpcSucceeded(observer);
    // fixed delay, fix rate = 60%
    httpFilterFaultConfig = FaultConfig.create(FaultDelay.forFixedDelay(5000L, FaultConfig.FractionalPercent.perMillion(600_000)), null, null);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    result = resolutionResultCaptor.getValue();
    configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, Collections.<String, String>emptyMap(), CallOptions.DEFAULT);
    verifyRpcDelayed(observer, 5000L);
    // fixed delay, fix rate = 40%
    httpFilterFaultConfig = FaultConfig.create(FaultDelay.forFixedDelay(5000L, FaultConfig.FractionalPercent.perMillion(400_000)), null, null);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    result = resolutionResultCaptor.getValue();
    configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, Collections.<String, String>emptyMap(), CallOptions.DEFAULT);
    verifyRpcSucceeded(observer);
}
Also used : InternalConfigSelector(io.grpc.InternalConfigSelector) ClientCall(io.grpc.ClientCall) NoopClientCall(io.grpc.internal.NoopClientCall) ResolutionResult(io.grpc.NameResolver.ResolutionResult) Test(org.junit.Test)

Example 22 with ResolutionResult

use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.

the class XdsNameResolverTest method resolved_simpleCallSucceeds_routeToRls.

@Test
public void resolved_simpleCallSucceeds_routeToRls() {
    when(mockRandom.nextInt(anyInt())).thenReturn(90, 10);
    resolver.start(mockListener);
    FakeXdsClient xdsClient = (FakeXdsClient) resolver.getXdsClient();
    xdsClient.deliverLdsUpdate(Collections.singletonList(Route.forAction(RouteMatch.withPathExactOnly(call1.getFullMethodNameForPath()), RouteAction.forClusterSpecifierPlugin(NamedPluginConfig.create("rls-plugin-foo", RlsPluginConfig.create(ImmutableMap.of("lookupService", "rls-cbt.googleapis.com"))), Collections.<HashPolicy>emptyList(), TimeUnit.SECONDS.toNanos(20L), null), ImmutableMap.<String, FilterConfig>of())));
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    ResolutionResult result = resolutionResultCaptor.getValue();
    assertThat(result.getAddresses()).isEmpty();
    @SuppressWarnings("unchecked") Map<String, ?> resultServiceConfig = (Map<String, ?>) result.getServiceConfig().getConfig();
    List<Map<String, ?>> rawLbConfigs = JsonUtil.getListOfObjects(resultServiceConfig, "loadBalancingConfig");
    Map<String, ?> lbConfig = Iterables.getOnlyElement(rawLbConfigs);
    assertThat(lbConfig.keySet()).containsExactly("cluster_manager_experimental");
    Map<String, ?> clusterManagerLbConfig = JsonUtil.getObject(lbConfig, "cluster_manager_experimental");
    Map<String, ?> expectedRlsLbConfig = ImmutableMap.of("routeLookupConfig", ImmutableMap.of("lookupService", "rls-cbt.googleapis.com"), "childPolicy", ImmutableList.of(ImmutableMap.of("cds_experimental", ImmutableMap.of())), "childPolicyConfigTargetFieldName", "cluster");
    Map<String, ?> expectedClusterManagerLbConfig = ImmutableMap.of("childPolicy", ImmutableMap.of("cluster_specifier_plugin:rls-plugin-foo", ImmutableMap.of("lbPolicy", ImmutableList.of(ImmutableMap.of("rls_experimental", expectedRlsLbConfig)))));
    assertThat(clusterManagerLbConfig).isEqualTo(expectedClusterManagerLbConfig);
    assertThat(result.getAttributes().get(InternalXdsAttributes.XDS_CLIENT_POOL)).isNotNull();
    InternalConfigSelector configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    assertCallSelectRlsPluginResult(call1, configSelector, "rls-plugin-foo", 20.0);
    // config changed
    xdsClient.deliverLdsUpdate(Collections.singletonList(Route.forAction(RouteMatch.withPathExactOnly(call1.getFullMethodNameForPath()), RouteAction.forClusterSpecifierPlugin(NamedPluginConfig.create("rls-plugin-foo", RlsPluginConfig.create(// changed
    ImmutableMap.of("lookupService", "rls-cbt-2.googleapis.com"))), Collections.<HashPolicy>emptyList(), // changed
    TimeUnit.SECONDS.toNanos(30L), null), ImmutableMap.<String, FilterConfig>of())));
    verify(mockListener, times(2)).onResult(resolutionResultCaptor.capture());
    ResolutionResult result2 = resolutionResultCaptor.getValue();
    @SuppressWarnings("unchecked") Map<String, ?> resultServiceConfig2 = (Map<String, ?>) result2.getServiceConfig().getConfig();
    List<Map<String, ?>> rawLbConfigs2 = JsonUtil.getListOfObjects(resultServiceConfig2, "loadBalancingConfig");
    Map<String, ?> lbConfig2 = Iterables.getOnlyElement(rawLbConfigs2);
    assertThat(lbConfig2.keySet()).containsExactly("cluster_manager_experimental");
    Map<String, ?> clusterManagerLbConfig2 = JsonUtil.getObject(lbConfig2, "cluster_manager_experimental");
    Map<String, ?> expectedRlsLbConfig2 = ImmutableMap.of("routeLookupConfig", ImmutableMap.of("lookupService", "rls-cbt-2.googleapis.com"), "childPolicy", ImmutableList.of(ImmutableMap.of("cds_experimental", ImmutableMap.of())), "childPolicyConfigTargetFieldName", "cluster");
    Map<String, ?> expectedClusterManagerLbConfig2 = ImmutableMap.of("childPolicy", ImmutableMap.of("cluster_specifier_plugin:rls-plugin-foo", ImmutableMap.of("lbPolicy", ImmutableList.of(ImmutableMap.of("rls_experimental", expectedRlsLbConfig2)))));
    assertThat(clusterManagerLbConfig2).isEqualTo(expectedClusterManagerLbConfig2);
    InternalConfigSelector configSelector2 = result.getAttributes().get(InternalConfigSelector.KEY);
    assertCallSelectRlsPluginResult(call1, configSelector2, "rls-plugin-foo", 30.0);
}
Also used : InternalConfigSelector(io.grpc.InternalConfigSelector) HashPolicy(io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy) ResolutionResult(io.grpc.NameResolver.ResolutionResult) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 23 with ResolutionResult

use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.

the class XdsNameResolverTest method resolved_faultAbortInLdsUpdate.

@Test
public void resolved_faultAbortInLdsUpdate() {
    resolver.start(mockListener);
    FakeXdsClient xdsClient = (FakeXdsClient) resolver.getXdsClient();
    // 50%
    when(mockRandom.nextInt(1000_000)).thenReturn(500_000);
    // header abort, header abort rate = 60 %
    FaultConfig httpFilterFaultConfig = FaultConfig.create(null, FaultAbort.forHeader(FaultConfig.FractionalPercent.perHundred(70)), null);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    ResolutionResult result = resolutionResultCaptor.getValue();
    InternalConfigSelector configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    // no header abort key provided in metadata, rpc should succeed
    ClientCall.Listener<Void> observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, Collections.<String, String>emptyMap(), CallOptions.DEFAULT);
    verifyRpcSucceeded(observer);
    // header abort http status key provided, rpc should fail
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, ImmutableMap.of(HEADER_ABORT_HTTP_STATUS_KEY.name(), "404", HEADER_ABORT_PERCENTAGE_KEY.name(), "60"), CallOptions.DEFAULT);
    verifyRpcFailed(observer, Status.UNIMPLEMENTED.withDescription("RPC terminated due to fault injection: HTTP status code 404"));
    // header abort grpc status key provided, rpc should fail
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, ImmutableMap.of(HEADER_ABORT_GRPC_STATUS_KEY.name(), String.valueOf(Status.UNAUTHENTICATED.getCode().value()), HEADER_ABORT_PERCENTAGE_KEY.name(), "60"), CallOptions.DEFAULT);
    verifyRpcFailed(observer, Status.UNAUTHENTICATED.withDescription("RPC terminated due to fault injection"));
    // header abort, both http and grpc code keys provided, rpc should fail with http code
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, ImmutableMap.of(HEADER_ABORT_HTTP_STATUS_KEY.name(), "404", HEADER_ABORT_GRPC_STATUS_KEY.name(), String.valueOf(Status.UNAUTHENTICATED.getCode().value()), HEADER_ABORT_PERCENTAGE_KEY.name(), "60"), CallOptions.DEFAULT);
    verifyRpcFailed(observer, Status.UNIMPLEMENTED.withDescription("RPC terminated due to fault injection: HTTP status code 404"));
    // header abort, no header rate, fix rate = 60 %
    httpFilterFaultConfig = FaultConfig.create(null, FaultAbort.forHeader(FaultConfig.FractionalPercent.perMillion(600_000)), null);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    result = resolutionResultCaptor.getValue();
    configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, ImmutableMap.of(HEADER_ABORT_HTTP_STATUS_KEY.name(), "404"), CallOptions.DEFAULT);
    verifyRpcFailed(observer, Status.UNIMPLEMENTED.withDescription("RPC terminated due to fault injection: HTTP status code 404"));
    // header abort, no header rate, fix rate = 0
    httpFilterFaultConfig = FaultConfig.create(null, FaultAbort.forHeader(FaultConfig.FractionalPercent.perMillion(0)), null);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    result = resolutionResultCaptor.getValue();
    configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, ImmutableMap.of(HEADER_ABORT_HTTP_STATUS_KEY.name(), "404"), CallOptions.DEFAULT);
    verifyRpcSucceeded(observer);
    // fixed abort, fix rate = 60%
    httpFilterFaultConfig = FaultConfig.create(null, FaultAbort.forStatus(Status.UNAUTHENTICATED.withDescription("unauthenticated"), FaultConfig.FractionalPercent.perMillion(600_000)), null);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    result = resolutionResultCaptor.getValue();
    configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, Collections.<String, String>emptyMap(), CallOptions.DEFAULT);
    verifyRpcFailed(observer, Status.UNAUTHENTICATED.withDescription("RPC terminated due to fault injection: unauthenticated"));
    // fixed abort, fix rate = 40%
    httpFilterFaultConfig = FaultConfig.create(null, FaultAbort.forStatus(Status.UNAUTHENTICATED.withDescription("unauthenticated"), FaultConfig.FractionalPercent.perMillion(400_000)), null);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    result = resolutionResultCaptor.getValue();
    configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    observer = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, Collections.<String, String>emptyMap(), CallOptions.DEFAULT);
    verifyRpcSucceeded(observer);
}
Also used : InternalConfigSelector(io.grpc.InternalConfigSelector) ClientCall(io.grpc.ClientCall) NoopClientCall(io.grpc.internal.NoopClientCall) ResolutionResult(io.grpc.NameResolver.ResolutionResult) Test(org.junit.Test)

Example 24 with ResolutionResult

use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.

the class XdsNameResolverTest method resolved_resourceUpdateAfterCallStarted.

@SuppressWarnings("unchecked")
@Test
public void resolved_resourceUpdateAfterCallStarted() {
    InternalConfigSelector configSelector = resolveToClusters();
    assertCallSelectClusterResult(call1, configSelector, cluster1, 15.0);
    TestCall<?, ?> firstCall = testCall;
    reset(mockListener);
    FakeXdsClient xdsClient = (FakeXdsClient) resolver.getXdsClient();
    xdsClient.deliverLdsUpdate(Arrays.asList(Route.forAction(RouteMatch.withPathExactOnly(call1.getFullMethodNameForPath()), RouteAction.forCluster("another-cluster", Collections.<HashPolicy>emptyList(), TimeUnit.SECONDS.toNanos(20L), null), ImmutableMap.<String, FilterConfig>of()), Route.forAction(RouteMatch.withPathExactOnly(call2.getFullMethodNameForPath()), RouteAction.forCluster(cluster2, Collections.<HashPolicy>emptyList(), TimeUnit.SECONDS.toNanos(15L), null), ImmutableMap.<String, FilterConfig>of())));
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    ResolutionResult result = resolutionResultCaptor.getValue();
    // Updated service config still contains cluster1 while it is removed resource. New calls no
    // longer routed to cluster1.
    assertServiceConfigForLoadBalancingConfig(Arrays.asList(cluster1, cluster2, "another-cluster"), (Map<String, ?>) result.getServiceConfig().getConfig());
    assertThat(result.getAttributes().get(InternalConfigSelector.KEY)).isSameInstanceAs(configSelector);
    assertCallSelectClusterResult(call1, configSelector, "another-cluster", 20.0);
    // completes previous call
    firstCall.deliverErrorStatus();
    verify(mockListener, times(2)).onResult(resolutionResultCaptor.capture());
    result = resolutionResultCaptor.getValue();
    assertServiceConfigForLoadBalancingConfig(Arrays.asList(cluster2, "another-cluster"), (Map<String, ?>) result.getServiceConfig().getConfig());
    verifyNoMoreInteractions(mockListener);
}
Also used : InternalConfigSelector(io.grpc.InternalConfigSelector) HashPolicy(io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy) ResolutionResult(io.grpc.NameResolver.ResolutionResult) Test(org.junit.Test)

Example 25 with ResolutionResult

use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.

the class XdsNameResolverTest method resolved_faultDelayWithMaxActiveStreamsInLdsUpdate.

@Test
public void resolved_faultDelayWithMaxActiveStreamsInLdsUpdate() {
    resolver.start(mockListener);
    FakeXdsClient xdsClient = (FakeXdsClient) resolver.getXdsClient();
    // 50%
    when(mockRandom.nextInt(1000_000)).thenReturn(500_000);
    FaultConfig httpFilterFaultConfig = FaultConfig.create(FaultDelay.forFixedDelay(5000L, FaultConfig.FractionalPercent.perMillion(1000_000)), null, /* maxActiveFaults= */
    1);
    xdsClient.deliverLdsUpdateWithFaultInjection(cluster1, httpFilterFaultConfig, null, null, null);
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    ResolutionResult result = resolutionResultCaptor.getValue();
    InternalConfigSelector configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    // Send two calls, then the first call should delayed and the second call should not be delayed
    // because maxActiveFaults is exceeded.
    ClientCall.Listener<Void> observer1 = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, Collections.<String, String>emptyMap(), CallOptions.DEFAULT);
    assertThat(testCall).isNull();
    ClientCall.Listener<Void> observer2 = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, Collections.<String, String>emptyMap(), CallOptions.DEFAULT);
    verifyRpcSucceeded(observer2);
    verifyRpcDelayed(observer1, 5000L);
    // Once all calls are finished, new call should be delayed.
    ClientCall.Listener<Void> observer3 = startNewCall(TestMethodDescriptors.voidMethod(), configSelector, Collections.<String, String>emptyMap(), CallOptions.DEFAULT);
    verifyRpcDelayed(observer3, 5000L);
}
Also used : InternalConfigSelector(io.grpc.InternalConfigSelector) ClientCall(io.grpc.ClientCall) NoopClientCall(io.grpc.internal.NoopClientCall) ResolutionResult(io.grpc.NameResolver.ResolutionResult) Test(org.junit.Test)

Aggregations

ResolutionResult (io.grpc.NameResolver.ResolutionResult)32 Test (org.junit.Test)29 InternalConfigSelector (io.grpc.InternalConfigSelector)16 HashPolicy (io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy)11 AddressResolver (io.grpc.internal.DnsNameResolver.AddressResolver)10 ResourceResolver (io.grpc.internal.DnsNameResolver.ResourceResolver)9 InetAddress (java.net.InetAddress)8 InetSocketAddress (java.net.InetSocketAddress)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 ClientCall (io.grpc.ClientCall)7 NoopClientCall (io.grpc.internal.NoopClientCall)7 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)5 UnknownHostException (java.net.UnknownHostException)5 JndiResourceResolver (io.grpc.internal.JndiResourceResolverFactory.JndiResourceResolver)4 IOException (java.io.IOException)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Route (io.grpc.xds.VirtualHost.Route)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Result (io.grpc.InternalConfigSelector.Result)2