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

grpc / grpc-java / #19986

18 Sep 2025 06:08PM UTC coverage: 88.539% (-0.008%) from 88.547%
#19986

push

github

web-flow
xds: Convert ClusterResolverLb to XdsDepManager

No longer need to hard-code pick_first because of gRFC A61.
https://github.com/grpc/proposal/pull/477

34664 of 39151 relevant lines covered (88.54%)

0.89 hits per line

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

95.95
/../xds/src/main/java/io/grpc/xds/ClusterResolverLoadBalancerProvider.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 static com.google.common.base.Preconditions.checkNotNull;
20

21
import com.google.common.base.MoreObjects;
22
import com.google.common.collect.ImmutableMap;
23
import com.google.protobuf.Struct;
24
import io.grpc.Internal;
25
import io.grpc.LoadBalancer;
26
import io.grpc.LoadBalancer.Helper;
27
import io.grpc.LoadBalancerProvider;
28
import io.grpc.LoadBalancerRegistry;
29
import io.grpc.NameResolver.ConfigOrError;
30
import io.grpc.Status;
31
import io.grpc.xds.EnvoyServerProtoData.OutlierDetection;
32
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
33
import io.grpc.xds.client.Bootstrapper.ServerInfo;
34
import java.util.Map;
35
import java.util.Objects;
36
import javax.annotation.Nullable;
37

38
/**
39
 * The provider for the cluster_resolver load balancing policy. This class should not be directly
40
 * referenced in code.  The policy should be accessed through
41
 * {@link io.grpc.LoadBalancerRegistry#getProvider} with the name "cluster_resolver_experimental".
42
 */
43
@Internal
44
public final class ClusterResolverLoadBalancerProvider extends LoadBalancerProvider {
45
  private final LoadBalancerRegistry lbRegistry;
46

47
  public ClusterResolverLoadBalancerProvider() {
1✔
48
    this.lbRegistry = null;
1✔
49
  }
1✔
50

51
  ClusterResolverLoadBalancerProvider(LoadBalancerRegistry lbRegistry) {
1✔
52
    this.lbRegistry = checkNotNull(lbRegistry, "lbRegistry");
1✔
53
  }
1✔
54

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

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

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

70
  @Override
71
  public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawLoadBalancingPolicyConfig) {
72
    return ConfigOrError.fromError(
×
73
        Status.INTERNAL.withDescription(getPolicyName() + " cannot be used from service config"));
×
74
  }
75

76
  @Override
77
  public LoadBalancer newLoadBalancer(Helper helper) {
78
    LoadBalancerRegistry lbRegistry = this.lbRegistry;
1✔
79
    if (lbRegistry == null) {
1✔
80
      lbRegistry = LoadBalancerRegistry.getDefaultRegistry();
1✔
81
    }
82
    return new ClusterResolverLoadBalancer(helper, lbRegistry);
1✔
83
  }
84

