diff --git a/java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/EndpointLifecycleManager.java b/java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/EndpointLifecycleManager.java index b0790a5e05b5..4a551a7b577a 100644 --- a/java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/EndpointLifecycleManager.java +++ b/java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/EndpointLifecycleManager.java @@ -133,6 +133,7 @@ static final class EndpointState { private final long probeIntervalSeconds; private final Duration idleEvictionDuration; private final Clock clock; + private final boolean enableBackgroundProbing; private final String defaultEndpointAddress; private ScheduledFuture evictionFuture; @@ -161,10 +162,26 @@ private PendingActiveAddressUpdate(Set activeAddresses, long generation) long probeIntervalSeconds, Duration idleEvictionDuration, Clock clock) { + this( + endpointCache, + probeIntervalSeconds, + idleEvictionDuration, + clock, + /* enableBackgroundProbing= */ true); + } + + @VisibleForTesting + EndpointLifecycleManager( + ChannelEndpointCache endpointCache, + long probeIntervalSeconds, + Duration idleEvictionDuration, + Clock clock, + boolean enableBackgroundProbing) { this.endpointCache = endpointCache; this.probeIntervalSeconds = probeIntervalSeconds; this.idleEvictionDuration = idleEvictionDuration; this.clock = clock; + this.enableBackgroundProbing = enableBackgroundProbing; this.defaultEndpointAddress = endpointCache.defaultChannel().getAddress(); this.activeAddressUpdateExecutor = Executors.newSingleThreadExecutor( @@ -439,6 +456,9 @@ private void createAndStartProbing(String address) { /** Starts periodic probing for an endpoint. */ private void startProbing(String address) { + if (!enableBackgroundProbing) { + return; + } EndpointState state = endpoints.get(address); if (state == null || isShutdown.get()) { return; diff --git a/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/EndpointLifecycleManagerTest.java b/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/EndpointLifecycleManagerTest.java index 5eb73362ee1f..95ba1cbaca76 100644 --- a/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/EndpointLifecycleManagerTest.java +++ b/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/EndpointLifecycleManagerTest.java @@ -275,7 +275,11 @@ public void transientFailureOscillationWithConnectingStillEvictsEndpoint() throw KeyRangeCacheTest.FakeEndpointCache cache = new KeyRangeCacheTest.FakeEndpointCache(); manager = new EndpointLifecycleManager( - cache, /* probeIntervalSeconds= */ 60, Duration.ofMinutes(30), Clock.systemUTC()); + cache, + /* probeIntervalSeconds= */ 60, + Duration.ofMinutes(30), + Clock.systemUTC(), + /* enableBackgroundProbing= */ false); registerAddresses(manager, "server1"); awaitCondition( @@ -310,7 +314,11 @@ public void readyResetsTransientFailureCounterAfterRecovery() throws Exception { KeyRangeCacheTest.FakeEndpointCache cache = new KeyRangeCacheTest.FakeEndpointCache(); manager = new EndpointLifecycleManager( - cache, /* probeIntervalSeconds= */ 60, Duration.ofMinutes(30), Clock.systemUTC()); + cache, + /* probeIntervalSeconds= */ 60, + Duration.ofMinutes(30), + Clock.systemUTC(), + /* enableBackgroundProbing= */ false); registerAddresses(manager, "server1"); awaitCondition(