Skip to content

Commit

Permalink
xds: Avoid depending on io.grpc.xds.Internal* classes
Browse files Browse the repository at this point in the history
Internal* classes should generally be accessors that are used outside of
the package/project. Only one attribute was used outside of xds, so
leave only that one attribute in InternalXdsAttributes. One attribute
was used by the internal.security package, so move the definition to the
same package to reduce the circular dependencies.
  • Loading branch information
ejona86 committed Jan 4, 2025
1 parent 1cf1927 commit 4a0f707
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 158 deletions.
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/CdsLoadBalancer2.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
}
logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
this.resolvedAddresses = resolvedAddresses;
xdsClientPool = resolvedAddresses.getAttributes().get(InternalXdsAttributes.XDS_CLIENT_POOL);
xdsClientPool = resolvedAddresses.getAttributes().get(XdsAttributes.XDS_CLIENT_POOL);
xdsClient = xdsClientPool.getObject();
CdsConfig config = (CdsConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
logger.log(XdsLogLevel.INFO, "Config: {0}", config);
Expand Down
19 changes: 10 additions & 9 deletions xds/src/main/java/io/grpc/xds/ClusterImplLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import io.grpc.xds.client.XdsClient;
import io.grpc.xds.client.XdsLogger;
import io.grpc.xds.client.XdsLogger.XdsLogLevel;
import io.grpc.xds.internal.security.SecurityProtocolNegotiators;
import io.grpc.xds.internal.security.SslContextProviderSupplier;
import io.grpc.xds.orca.OrcaPerRequestUtil;
import io.grpc.xds.orca.OrcaPerRequestUtil.OrcaPerRequestReportListener;
Expand Down Expand Up @@ -114,12 +115,12 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
Attributes attributes = resolvedAddresses.getAttributes();
if (xdsClientPool == null) {
xdsClientPool = attributes.get(InternalXdsAttributes.XDS_CLIENT_POOL);
xdsClientPool = attributes.get(XdsAttributes.XDS_CLIENT_POOL);
assert xdsClientPool != null;
xdsClient = xdsClientPool.getObject();
}
if (callCounterProvider == null) {
callCounterProvider = attributes.get(InternalXdsAttributes.CALL_COUNTER_PROVIDER);
callCounterProvider = attributes.get(XdsAttributes.CALL_COUNTER_PROVIDER);
}

ClusterImplConfig config =
Expand Down Expand Up @@ -236,9 +237,9 @@ public Subchannel createSubchannel(CreateSubchannelArgs args) {
.set(ATTR_CLUSTER_LOCALITY, localityAtomicReference);
if (GrpcUtil.getFlag("GRPC_EXPERIMENTAL_XDS_AUTHORITY_REWRITE", false)) {
String hostname = args.getAddresses().get(0).getAttributes()
.get(InternalXdsAttributes.ATTR_ADDRESS_NAME);
.get(XdsAttributes.ATTR_ADDRESS_NAME);
if (hostname != null) {
attrsBuilder.set(InternalXdsAttributes.ATTR_ADDRESS_NAME, hostname);
attrsBuilder.set(XdsAttributes.ATTR_ADDRESS_NAME, hostname);
}
}
args = args.toBuilder().setAddresses(addresses).setAttributes(attrsBuilder.build()).build();
Expand Down Expand Up @@ -287,10 +288,10 @@ private List<EquivalentAddressGroup> withAdditionalAttributes(
List<EquivalentAddressGroup> newAddresses = new ArrayList<>();
for (EquivalentAddressGroup eag : addresses) {
Attributes.Builder attrBuilder = eag.getAttributes().toBuilder().set(
InternalXdsAttributes.ATTR_CLUSTER_NAME, cluster);
XdsAttributes.ATTR_CLUSTER_NAME, cluster);
if (sslContextProviderSupplier != null) {
attrBuilder.set(
InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER,
SecurityProtocolNegotiators.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER,
sslContextProviderSupplier);
}
newAddresses.add(new EquivalentAddressGroup(eag.getAddresses(), attrBuilder.build()));
Expand All @@ -299,8 +300,8 @@ private List<EquivalentAddressGroup> withAdditionalAttributes(
}

private ClusterLocality createClusterLocalityFromAttributes(Attributes addressAttributes) {
Locality locality = addressAttributes.get(InternalXdsAttributes.ATTR_LOCALITY);
String localityName = addressAttributes.get(InternalXdsAttributes.ATTR_LOCALITY_NAME);
Locality locality = addressAttributes.get(XdsAttributes.ATTR_LOCALITY);
String localityName = addressAttributes.get(XdsAttributes.ATTR_LOCALITY_NAME);

// Endpoint addresses resolved by ClusterResolverLoadBalancer should always contain
// attributes with its locality, including endpoints in LOGICAL_DNS clusters.
Expand Down Expand Up @@ -431,7 +432,7 @@ public PickResult pickSubchannel(PickSubchannelArgs args) {
result = PickResult.withSubchannel(result.getSubchannel(),
result.getStreamTracerFactory(),
result.getSubchannel().getAttributes().get(
InternalXdsAttributes.ATTR_ADDRESS_NAME));
XdsAttributes.ATTR_ADDRESS_NAME));
}
}
return result;
Expand Down
18 changes: 9 additions & 9 deletions xds/src/main/java/io/grpc/xds/ClusterResolverLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ final class ClusterResolverLoadBalancer extends LoadBalancer {
public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
if (xdsClientPool == null) {
xdsClientPool = resolvedAddresses.getAttributes().get(InternalXdsAttributes.XDS_CLIENT_POOL);
xdsClientPool = resolvedAddresses.getAttributes().get(XdsAttributes.XDS_CLIENT_POOL);
xdsClient = xdsClientPool.getObject();
}
ClusterResolverConfig config =
Expand Down Expand Up @@ -423,12 +423,12 @@ public void run() {
String localityName = localityName(locality);
Attributes attr =
endpoint.eag().getAttributes().toBuilder()
.set(InternalXdsAttributes.ATTR_LOCALITY, locality)
.set(InternalXdsAttributes.ATTR_LOCALITY_NAME, localityName)
.set(InternalXdsAttributes.ATTR_LOCALITY_WEIGHT,
.set(XdsAttributes.ATTR_LOCALITY, locality)
.set(XdsAttributes.ATTR_LOCALITY_NAME, localityName)
.set(XdsAttributes.ATTR_LOCALITY_WEIGHT,
localityLbInfo.localityWeight())
.set(InternalXdsAttributes.ATTR_SERVER_WEIGHT, weight)
.set(InternalXdsAttributes.ATTR_ADDRESS_NAME, endpoint.hostname())
.set(XdsAttributes.ATTR_SERVER_WEIGHT, weight)
.set(XdsAttributes.ATTR_ADDRESS_NAME, endpoint.hostname())
.build();
EquivalentAddressGroup eag = new EquivalentAddressGroup(
endpoint.eag().getAddresses(), attr);
Expand Down Expand Up @@ -630,9 +630,9 @@ public void run() {
// to handle such it.
String localityName = localityName(LOGICAL_DNS_CLUSTER_LOCALITY);
Attributes attr = eag.getAttributes().toBuilder()
.set(InternalXdsAttributes.ATTR_LOCALITY, LOGICAL_DNS_CLUSTER_LOCALITY)
.set(InternalXdsAttributes.ATTR_LOCALITY_NAME, localityName)
.set(InternalXdsAttributes.ATTR_ADDRESS_NAME, dnsHostName)
.set(XdsAttributes.ATTR_LOCALITY, LOGICAL_DNS_CLUSTER_LOCALITY)
.set(XdsAttributes.ATTR_LOCALITY_NAME, localityName)
.set(XdsAttributes.ATTR_ADDRESS_NAME, dnsHostName)
.build();
eag = new EquivalentAddressGroup(eag.getAddresses(), attr);
eag = AddressFilter.setPathFilter(eag, Arrays.asList(priorityName, localityName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package io.grpc.xds;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.grpc.xds.InternalXdsAttributes.ATTR_DRAIN_GRACE_NANOS;
import static io.grpc.xds.InternalXdsAttributes.ATTR_FILTER_CHAIN_SELECTOR_MANAGER;
import static io.grpc.xds.XdsAttributes.ATTR_DRAIN_GRACE_NANOS;
import static io.grpc.xds.XdsAttributes.ATTR_FILTER_CHAIN_SELECTOR_MANAGER;
import static io.grpc.xds.XdsServerWrapper.ATTR_SERVER_ROUTING_CONFIG;
import static io.grpc.xds.internal.security.SecurityProtocolNegotiators.ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER;

Expand Down
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public ClientInterceptor buildClientInterceptor(FilterConfig config,
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {

/*String clusterName = callOptions.getOption(InternalXdsAttributes.ATTR_CLUSTER_NAME);
/*String clusterName = callOptions.getOption(XdsAttributes.ATTR_CLUSTER_NAME);
if (clusterName == null) {
return next.newCall(method, callOptions);
}*/
Expand Down
81 changes: 2 additions & 79 deletions xds/src/main/java/io/grpc/xds/InternalXdsAttributes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 The gRPC Authors
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,96 +18,19 @@

import io.grpc.Attributes;
import io.grpc.EquivalentAddressGroup;
import io.grpc.Grpc;
import io.grpc.Internal;
import io.grpc.NameResolver;
import io.grpc.internal.ObjectPool;
import io.grpc.xds.XdsNameResolverProvider.CallCounterProvider;
import io.grpc.xds.client.Locality;
import io.grpc.xds.client.XdsClient;
import io.grpc.xds.internal.security.SslContextProviderSupplier;

/**
* Internal attributes used for xDS implementation. Do not use.
*/
@Internal
public final class InternalXdsAttributes {

// TODO(sanjaypujare): move to xds internal package.
/** Attribute key for SslContextProviderSupplier (used from client) for a subchannel. */
@Grpc.TransportAttr
public static final Attributes.Key<SslContextProviderSupplier>
ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER =
Attributes.Key.create("io.grpc.xds.internal.security.SslContextProviderSupplier");

/**
* Attribute key for passing around the XdsClient object pool across NameResolver/LoadBalancers.
*/
@NameResolver.ResolutionResultAttr
static final Attributes.Key<ObjectPool<XdsClient>> XDS_CLIENT_POOL =
Attributes.Key.create("io.grpc.xds.InternalXdsAttributes.xdsClientPool");

/**
* Attribute key for obtaining the global provider that provides atomics for aggregating
* outstanding RPCs sent to each cluster.
*/
@NameResolver.ResolutionResultAttr
static final Attributes.Key<CallCounterProvider> CALL_COUNTER_PROVIDER =
Attributes.Key.create("io.grpc.xds.InternalXdsAttributes.callCounterProvider");

/**
* Map from localities to their weights.
*/
@NameResolver.ResolutionResultAttr
static final Attributes.Key<Integer> ATTR_LOCALITY_WEIGHT =
Attributes.Key.create("io.grpc.xds.InternalXdsAttributes.localityWeight");

/**
* Name of the cluster that provides this EquivalentAddressGroup.
*/
@Internal
@EquivalentAddressGroup.Attr
public static final Attributes.Key<String> ATTR_CLUSTER_NAME =
Attributes.Key.create("io.grpc.xds.InternalXdsAttributes.clusterName");

/**
* The locality that this EquivalentAddressGroup is in.
*/
@EquivalentAddressGroup.Attr
static final Attributes.Key<Locality> ATTR_LOCALITY =
Attributes.Key.create("io.grpc.xds.InternalXdsAttributes.locality");

/**
* The name of the locality that this EquivalentAddressGroup is in.
*/
@EquivalentAddressGroup.Attr
static final Attributes.Key<String> ATTR_LOCALITY_NAME =
Attributes.Key.create("io.grpc.xds.InternalXdsAttributes.localityName");

/**
* Endpoint weight for load balancing purposes.
*/
@EquivalentAddressGroup.Attr
static final Attributes.Key<Long> ATTR_SERVER_WEIGHT =
Attributes.Key.create("io.grpc.xds.InternalXdsAttributes.serverWeight");

/** Name associated with individual address, if available (e.g., DNS name). */
@EquivalentAddressGroup.Attr
static final Attributes.Key<String> ATTR_ADDRESS_NAME =
Attributes.Key.create("io.grpc.xds.InternalXdsAttributes.addressName");

/**
* Filter chain match for network filters.
*/
@Grpc.TransportAttr
static final Attributes.Key<FilterChainSelectorManager>
ATTR_FILTER_CHAIN_SELECTOR_MANAGER = Attributes.Key.create(
"io.grpc.xds.InternalXdsAttributes.filterChainSelectorManager");

/** Grace time to use when draining. Null for an infinite grace time. */
@Grpc.TransportAttr
static final Attributes.Key<Long> ATTR_DRAIN_GRACE_NANOS =
Attributes.Key.create("io.grpc.xds.InternalXdsAttributes.drainGraceTime");
XdsAttributes.ATTR_CLUSTER_NAME;

private InternalXdsAttributes() {}
}
4 changes: 2 additions & 2 deletions xds/src/main/java/io/grpc/xds/RingHashLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
Map<EquivalentAddressGroup, Long> serverWeights = new HashMap<>();
long totalWeight = 0L;
for (EquivalentAddressGroup eag : addrList) {
Long weight = eag.getAttributes().get(InternalXdsAttributes.ATTR_SERVER_WEIGHT);
Long weight = eag.getAttributes().get(XdsAttributes.ATTR_SERVER_WEIGHT);
// Support two ways of server weighing: either multiple instances of the same address
// or each address contains a per-address weight attribute. If a weight is not provided,
// each occurrence of the address will be counted a weight value of one.
Expand Down Expand Up @@ -241,7 +241,7 @@ private Status validateAddrList(List<EquivalentAddressGroup> addrList) {

long totalWeight = 0;
for (EquivalentAddressGroup eag : addrList) {
Long weight = eag.getAttributes().get(InternalXdsAttributes.ATTR_SERVER_WEIGHT);
Long weight = eag.getAttributes().get(XdsAttributes.ATTR_SERVER_WEIGHT);

if (weight == null) {
weight = 1L;
Expand Down
4 changes: 2 additions & 2 deletions xds/src/main/java/io/grpc/xds/WrrLocalityLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
Map<String, Integer> localityWeights = new HashMap<>();
for (EquivalentAddressGroup eag : resolvedAddresses.getAddresses()) {
Attributes eagAttrs = eag.getAttributes();
String locality = eagAttrs.get(InternalXdsAttributes.ATTR_LOCALITY_NAME);
Integer localityWeight = eagAttrs.get(InternalXdsAttributes.ATTR_LOCALITY_WEIGHT);
String locality = eagAttrs.get(XdsAttributes.ATTR_LOCALITY_NAME);
Integer localityWeight = eagAttrs.get(XdsAttributes.ATTR_LOCALITY_WEIGHT);

if (locality == null) {
Status unavailableStatus = Status.UNAVAILABLE.withDescription(
Expand Down
101 changes: 101 additions & 0 deletions xds/src/main/java/io/grpc/xds/XdsAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2019 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.xds;

import io.grpc.Attributes;
import io.grpc.EquivalentAddressGroup;
import io.grpc.Grpc;
import io.grpc.NameResolver;
import io.grpc.internal.ObjectPool;
import io.grpc.xds.XdsNameResolverProvider.CallCounterProvider;
import io.grpc.xds.client.Locality;
import io.grpc.xds.client.XdsClient;

/**
* Attributes used for xDS implementation.
*/
final class XdsAttributes {
/**
* Attribute key for passing around the XdsClient object pool across NameResolver/LoadBalancers.
*/
@NameResolver.ResolutionResultAttr
static final Attributes.Key<ObjectPool<XdsClient>> XDS_CLIENT_POOL =
Attributes.Key.create("io.grpc.xds.XdsAttributes.xdsClientPool");

/**
* Attribute key for obtaining the global provider that provides atomics for aggregating
* outstanding RPCs sent to each cluster.
*/
@NameResolver.ResolutionResultAttr
static final Attributes.Key<CallCounterProvider> CALL_COUNTER_PROVIDER =
Attributes.Key.create("io.grpc.xds.XdsAttributes.callCounterProvider");

/**
* Map from localities to their weights.
*/
@NameResolver.ResolutionResultAttr
static final Attributes.Key<Integer> ATTR_LOCALITY_WEIGHT =
Attributes.Key.create("io.grpc.xds.XdsAttributes.localityWeight");

/**
* Name of the cluster that provides this EquivalentAddressGroup.
*/
@EquivalentAddressGroup.Attr
public static final Attributes.Key<String> ATTR_CLUSTER_NAME =
Attributes.Key.create("io.grpc.xds.XdsAttributes.clusterName");

/**
* The locality that this EquivalentAddressGroup is in.
*/
@EquivalentAddressGroup.Attr
static final Attributes.Key<Locality> ATTR_LOCALITY =
Attributes.Key.create("io.grpc.xds.XdsAttributes.locality");

/**
* The name of the locality that this EquivalentAddressGroup is in.
*/
@EquivalentAddressGroup.Attr
static final Attributes.Key<String> ATTR_LOCALITY_NAME =
Attributes.Key.create("io.grpc.xds.XdsAttributes.localityName");

/**
* Endpoint weight for load balancing purposes.
*/
@EquivalentAddressGroup.Attr
static final Attributes.Key<Long> ATTR_SERVER_WEIGHT =
Attributes.Key.create("io.grpc.xds.XdsAttributes.serverWeight");

/** Name associated with individual address, if available (e.g., DNS name). */
@EquivalentAddressGroup.Attr
static final Attributes.Key<String> ATTR_ADDRESS_NAME =
Attributes.Key.create("io.grpc.xds.XdsAttributes.addressName");

/**
* Filter chain match for network filters.
*/
@Grpc.TransportAttr
static final Attributes.Key<FilterChainSelectorManager>
ATTR_FILTER_CHAIN_SELECTOR_MANAGER = Attributes.Key.create(
"io.grpc.xds.XdsAttributes.filterChainSelectorManager");

/** Grace time to use when draining. Null for an infinite grace time. */
@Grpc.TransportAttr
static final Attributes.Key<Long> ATTR_DRAIN_GRACE_NANOS =
Attributes.Key.create("io.grpc.xds.XdsAttributes.drainGraceTime");

private XdsAttributes() {}
}
4 changes: 2 additions & 2 deletions xds/src/main/java/io/grpc/xds/XdsNameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ private void updateResolutionResult() {
ConfigOrError parsedServiceConfig = serviceConfigParser.parseServiceConfig(rawServiceConfig);
Attributes attrs =
Attributes.newBuilder()
.set(InternalXdsAttributes.XDS_CLIENT_POOL, xdsClientPool)
.set(InternalXdsAttributes.CALL_COUNTER_PROVIDER, callCounterProvider)
.set(XdsAttributes.XDS_CLIENT_POOL, xdsClientPool)
.set(XdsAttributes.CALL_COUNTER_PROVIDER, callCounterProvider)
.set(InternalConfigSelector.KEY, configSelector)
.build();
ResolutionResult result =
Expand Down
4 changes: 2 additions & 2 deletions xds/src/main/java/io/grpc/xds/XdsServerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static io.grpc.xds.InternalXdsAttributes.ATTR_DRAIN_GRACE_NANOS;
import static io.grpc.xds.InternalXdsAttributes.ATTR_FILTER_CHAIN_SELECTOR_MANAGER;
import static io.grpc.xds.XdsAttributes.ATTR_DRAIN_GRACE_NANOS;
import static io.grpc.xds.XdsAttributes.ATTR_FILTER_CHAIN_SELECTOR_MANAGER;

import com.google.common.annotations.VisibleForTesting;
import com.google.errorprone.annotations.DoNotCall;
Expand Down
Loading

0 comments on commit 4a0f707

Please sign in to comment.