• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19438

28 Aug 2024 09:34PM UTC coverage: 84.493% (-0.02%) from 84.516%
#19438

push

github

ejona86
xds: ClusterManagerLB must update child configuration

While child LB policies are unlikey to change for each cluster name (RLS
returns regular cluster names, so should be unique), and the
configuration for CDS policies won't change, RLS configuration can
definitely change.

33407 of 39538 relevant lines covered (84.49%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.7
/../xds/src/main/java/io/grpc/xds/ClusterManagerLoadBalancerProvider.java
1
/*
2
 * Copyright 2020 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.xds;
18

19
import com.google.common.annotations.VisibleForTesting;
20
import com.google.common.base.MoreObjects;
21
import io.grpc.Internal;
22
import io.grpc.LoadBalancer;
23
import io.grpc.LoadBalancer.Helper;
24
import io.grpc.LoadBalancerProvider;
25
import io.grpc.LoadBalancerRegistry;
26
import io.grpc.NameResolver.ConfigOrError;
27
import io.grpc.Status;
28
import io.grpc.internal.JsonUtil;
29
import io.grpc.util.GracefulSwitchLoadBalancer;
30
import java.util.Collections;
31
import java.util.LinkedHashMap;
32
import java.util.Map;
33
import java.util.Objects;
34
import javax.annotation.Nullable;
35

36
/**
37
 * The provider for the cluster_manager load balancing policy. This class should not be directly
38
 * referenced in code.  The policy should be accessed through
39
 * {@link LoadBalancerRegistry#getProvider} with the name "cluster_manager_experimental".
40
 */
41
@Internal
42
public class ClusterManagerLoadBalancerProvider extends LoadBalancerProvider {
43

44
  @Nullable
45
  private final LoadBalancerRegistry lbRegistry;
46

47
  public ClusterManagerLoadBalancerProvider() {
48
    this(null);
1✔
49
  }
1✔
50

51
  @VisibleForTesting
52
  ClusterManagerLoadBalancerProvider(@Nullable LoadBalancerRegistry lbRegistry) {
1✔
53
    this.lbRegistry = lbRegistry;
1✔
54
  }
1✔
55

56
  @Override
57
  public boolean isAvailable() {
58
    return true;
1✔
59
  }
60

61
  @Override
62
  public int getPriority() {
63
    return 5;
1✔
64
  }
65

66
  @Override
67
  public String getPolicyName() {
68
    return XdsLbPolicies.CLUSTER_MANAGER_POLICY_NAME;
1✔
69
  }
70

71
  @Override
72
  public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
73
    Map<String, Object> parsedChildPolicies = new LinkedHashMap<>();
1✔
74
    try {
75
      Map<String, ?> childPolicies = JsonUtil.getObject(rawConfig, "childPolicy");
1✔
76
      if (childPolicies == null || childPolicies.isEmpty()) {
1✔
77
        return ConfigOrError.fromError(Status.INTERNAL.withDescription(
1✔
78
            "No child policy provided for cluster_manager LB policy: " + rawConfig));
79
      }
80
      for (String name : childPolicies.keySet()) {
1✔
81
        Map<String, ?> childPolicy = JsonUtil.getObject(childPolicies, name);
1✔
82
        if (childPolicy == null) {
1✔
83
          return ConfigOrError.fromError(Status.INTERNAL.withDescription(
×
84
              "No config for child " + name + " in cluster_manager LB policy: " + rawConfig));
85
        }
86
        LoadBalancerRegistry registry =
87
            lbRegistry != null ? lbRegistry : LoadBalancerRegistry.getDefaultRegistry();
1✔
88
        ConfigOrError childConfig = GracefulSwitchLoadBalancer.parseLoadBalancingPolicyConfig(
1✔
89
            JsonUtil.getListOfObjects(childPolicy, "lbPolicy"), registry);
1✔
90
        if (childConfig.getError() != null) {
1✔
91
          Status error = childConfig.getError();
×
92
          return ConfigOrError.fromError(
×
93
              Status.INTERNAL
94
                  .withCause(error.getCause())
×
95
                  .withDescription(error.getDescription())
×
96
                  .augmentDescription("Failed to parse config for child " + name));
×
97
        }
98
        parsedChildPolicies.put(name, childConfig.getConfig());
1✔
99
      }
1✔
100
    } catch (RuntimeException e) {
×
101
      return ConfigOrError.fromError(
×
102
          Status.INTERNAL.withCause(e).withDescription(
×
103
              "Failed to parse cluster_manager LB config: " + rawConfig));
104
    }
1✔
105
    return ConfigOrError.fromConfig(new ClusterManagerConfig(parsedChildPolicies));
1✔
106
  }
107

108
  @Override
109
  public LoadBalancer newLoadBalancer(Helper helper) {
110
    return new ClusterManagerLoadBalancer(helper);
1✔
111
  }
112

113
  static class ClusterManagerConfig {
114
    final Map<String, Object> childPolicies;
115

116
    ClusterManagerConfig(Map<String, Object> childPolicies) {
1✔
117
      this.childPolicies = Collections.unmodifiableMap(childPolicies);
1✔
118
    }
1✔
119

120
    @Override
121
    public boolean equals(Object o) {
122
      if (this == o) {
×
123
        return true;
×
124
      }
125
      if (!(o instanceof ClusterManagerConfig)) {
×
126
        return false;
×
127
      }
128
      ClusterManagerConfig config = (ClusterManagerConfig) o;
×
129
      return Objects.equals(childPolicies, config.childPolicies);
×
130
    }
131

132
    @Override
133
    public int hashCode() {
134
      return Objects.hash(childPolicies);
×
135
    }
136

137
    @Override
138
    public String toString() {
139
      return MoreObjects.toStringHelper(this)
×
140
          .add("childPolicies", childPolicies)
×
141
          .toString();
×
142
    }
143
  }
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc