Search in sources :

Example 16 with PriorityLbConfig

use of io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig in project grpc-java by grpc.

the class PriorityLoadBalancer method handleResolvedAddresses.

@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
    logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
    this.resolvedAddresses = resolvedAddresses;
    PriorityLbConfig config = (PriorityLbConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
    checkNotNull(config, "missing priority lb config");
    priorityNames = config.priorities;
    priorityConfigs = config.childConfigs;
    Set<String> prioritySet = new HashSet<>(config.priorities);
    for (String priority : children.keySet()) {
        if (!prioritySet.contains(priority)) {
            children.get(priority).deactivate();
        }
    }
    for (String priority : priorityNames) {
        if (children.containsKey(priority)) {
            children.get(priority).updateResolvedAddresses();
        }
    }
    // Not to report connecting in case a pending priority bumps up on top of the current READY
    // priority.
    tryNextPriority(false);
}
Also used : PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) HashSet(java.util.HashSet)

Example 17 with PriorityLbConfig

use of io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig in project grpc-java by grpc.

the class PriorityLoadBalancerProviderTest method priorityLbConfig_missingChildConfig.

@SuppressWarnings("ExpectedExceptionChecker")
@Test
public void priorityLbConfig_missingChildConfig() {
    Map<String, PriorityChildConfig> childConfigs = ImmutableMap.of("p1", new PriorityChildConfig(new PolicySelection(mock(LoadBalancerProvider.class), null), true));
    List<String> priorities = ImmutableList.of("p0", "p1");
    thrown.expect(IllegalArgumentException.class);
    new PriorityLbConfig(childConfigs, priorities);
}
Also used : PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) LoadBalancerProvider(io.grpc.LoadBalancerProvider) PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Test(org.junit.Test)

Example 18 with PriorityLbConfig

use of io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig in project grpc-java by grpc.

the class ClusterResolverLoadBalancerTest method onlyEdsClusters_receivedEndpoints.

