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

grpc / grpc-java / #19249

24 May 2024 10:08PM UTC coverage: 88.444% (-0.07%) from 88.512%
#19249

push

github

web-flow
xds: Plumb the Cluster's filterMetadata to RPCs

This will be used by CSM observability, and may get exposed to further
uses in the future.

32060 of 36249 relevant lines covered (88.44%)

0.88 hits per line

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

39.68
/../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.NameResolver.ConfigOrError;
29
import io.grpc.Status;
30
import io.grpc.internal.ServiceConfigUtil.PolicySelection;
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.List;
35
import java.util.Map;
36
import java.util.Objects;
37
import javax.annotation.Nullable;
38

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

47
  @Override
48
  public boolean isAvailable() {
49
    return true;
1✔
50
  }
51

52
  @Override
53
  public int getPriority() {
54
    return 5;
1✔
55
  }
56

57
  @Override
58
  public String getPolicyName() {
59
    return XdsLbPolicies.CLUSTER_RESOLVER_POLICY_NAME;
1✔
60
  }
61

62
  @Override
63
  public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawLoadBalancingPolicyConfig) {
64
    return ConfigOrError.fromError(
×
65
        Status.INTERNAL.withDescription(getPolicyName() + " cannot be used from service config"));
×
66
  }
67

68
  @Override
69
  public LoadBalancer newLoadBalancer(Helper helper) {
70
    return new ClusterResolverLoadBalancer(helper);
1✔
71
  }
72

