Skip to content

Commit a8882ab

Browse files
technigelutovich
authored andcommitted
Added method and test
1 parent ebb653d commit a8882ab

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

driver/src/main/java/org/neo4j/driver/v1/GraphDatabase.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
package org.neo4j.driver.v1;
2020

2121
import java.net.URI;
22+
import java.util.List;
2223

2324
import org.neo4j.driver.internal.DriverFactory;
2425
import org.neo4j.driver.internal.cluster.RoutingSettings;
2526
import org.neo4j.driver.internal.retry.RetrySettings;
27+
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
2628

2729
/**
2830
* Creates {@link Driver drivers}, optionally letting you {@link #driver(URI, Config)} to configure them.
@@ -131,4 +133,44 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
131133

132134
return new DriverFactory().newInstance( uri, authToken, routingSettings, retrySettings, config );
133135
}
136+
137+
/**
138+
* Try to create a bolt+routing driver from the first available address.
139+
* This is wrapper for the {@link #driver} method that finds the first
140+
* server to respond positively.
141+
*
142+
* @param addresses a list of server addresses for Neo4j instances
143+
* @param authToken authentication to use, see {@link AuthTokens}
144+
* @param config user defined configuration
145+
* @return a new driver instance
146+
*/
147+
public static Driver routingDriverFromFirstAvailableAddress( List<String> addresses, AuthToken authToken, Config config )
148+
{
149+
for( String address: addresses )
150+
{
151+
try
152+
{
153+
return driver( "bolt+routing://" + address, authToken, config );
154+
}
155+
catch( ServiceUnavailableException e )
156+
{
157+
// try the next one
158+
}
159+
}
160+
throw new ServiceUnavailableException( "Failed to discover an available server" );
161+
}
162+
163+
/**
164+
* Try to create a bolt+routing driver from the first available address.
165+
* This is wrapper for the {@link #driver} method that finds the first
166+
* server to respond positively.
167+
*
168+
* @param addresses a list of server addresses for Neo4j instances
169+
* @param authToken authentication to use, see {@link AuthTokens}
170+
* @return a new driver instance
171+
*/
172+
public static Driver routingDriverFromFirstAvailableAddress( List<String> addresses, AuthToken authToken )
173+
{
174+
return routingDriverFromFirstAvailableAddress( addresses, authToken, Config.defaultConfig() );
175+
}
134176
}

driver/src/test/java/org/neo4j/driver/v1/integration/CausalClusteringIT.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Test;
2323

2424
import java.net.URI;
25+
import java.util.ArrayList;
2526
import java.util.List;
2627
import java.util.concurrent.Callable;
2728
import java.util.concurrent.CountDownLatch;
@@ -85,6 +86,16 @@ public void shouldExecuteReadAndWritesWhenDriverSuppliedWithAddressOfLeader() th
8586
assertEquals( 1, count );
8687
}
8788

89+
@Test
90+
public void shouldExecuteReadAndWritesWhenRouterIsDiscovered() throws Exception
91+
{
92+
Cluster cluster = clusterRule.getCluster();
93+
94+
int count = executeWriteAndReadThroughBoltOnFirstAvailableAddress( cluster.anyReadReplica(), cluster.leader() );
95+
96+
assertEquals( 1, count );
97+
}
98+
8899
@Test
89100
public void shouldExecuteReadAndWritesWhenDriverSuppliedWithAddressOfFollower() throws Exception
90101
{
@@ -446,6 +457,19 @@ private int executeWriteAndReadThroughBolt( ClusterMember member ) throws Timeou
446457
}
447458
}
448459

460+
private int executeWriteAndReadThroughBoltOnFirstAvailableAddress( ClusterMember... members ) throws TimeoutException, InterruptedException
461+
{
462+
List<String> addresses = new ArrayList<>( members.length );
463+
for ( ClusterMember member : members )
464+
{
465+
addresses.add( member.getRoutingUri().getAuthority() );
466+
}
467+
try ( Driver driver = discoverDriver( addresses ) )
468+
{
469+
return inExpirableSession( driver, createWritableSession( null ), executeWriteAndRead() );
470+
}
471+
}
472+
449473
private Function<Driver,Session> createSession()
450474
{
451475
return new Function<Driver,Session>()
@@ -592,6 +616,24 @@ public Logger getLog( String name )
592616
return GraphDatabase.driver( boltUri, clusterRule.getDefaultAuthToken(), config );
593617
}
594618

619+
private Driver discoverDriver( List<String> addresses )
620+
{
621+
Logging devNullLogging = new Logging()
622+
{
623+
@Override
624+
public Logger getLog( String name )
625+
{
626+
return DevNullLogger.DEV_NULL_LOGGER;
627+
}
628+
};
629+
630+
Config config = Config.build()
631+
.withLogging( devNullLogging )
632+
.toConfig();
633+
634+
return GraphDatabase.routingDriverFromFirstAvailableAddress( addresses, clusterRule.getDefaultAuthToken(), config );
635+
}
636+
595637
private static void createNodesInDifferentThreads( int count, final Driver driver ) throws Exception
596638
{
597639
final CountDownLatch beforeRunLatch = new CountDownLatch( count );

0 commit comments

Comments
 (0)