Skip to content

Commit

Permalink
DNS change fix
Browse files Browse the repository at this point in the history
This update fixes an issue when the driver fails to update the routing table if the router DNS entry returns a different IP address.
  • Loading branch information
injectives committed Feb 8, 2021
1 parent d60da4c commit 310a9f9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ private ClusterComposition handleRoutingProcedureError( Throwable error, Routing
public List<BoltServerAddress> 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<BoltServerAddress> resolveAll( BoltServerAddress address )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -400,22 +402,36 @@ 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
verify( logger, never() ).info( startsWith( "Unable to fetch new routing table, will try again in " ) );
assertEquals( 0, eventExecutor.scheduleDelays().size() );
}

@Test
void shouldNotResolveToIPs()
{
ServerAddressResolver resolver = resolverMock( A, A );
Rediscovery rediscovery = new RediscoveryImpl( A, null, null, null, resolver, null );

List<BoltServerAddress> 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 );
Expand Down

0 comments on commit 310a9f9

Please sign in to comment.