Skip to content

Commit 240f731

Browse files
committed
xds: Avoid changing cache when watching children in XdsDepManager
The watchers can be completely regular, so the base class can do the cache management while the subclasses are only concerned with subscribing to children.
1 parent 26bd0ee commit 240f731

File tree

1 file changed

+22
-52
lines changed

1 file changed

+22
-52
lines changed

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

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ private static void addConfigForCluster(
333333
Status.INTERNAL.withDescription("Logical DNS in dependency manager unsupported")));
334334
break;
335335
default:
336-
throw new IllegalStateException("Unexpected value: " + cdsUpdate.clusterType());
336+
child = new EndpointConfig(StatusOr.fromStatus(Status.UNAVAILABLE.withDescription(
337+
"Unknown type in cluster " + clusterName + " " + cdsUpdate.clusterType())));
337338
}
338339
if (clusters.containsKey(clusterName)) {
339340
// If a cycle is detected, we'll have detected it while recursing, so now there will be a key
@@ -520,7 +521,7 @@ public void onError(Status error) {
520521
}
521522
// Don't update configuration on error, if we've already received configuration
522523
if (!hasDataValue()) {
523-
setDataAsStatus(Status.UNAVAILABLE.withDescription(
524+
this.data = StatusOr.fromStatus(Status.UNAVAILABLE.withDescription(
524525
String.format("Error retrieving %s: %s: %s",
525526
toContextString(), error.getCode(), error.getDescription())));
526527
maybePublishConfig();
@@ -534,11 +535,25 @@ public void onResourceDoesNotExist(String resourceName) {
534535
}
535536

536537
checkArgument(this.resourceName.equals(resourceName), "Resource name does not match");
537-
setDataAsStatus(Status.UNAVAILABLE.withDescription(
538+
this.data = StatusOr.fromStatus(Status.UNAVAILABLE.withDescription(
538539
toContextString() + " does not exist" + nodeInfo()));
539540
maybePublishConfig();
540541
}
541542

543+
@Override
544+
public void onChanged(T update) {
545+
checkNotNull(update, "update");
546+
if (cancelled) {
547+
return;
548+
}
549+
550+
this.data = StatusOr.fromValue(update);
551+
subscribeToChildren(update);
552+
maybePublishConfig();
553+
}
554+
555+
protected abstract void subscribeToChildren(T update);
556+
542557
public void close() {
543558
cancelled = true;
544559
xdsClient.cancelXdsResourceWatch(type, resourceName, this);
@@ -557,20 +572,6 @@ boolean hasDataValue() {
557572
return data != null && data.hasValue();
558573
}
559574

560-
String resourceName() {
561-
return resourceName;
562-
}
563-
564-
protected void setData(T data) {
565-
checkNotNull(data, "data");
566-
this.data = StatusOr.fromValue(data);
567-
}
568-
569-
protected void setDataAsStatus(Status status) {
570-
checkNotNull(status, "status");
571-
this.data = StatusOr.fromStatus(status);
572-
}
573-
574575
public String toContextString() {
575576
return toContextStr(type.typeName(), resourceName);
576577
}
@@ -588,12 +589,7 @@ private LdsWatcher(String resourceName) {
588589
}
589590

590591
@Override
591-
public void onChanged(XdsListenerResource.LdsUpdate update) {
592-
checkNotNull(update, "update");
593-
if (cancelled) {
594-
return;
595-
}
596-
592+
public void subscribeToChildren(XdsListenerResource.LdsUpdate update) {
597593
HttpConnectionManager httpConnectionManager = update.httpConnectionManager();
598594
List<VirtualHost> virtualHosts;
599595
if (httpConnectionManager == null) {
@@ -610,9 +606,6 @@ public void onChanged(XdsListenerResource.LdsUpdate update) {
610606
if (rdsName != null) {
611607
addRdsWatcher(rdsName);
612608
}
613-
614-
setData(update);
615-
maybePublishConfig();
616609
}
617610

618611
private String getRdsName(XdsListenerResource.LdsUpdate update) {
@@ -680,14 +673,8 @@ public RdsWatcher(String resourceName) {
680673
}
681674

682675
@Override
683-
public void onChanged(RdsUpdate update) {
684-
checkNotNull(update, "update");
685-
if (cancelled) {
686-
return;
687-
}
688-
setData(update);
676+
public void subscribeToChildren(RdsUpdate update) {
689677
updateRoutes(update.virtualHosts);
690-
maybePublishConfig();
691678
}
692679

693680
@Override
@@ -705,31 +692,20 @@ private class CdsWatcher extends XdsWatcherBase<XdsClusterResource.CdsUpdate> {
705692
}
706693

707694
@Override
708-
public void onChanged(XdsClusterResource.CdsUpdate update) {
709-
checkNotNull(update, "update");
710-
if (cancelled) {
711-
return;
712-
}
695+
public void subscribeToChildren(XdsClusterResource.CdsUpdate update) {
713696
switch (update.clusterType()) {
714697
case EDS:
715-
setData(update);
716698
addEdsWatcher(getEdsServiceName());
717699
break;
718700
case LOGICAL_DNS:
719-
setData(update);
720701
// no eds needed
721702
break;
722703
case AGGREGATE:
723-
setData(update);
724704
update.prioritizedClusterNames()
725705
.forEach(name -> addClusterWatcher(name));
726706
break;
727707
default:
728-
Status error = Status.UNAVAILABLE.withDescription(
729-
"unknown cluster type in " + resourceName() + " " + update.clusterType());
730-
setDataAsStatus(error);
731708
}
732-
maybePublishConfig();
733709
}
734710

735711
public String getEdsServiceName() {
@@ -749,12 +725,6 @@ private EdsWatcher(String resourceName) {
749725
}
750726

751727
@Override
752-
public void onChanged(XdsEndpointResource.EdsUpdate update) {
753-
if (cancelled) {
754-
return;
755-
}
756-
setData(checkNotNull(update, "update"));
757-
maybePublishConfig();
758-
}
728+
public void subscribeToChildren(XdsEndpointResource.EdsUpdate update) {}
759729
}
760730
}

0 commit comments

Comments
 (0)