@Test
public void onlyEdsClusters_receivedEndpoints() {
    ClusterResolverConfig config = new ClusterResolverConfig(Arrays.asList(edsDiscoveryMechanism1, edsDiscoveryMechanism2), roundRobin);
    deliverLbConfig(config);
    assertThat(xdsClient.watchers.keySet()).containsExactly(EDS_SERVICE_NAME1, EDS_SERVICE_NAME2);
    assertThat(childBalancers).isEmpty();
    // CLUSTER1 has priority 1 (priority3), which has locality 2, which has endpoint3.
    // CLUSTER2 has priority 1 (priority1) and 2 (priority2); priority1 has locality1,
    // which has endpoint1 and endpoint2; priority2 has locality3, which has endpoint4.
    EquivalentAddressGroup endpoint1 = makeAddress("endpoint-addr-1");
    EquivalentAddressGroup endpoint2 = makeAddress("endpoint-addr-2");
    EquivalentAddressGroup endpoint3 = makeAddress("endpoint-addr-3");
    EquivalentAddressGroup endpoint4 = makeAddress("endpoint-addr-4");
    LocalityLbEndpoints localityLbEndpoints1 = LocalityLbEndpoints.create(Arrays.asList(LbEndpoint.create(endpoint1, 100, true), LbEndpoint.create(endpoint2, 100, true)), 70, /* localityWeight */
    1);
    LocalityLbEndpoints localityLbEndpoints2 = LocalityLbEndpoints.create(Collections.singletonList(LbEndpoint.create(endpoint3, 100, true)), 10, /* localityWeight */
    1);
    LocalityLbEndpoints localityLbEndpoints3 = LocalityLbEndpoints.create(Collections.singletonList(LbEndpoint.create(endpoint4, 100, true)), 20, /* localityWeight */
    2);
    String priority1 = CLUSTER2 + "[priority1]";
    String priority2 = CLUSTER2 + "[priority2]";
    String priority3 = CLUSTER1 + "[priority1]";
    // CLUSTER2: locality1 with priority 1 and locality3 with priority 2.
    xdsClient.deliverClusterLoadAssignment(EDS_SERVICE_NAME2, ImmutableMap.of(locality1, localityLbEndpoints1, locality3, localityLbEndpoints3));
    // not created until all clusters resolved
    assertThat(childBalancers).isEmpty();
    // CLUSTER1: locality2 with priority 1.
    xdsClient.deliverClusterLoadAssignment(EDS_SERVICE_NAME1, Collections.singletonMap(locality2, localityLbEndpoints2));
    // Endpoints of all clusters have been resolved.
    assertThat(childBalancers).hasSize(1);
    FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
    assertThat(childBalancer.name).isEqualTo(PRIORITY_POLICY_NAME);
    PriorityLbConfig priorityLbConfig = (PriorityLbConfig) childBalancer.config;
    assertThat(priorityLbConfig.priorities).containsExactly(priority3, priority1, priority2).inOrder();
    PriorityChildConfig priorityChildConfig1 = priorityLbConfig.childConfigs.get(priority1);
    assertThat(priorityChildConfig1.ignoreReresolution).isTrue();
    assertThat(priorityChildConfig1.policySelection.getProvider().getPolicyName()).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
    ClusterImplConfig clusterImplConfig1 = (ClusterImplConfig) priorityChildConfig1.policySelection.getConfig();
    assertClusterImplConfig(clusterImplConfig1, CLUSTER2, EDS_SERVICE_NAME2, LRS_SERVER_INFO, 200L, tlsContext, Collections.<DropOverload>emptyList(), WEIGHTED_TARGET_POLICY_NAME);
    WeightedTargetConfig weightedTargetConfig1 = (WeightedTargetConfig) clusterImplConfig1.childPolicy.getConfig();
    assertThat(weightedTargetConfig1.targets.keySet()).containsExactly(locality1.toString());
    WeightedPolicySelection target1 = weightedTargetConfig1.targets.get(locality1.toString());
    assertThat(target1.weight).isEqualTo(70);
    assertThat(target1.policySelection.getProvider().getPolicyName()).isEqualTo("round_robin");
    PriorityChildConfig priorityChildConfig2 = priorityLbConfig.childConfigs.get(priority2);
    assertThat(priorityChildConfig2.ignoreReresolution).isTrue();
    assertThat(priorityChildConfig2.policySelection.getProvider().getPolicyName()).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
    ClusterImplConfig clusterImplConfig2 = (ClusterImplConfig) priorityChildConfig2.policySelection.getConfig();
    assertClusterImplConfig(clusterImplConfig2, CLUSTER2, EDS_SERVICE_NAME2, LRS_SERVER_INFO, 200L, tlsContext, Collections.<DropOverload>emptyList(), WEIGHTED_TARGET_POLICY_NAME);
    WeightedTargetConfig weightedTargetConfig2 = (WeightedTargetConfig) clusterImplConfig2.childPolicy.getConfig();
    assertThat(weightedTargetConfig2.targets.keySet()).containsExactly(locality3.toString());
    WeightedPolicySelection target2 = weightedTargetConfig2.targets.get(locality3.toString());
    assertThat(target2.weight).isEqualTo(20);
    assertThat(target2.policySelection.getProvider().getPolicyName()).isEqualTo("round_robin");
    List<EquivalentAddressGroup> priorityAddrs1 = AddressFilter.filter(childBalancer.addresses, priority1);
    assertThat(priorityAddrs1).hasSize(2);
    assertAddressesEqual(Arrays.asList(endpoint1, endpoint2), priorityAddrs1);
    List<EquivalentAddressGroup> priorityAddrs2 = AddressFilter.filter(childBalancer.addresses, priority2);
    assertThat(priorityAddrs2).hasSize(1);
    assertAddressesEqual(Collections.singletonList(endpoint4), priorityAddrs2);
    PriorityChildConfig priorityChildConfig3 = priorityLbConfig.childConfigs.get(priority3);
    assertThat(priorityChildConfig3.ignoreReresolution).isTrue();
    assertThat(priorityChildConfig3.policySelection.getProvider().getPolicyName()).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
    ClusterImplConfig clusterImplConfig3 = (ClusterImplConfig) priorityChildConfig3.policySelection.getConfig();
    assertClusterImplConfig(clusterImplConfig3, CLUSTER1, EDS_SERVICE_NAME1, LRS_SERVER_INFO, 100L, tlsContext, Collections.<DropOverload>emptyList(), WEIGHTED_TARGET_POLICY_NAME);
    WeightedTargetConfig weightedTargetConfig3 = (WeightedTargetConfig) clusterImplConfig3.childPolicy.getConfig();
    assertThat(weightedTargetConfig3.targets.keySet()).containsExactly(locality2.toString());
    WeightedPolicySelection target3 = weightedTargetConfig3.targets.get(locality2.toString());
    assertThat(target3.weight).isEqualTo(10);
    assertThat(target3.policySelection.getProvider().getPolicyName()).isEqualTo("round_robin");
    List<EquivalentAddressGroup> priorityAddrs3 = AddressFilter.filter(childBalancer.addresses, priority3);
    assertThat(priorityAddrs3).hasSize(1);
    assertAddressesEqual(Collections.singletonList(endpoint3), priorityAddrs3);
}
Also used : PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) ClusterResolverConfig(io.grpc.xds.ClusterResolverLoadBalancerProvider.ClusterResolverConfig) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) LocalityLbEndpoints(io.grpc.xds.Endpoints.LocalityLbEndpoints) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) Test(org.junit.Test)

