From 310a9f93bfdf769b5cf6be48fd3e76b167c59e2d Mon Sep 17 00:00:00 2001 From: Dmitriy Tverdiakov Date: Thu, 4 Feb 2021 11:45:56 +0200 Subject: [PATCH] DNS change fix This update fixes an issue when the driver fails to update the routing table if the router DNS entry returns a different IP address. --- .../driver/internal/BoltServerAddress.java | 6 ++++- .../internal/cluster/RediscoveryImpl.java | 6 ++--- .../internal/cluster/RediscoveryTest.java | 22 ++++++++++++++++--- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/driver/src/main/java/org/neo4j/driver/internal/BoltServerAddress.java b/driver/src/main/java/org/neo4j/driver/internal/BoltServerAddress.java index c4620d753f..159ced30d4 100644 --- a/driver/src/main/java/org/neo4j/driver/internal/BoltServerAddress.java +++ b/driver/src/main/java/org/neo4j/driver/internal/BoltServerAddress.java @@ -25,7 +25,6 @@ import java.net.UnknownHostException; import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; import java.util.stream.Stream; import org.neo4j.driver.net.ServerAddress; @@ -154,6 +153,11 @@ public int port() return port; } + public boolean isResolved() + { + return resolved != null; + } + private static String hostFrom( URI uri ) { String host = uri.getHost(); diff --git a/driver/src/main/java/org/neo4j/driver/internal/cluster/RediscoveryImpl.java b/driver/src/main/java/org/neo4j/driver/internal/cluster/RediscoveryImpl.java index 99bcef1bc3..fe15f2db6b 100644 --- a/driver/src/main/java/org/neo4j/driver/internal/cluster/RediscoveryImpl.java +++ b/driver/src/main/java/org/neo4j/driver/internal/cluster/RediscoveryImpl.java @@ -268,9 +268,9 @@ private ClusterComposition handleRoutingProcedureError( Throwable error, Routing public List resolve() { return resolver.resolve( initialRouter ) - .stream() - .flatMap( resolved -> resolveAll( BoltServerAddress.from( resolved ) ) ) - .collect( toList() ); // collect to list to preserve the order + .stream() + .map( BoltServerAddress::from ) + .collect( toList() ); // collect to list to preserve the order } private Stream resolveAll( BoltServerAddress address ) diff --git a/driver/src/test/java/org/neo4j/driver/internal/cluster/RediscoveryTest.java b/driver/src/test/java/org/neo4j/driver/internal/cluster/RediscoveryTest.java index 21b7b98b93..01df9b3550 100644 --- a/driver/src/test/java/org/neo4j/driver/internal/cluster/RediscoveryTest.java +++ b/driver/src/test/java/org/neo4j/driver/internal/cluster/RediscoveryTest.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.neo4j.driver.Logger; @@ -49,6 +50,7 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; @@ -400,7 +402,8 @@ void shouldNotLogWhenSingleRetryAttemptFails() Rediscovery rediscovery = new RediscoveryImpl( A, settings, compositionProvider, eventExecutor, resolver, logger ); RoutingTable table = routingTableMock( A ); - ServiceUnavailableException e = assertThrows( ServiceUnavailableException.class, () -> await( rediscovery.lookupClusterComposition( table, pool, empty() ) ) ); + ServiceUnavailableException e = + assertThrows( ServiceUnavailableException.class, () -> await( rediscovery.lookupClusterComposition( table, pool, empty() ) ) ); assertThat( e.getMessage(), containsString( "Could not perform discovery" ) ); // rediscovery should not log about retries and should not schedule any retries @@ -408,14 +411,27 @@ void shouldNotLogWhenSingleRetryAttemptFails() assertEquals( 0, eventExecutor.scheduleDelays().size() ); } + @Test + void shouldNotResolveToIPs() + { + ServerAddressResolver resolver = resolverMock( A, A ); + Rediscovery rediscovery = new RediscoveryImpl( A, null, null, null, resolver, null ); + + List addresses = rediscovery.resolve(); + + verify( resolver, times( 1 ) ).resolve( A ); + assertEquals( 1, addresses.size() ); + assertFalse( addresses.get( 0 ).isResolved() ); + } + private Rediscovery newRediscovery( BoltServerAddress initialRouter, ClusterCompositionProvider compositionProvider, - ServerAddressResolver resolver ) + ServerAddressResolver resolver ) { return newRediscovery( initialRouter, compositionProvider, resolver, DEV_NULL_LOGGER ); } private Rediscovery newRediscovery( BoltServerAddress initialRouter, ClusterCompositionProvider compositionProvider, - ServerAddressResolver resolver, Logger logger ) + ServerAddressResolver resolver, Logger logger ) { RoutingSettings settings = new RoutingSettings( 1, 0, 0 ); return new RediscoveryImpl( initialRouter, settings, compositionProvider, GlobalEventExecutor.INSTANCE, resolver, logger );