Skip to content

Commit 7766216

Browse files
author
Zhen
committed
Resolve dns to ip
1 parent 37b49bd commit 7766216

File tree

8 files changed

+146
-27
lines changed

8 files changed

+146
-27
lines changed

driver/src/main/java/org/neo4j/driver/internal/cluster/ClusterRoutingTable.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ public synchronized Set<BoltServerAddress> update( ClusterComposition cluster )
7878
@Override
7979
public synchronized void forget( BoltServerAddress address )
8080
{
81-
// Don't remove it from the set of routers, since that might mean we lose our ability to re-discover,
82-
// just remove it from the set of readers and writers, so that we don't use it for actual work without
83-
// performing discovery first.
81+
routers.remove( address );
8482
readers.remove( address );
8583
writers.remove( address );
8684
}
@@ -115,11 +113,6 @@ public void removeWriter( BoltServerAddress toRemove )
115113
writers.remove( toRemove );
116114
}
117115

118-
@Override
119-
public void removeRouter( BoltServerAddress toRemove )
120-
{
121-
routers.remove( toRemove );
122-
}
123116

124117
@Override
125118
public String toString()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.cluster;
20+
21+
import java.net.InetAddress;
22+
import java.net.UnknownHostException;
23+
import java.util.HashSet;
24+
import java.util.Set;
25+
26+
import org.neo4j.driver.internal.net.BoltServerAddress;
27+
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
28+
29+
public interface DnsResolver
30+
{
31+
DnsResolver DEFAULT = new DnsResolver()
32+
{
33+
@Override
34+
public Set<BoltServerAddress> resolve( BoltServerAddress initialRouter )
35+
{
36+
try
37+
{
38+
InetAddress[] ipAddresses = InetAddress.getAllByName( initialRouter.host() );
39+
Set<BoltServerAddress> addresses = new HashSet<>( ipAddresses.length );
40+
41+
for ( int i = 0; i < ipAddresses.length; i ++ )
42+
{
43+
addresses.add( new BoltServerAddress( ipAddresses[i].getHostAddress(), initialRouter.port() ) );
44+
}
45+
46+
return addresses;
47+
48+
}
49+
catch ( UnknownHostException e )
50+
{
51+
throw new ServiceUnavailableException(
52+
"Failed to resolve URI `" + initialRouter + "` to IPs due to error: " + e.getMessage(), e );
53+
}
54+
}
55+
};
56+
57+
Set<BoltServerAddress> resolve( BoltServerAddress initialRouter );
58+
}

driver/src/main/java/org/neo4j/driver/internal/cluster/LoadBalancer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,6 @@ private static Rediscovery createRediscovery( BoltServerAddress initialRouter, R
157157
Clock clock, Logger log )
158158
{
159159
ClusterCompositionProvider clusterComposition = new GetServersProcedureClusterCompositionProvider( clock, log );
160-
return new Rediscovery( initialRouter, settings, clock, log, clusterComposition );
160+
return new Rediscovery( initialRouter, settings, clock, log, clusterComposition, DnsResolver.DEFAULT );
161161
}
162162
}

driver/src/main/java/org/neo4j/driver/internal/cluster/Rediscovery.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package org.neo4j.driver.internal.cluster;
2020

21+
import java.util.HashSet;
22+
import java.util.Set;
23+
2124
import org.neo4j.driver.internal.net.BoltServerAddress;
2225
import org.neo4j.driver.internal.spi.Connection;
2326
import org.neo4j.driver.internal.spi.ConnectionPool;
@@ -37,15 +40,17 @@ public class Rediscovery
3740
private final Clock clock;
3841
private final Logger logger;
3942
private final ClusterCompositionProvider provider;
43+
private final DnsResolver dnsResolver;
4044

4145
public Rediscovery( BoltServerAddress initialRouter, RoutingSettings settings, Clock clock, Logger logger,
42-
ClusterCompositionProvider provider )
46+
ClusterCompositionProvider provider, DnsResolver dnsResolver )
4347
{
4448
this.initialRouter = initialRouter;
4549
this.settings = settings;
4650
this.clock = clock;
4751
this.logger = logger;
4852
this.provider = provider;
53+
this.dnsResolver = dnsResolver;
4954
}
5055

