Skip to content

Commit

Permalink
Move private methods ahead of classes
Browse files Browse the repository at this point in the history
  • Loading branch information
larry-safran committed Feb 11, 2025
1 parent 105e580 commit 9aa5102
Showing 1 changed file with 123 additions and 125 deletions.
248 changes: 123 additions & 125 deletions xds/src/main/java/io/grpc/xds/XdsDependencyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ private void cancelEdsWatcher(EdsWatcher watcher, CdsWatcher parentContext) {
}
}



private <T extends ResourceUpdate> void cancelWatcher(XdsWatcherBase<T> watcher) {
syncContext.throwIfNotInThisSynchronizationContext();

Expand Down Expand Up @@ -428,6 +426,129 @@ public String toString() {
return logId.toString();
}

// Returns true if the watcher was added, false if it already exists
private boolean addEdsWatcher(String edsServiceName, CdsWatcher parentContext) {
TypeWatchers<?> typeWatchers = resourceWatchers.get(XdsEndpointResource.getInstance());
if (typeWatchers == null || !typeWatchers.watchers.containsKey(edsServiceName)) {
addWatcher(new EdsWatcher(edsServiceName, parentContext));
return true;
}

EdsWatcher watcher = (EdsWatcher) typeWatchers.watchers.get(edsServiceName);
watcher.addParentContext(parentContext); // Is a set, so don't need to check for existence
return false;
}

private void addClusterWatcher(String clusterName, Object parentContext, int depth) {
TypeWatchers<?> clusterWatchers = resourceWatchers.get(CLUSTER_RESOURCE);
if (clusterWatchers != null) {
CdsWatcher watcher = (CdsWatcher) clusterWatchers.watchers.get(clusterName);
if (watcher != null) {
watcher.parentContexts.put(parentContext, depth);
return;
}
}

addWatcher(new CdsWatcher(clusterName, parentContext, depth));
}

private void updateRoutes(List<VirtualHost> virtualHosts, Object newParentContext,
VirtualHost oldVirtualHost, boolean sameParentContext) {
VirtualHost virtualHost =
RoutingUtils.findVirtualHostForHostName(virtualHosts, dataPlaneAuthority);
if (virtualHost == null) {
String error = "Failed to find virtual host matching hostname: " + dataPlaneAuthority;
logger.log(XdsLogger.XdsLogLevel.WARNING, error);
cleanUpRoutes();
xdsConfigWatcher.onError(
"xDS node ID:" + dataPlaneAuthority, Status.UNAVAILABLE.withDescription(error));
return;
}

Set<String> newClusters = getClusterNamesFromVirtualHost(virtualHost);
Set<String> oldClusters = getClusterNamesFromVirtualHost(oldVirtualHost);

if (sameParentContext) {
// Calculate diffs.
Set<String> addedClusters = Sets.difference(newClusters, oldClusters);
Set<String> deletedClusters = Sets.difference(oldClusters, newClusters);

deletedClusters.forEach(watcher ->
cancelClusterWatcherTree(getCluster(watcher), newParentContext));
addedClusters.forEach((cluster) -> addClusterWatcher(cluster, newParentContext, 1));
} else {
newClusters.forEach((cluster) -> addClusterWatcher(cluster, newParentContext, 1));
}
}

private static Set<String> getClusterNamesFromVirtualHost(VirtualHost virtualHost) {
if (virtualHost == null) {
return Collections.emptySet();
}

// Get all cluster names to which requests can be routed through the virtual host.
Set<String> clusters = new HashSet<>();
for (VirtualHost.Route route : virtualHost.routes()) {
VirtualHost.Route.RouteAction action = route.routeAction();
if (action == null) {
continue;

Check warning on line 494 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#L494

Added line #L494 was not covered by tests
}
if (action.cluster() != null) {
clusters.add(action.cluster());
} else if (action.weightedClusters() != null) {
for (ClusterWeight weighedCluster : action.weightedClusters()) {
clusters.add(weighedCluster.name());
}

Check warning on line 501 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#L500-L501

Added lines #L500 - L501 were not covered by tests
}
}

return clusters;
}

@Nullable
private VirtualHost getActiveVirtualHost() {
TypeWatchers<?> rdsWatchers = resourceWatchers.get(XdsRouteConfigureResource.getInstance());
if (rdsWatchers == null) {
return null;
}

RdsWatcher activeRdsWatcher =
(RdsWatcher) rdsWatchers.watchers.values().stream().findFirst().orElse(null);
if (activeRdsWatcher == null || activeRdsWatcher.missingResult()
|| !activeRdsWatcher.getData().hasValue()) {
return null;
}

return RoutingUtils.findVirtualHostForHostName(
activeRdsWatcher.getData().getValue().virtualHosts, dataPlaneAuthority);
}

// Must be in SyncContext
private void cleanUpRoutes() {
// Remove RdsWatcher & CDS Watchers
TypeWatchers<?> rdsResourceWatcher =
resourceWatchers.get(XdsRouteConfigureResource.getInstance());
if (rdsResourceWatcher == null || rdsResourceWatcher.watchers.isEmpty()) {
return;
}

XdsWatcherBase<?> watcher = rdsResourceWatcher.watchers.values().stream().findFirst().get();
cancelWatcher(watcher);

Check warning on line 536 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#L535-L536

Added lines #L535 - L536 were not covered by tests

// Remove CdsWatchers pointed to by the RdsWatcher
RdsWatcher rdsWatcher = (RdsWatcher) watcher;

Check warning on line 539 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#L539

Added line #L539 was not covered by tests
for (String cName : rdsWatcher.getCdsNames()) {
CdsWatcher cdsWatcher = getCluster(cName);

Check warning on line 541 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#L541

Added line #L541 was not covered by tests
if (cdsWatcher != null) {
cancelClusterWatcherTree(cdsWatcher, rdsWatcher);

Check warning on line 543 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#L543

Added line #L543 was not covered by tests
}
}
}

Check warning on line 546 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#L545-L546

Added lines #L545 - L546 were not covered by tests

private CdsWatcher getCluster(String clusterName) {
return (CdsWatcher) resourceWatchers.get(CLUSTER_RESOURCE).watchers.get(clusterName);
}

private static class TypeWatchers<T extends ResourceUpdate> {
// Key is resource name
final Map<String, XdsWatcherBase<T>> watchers = new HashMap<>();
Expand Down Expand Up @@ -720,32 +841,6 @@ public void onResourceDoesNotExist(String resourceName) {
}
}

// Returns true if the watcher was added, false if it already exists
private boolean addEdsWatcher(String edsServiceName, CdsWatcher parentContext) {
TypeWatchers<?> typeWatchers = resourceWatchers.get(XdsEndpointResource.getInstance());
if (typeWatchers == null || !typeWatchers.watchers.containsKey(edsServiceName)) {
addWatcher(new EdsWatcher(edsServiceName, parentContext));
return true;
}

EdsWatcher watcher = (EdsWatcher) typeWatchers.watchers.get(edsServiceName);
watcher.addParentContext(parentContext); // Is a set, so don't need to check for existence
return false;
}

private void addClusterWatcher(String clusterName, Object parentContext, int depth) {
TypeWatchers<?> clusterWatchers = resourceWatchers.get(CLUSTER_RESOURCE);
if (clusterWatchers != null) {
CdsWatcher watcher = (CdsWatcher) clusterWatchers.watchers.get(clusterName);
if (watcher != null) {
watcher.parentContexts.put(parentContext, depth);
return;
}
}

addWatcher(new CdsWatcher(clusterName, parentContext, depth));
}

private class EdsWatcher extends XdsWatcherBase<XdsEndpointResource.EdsUpdate> {
private final Set<CdsWatcher> parentContexts = new HashSet<>();

Expand All @@ -770,101 +865,4 @@ void addParentContext(CdsWatcher parentContext) {
parentContexts.add(checkNotNull(parentContext, "parentContext"));
}
}

private void updateRoutes(List<VirtualHost> virtualHosts, Object newParentContext,
VirtualHost oldVirtualHost, boolean sameParentContext) {
VirtualHost virtualHost =
RoutingUtils.findVirtualHostForHostName(virtualHosts, dataPlaneAuthority);
if (virtualHost == null) {
String error = "Failed to find virtual host matching hostname: " + dataPlaneAuthority;
logger.log(XdsLogger.XdsLogLevel.WARNING, error);
cleanUpRoutes();
xdsConfigWatcher.onError(
"xDS node ID:" + dataPlaneAuthority, Status.UNAVAILABLE.withDescription(error));
return;
}

Set<String> newClusters = getClusterNamesFromVirtualHost(virtualHost);
Set<String> oldClusters = getClusterNamesFromVirtualHost(oldVirtualHost);

if (sameParentContext) {
// Calculate diffs.
Set<String> addedClusters = Sets.difference(newClusters, oldClusters);
Set<String> deletedClusters = Sets.difference(oldClusters, newClusters);

deletedClusters.forEach(watcher ->
cancelClusterWatcherTree(getCluster(watcher), newParentContext));
addedClusters.forEach((cluster) -> addClusterWatcher(cluster, newParentContext, 1));
} else {
newClusters.forEach((cluster) -> addClusterWatcher(cluster, newParentContext, 1));
}
}

private static Set<String> getClusterNamesFromVirtualHost(VirtualHost virtualHost) {
if (virtualHost == null) {
return Collections.emptySet();
}

// Get all cluster names to which requests can be routed through the virtual host.
Set<String> clusters = new HashSet<>();
for (VirtualHost.Route route : virtualHost.routes()) {
VirtualHost.Route.RouteAction action = route.routeAction();
if (action == null) {
continue;
}
if (action.cluster() != null) {
clusters.add(action.cluster());
} else if (action.weightedClusters() != null) {
for (ClusterWeight weighedCluster : action.weightedClusters()) {
clusters.add(weighedCluster.name());
}
}
}

return clusters;
}

@Nullable
private VirtualHost getActiveVirtualHost() {
TypeWatchers<?> rdsWatchers = resourceWatchers.get(XdsRouteConfigureResource.getInstance());
if (rdsWatchers == null) {
return null;
}

RdsWatcher activeRdsWatcher =
(RdsWatcher) rdsWatchers.watchers.values().stream().findFirst().orElse(null);
if (activeRdsWatcher == null || activeRdsWatcher.missingResult()
|| !activeRdsWatcher.getData().hasValue()) {
return null;
}

return RoutingUtils.findVirtualHostForHostName(
activeRdsWatcher.getData().getValue().virtualHosts, dataPlaneAuthority);
}

// Must be in SyncContext
private void cleanUpRoutes() {
// Remove RdsWatcher & CDS Watchers
TypeWatchers<?> rdsResourceWatcher =
resourceWatchers.get(XdsRouteConfigureResource.getInstance());
if (rdsResourceWatcher == null || rdsResourceWatcher.watchers.isEmpty()) {
return;
}

XdsWatcherBase<?> watcher = rdsResourceWatcher.watchers.values().stream().findFirst().get();
cancelWatcher(watcher);

// Remove CdsWatchers pointed to by the RdsWatcher
RdsWatcher rdsWatcher = (RdsWatcher) watcher;
for (String cName : rdsWatcher.getCdsNames()) {
CdsWatcher cdsWatcher = getCluster(cName);
if (cdsWatcher != null) {
cancelClusterWatcherTree(cdsWatcher, rdsWatcher);
}
}
}

private CdsWatcher getCluster(String clusterName) {
return (CdsWatcher) resourceWatchers.get(CLUSTER_RESOURCE).watchers.get(clusterName);
}
}

0 comments on commit 9aa5102

Please sign in to comment.