Skip to content

Commit

Permalink
xds: make XdsAttributes really public internal (#7815)
Browse files Browse the repository at this point in the history
Change XdsAttributes to InternalXdsAttributes. It is public internal and may be accessed out of grpc-xds.
  • Loading branch information
voidzcy committed Jan 15, 2021
1 parent 458b0e4 commit 9016cf5
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 34 deletions.
1 change: 1 addition & 0 deletions xds/build.gradle
Expand Up @@ -94,6 +94,7 @@ javadoc {
exclude 'io/grpc/xds/XdsInitializationException.java'
exclude 'io/grpc/xds/XdsNameResolverProvider.java'
exclude 'io/grpc/xds/internal/**'
exclude 'io/grpc/xds/Internal*'
}

shadowJar {
Expand Down
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/CdsLoadBalancer2.java
Expand Up @@ -86,7 +86,7 @@ public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
}
logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
this.resolvedAddresses = resolvedAddresses;
xdsClientPool = resolvedAddresses.getAttributes().get(XdsAttributes.XDS_CLIENT_POOL);
xdsClientPool = resolvedAddresses.getAttributes().get(InternalXdsAttributes.XDS_CLIENT_POOL);
xdsClient = xdsClientPool.getObject();
CdsConfig config = (CdsConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
logger.log(XdsLogLevel.INFO, "Config: {0}", config);
Expand Down
8 changes: 4 additions & 4 deletions xds/src/main/java/io/grpc/xds/ClusterImplLoadBalancer.java
Expand Up @@ -102,11 +102,11 @@ public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
Attributes attributes = resolvedAddresses.getAttributes();
if (xdsClientPool == null) {
xdsClientPool = attributes.get(XdsAttributes.XDS_CLIENT_POOL);
xdsClientPool = attributes.get(InternalXdsAttributes.XDS_CLIENT_POOL);
xdsClient = xdsClientPool.getObject();
}
if (callCounterProvider == null) {
callCounterProvider = attributes.get(XdsAttributes.CALL_COUNTER_PROVIDER);
callCounterProvider = attributes.get(InternalXdsAttributes.CALL_COUNTER_PROVIDER);
}
ClusterImplConfig config =
(ClusterImplConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
Expand All @@ -130,7 +130,7 @@ public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
childLbHelper.updateSslContextProviderSupplier(config.tlsContext);
if (loadStatsStore != null) {
attributes = attributes.toBuilder()
.set(XdsAttributes.ATTR_CLUSTER_SERVICE_LOAD_STATS_STORE, loadStatsStore).build();
.set(InternalXdsAttributes.ATTR_CLUSTER_SERVICE_LOAD_STATS_STORE, loadStatsStore).build();
}
childLb.handleResolvedAddresses(
resolvedAddresses.toBuilder()
Expand Down Expand Up @@ -200,7 +200,7 @@ public Subchannel createSubchannel(CreateSubchannelArgs args) {
for (EquivalentAddressGroup eag : args.getAddresses()) {
Attributes attributes =
eag.getAttributes().toBuilder()
.set(XdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER,
.set(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER,
sslContextProviderSupplier)
.build();
addresses.add(new EquivalentAddressGroup(eag.getAddresses(), attributes));
Expand Down
Expand Up @@ -114,7 +114,7 @@ final class ClusterResolverLoadBalancer extends LoadBalancer {
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
if (xdsClientPool == null) {
xdsClientPool = resolvedAddresses.getAttributes().get(XdsAttributes.XDS_CLIENT_POOL);
xdsClientPool = resolvedAddresses.getAttributes().get(InternalXdsAttributes.XDS_CLIENT_POOL);
xdsClient = xdsClientPool.getObject();
}
ClusterResolverConfig config =
Expand Down
Expand Up @@ -26,17 +26,21 @@
import io.grpc.xds.internal.sds.SslContextProviderSupplier;

/**
* Special attributes that are only useful to gRPC in the XDS context.
* Internal attributes used for xDS implementation. Do not use.
*/
@Internal
public final class XdsAttributes {
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.sds.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.XdsAttributes.xdsClientPool");
Expand All @@ -54,5 +58,5 @@ public final class XdsAttributes {
static final Attributes.Key<LoadStatsStore> ATTR_CLUSTER_SERVICE_LOAD_STATS_STORE =
Attributes.Key.create("io.grpc.xds.XdsAttributes.loadStatsStore");

private XdsAttributes() {}
private InternalXdsAttributes() {}
}
4 changes: 2 additions & 2 deletions xds/src/main/java/io/grpc/xds/LrsLoadBalancer.java
Expand Up @@ -52,8 +52,8 @@ final class LrsLoadBalancer extends LoadBalancer {
@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
LrsConfig config = (LrsConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
LoadStatsStore store =
resolvedAddresses.getAttributes().get(XdsAttributes.ATTR_CLUSTER_SERVICE_LOAD_STATS_STORE);
LoadStatsStore store = resolvedAddresses.getAttributes().get(
InternalXdsAttributes.ATTR_CLUSTER_SERVICE_LOAD_STATS_STORE);
checkNotNull(config, "missing LRS lb config");
checkNotNull(store, "missing cluster service stats object");
checkAndSetUp(config, store);
Expand Down
4 changes: 2 additions & 2 deletions xds/src/main/java/io/grpc/xds/XdsNameResolver.java
Expand Up @@ -190,8 +190,8 @@ private void updateResolutionResult() {
ConfigOrError parsedServiceConfig = serviceConfigParser.parseServiceConfig(rawServiceConfig);
Attributes attrs =
Attributes.newBuilder()
.set(XdsAttributes.XDS_CLIENT_POOL, xdsClientPool)
.set(XdsAttributes.CALL_COUNTER_PROVIDER, callCounterProvider)
.set(InternalXdsAttributes.XDS_CLIENT_POOL, xdsClientPool)
.set(InternalXdsAttributes.CALL_COUNTER_PROVIDER, callCounterProvider)
.set(InternalConfigSelector.KEY, configSelector)
.build();
ResolutionResult result =
Expand Down
Expand Up @@ -32,7 +32,7 @@
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.ProtocolNegotiationEvent;
import io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext;
import io.grpc.xds.XdsAttributes;
import io.grpc.xds.InternalXdsAttributes;
import io.grpc.xds.XdsClientWrapperForServerSds;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerAdapter;
Expand Down Expand Up @@ -189,7 +189,8 @@ public AsciiString scheme() {
public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
// check if SslContextProviderSupplier was passed via attributes
SslContextProviderSupplier localSslContextProviderSupplier =
grpcHandler.getEagAttributes().get(XdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
grpcHandler.getEagAttributes().get(
InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
if (localSslContextProviderSupplier == null) {
checkNotNull(
fallbackProtocolNegotiator, "No TLS config and no fallbackProtocolNegotiator!");
Expand Down
4 changes: 3 additions & 1 deletion xds/src/test/java/io/grpc/xds/CdsLoadBalancer2Test.java
Expand Up @@ -130,7 +130,9 @@ public void setUp() {
.setAddresses(Collections.<EquivalentAddressGroup>emptyList())
.setAttributes(
// Other attributes not used by cluster_resolver LB are omitted.
Attributes.newBuilder().set(XdsAttributes.XDS_CLIENT_POOL, xdsClientPool).build())
Attributes.newBuilder()
.set(InternalXdsAttributes.XDS_CLIENT_POOL, xdsClientPool)
.build())
.setLoadBalancingPolicyConfig(new CdsConfig(CLUSTER))
.build());
assertThat(Iterables.getOnlyElement(xdsClient.watchers.keySet())).isEqualTo(CLUSTER);
Expand Down
20 changes: 11 additions & 9 deletions xds/src/test/java/io/grpc/xds/ClusterImplLoadBalancerTest.java
Expand Up @@ -154,10 +154,10 @@ public void handleResolvedAddresses_propagateToChildPolicy() {
FakeLoadBalancer childBalancer = Iterables.getOnlyElement(downstreamBalancers);
assertThat(Iterables.getOnlyElement(childBalancer.addresses)).isEqualTo(endpoint);
assertThat(childBalancer.config).isSameInstanceAs(weightedTargetConfig);
assertThat(childBalancer.attributes.get(XdsAttributes.XDS_CLIENT_POOL))
assertThat(childBalancer.attributes.get(InternalXdsAttributes.XDS_CLIENT_POOL))
.isSameInstanceAs(xdsClientPool);
assertThat(childBalancer.attributes.get(XdsAttributes.ATTR_CLUSTER_SERVICE_LOAD_STATS_STORE))
.isNotNull();
assertThat(childBalancer.attributes.get(
InternalXdsAttributes.ATTR_CLUSTER_SERVICE_LOAD_STATS_STORE)).isNotNull();
}

@Test
Expand Down Expand Up @@ -224,7 +224,9 @@ public void dropRpcsWithRespectToLbConfigDropCategories() {
ResolvedAddresses.newBuilder()
.setAddresses(Collections.singletonList(endpoint))
.setAttributes(
Attributes.newBuilder().set(XdsAttributes.XDS_CLIENT_POOL, xdsClientPool).build())
Attributes.newBuilder()
.set(InternalXdsAttributes.XDS_CLIENT_POOL, xdsClientPool)
.build())
.setLoadBalancingPolicyConfig(config)
.build());
result = currentPicker.pickSubchannel(mock(PickSubchannelArgs.class));
Expand Down Expand Up @@ -409,7 +411,7 @@ private void subtest_endpointConnectionWithTls(boolean enableSecurity) {
Subchannel subchannel = leafBalancer.helper.createSubchannel(args);
for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
SslContextProviderSupplier supplier =
eag.getAttributes().get(XdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
if (enableSecurity) {
assertThat(supplier.getUpstreamTlsContext()).isEqualTo(upstreamTlsContext);
} else {
Expand All @@ -425,7 +427,7 @@ private void subtest_endpointConnectionWithTls(boolean enableSecurity) {
assertThat(Iterables.getOnlyElement(downstreamBalancers)).isSameInstanceAs(leafBalancer);
subchannel = leafBalancer.helper.createSubchannel(args); // creates new connections
for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
assertThat(eag.getAttributes().get(XdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER))
assertThat(eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER))
.isNull();
}

Expand All @@ -443,7 +445,7 @@ private void subtest_endpointConnectionWithTls(boolean enableSecurity) {
subchannel = leafBalancer.helper.createSubchannel(args); // creates new connections
for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
SslContextProviderSupplier supplier =
eag.getAttributes().get(XdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
if (enableSecurity) {
assertThat(supplier.getUpstreamTlsContext()).isEqualTo(upstreamTlsContext);
} else {
Expand All @@ -459,8 +461,8 @@ private void deliverAddressesAndConfig(List<EquivalentAddressGroup> addresses,
.setAddresses(addresses)
.setAttributes(
Attributes.newBuilder()
.set(XdsAttributes.XDS_CLIENT_POOL, xdsClientPool)
.set(XdsAttributes.CALL_COUNTER_PROVIDER, callCounterProvider)
.set(InternalXdsAttributes.XDS_CLIENT_POOL, xdsClientPool)
.set(InternalXdsAttributes.CALL_COUNTER_PROVIDER, callCounterProvider)
.build())
.setLoadBalancingPolicyConfig(config)
.build());
Expand Down
Expand Up @@ -736,7 +736,9 @@ private void deliverLbConfig(ClusterResolverConfig config) {
.setAddresses(Collections.<EquivalentAddressGroup>emptyList())
.setAttributes(
// Other attributes not used by cluster_resolver LB are omitted.
Attributes.newBuilder().set(XdsAttributes.XDS_CLIENT_POOL, xdsClientPool).build())
Attributes.newBuilder()
.set(InternalXdsAttributes.XDS_CLIENT_POOL, xdsClientPool)
.build())
.setLoadBalancingPolicyConfig(config)
.build());
}
Expand Down
2 changes: 1 addition & 1 deletion xds/src/test/java/io/grpc/xds/LrsLoadBalancerTest.java
Expand Up @@ -158,7 +158,7 @@ private void deliverResolvedAddresses(
.setAddresses(addresses)
.setAttributes(
Attributes.newBuilder()
.set(XdsAttributes.ATTR_CLUSTER_SERVICE_LOAD_STATS_STORE, loadRecorder)
.set(InternalXdsAttributes.ATTR_CLUSTER_SERVICE_LOAD_STATS_STORE, loadRecorder)
.build())
.setLoadBalancingPolicyConfig(config)
.build();
Expand Down
6 changes: 3 additions & 3 deletions xds/src/test/java/io/grpc/xds/XdsNameResolverTest.java
Expand Up @@ -444,7 +444,7 @@ public void resolved_simpleCallSucceeds_routeToWeightedCluster() {
assertThat(result.getAddresses()).isEmpty();
assertServiceConfigForLoadBalancingConfig(
Arrays.asList(cluster1, cluster2), (Map<String, ?>) result.getServiceConfig().getConfig());
assertThat(result.getAttributes().get(XdsAttributes.XDS_CLIENT_POOL)).isNotNull();
assertThat(result.getAttributes().get(InternalXdsAttributes.XDS_CLIENT_POOL)).isNotNull();
InternalConfigSelector configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
assertCallSelectResult(call1, configSelector, cluster2, 20.0);
assertCallSelectResult(call1, configSelector, cluster1, 20.0);
Expand Down Expand Up @@ -504,8 +504,8 @@ private InternalConfigSelector resolveToClusters() {
assertThat(result.getAddresses()).isEmpty();
assertServiceConfigForLoadBalancingConfig(
Arrays.asList(cluster1, cluster2), (Map<String, ?>) result.getServiceConfig().getConfig());
assertThat(result.getAttributes().get(XdsAttributes.XDS_CLIENT_POOL)).isNotNull();
assertThat(result.getAttributes().get(XdsAttributes.CALL_COUNTER_PROVIDER)).isNotNull();
assertThat(result.getAttributes().get(InternalXdsAttributes.XDS_CLIENT_POOL)).isNotNull();
assertThat(result.getAttributes().get(InternalXdsAttributes.CALL_COUNTER_PROVIDER)).isNotNull();
return result.getAttributes().get(InternalConfigSelector.KEY);
}

Expand Down
4 changes: 2 additions & 2 deletions xds/src/test/java/io/grpc/xds/XdsSdsClientServerTest.java
Expand Up @@ -396,7 +396,7 @@ private SimpleServiceGrpc.SimpleServiceBlockingStub getBlockingStub(
Attributes attrs =
(upstreamTlsContext != null)
? Attributes.newBuilder()
.set(XdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER,
.set(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER,
new SslContextProviderSupplier(
upstreamTlsContext, new TlsContextManagerImpl(mockBootstrapper)))
.build()
Expand Down Expand Up @@ -425,7 +425,7 @@ private SimpleServiceGrpc.SimpleServiceBlockingStub getBlockingStubNewApi(
Attributes attrs =
(upstreamTlsContext != null)
? Attributes.newBuilder()
.set(XdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER,
.set(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER,
new SslContextProviderSupplier(
upstreamTlsContext, new TlsContextManagerImpl(mockBootstrapper)))
.build()
Expand Down
Expand Up @@ -41,7 +41,7 @@
import io.grpc.netty.InternalProtocolNegotiators;
import io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext;
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
import io.grpc.xds.XdsAttributes;
import io.grpc.xds.InternalXdsAttributes;
import io.grpc.xds.XdsClientWrapperForServerSds;
import io.grpc.xds.XdsClientWrapperForServerSdsTest;
import io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsHandler;
Expand Down Expand Up @@ -183,7 +183,7 @@ public void clientSdsProtocolNegotiatorNewHandler_withTlsContextAttribute() {
when(mockHandler.getEagAttributes())
.thenReturn(
Attributes.newBuilder()
.set(XdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER,
.set(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER,
new SslContextProviderSupplier(upstreamTlsContext, mockTlsContextManager))
.build());
ChannelHandler newHandler = pn.newHandler(mockHandler);
Expand Down

0 comments on commit 9016cf5

Please sign in to comment.