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 io.grpc.Internal; 23 import io.grpc.LoadBalancer; 24 import io.grpc.LoadBalancer.Helper; 25 import io.grpc.LoadBalancerProvider; 26 import io.grpc.LoadBalancerRegistry; 27 import io.grpc.NameResolver.ConfigOrError; 28 import io.grpc.Status; 29 import io.grpc.internal.ServiceConfigUtil.PolicySelection; 30 import io.grpc.xds.Bootstrapper.ServerInfo; 31 import io.grpc.xds.Endpoints.DropOverload; 32 import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext; 33 import java.util.ArrayList; 34 import java.util.Collections; 35 import java.util.List; 36 import java.util.Map; 37 import javax.annotation.Nullable; 38 39 /** 40 * The provider for the cluster_impl load balancing policy. This class should not be directly 41 * referenced in code. The policy should be accessed through 42 * {@link LoadBalancerRegistry#getProvider} with the name "cluster_impl_experimental". 43 */ 44 @Internal 45 public final class ClusterImplLoadBalancerProvider extends LoadBalancerProvider { 46 47 @Override isAvailable()48 public boolean isAvailable() { 49 return true; 50 } 51 52 @Override getPriority()53 public int getPriority() { 54 return 5; 55 } 56 57 @Override getPolicyName()58 public String getPolicyName() { 59 return XdsLbPolicies.CLUSTER_IMPL_POLICY_NAME; 60 } 61 62 @Override parseLoadBalancingPolicyConfig(Map<String, ?> rawLoadBalancingPolicyConfig)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 newLoadBalancer(Helper helper)69 public LoadBalancer newLoadBalancer(Helper helper) { 70 return new ClusterImplLoadBalancer(helper); 71 } 72 73 static final class ClusterImplConfig { 74 // Name of the cluster. 75 final String cluster; 76 // Resource name used in discovering endpoints via EDS. Only valid for EDS clusters. 77 @Nullable 78 final String edsServiceName; 79 // Load report server info. Null if load reporting is disabled. 80 @Nullable 81 final ServerInfo lrsServerInfo; 82 // Cluster-level max concurrent request threshold. Null if not specified. 83 @Nullable 84 final Long maxConcurrentRequests; 85 // TLS context for connections to endpoints. 86 @Nullable 87 final UpstreamTlsContext tlsContext; 88 // Drop configurations. 89 final List<DropOverload> dropCategories; 90 // Provides the direct child policy and its config. 91 final PolicySelection childPolicy; 92 ClusterImplConfig(String cluster, @Nullable String edsServiceName, @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests, List<DropOverload> dropCategories, PolicySelection childPolicy, @Nullable UpstreamTlsContext tlsContext)93 ClusterImplConfig(String cluster, @Nullable String edsServiceName, 94 @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests, 95 List<DropOverload> dropCategories, PolicySelection childPolicy, 96 @Nullable UpstreamTlsContext tlsContext) { 97 this.cluster = checkNotNull(cluster, "cluster"); 98 this.edsServiceName = edsServiceName; 99 this.lrsServerInfo = lrsServerInfo; 100 this.maxConcurrentRequests = maxConcurrentRequests; 101 this.tlsContext = tlsContext; 102 this.dropCategories = Collections.unmodifiableList( 103 new ArrayList<>(checkNotNull(dropCategories, "dropCategories"))); 104 this.childPolicy = checkNotNull(childPolicy, "childPolicy"); 105 } 106 107 @Override toString()108 public String toString() { 109 return MoreObjects.toStringHelper(this) 110 .add("cluster", cluster) 111 .add("edsServiceName", edsServiceName) 112 .add("lrsServerInfo", lrsServerInfo) 113 .add("maxConcurrentRequests", maxConcurrentRequests) 114 // Exclude tlsContext as its string representation is cumbersome. 115 .add("dropCategories", dropCategories) 116 .add("childPolicy", childPolicy) 117 .toString(); 118 } 119 } 120 } 121