Search in sources :

Example 1 with GracefulSwitchLoadBalancer

use of io.grpc.util.GracefulSwitchLoadBalancer in project grpc-java by grpc.

the class WeightedTargetLoadBalancer method handleResolvedAddresses.

@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
    logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
    Object lbConfig = resolvedAddresses.getLoadBalancingPolicyConfig();
    checkNotNull(lbConfig, "missing weighted_target lb config");
    WeightedTargetConfig weightedTargetConfig = (WeightedTargetConfig) lbConfig;
    Map<String, WeightedPolicySelection> newTargets = weightedTargetConfig.targets;
    for (String targetName : newTargets.keySet()) {
        WeightedPolicySelection weightedChildLbConfig = newTargets.get(targetName);
        if (!targets.containsKey(targetName)) {
            ChildHelper childHelper = new ChildHelper(targetName);
            GracefulSwitchLoadBalancer childBalancer = new GracefulSwitchLoadBalancer(childHelper);
            childBalancer.switchTo(weightedChildLbConfig.policySelection.getProvider());
            childHelpers.put(targetName, childHelper);
            childBalancers.put(targetName, childBalancer);
        } else if (!weightedChildLbConfig.policySelection.getProvider().equals(targets.get(targetName).policySelection.getProvider())) {
            childBalancers.get(targetName).switchTo(weightedChildLbConfig.policySelection.getProvider());
        }
    }
    targets = newTargets;
    for (String targetName : targets.keySet()) {
        childBalancers.get(targetName).handleResolvedAddresses(resolvedAddresses.toBuilder().setAddresses(AddressFilter.filter(resolvedAddresses.getAddresses(), targetName)).setLoadBalancingPolicyConfig(targets.get(targetName).policySelection.getConfig()).build());
    }
    // TODO(zdapeng): cache removed target for 15 minutes.
    for (String targetName : childBalancers.keySet()) {
        if (!targets.containsKey(targetName)) {
            childBalancers.get(targetName).shutdown();
        }
    }
    childBalancers.keySet().retainAll(targets.keySet());
    childHelpers.keySet().retainAll(targets.keySet());
    updateOverallBalancingState();
}
Also used : GracefulSwitchLoadBalancer(io.grpc.util.GracefulSwitchLoadBalancer) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)

Aggregations

GracefulSwitchLoadBalancer (io.grpc.util.GracefulSwitchLoadBalancer)1 WeightedPolicySelection (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)1 WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)1