Search in sources :

Example 31 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class WeightedTargetLoadBalancerProvider method parseLoadBalancingPolicyConfig.

@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
    try {
        Map<String, ?> targets = JsonUtil.getObject(rawConfig, "targets");
        if (targets == null || targets.isEmpty()) {
            return ConfigOrError.fromError(Status.INTERNAL.withDescription("No targets provided for weighted_target LB policy:\n " + rawConfig));
        }
        Map<String, WeightedPolicySelection> parsedChildConfigs = new LinkedHashMap<>();
        for (String name : targets.keySet()) {
            Map<String, ?> rawWeightedTarget = JsonUtil.getObject(targets, name);
            if (rawWeightedTarget == null || rawWeightedTarget.isEmpty()) {
                return ConfigOrError.fromError(Status.INTERNAL.withDescription("No config for target " + name + " in weighted_target LB policy:\n " + rawConfig));
            }
            Integer weight = JsonUtil.getNumberAsInteger(rawWeightedTarget, "weight");
            if (weight == null || weight < 1) {
                return ConfigOrError.fromError(Status.INTERNAL.withDescription("Wrong weight for target " + name + " in weighted_target LB policy:\n " + rawConfig));
            }
            List<LbConfig> childConfigCandidates = ServiceConfigUtil.unwrapLoadBalancingConfigList(JsonUtil.getListOfObjects(rawWeightedTarget, "childPolicy"));
            if (childConfigCandidates == null || childConfigCandidates.isEmpty()) {
                return ConfigOrError.fromError(Status.INTERNAL.withDescription("No child policy for target " + name + " in weighted_target LB policy:\n " + rawConfig));
            }
            LoadBalancerRegistry lbRegistry = this.lbRegistry == null ? LoadBalancerRegistry.getDefaultRegistry() : this.lbRegistry;
            ConfigOrError selectedConfig = ServiceConfigUtil.selectLbPolicyFromList(childConfigCandidates, lbRegistry);
            if (selectedConfig.getError() != null) {
                return selectedConfig;
            }
            PolicySelection policySelection = (PolicySelection) selectedConfig.getConfig();
            parsedChildConfigs.put(name, new WeightedPolicySelection(weight, policySelection));
        }
        return ConfigOrError.fromConfig(new WeightedTargetConfig(parsedChildConfigs));
    } catch (RuntimeException e) {
        return ConfigOrError.fromError(Status.fromThrowable(e).withDescription("Failed to parse weighted_target LB config: " + rawConfig));
    }
}
Also used : LoadBalancerRegistry(io.grpc.LoadBalancerRegistry) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) LinkedHashMap(java.util.LinkedHashMap) LbConfig(io.grpc.internal.ServiceConfigUtil.LbConfig) ConfigOrError(io.grpc.NameResolver.ConfigOrError)

Example 32 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class ClusterManagerLoadBalancer method handleResolvedAddresses.

@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
    logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
    ClusterManagerConfig config = (ClusterManagerConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
    Map<String, PolicySelection> newChildPolicies = config.childPolicies;
    logger.log(XdsLogLevel.INFO, "Received cluster_manager lb config: child names={0}", newChildPolicies.keySet());
    for (Map.Entry<String, PolicySelection> entry : newChildPolicies.entrySet()) {
        final String name = entry.getKey();
        LoadBalancerProvider childPolicyProvider = entry.getValue().getProvider();
        Object childConfig = entry.getValue().getConfig();
        if (!childLbStates.containsKey(name)) {
            childLbStates.put(name, new ChildLbState(name, childPolicyProvider));
        } else {
            childLbStates.get(name).reactivate(childPolicyProvider);
        }
        LoadBalancer childLb = childLbStates.get(name).lb;
        ResolvedAddresses childAddresses = resolvedAddresses.toBuilder().setLoadBalancingPolicyConfig(childConfig).build();
        childLb.handleResolvedAddresses(childAddresses);
    }
    for (String name : childLbStates.keySet()) {
        if (!newChildPolicies.containsKey(name)) {
            childLbStates.get(name).deactivate();
        }
    }
    // Must update channel picker before return so that new RPCs will not be routed to deleted
    // clusters and resolver can remove them in service config.
    updateOverallBalancingState();
}
Also used : ClusterManagerConfig(io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig) LoadBalancerProvider(io.grpc.LoadBalancerProvider) LoadBalancer(io.grpc.LoadBalancer) GracefulSwitchLoadBalancer(io.grpc.util.GracefulSwitchLoadBalancer) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection 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 34 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class ClusterImplLoadBalancerTest method handleResolvedAddresses_propagateToChildPolicy.

