diff --git a/grpc-gcp/src/main/java/com/google/cloud/grpc/GcpMultiEndpointChannel.java b/grpc-gcp/src/main/java/com/google/cloud/grpc/GcpMultiEndpointChannel.java index 90edfa8c..44e04178 100644 --- a/grpc-gcp/src/main/java/com/google/cloud/grpc/GcpMultiEndpointChannel.java +++ b/grpc-gcp/src/main/java/com/google/cloud/grpc/GcpMultiEndpointChannel.java @@ -394,8 +394,11 @@ public ClientCall newCall( } /** - * The authority of the destination this channel connects to. Typically this is in the format - * {@code host:port}. + * The authority of the current endpoint of the default MultiEndpoint. Typically, this is in the + * format {@code host:port}. + * + * To get the authority of the current endpoint of another MultiEndpoint use {@link + * #authorityFor(String)} method. * * This may return different values over time because MultiEndpoint may switch between endpoints. * @@ -405,4 +408,18 @@ public ClientCall newCall( public String authority() { return pools.get(defaultMultiEndpoint.getCurrentId()).authority(); } + + /** + * The authority of the current endpoint of the specified MultiEndpoint. Typically, this is in the + * format {@code host:port}. + * + * This may return different values over time because MultiEndpoint may switch between endpoints. + */ + public String authorityFor(String multiEndpointName) { + MultiEndpoint multiEndpoint = multiEndpoints.get(multiEndpointName); + if (multiEndpoint == null) { + return null; + } + return pools.get(multiEndpoint.getCurrentId()).authority(); + } } diff --git a/grpc-gcp/src/test/java/com/google/cloud/grpc/SpannerIntegrationTest.java b/grpc-gcp/src/test/java/com/google/cloud/grpc/SpannerIntegrationTest.java index 56b890e7..780c6f58 100644 --- a/grpc-gcp/src/test/java/com/google/cloud/grpc/SpannerIntegrationTest.java +++ b/grpc-gcp/src/test/java/com/google/cloud/grpc/SpannerIntegrationTest.java @@ -566,6 +566,14 @@ public void testSpannerMultiEndpointClient() throws IOException, InterruptedExce final String followerPoolIndex = String.format("pool-%d", currentIndex); final String leaderPoolIndex = String.format("pool-%d", currentIndex - 1); + // Make sure authorities are overridden by channel configurator. + assertThat(gcpMultiEndpointChannel.authority()).isEqualTo(SPANNER_TARGET); + assertThat(gcpMultiEndpointChannel.authorityFor("leader")) + .isEqualTo(SPANNER_TARGET); + assertThat(gcpMultiEndpointChannel.authorityFor("follower")) + .isEqualTo(SPANNER_TARGET); + assertThat(gcpMultiEndpointChannel.authorityFor("no-such-name")).isNull(); + TimeUnit.MILLISECONDS.sleep(200); List logMessages = logRecords.stream()