Example 19 with PriorityLbConfig

use of io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig in project grpc-java by grpc.

the class ClusterResolverLoadBalancerTest method edsClustersWithLeastRequestEndpointLbPolicy.

@Test
public void edsClustersWithLeastRequestEndpointLbPolicy() {
    ClusterResolverConfig config = new ClusterResolverConfig(Collections.singletonList(edsDiscoveryMechanism1), leastRequest);
    deliverLbConfig(config);
    assertThat(xdsClient.watchers.keySet()).containsExactly(EDS_SERVICE_NAME1);
    assertThat(childBalancers).isEmpty();
    // Simple case with one priority and one locality
    EquivalentAddressGroup endpoint = makeAddress("endpoint-addr-1");
    LocalityLbEndpoints localityLbEndpoints = LocalityLbEndpoints.create(Arrays.asList(LbEndpoint.create(endpoint, 0, /* loadBalancingWeight */
    true)), 100, /* localityWeight */
    1);
    xdsClient.deliverClusterLoadAssignment(EDS_SERVICE_NAME1, ImmutableMap.of(locality1, localityLbEndpoints));
    assertThat(childBalancers).hasSize(1);
    FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
    assertThat(childBalancer.addresses).hasSize(1);
    EquivalentAddressGroup addr = childBalancer.addresses.get(0);
    assertThat(addr.getAddresses()).isEqualTo(endpoint.getAddresses());
    assertThat(childBalancer.name).isEqualTo(PRIORITY_POLICY_NAME);
    PriorityLbConfig priorityLbConfig = (PriorityLbConfig) childBalancer.config;
    assertThat(priorityLbConfig.priorities).containsExactly(CLUSTER1 + "[priority1]");
    PriorityChildConfig priorityChildConfig = Iterables.getOnlyElement(priorityLbConfig.childConfigs.values());
    assertThat(priorityChildConfig.policySelection.getProvider().getPolicyName()).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
    ClusterImplConfig clusterImplConfig = (ClusterImplConfig) priorityChildConfig.policySelection.getConfig();
    assertClusterImplConfig(clusterImplConfig, CLUSTER1, EDS_SERVICE_NAME1, LRS_SERVER_INFO, 100L, tlsContext, Collections.<DropOverload>emptyList(), WEIGHTED_TARGET_POLICY_NAME);
    WeightedTargetConfig weightedTargetConfig = (WeightedTargetConfig) clusterImplConfig.childPolicy.getConfig();
    assertThat(weightedTargetConfig.targets.keySet()).containsExactly(locality1.toString());
}
Also used : PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) ClusterResolverConfig(io.grpc.xds.ClusterResolverLoadBalancerProvider.ClusterResolverConfig) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) LocalityLbEndpoints(io.grpc.xds.Endpoints.LocalityLbEndpoints) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) Test(org.junit.Test)

