Skip to content

Commit 0f26b2d

Browse files
committed
Add more checking for fallback being enabled
1 parent c6db989 commit 0f26b2d

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

xds/src/main/java/io/grpc/xds/client/ControlPlaneClient.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.grpc.xds.client;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
2019
import static com.google.common.base.Preconditions.checkNotNull;
2120
import static com.google.common.base.Preconditions.checkState;
2221

@@ -444,7 +443,8 @@ private void handleRpcStreamClosed(Status status) {
444443
? "ADS stream closed with status {0}: {1}. Cause: {2}"
445444
: "ADS stream failed with status {0}: {1}. Cause: {2}";
446445
logger.log(
447-
XdsLogLevel.ERROR, errorMsg, status.getCode(), status.getDescription(), status.getCause());
446+
XdsLogLevel.ERROR, errorMsg, status.getCode(), status.getDescription(),
447+
status.getCause());
448448
}
449449
closed = true;
450450
xdsResponseHandler.handleStreamClosed(status);

xds/src/main/java/io/grpc/xds/client/XdsClientImpl.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ private boolean fallBackToCpc(ControlPlaneClient fallbackCpc, String authority,
642642
}
643643

644644
private void doFallbackIfNecessary(ControlPlaneClient cpc, String authority) {
645-
if (cpc == null) {
645+
if (cpc == null || !BootstrapperImpl.isEnabledXdsFallback()) {
646646
return;
647647
}
648648

@@ -668,17 +668,14 @@ private void setCpcForAuthority(String authority, ControlPlaneClient cpc) {
668668
continue;
669669
}
670670
for (ResourceSubscriber<?> subscriber : subscriberTypeEntry.getValue().values()) {
671-
if (!Objects.equals(subscriber.authority, authority)) {
671+
if (subscriber.controlPlaneClient == cpc
672+
|| !Objects.equals(subscriber.authority, authority)) {
672673
continue;
673674
}
674-
if (subscriber.controlPlaneClient != cpc) {
675-
subscriber.data = null;
676-
subscriber.absent = false;
677-
subscriber.controlPlaneClient = cpc;
678-
subscriber.restartTimer();
679-
} else {
680-
subscriber.controlPlaneClient = cpc;
681-
}
675+
subscriber.data = null;
676+
subscriber.absent = false;
677+
subscriber.controlPlaneClient = cpc;
678+
subscriber.restartTimer();
682679
}
683680
}
684681

@@ -1090,6 +1087,11 @@ && compareCpClients(activeCpClient, controlPlaneClient, authority) < 0) {
10901087
restartMatchingSubscriberTimers(controlPlaneClient, authority);
10911088
controlPlaneClient.sendDiscoveryRequests(authority);
10921089

1090+
// If fallback isn't enabled, we're done.
1091+
if (!BootstrapperImpl.isEnabledXdsFallback()) {
1092+
return;
1093+
}
1094+
10931095
// Shutdown any lower priority control plane clients.
10941096
Iterator<ControlPlaneClient> iterator = serverCpClientMap.values().iterator();
10951097
while (iterator.hasNext()) {

xds/src/test/java/io/grpc/xds/ControlPlaneRule.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public class ControlPlaneRule extends TestWatcher {
8787
private XdsTestControlPlaneService controlPlaneService;
8888
private XdsTestLoadReportingService loadReportingService;
8989
private XdsNameResolverProvider nameResolverProvider;
90-
private int port;
90+
private final int port;
9191

9292
public ControlPlaneRule() {
9393
this(0);
@@ -219,7 +219,7 @@ void setCdsConfig(Cluster cluster) {
219219
setCdsConfig(CLUSTER_NAME, cluster);
220220
}
221221

222-
void setCdsConfig(String clusterName, Cluster cluster) {
222+
void setCdsConfig(String clusterName, Cluster cluster) {
223223
getService().setXdsConfig(ADS_TYPE_URL_CDS,
224224
ImmutableMap.<String, Message>of(clusterName, cluster));
225225
}
@@ -316,6 +316,7 @@ static Listener buildClientListener(String name) {
316316
static Listener buildClientListener(String name, String identifier) {
317317
return buildClientListener(name, identifier, RDS_NAME);
318318
}
319+
319320
static Listener buildClientListener(String name, String identifier, String rdsName) {
320321
HttpFilter httpFilter = HttpFilter.newBuilder()
321322
.setName(identifier)

xds/src/test/java/io/grpc/xds/XdsClientFallbackTest.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,26 @@ public void onResourceDoesNotExist(String resourceName) {
103103
@Mock
104104
private XdsClient.ResourceWatcher<XdsRouteConfigureResource.RdsUpdate> rdsWatcher2;
105105

106-
@Mock
107-
private XdsClient.ResourceWatcher<XdsClusterResource.CdsUpdate> cdsWatcher;
106+
private XdsClient.ResourceWatcher<XdsClusterResource.CdsUpdate> raalCdsWatcher =
107+
new XdsClient.ResourceWatcher<XdsClusterResource.CdsUpdate>() {
108+
109+
@Override
110+
public void onChanged(XdsClusterResource.CdsUpdate update) {
111+
log.log(Level.FINE, "CDS update: " + update);
112+
}
113+
114+
@Override
115+
public void onError(Status error) {
116+
log.log(Level.FINE, "CDS update error: " + error.getDescription());
117+
}
118+
119+
@Override
120+
public void onResourceDoesNotExist(String resourceName) {
121+
log.log(Level.FINE, "CDS resource does not exist: " + resourceName);
122+
}
123+
};
124+
private XdsClient.ResourceWatcher<XdsClusterResource.CdsUpdate> cdsWatcher =
125+
mock(XdsClient.ResourceWatcher.class, delegatesTo(raalCdsWatcher));
108126
@Mock
109127
private XdsClient.ResourceWatcher<XdsClusterResource.CdsUpdate> cdsWatcher2;
110128

0 commit comments

Comments
 (0)