73
  static final class ClusterResolverConfig {
74
    // Ordered list of clusters to be resolved.
75
    final List<DiscoveryMechanism> discoveryMechanisms;
76
    // Endpoint-level load balancing policy with config
77
    // (round_robin, least_request_experimental or ring_hash_experimental).
78
    final PolicySelection lbPolicy;
79

80
    ClusterResolverConfig(List<DiscoveryMechanism> discoveryMechanisms, PolicySelection lbPolicy) {
1✔
81
      this.discoveryMechanisms = checkNotNull(discoveryMechanisms, "discoveryMechanisms");
1✔
82
      this.lbPolicy = checkNotNull(lbPolicy, "lbPolicy");
1✔
83
    }
1✔
84

85
    @Override
86
    public int hashCode() {
87
      return Objects.hash(discoveryMechanisms, lbPolicy);
×
88
    }
89

90
    @Override
91
    public boolean equals(Object o) {
92
      if (this == o) {
×
93
        return true;
×
94
      }
95
      if (o == null || getClass() != o.getClass()) {
×
96
        return false;
×
97
      }
98
      ClusterResolverConfig that = (ClusterResolverConfig) o;
×
99
      return discoveryMechanisms.equals(that.discoveryMechanisms)
×
100
          && lbPolicy.equals(that.lbPolicy);
×
101
    }
102

103
    @Override
104
    public String toString() {
105
      return MoreObjects.toStringHelper(this)
×
106
          .add("discoveryMechanisms", discoveryMechanisms)
×
107
          .add("lbPolicy", lbPolicy)
×
108
          .toString();
×
109
    }
110

111
    // Describes the mechanism for a specific cluster.
112
    static final class DiscoveryMechanism {
113
      // Name of the cluster to resolve.
114
      final String cluster;
115
      // Type of the cluster.
116
      final Type type;
117
      // Load reporting server info. Null if not enabled.
118
      @Nullable
119
      final ServerInfo lrsServerInfo;
120
      // Cluster-level max concurrent request threshold. Null if not specified.
121
      @Nullable
122
      final Long maxConcurrentRequests;
123
      // TLS context for connections to endpoints in the cluster.
124
      @Nullable
125
      final UpstreamTlsContext tlsContext;
126
      // Resource name for resolving endpoints via EDS. Only valid for EDS clusters.
127
      @Nullable
128
      final String edsServiceName;
129
      // Hostname for resolving endpoints via DNS. Only valid for LOGICAL_DNS clusters.
130
      @Nullable
131
      final String dnsHostName;
132
      @Nullable
133
      final OutlierDetection outlierDetection;
134
      final Map<String, Struct> filterMetadata;
135

136
      enum Type {
1✔
137
        EDS,
1✔
138
        LOGICAL_DNS,
1✔
139
      }
140

141
      private DiscoveryMechanism(String cluster, Type type, @Nullable String edsServiceName,
142
          @Nullable String dnsHostName, @Nullable ServerInfo lrsServerInfo,
143
          @Nullable Long maxConcurrentRequests, @Nullable UpstreamTlsContext tlsContext,
144
          Map<String, Struct> filterMetadata, @Nullable OutlierDetection outlierDetection) {
1✔
145
        this.cluster = checkNotNull(cluster, "cluster");
1✔
146
        this.type = checkNotNull(type, "type");
1✔
147
        this.edsServiceName = edsServiceName;
1✔
148
        this.dnsHostName = dnsHostName;
1✔
149
        this.lrsServerInfo = lrsServerInfo;
1✔
150
        this.maxConcurrentRequests = maxConcurrentRequests;
1✔
151
        this.tlsContext = tlsContext;
1✔
152
        this.filterMetadata = ImmutableMap.copyOf(checkNotNull(filterMetadata, "filterMetadata"));
1✔
153
        this.outlierDetection = outlierDetection;
1✔
154
      }
1✔
155

156
      static DiscoveryMechanism forEds(String cluster, @Nullable String edsServiceName,
157
          @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests,
158
          @Nullable UpstreamTlsContext tlsContext, Map<String, Struct> filterMetadata,
159
          OutlierDetection outlierDetection) {
160
        return new DiscoveryMechanism(cluster, Type.EDS, edsServiceName, null, lrsServerInfo,
1✔
161
            maxConcurrentRequests, tlsContext, filterMetadata, outlierDetection);
162
      }
163

164
      static DiscoveryMechanism forLogicalDns(String cluster, String dnsHostName,
165
          @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests,
166
          @Nullable UpstreamTlsContext tlsContext, Map<String, Struct> filterMetadata) {
167
        return new DiscoveryMechanism(cluster, Type.LOGICAL_DNS, null, dnsHostName,
1✔
168
            lrsServerInfo, maxConcurrentRequests, tlsContext, filterMetadata, null);
169
      }
170

171
      @Override
172
      public int hashCode() {
173
        return Objects.hash(cluster, type, lrsServerInfo, maxConcurrentRequests, tlsContext,
×
174
            edsServiceName, dnsHostName, filterMetadata, outlierDetection);
175
      }
176

177
      @Override
178
      public boolean equals(Object o) {
179
        if (this == o) {
×
180
          return true;
×
181
        }
182
        if (o == null || getClass() != o.getClass()) {
×
183
          return false;
×
184
        }
185
        DiscoveryMechanism that = (DiscoveryMechanism) o;
×
186
        return cluster.equals(that.cluster)
×
187
            && type == that.type
188
            && Objects.equals(edsServiceName, that.edsServiceName)
×
189
            && Objects.equals(dnsHostName, that.dnsHostName)
×
190
            && Objects.equals(lrsServerInfo, that.lrsServerInfo)
×
191
            && Objects.equals(maxConcurrentRequests, that.maxConcurrentRequests)
×
192
            && Objects.equals(tlsContext, that.tlsContext)
×
193
            && Objects.equals(filterMetadata, that.filterMetadata)
×
194
            && Objects.equals(outlierDetection, that.outlierDetection);
×
195
      }
196

197
      @Override
198
      public String toString() {
199
        MoreObjects.ToStringHelper toStringHelper =
×
200
            MoreObjects.toStringHelper(this)
×
201
                .add("cluster", cluster)
×
202
                .add("type", type)
×
203
                .add("edsServiceName", edsServiceName)
×
204
                .add("dnsHostName", dnsHostName)
×
205
                .add("lrsServerInfo", lrsServerInfo)
×
206
                // Exclude tlsContext as its string representation is cumbersome.
207
                .add("maxConcurrentRequests", maxConcurrentRequests)
×
208
                .add("filterMetadata", filterMetadata)
×
209
                // Exclude outlierDetection as its string representation is long.
210
                ;
211
        return toStringHelper.toString();
×
212
      }
213
    }
214
  }
215
}
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

© 2026 Coveralls, Inc