Example 20 with PriorityLbConfig

use of io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig in project grpc-java by grpc.

the class ClusterResolverLoadBalancerTest method handleNameResolutionErrorFromUpstream_afterChildLbCreated_fallThrough.

@Test
public void handleNameResolutionErrorFromUpstream_afterChildLbCreated_fallThrough() {
    ClusterResolverConfig config = new ClusterResolverConfig(Arrays.asList(edsDiscoveryMechanism1, logicalDnsDiscoveryMechanism), roundRobin);
    deliverLbConfig(config);
    assertThat(xdsClient.watchers.keySet()).containsExactly(EDS_SERVICE_NAME1);
    FakeNameResolver resolver = assertResolverCreated("/" + DNS_HOST_NAME);
    assertThat(childBalancers).isEmpty();
    reset(helper);
    EquivalentAddressGroup endpoint1 = makeAddress("endpoint-addr-1");
    EquivalentAddressGroup endpoint2 = makeAddress("endpoint-addr-2");
    LocalityLbEndpoints localityLbEndpoints = LocalityLbEndpoints.create(Collections.singletonList(LbEndpoint.create(endpoint1, 100, true)), 10, /* localityWeight */
    1);
    xdsClient.deliverClusterLoadAssignment(EDS_SERVICE_NAME1, Collections.singletonMap(locality1, localityLbEndpoints));
    resolver.deliverEndpointAddresses(Collections.singletonList(endpoint2));
    assertThat(childBalancers).hasSize(1);
    FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
    assertThat(((PriorityLbConfig) childBalancer.config).priorities).containsExactly(CLUSTER1 + "[priority1]", CLUSTER_DNS + "[priority0]");
    assertAddressesEqual(Arrays.asList(endpoint1, endpoint2), childBalancer.addresses);
    loadBalancer.handleNameResolutionError(Status.UNAVAILABLE.withDescription("unreachable"));
    assertThat(childBalancer.upstreamError.getCode()).isEqualTo(Code.UNAVAILABLE);
    assertThat(childBalancer.upstreamError.getDescription()).isEqualTo("unreachable");
    verify(helper, never()).updateBalancingState(any(ConnectivityState.class), any(SubchannelPicker.class));
}
Also used : SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) ConnectivityState(io.grpc.ConnectivityState) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) ClusterResolverConfig(io.grpc.xds.ClusterResolverLoadBalancerProvider.ClusterResolverConfig) LocalityLbEndpoints(io.grpc.xds.Endpoints.LocalityLbEndpoints) Test(org.junit.Test)

Aggregations

PriorityLbConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig)20 Test (org.junit.Test)19 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)17 PriorityChildConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig)15 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)10 ClusterResolverConfig (io.grpc.xds.ClusterResolverLoadBalancerProvider.ClusterResolverConfig)9 LocalityLbEndpoints (io.grpc.xds.Endpoints.LocalityLbEndpoints)8 Helper (io.grpc.LoadBalancer.Helper)6 ClusterImplConfig (io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig)5 LoadBalancer (io.grpc.LoadBalancer)4 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)4 WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)3 ErrorPicker (io.grpc.xds.XdsSubchannelPickers.ErrorPicker)3 Attributes (io.grpc.Attributes)2 PickResult (io.grpc.LoadBalancer.PickResult)2 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)2 Subchannel (io.grpc.LoadBalancer.Subchannel)2 LoadBalancerProvider (io.grpc.LoadBalancerProvider)2 Status (io.grpc.Status)2 ConnectivityState (io.grpc.ConnectivityState)1