@Test
public void handleResolvedAddresses_propagateToChildPolicy() {
    FakeLoadBalancerProvider weightedTargetProvider = new FakeLoadBalancerProvider(XdsLbPolicies.WEIGHTED_TARGET_POLICY_NAME);
    Object weightedTargetConfig = new Object();
    ClusterImplConfig config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
    EquivalentAddressGroup endpoint = makeAddress("endpoint-addr", locality);
    deliverAddressesAndConfig(Collections.singletonList(endpoint), config);
    FakeLoadBalancer childBalancer = Iterables.getOnlyElement(downstreamBalancers);
    assertThat(Iterables.getOnlyElement(childBalancer.addresses)).isEqualTo(endpoint);
    assertThat(childBalancer.config).isSameInstanceAs(weightedTargetConfig);
    assertThat(childBalancer.attributes.get(InternalXdsAttributes.XDS_CLIENT_POOL)).isSameInstanceAs(xdsClientPool);
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) Test(org.junit.Test)

Example 35 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class ClusterImplLoadBalancerTest method endpointAddressesAttachedWithClusterName.

@Test
public void endpointAddressesAttachedWithClusterName() {
    LoadBalancerProvider weightedTargetProvider = new WeightedTargetLoadBalancerProvider();
    WeightedTargetConfig weightedTargetConfig = buildWeightedTargetConfig(ImmutableMap.of(locality, 10));
    ClusterImplConfig config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
    // One locality with two endpoints.
    EquivalentAddressGroup endpoint1 = makeAddress("endpoint-addr1", locality);
    EquivalentAddressGroup endpoint2 = makeAddress("endpoint-addr2", locality);
    deliverAddressesAndConfig(Arrays.asList(endpoint1, endpoint2), config);
    // one leaf balancer
    assertThat(downstreamBalancers).hasSize(1);
    FakeLoadBalancer leafBalancer = Iterables.getOnlyElement(downstreamBalancers);
    assertThat(leafBalancer.name).isEqualTo("round_robin");
    // Simulates leaf load balancer creating subchannels.
    CreateSubchannelArgs args = CreateSubchannelArgs.newBuilder().setAddresses(leafBalancer.addresses).build();
    Subchannel subchannel = leafBalancer.helper.createSubchannel(args);
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        assertThat(eag.getAttributes().get(InternalXdsAttributes.ATTR_CLUSTER_NAME)).isEqualTo(CLUSTER);
    }
}
Also used : CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) LoadBalancerProvider(io.grpc.LoadBalancerProvider) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) Test(org.junit.Test)

Aggregations

PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)36 Test (org.junit.Test)27 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)20 LoadBalancerProvider (io.grpc.LoadBalancerProvider)13 PriorityChildConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig)12 WeightedPolicySelection (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)12 Helper (io.grpc.LoadBalancer.Helper)10 ConfigOrError (io.grpc.NameResolver.ConfigOrError)10 ClusterImplConfig (io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig)10 PriorityLbConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig)10 WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)9 Subchannel (io.grpc.LoadBalancer.Subchannel)8 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)7 PickResult (io.grpc.LoadBalancer.PickResult)6 LoadBalancer (io.grpc.LoadBalancer)5 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)4 ClusterStats (io.grpc.xds.Stats.ClusterStats)4 SocketAddress (java.net.SocketAddress)4 Map (java.util.Map)4 Attributes (io.grpc.Attributes)3