5156
// Given the current routing table and connection pool, use the connection composition provider to fetch a new
@@ -76,8 +81,8 @@ public ClusterComposition lookupClusterComposition( ConnectionPool connections,
7681
private ClusterComposition lookupClusterCompositionOnKnownRouters( ConnectionPool connections,
7782
RoutingTable routingTable )
7883
{
79-
boolean triedInitialRouter = false;
8084
int size = routingTable.routerSize();
85+
Set<BoltServerAddress> triedServers = new HashSet<>( size );
8186
for ( int i = 0; i < size; i++ )
8287
{
8388
BoltServerAddress address = routingTable.nextRouter();
@@ -86,23 +91,29 @@ private ClusterComposition lookupClusterCompositionOnKnownRouters( ConnectionPoo
8691
break;
8792
}
8893

89-
if ( address.equals( initialRouter ) )
94+
ClusterComposition composition = lookupClusterCompositionOnRouter( address, connections, routingTable );
95+
if ( composition != null )
96+
{
97+
return composition;
98+
}
99+
else
90100
{
91-
triedInitialRouter = true;
101+
triedServers.add( address );
92102
}
103+
}
93104

105+
Set<BoltServerAddress> ips = dnsResolver.resolve( initialRouter );
106+
ips.removeAll( triedServers );
107+
for ( BoltServerAddress address : ips )
108+
{
94109
ClusterComposition composition = lookupClusterCompositionOnRouter( address, connections, routingTable );
95110
if ( composition != null )
96111
{
97112
return composition;
98113
}
99114
}
100115

101-
if ( triedInitialRouter )
102-
{
103-
return null;
104-
}
105-
return lookupClusterCompositionOnRouter( initialRouter, connections, routingTable );
116+
return null;
106117
}
107118

108119
private ClusterComposition lookupClusterCompositionOnRouter( BoltServerAddress routerAddress,
@@ -122,7 +133,7 @@ private ClusterComposition lookupClusterCompositionOnRouter( BoltServerAddress r
122133
{
123134
// connection turned out to be broken
124135
logger.error( format( "Failed to connect to routing server '%s'.", routerAddress ), t );
125-
routingTable.removeRouter( routerAddress );
136+
routingTable.forget( routerAddress );
126137
return null;
127138
}
128139

driver/src/main/java/org/neo4j/driver/internal/cluster/RoutingTable.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,4 @@ public interface RoutingTable
3939
int routerSize();
4040

4141
void removeWriter( BoltServerAddress toRemove );
42-
43-
void removeRouter( BoltServerAddress toRemove );
4442
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.cluster;
20+
21+
import org.junit.Test;
22+
23+
import java.util.Set;
24+
25+
import org.neo4j.driver.internal.net.BoltServerAddress;
26+
27+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
28+
import static org.junit.Assert.assertThat;
29+
import static org.neo4j.driver.internal.cluster.DnsResolver.DEFAULT;
30+
31+
public class DnsResolverTest
32+
{
33+
@Test
34+
public void shouldResolveDNSToIPs()
35+
{
36+
Set<BoltServerAddress> resolve = DEFAULT.resolve( new BoltServerAddress( "google.com", 80 ) );
37+
assertThat( resolve.size(), greaterThanOrEqualTo( 1 ) );
38+
}
39+
40+
@Test
41+
public void shouldResolveLocalhostDNSToIPs()
42+
{
43+
Set<BoltServerAddress> resolve = DEFAULT.resolve( new BoltServerAddress( "127.0.0.1", 80 ) );
44+
assertThat( resolve.size(), greaterThanOrEqualTo( 1 ) );
45+
}
46+
}

driver/src/test/java/org/neo4j/driver/internal/cluster/RediscoveryTest.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
import java.util.ArrayList;
2828
import java.util.Collection;
29+
import java.util.HashSet;
2930
import java.util.List;
31+
import java.util.Set;
3032

3133
import org.neo4j.driver.internal.net.BoltServerAddress;
3234
import org.neo4j.driver.internal.spi.Connection;
@@ -92,7 +94,7 @@ public void shouldTryConfiguredMaxRoutingFailures() throws Exception
9294
when( mockedProvider.getClusterComposition( any( Connection.class ) ) )
9395
.thenReturn( success( INVALID_CLUSTER_COMPOSITION ) );
9496

95-
Rediscovery rediscovery = new Rediscovery( A, settings, clock, DEV_NULL_LOGGER, mockedProvider );
97+
Rediscovery rediscovery = new Rediscovery( A, settings, clock, DEV_NULL_LOGGER, mockedProvider, directMapProvider );
9698

9799
// when
98100
try
@@ -372,7 +374,6 @@ public void shouldUseInitialRouterWhenNoneOfExistingRoutersRespond()
372374
.thenThrow( new ServiceUnavailableException( "Can't connect" ) );
373375

374376
RoutingTable routingTable = new TestRoutingTable( B, C );
375-
376377
ClusterComposition composition = rediscover( A, connections, routingTable, clusterComposition );
377378

378379
assertEquals( VALID_CLUSTER_COMPOSITION, composition );
@@ -454,10 +455,22 @@ private static ClusterComposition rediscover( BoltServerAddress initialRouter, C
454455
Clock mockedClock = mock( Clock.class );
455456
Logger mockedLogger = mock( Logger.class );
456457

457-
Rediscovery rediscovery = new Rediscovery( initialRouter, settings, mockedClock, mockedLogger, provider );
458+
Rediscovery rediscovery = new Rediscovery( initialRouter, settings, mockedClock, mockedLogger, provider,
459+
directMapProvider );
458460
return rediscovery.lookupClusterComposition( connections, routingTable );
459461
}
460462

463+
private static DnsResolver directMapProvider = new DnsResolver()
464+
{
465+
@Override
466+
public Set<BoltServerAddress> resolve( BoltServerAddress initialRouter )
467+
{
468+
Set<BoltServerAddress> directMap = new HashSet<>( 1 );
469+
directMap.add( initialRouter );
470+
return directMap;
471+
}
472+
};
473+
461474
private static class TestRoutingTable extends ClusterRoutingTable
462475
{
463476
final List<BoltServerAddress> removedRouters = new ArrayList<>();
@@ -468,9 +481,9 @@ private static class TestRoutingTable extends ClusterRoutingTable
468481
}
469482

470483
@Override
471-
public void removeRouter( BoltServerAddress router )
484+
public void forget( BoltServerAddress router )
472485
{
473-
super.removeRouter( router );
486+
super.forget( router );
474487
removedRouters.add( router );
475488
}
476489
}

driver/src/test/java/org/neo4j/driver/internal/cluster/RoutingPooledConnectionErrorHandlingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private void verifyServiceUnavailableHandling( Connection connection, RoutingTab
225225
assertThat( e.getCause(), instanceOf( ServiceUnavailableException.class ) );
226226

227227
BoltServerAddress address = connection.boltServerAddress();
228-
assertThat( routingTable, containsRouter( address ) );
228+
assertThat( routingTable, not( containsRouter( address ) ) );
229229
assertThat( routingTable, not( containsReader( address ) ) );
230230
assertThat( routingTable, not( containsWriter( address ) ) );
231231
assertFalse( connectionPool.hasAddress( address ) );

0 commit comments

Comments
 (0)