Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions xds/src/main/java/io/grpc/xds/XdsDependencyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
for (Map.Entry<String, XdsWatcherBase<T>> watcherEntry : watchers.watchers.entrySet()) {
xdsClient.cancelXdsResourceWatch(watchers.resourceType, watcherEntry.getKey(),
watcherEntry.getValue());
watcherEntry.getValue().cancelled = true;
}
}

Expand Down Expand Up @@ -591,6 +592,9 @@
@Override
public void onError(Status error) {
checkNotNull(error, "error");
if (cancelled) {
Copy link
Copy Markdown
Member

@shivaspeaks shivaspeaks Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Eric, one small doubt in this. How did we decide to put this after checkNotNull? I thought if we have already cancelled the watcher, we should just return. Checking not null should probably go after cancelled check?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkNotNull() is checking the API was called correctly. Enforcing API requirements is no less relevant if the watcher is cancelled, although it is true that this implementation need not the results. If it is null here, it is a bug that needs fixing, independent of XdsDependencyManager's state. The robustness principal has been largely found to be a mistake. It is better to be strict of inputs, so bugs have a short lifetime and the system works as one expects. I want things to fail as early as possible, as there they are most obvious.

(I'd agree that the ordering could have been the other way here, and I did consider it. But what value is skipping the null check?)

return;

Check warning on line 596 in xds/src/main/java/io/grpc/xds/XdsDependencyManager.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/XdsDependencyManager.java#L596

Added line #L596 was not covered by tests
}
// Don't update configuration on error, if we've already received configuration
if (!hasDataValue()) {
setDataAsStatus(Status.UNAVAILABLE.withDescription(
Expand Down Expand Up @@ -659,6 +663,9 @@
@Override
public void onChanged(XdsListenerResource.LdsUpdate update) {
checkNotNull(update, "update");
if (cancelled) {
return;
}

HttpConnectionManager httpConnectionManager = update.httpConnectionManager();
List<VirtualHost> virtualHosts;
Expand Down Expand Up @@ -787,6 +794,9 @@
@Override
public void onChanged(RdsUpdate update) {
checkNotNull(update, "update");
if (cancelled) {
return;
}
List<VirtualHost> oldVirtualHosts = hasDataValue()
? getData().getValue().virtualHosts
: Collections.emptyList();
Expand Down Expand Up @@ -815,6 +825,9 @@
@Override
public void onChanged(XdsClusterResource.CdsUpdate update) {
checkNotNull(update, "update");
if (cancelled) {
return;
}
switch (update.clusterType()) {
case EDS:
setData(update);
Expand Down Expand Up @@ -895,6 +908,9 @@

@Override
public void onChanged(XdsEndpointResource.EdsUpdate update) {
if (cancelled) {
return;
}
setData(checkNotNull(update, "update"));
maybePublishConfig();
}
Expand Down
137 changes: 135 additions & 2 deletions xds/src/test/java/io/grpc/xds/XdsDependencyManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.protobuf.Message;
import io.envoyproxy.envoy.config.cluster.v3.Cluster;
import io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment;
Expand All @@ -65,7 +66,7 @@
import io.grpc.xds.XdsConfig.XdsClusterConfig;
import io.grpc.xds.XdsEndpointResource.EdsUpdate;
import io.grpc.xds.client.CommonBootstrapperTestUtils;
import io.grpc.xds.client.XdsClientImpl;
import io.grpc.xds.client.XdsClient;
import io.grpc.xds.client.XdsClientMetricReporter;
import io.grpc.xds.client.XdsTransportFactory;
import java.io.Closeable;
Expand Down Expand Up @@ -115,7 +116,7 @@ public class XdsDependencyManagerTest {
});

private ManagedChannel channel;
private XdsClientImpl xdsClient;
private XdsClient xdsClient;
private XdsDependencyManager xdsDependencyManager;
private TestWatcher xdsConfigWatcher;
private Server xdsServer;
Expand Down Expand Up @@ -715,6 +716,138 @@ public void testCdsError() throws IOException {
assertThat(status.getDescription()).contains(XdsTestUtils.CLUSTER_NAME);
}

@Test
public void ldsUpdateAfterShutdown() {
XdsTestUtils.setAdsConfig(controlPlaneService, serverName, "RDS", "CDS", "EDS",
ENDPOINT_HOSTNAME, ENDPOINT_PORT);

xdsDependencyManager = new XdsDependencyManager(xdsClient, xdsConfigWatcher, syncContext,
serverName, serverName, nameResolverArgs, scheduler);

verify(xdsConfigWatcher, timeout(1000)).onUpdate(any());

@SuppressWarnings("unchecked")
XdsClient.ResourceWatcher<XdsListenerResource.LdsUpdate> resourceWatcher =
mock(XdsClient.ResourceWatcher.class);
xdsClient.watchXdsResource(
XdsListenerResource.getInstance(),
serverName,
resourceWatcher,
MoreExecutors.directExecutor());
verify(resourceWatcher, timeout(5000)).onChanged(any());

syncContext.execute(() -> {
// Shutdown before any updates. This will unsubscribe from XdsClient, but only after this
// Runnable returns
xdsDependencyManager.shutdown();

XdsTestUtils.setAdsConfig(controlPlaneService, serverName, "RDS2", "CDS", "EDS",
ENDPOINT_HOSTNAME, ENDPOINT_PORT);
verify(resourceWatcher, timeout(5000).times(2)).onChanged(any());
xdsClient.cancelXdsResourceWatch(
XdsListenerResource.getInstance(), serverName, resourceWatcher);
});
}

@Test
public void rdsUpdateAfterShutdown() {
XdsTestUtils.setAdsConfig(controlPlaneService, serverName, "RDS", "CDS", "EDS",
ENDPOINT_HOSTNAME, ENDPOINT_PORT);

xdsDependencyManager = new XdsDependencyManager(xdsClient, xdsConfigWatcher, syncContext,
serverName, serverName, nameResolverArgs, scheduler);

verify(xdsConfigWatcher, timeout(1000)).onUpdate(any());

@SuppressWarnings("unchecked")
XdsClient.ResourceWatcher<XdsRouteConfigureResource.RdsUpdate> resourceWatcher =
mock(XdsClient.ResourceWatcher.class);
xdsClient.watchXdsResource(
XdsRouteConfigureResource.getInstance(),
"RDS",
resourceWatcher,
MoreExecutors.directExecutor());
verify(resourceWatcher, timeout(5000)).onChanged(any());

syncContext.execute(() -> {
// Shutdown before any updates. This will unsubscribe from XdsClient, but only after this
// Runnable returns
xdsDependencyManager.shutdown();

XdsTestUtils.setAdsConfig(controlPlaneService, serverName, "RDS", "CDS2", "EDS",
ENDPOINT_HOSTNAME, ENDPOINT_PORT);
verify(resourceWatcher, timeout(5000).times(2)).onChanged(any());
xdsClient.cancelXdsResourceWatch(
XdsRouteConfigureResource.getInstance(), serverName, resourceWatcher);
});
}

@Test
public void cdsUpdateAfterShutdown() {
XdsTestUtils.setAdsConfig(controlPlaneService, serverName, "RDS", "CDS", "EDS",
ENDPOINT_HOSTNAME, ENDPOINT_PORT);

xdsDependencyManager = new XdsDependencyManager(xdsClient, xdsConfigWatcher, syncContext,
serverName, serverName, nameResolverArgs, scheduler);

verify(xdsConfigWatcher, timeout(1000)).onUpdate(any());

@SuppressWarnings("unchecked")
XdsClient.ResourceWatcher<XdsClusterResource.CdsUpdate> resourceWatcher =
mock(XdsClient.ResourceWatcher.class);
xdsClient.watchXdsResource(
XdsClusterResource.getInstance(),
"CDS",
resourceWatcher,
MoreExecutors.directExecutor());
verify(resourceWatcher, timeout(5000)).onChanged(any());

syncContext.execute(() -> {
// Shutdown before any updates. This will unsubscribe from XdsClient, but only after this
// Runnable returns
xdsDependencyManager.shutdown();

XdsTestUtils.setAdsConfig(controlPlaneService, serverName, "RDS", "CDS", "EDS2",
ENDPOINT_HOSTNAME, ENDPOINT_PORT);
verify(resourceWatcher, timeout(5000).times(2)).onChanged(any());
xdsClient.cancelXdsResourceWatch(
XdsClusterResource.getInstance(), serverName, resourceWatcher);
});
}

@Test
public void edsUpdateAfterShutdown() {
XdsTestUtils.setAdsConfig(controlPlaneService, serverName, "RDS", "CDS", "EDS",
ENDPOINT_HOSTNAME, ENDPOINT_PORT);

xdsDependencyManager = new XdsDependencyManager(xdsClient, xdsConfigWatcher, syncContext,
serverName, serverName, nameResolverArgs, scheduler);

verify(xdsConfigWatcher, timeout(1000)).onUpdate(any());

@SuppressWarnings("unchecked")
XdsClient.ResourceWatcher<XdsEndpointResource.EdsUpdate> resourceWatcher =
mock(XdsClient.ResourceWatcher.class);
xdsClient.watchXdsResource(
XdsEndpointResource.getInstance(),
"EDS",
resourceWatcher,
MoreExecutors.directExecutor());
verify(resourceWatcher, timeout(5000)).onChanged(any());

syncContext.execute(() -> {
// Shutdown before any updates. This will unsubscribe from XdsClient, but only after this
// Runnable returns
xdsDependencyManager.shutdown();

XdsTestUtils.setAdsConfig(controlPlaneService, serverName, "RDS", "CDS", "EDS",
ENDPOINT_HOSTNAME + "2", ENDPOINT_PORT);
verify(resourceWatcher, timeout(5000).times(2)).onChanged(any());
xdsClient.cancelXdsResourceWatch(
XdsEndpointResource.getInstance(), serverName, resourceWatcher);
});
}

private Listener buildInlineClientListener(String rdsName, String clusterName) {
return XdsTestUtils.buildInlineClientListener(rdsName, clusterName, serverName);
}
Expand Down