85
  static final class ClusterResolverConfig {
86
    // Cluster to be resolved.
87
    final DiscoveryMechanism discoveryMechanism;
88
    // GracefulSwitch configuration
89
    final Object lbConfig;
90
    private final boolean isHttp11ProxyAvailable;
91

92
    ClusterResolverConfig(DiscoveryMechanism discoveryMechanism, Object lbConfig,
93
        boolean isHttp11ProxyAvailable) {
1✔
94
      this.discoveryMechanism = checkNotNull(discoveryMechanism, "discoveryMechanism");
1✔
95
      this.lbConfig = checkNotNull(lbConfig, "lbConfig");
1✔
96
      this.isHttp11ProxyAvailable = isHttp11ProxyAvailable;
1✔
97
    }
1✔
98

99
    boolean isHttp11ProxyAvailable() {
100
      return isHttp11ProxyAvailable;
1✔
101
    }
102

103
    @Override
104
    public int hashCode() {
105
      return Objects.hash(discoveryMechanism, lbConfig, isHttp11ProxyAvailable);
1✔
106
    }
107

108
    @Override
109
    public boolean equals(Object o) {
110
      if (this == o) {
1✔
111
        return true;
1✔
112
      }
113
      if (o == null || getClass() != o.getClass()) {
1✔
114
        return false;
1✔
115
      }
116
      ClusterResolverConfig that = (ClusterResolverConfig) o;
1✔
117
      return discoveryMechanism.equals(that.discoveryMechanism)
1✔
118
          && lbConfig.equals(that.lbConfig)
1✔
119
          && isHttp11ProxyAvailable == that.isHttp11ProxyAvailable;
120
    }
121

122
    @Override
123
    public String toString() {
124
      return MoreObjects.toStringHelper(this)
1✔
125
          .add("discoveryMechanism", discoveryMechanism)
1✔
126
          .add("lbConfig", lbConfig)
1✔
127
          .add("isHttp11ProxyAvailable", isHttp11ProxyAvailable)
1✔
128
          .toString();
1✔
129
    }
130

131
    // Describes the mechanism for a specific cluster.
132
    static final class DiscoveryMechanism {
133
      // Name of the cluster to resolve.
134
      final String cluster;
135
      // Type of the cluster.
136
      final Type type;
137
      // Load reporting server info. Null if not enabled.
138
      @Nullable
139
      final ServerInfo lrsServerInfo;
140
      // Cluster-level max concurrent request threshold. Null if not specified.
141
      @Nullable
142
      final Long maxConcurrentRequests;
143
      // TLS context for connections to endpoints in the cluster.
144
      @Nullable
145
      final UpstreamTlsContext tlsContext;
146
      // Resource name for resolving endpoints via EDS. Only valid for EDS clusters.
147
      @Nullable
148
      final String edsServiceName;
149
      // Hostname for resolving endpoints via DNS. Only valid for LOGICAL_DNS clusters.
150
      @Nullable
151
      final String dnsHostName;
152
      @Nullable
153
      final OutlierDetection outlierDetection;
154
      final Map<String, Struct> filterMetadata;
155

156
      enum Type {
1✔
157
        EDS,
1✔
158
        LOGICAL_DNS,
1✔
159
      }
160

161
      private DiscoveryMechanism(String cluster, Type type, @Nullable String edsServiceName,
162
          @Nullable String dnsHostName, @Nullable ServerInfo lrsServerInfo,
163
          @Nullable Long maxConcurrentRequests, @Nullable UpstreamTlsContext tlsContext,
164
          Map<String, Struct> filterMetadata, @Nullable OutlierDetection outlierDetection) {
1✔
165
        this.cluster = checkNotNull(cluster, "cluster");
1✔
166
        this.type = checkNotNull(type, "type");
1✔
167
        this.edsServiceName = edsServiceName;
1✔
168
        this.dnsHostName = dnsHostName;
1✔
169
        this.lrsServerInfo = lrsServerInfo;
1✔
170
        this.maxConcurrentRequests = maxConcurrentRequests;
1✔
171
        this.tlsContext = tlsContext;
1✔
172
        this.filterMetadata = ImmutableMap.copyOf(checkNotNull(filterMetadata, "filterMetadata"));
1✔
173
        this.outlierDetection = outlierDetection;
1✔
174
      }
1✔
175

176
      static DiscoveryMechanism forEds(String cluster, @Nullable String edsServiceName,
177
          @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests,
178
          @Nullable UpstreamTlsContext tlsContext, Map<String, Struct> filterMetadata,
179
          OutlierDetection outlierDetection) {
180
        return new DiscoveryMechanism(cluster, Type.EDS, edsServiceName, null, lrsServerInfo,
1✔
181
            maxConcurrentRequests, tlsContext, filterMetadata, outlierDetection);
182
      }
183

184
      static DiscoveryMechanism forLogicalDns(String cluster, String dnsHostName,
185
          @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests,
186
          @Nullable UpstreamTlsContext tlsContext, Map<String, Struct> filterMetadata) {
187
        return new DiscoveryMechanism(cluster, Type.LOGICAL_DNS, null, dnsHostName,
1✔
188
            lrsServerInfo, maxConcurrentRequests, tlsContext, filterMetadata, null);
189
      }
190

191
      @Override
192
      public int hashCode() {
193
        return Objects.hash(cluster, type, lrsServerInfo, maxConcurrentRequests, tlsContext,
1✔
194
            edsServiceName, dnsHostName, filterMetadata, outlierDetection);
195
      }
196

197
      @Override
198
      public boolean equals(Object o) {
199
        if (this == o) {
1✔
200
          return true;
1✔
201
        }
202
        if (o == null || getClass() != o.getClass()) {
1✔
203
          return false;
×
204
        }
205
        DiscoveryMechanism that = (DiscoveryMechanism) o;
1✔
206
        return cluster.equals(that.cluster)
1✔
207
            && type == that.type
208
            && Objects.equals(edsServiceName, that.edsServiceName)
1✔
209
            && Objects.equals(dnsHostName, that.dnsHostName)
1✔
210
            && Objects.equals(lrsServerInfo, that.lrsServerInfo)
1✔
211
            && Objects.equals(maxConcurrentRequests, that.maxConcurrentRequests)
1✔
212
            && Objects.equals(tlsContext, that.tlsContext)
1✔
213
            && Objects.equals(filterMetadata, that.filterMetadata)
1✔
214
            && Objects.equals(outlierDetection, that.outlierDetection);
1✔
215
      }
216

217
      @Override
218
      public String toString() {
219
        MoreObjects.ToStringHelper toStringHelper =
1✔
220
            MoreObjects.toStringHelper(this)
1✔
221
                .add("cluster", cluster)
1✔
222
                .add("type", type)
1✔
223
                .add("edsServiceName", edsServiceName)
1✔
224
                .add("dnsHostName", dnsHostName)
1✔
225
                .add("lrsServerInfo", lrsServerInfo)
1✔
226
                // Exclude tlsContext as its string representation is cumbersome.
227
                .add("maxConcurrentRequests", maxConcurrentRequests)
1✔
228
                .add("filterMetadata", filterMetadata)
1✔
229
                // Exclude outlierDetection as its string representation is long.
230
                ;
231
        return toStringHelper.toString();
1✔
232
      }
233
    }
234
  }
235
}
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