From 3202370684ec74a8e986a116b3e1476ff0a3fedc Mon Sep 17 00:00:00 2001 From: Anirudh Ramachandra Date: Thu, 1 Feb 2024 22:51:00 +0530 Subject: [PATCH] Allow users to start watching xDS resources (#10864) --- xds/src/main/java/io/grpc/xds/XdsClient.java | 24 +++++++++++-------- .../main/java/io/grpc/xds/XdsClientImpl.java | 13 +++++----- .../io/grpc/xds/CdsLoadBalancer2Test.java | 11 +++++---- .../xds/ClusterResolverLoadBalancerTest.java | 13 +++++----- .../java/io/grpc/xds/XdsNameResolverTest.java | 14 +++++------ .../java/io/grpc/xds/XdsServerTestHelper.java | 14 +++++------ 6 files changed, 48 insertions(+), 41 deletions(-) diff --git a/xds/src/main/java/io/grpc/xds/XdsClient.java b/xds/src/main/java/io/grpc/xds/XdsClient.java index 267d5468b6..c8163aa981 100644 --- a/xds/src/main/java/io/grpc/xds/XdsClient.java +++ b/xds/src/main/java/io/grpc/xds/XdsClient.java @@ -26,6 +26,7 @@ import com.google.common.net.UrlEscapers; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.protobuf.Any; +import io.grpc.ExperimentalApi; import io.grpc.Status; import io.grpc.xds.Bootstrapper.ServerInfo; import io.grpc.xds.LoadStatsManager2.ClusterDropStats; @@ -115,7 +116,8 @@ public abstract class XdsClient { /** * Watcher interface for a single requested xDS resource. */ - interface ResourceWatcher { + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/10862") + public interface ResourceWatcher { /** * Called when the resource discovery RPC encounters some transient error. @@ -123,7 +125,7 @@ public abstract class XdsClient { *

Note that we expect that the implementer to: * - Comply with the guarantee to not generate certain statuses by the library: * https://grpc.github.io/grpc/core/md_doc_statuscodes.html. If the code needs to be - * propagated to the channel, override it with {@link Status.Code#UNAVAILABLE}. + * propagated to the channel, override it with {@link io.grpc.Status.Code#UNAVAILABLE}. * - Keep {@link Status} description in one form or another, as it contains valuable debugging * information. */ @@ -306,23 +308,25 @@ public abstract class XdsClient { /** * Registers a data watcher for the given Xds resource. */ - void watchXdsResource(XdsResourceType type, String resourceName, - ResourceWatcher watcher, - Executor executor) { + public void watchXdsResource(XdsResourceType type, + String resourceName, + ResourceWatcher watcher, + Executor executor) { throw new UnsupportedOperationException(); } - void watchXdsResource(XdsResourceType type, String resourceName, - ResourceWatcher watcher) { + public void watchXdsResource(XdsResourceType type, + String resourceName, + ResourceWatcher watcher) { watchXdsResource(type, resourceName, watcher, MoreExecutors.directExecutor()); } /** * Unregisters the given resource watcher. */ - void cancelXdsResourceWatch(XdsResourceType type, - String resourceName, - ResourceWatcher watcher) { + public void cancelXdsResourceWatch(XdsResourceType type, + String resourceName, + ResourceWatcher watcher) { throw new UnsupportedOperationException(); } diff --git a/xds/src/main/java/io/grpc/xds/XdsClientImpl.java b/xds/src/main/java/io/grpc/xds/XdsClientImpl.java index 64d29cd4c3..577d7e7925 100644 --- a/xds/src/main/java/io/grpc/xds/XdsClientImpl.java +++ b/xds/src/main/java/io/grpc/xds/XdsClientImpl.java @@ -282,9 +282,10 @@ final class XdsClientImpl extends XdsClient } @Override - void watchXdsResource(XdsResourceType type, String resourceName, - ResourceWatcher watcher, - Executor watcherExecutor) { + public void watchXdsResource(XdsResourceType type, + String resourceName, + ResourceWatcher watcher, + Executor watcherExecutor) { syncContext.execute(new Runnable() { @Override @SuppressWarnings("unchecked") @@ -309,9 +310,9 @@ final class XdsClientImpl extends XdsClient } @Override - void cancelXdsResourceWatch(XdsResourceType type, - String resourceName, - ResourceWatcher watcher) { + public void cancelXdsResourceWatch(XdsResourceType type, + String resourceName, + ResourceWatcher watcher) { syncContext.execute(new Runnable() { @Override @SuppressWarnings("unchecked") diff --git a/xds/src/test/java/io/grpc/xds/CdsLoadBalancer2Test.java b/xds/src/test/java/io/grpc/xds/CdsLoadBalancer2Test.java index 3014c809ec..62fb929336 100644 --- a/xds/src/test/java/io/grpc/xds/CdsLoadBalancer2Test.java +++ b/xds/src/test/java/io/grpc/xds/CdsLoadBalancer2Test.java @@ -789,8 +789,9 @@ public class CdsLoadBalancer2Test { @Override @SuppressWarnings("unchecked") - void watchXdsResource(XdsResourceType type, String resourceName, - ResourceWatcher watcher, Executor syncContext) { + public void watchXdsResource(XdsResourceType type, + String resourceName, + ResourceWatcher watcher, Executor syncContext) { assertThat(type.typeName()).isEqualTo("CDS"); watchers.computeIfAbsent(resourceName, k -> new ArrayList<>()) .add((ResourceWatcher)watcher); @@ -798,9 +799,9 @@ public class CdsLoadBalancer2Test { @Override @SuppressWarnings("unchecked") - void cancelXdsResourceWatch(XdsResourceType type, - String resourceName, - ResourceWatcher watcher) { + public void cancelXdsResourceWatch(XdsResourceType type, + String resourceName, + ResourceWatcher watcher) { assertThat(type.typeName()).isEqualTo("CDS"); assertThat(watchers).containsKey(resourceName); List> watcherList = watchers.get(resourceName); diff --git a/xds/src/test/java/io/grpc/xds/ClusterResolverLoadBalancerTest.java b/xds/src/test/java/io/grpc/xds/ClusterResolverLoadBalancerTest.java index 76756e6330..cc942a1ec2 100644 --- a/xds/src/test/java/io/grpc/xds/ClusterResolverLoadBalancerTest.java +++ b/xds/src/test/java/io/grpc/xds/ClusterResolverLoadBalancerTest.java @@ -1184,9 +1184,10 @@ public class ClusterResolverLoadBalancerTest { @Override @SuppressWarnings("unchecked") - void watchXdsResource(XdsResourceType type, String resourceName, - ResourceWatcher watcher, - Executor syncContext) { + public void watchXdsResource(XdsResourceType type, + String resourceName, + ResourceWatcher watcher, + Executor syncContext) { assertThat(type.typeName()).isEqualTo("EDS"); assertThat(watchers).doesNotContainKey(resourceName); watchers.put(resourceName, (ResourceWatcher) watcher); @@ -1194,9 +1195,9 @@ public class ClusterResolverLoadBalancerTest { @Override @SuppressWarnings("unchecked") - void cancelXdsResourceWatch(XdsResourceType type, - String resourceName, - ResourceWatcher watcher) { + public void cancelXdsResourceWatch(XdsResourceType type, + String resourceName, + ResourceWatcher watcher) { assertThat(type.typeName()).isEqualTo("EDS"); assertThat(watchers).containsKey(resourceName); watchers.remove(resourceName); diff --git a/xds/src/test/java/io/grpc/xds/XdsNameResolverTest.java b/xds/src/test/java/io/grpc/xds/XdsNameResolverTest.java index 18b9ceffb6..d93a9c9935 100644 --- a/xds/src/test/java/io/grpc/xds/XdsNameResolverTest.java +++ b/xds/src/test/java/io/grpc/xds/XdsNameResolverTest.java @@ -1915,10 +1915,10 @@ public class XdsNameResolverTest { @Override @SuppressWarnings("unchecked") - void watchXdsResource(XdsResourceType resourceType, - String resourceName, - ResourceWatcher watcher, - Executor syncContext) { + public void watchXdsResource(XdsResourceType resourceType, + String resourceName, + ResourceWatcher watcher, + Executor syncContext) { switch (resourceType.typeName()) { case "LDS": @@ -1939,9 +1939,9 @@ public class XdsNameResolverTest { } @Override - void cancelXdsResourceWatch(XdsResourceType type, - String resourceName, - ResourceWatcher watcher) { + public void cancelXdsResourceWatch(XdsResourceType type, + String resourceName, + ResourceWatcher watcher) { switch (type.typeName()) { case "LDS": assertThat(ldsResource).isNotNull(); diff --git a/xds/src/test/java/io/grpc/xds/XdsServerTestHelper.java b/xds/src/test/java/io/grpc/xds/XdsServerTestHelper.java index 5e2ab30de3..59dcffb22c 100644 --- a/xds/src/test/java/io/grpc/xds/XdsServerTestHelper.java +++ b/xds/src/test/java/io/grpc/xds/XdsServerTestHelper.java @@ -181,10 +181,10 @@ public class XdsServerTestHelper { @Override @SuppressWarnings("unchecked") - void watchXdsResource(XdsResourceType resourceType, - String resourceName, - ResourceWatcher watcher, - Executor syncContext) { + public void watchXdsResource(XdsResourceType resourceType, + String resourceName, + ResourceWatcher watcher, + Executor syncContext) { switch (resourceType.typeName()) { case "LDS": assertThat(ldsWatcher).isNull(); @@ -201,9 +201,9 @@ public class XdsServerTestHelper { } @Override - void cancelXdsResourceWatch(XdsResourceType type, - String resourceName, - ResourceWatcher watcher) { + public void cancelXdsResourceWatch(XdsResourceType type, + String resourceName, + ResourceWatcher watcher) { switch (type.typeName()) { case "LDS": assertThat(ldsWatcher).isNotNull();