Skip to content

Commit fcc578d

Browse files
committed
Minor cleanups
1 parent 5ea5cd1 commit fcc578d

File tree

5 files changed

+45
-53
lines changed

5 files changed

+45
-53
lines changed

driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,19 @@ protected LoadBalancer createLoadBalancer( BoltServerAddress address, Connection
150150
RoutingSettings routingSettings )
151151
{
152152
return new LoadBalancer( address, routingSettings, connectionPool, createClock(), config.logging(),
153-
loadBalancingStrategy( config, connectionPool ) );
153+
createLoadBalancingStrategy( config, connectionPool ) );
154154
}
155155

156-
private LoadBalancingStrategy loadBalancingStrategy( Config config,
157-
ConnectionPool connectionPool )
156+
private LoadBalancingStrategy createLoadBalancingStrategy( Config config, ConnectionPool connectionPool )
158157
{
159-
if ( config.loadBalancingStrategy() == Config.LoadBalancingStrategy.ROUND_ROBIN )
158+
switch ( config.loadBalancingStrategy() )
160159
{
160+
case ROUND_ROBIN:
161161
return new RoundRobinLoadBalancingStrategy();
162-
}
163-
else
164-
{
162+
case LEAST_CONNECTED:
165163
return new LeastConnectedLoadBalancingStrategy( connectionPool );
164+
default:
165+
throw new IllegalArgumentException( "Unknown load balancing strategy: " + config.loadBalancingStrategy() );
166166
}
167167
}
168168

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,18 @@ public class LoadBalancer implements ConnectionProvider, RoutingErrorHandler, Au
5555
public LoadBalancer( BoltServerAddress initialRouter, RoutingSettings settings, ConnectionPool connections,
5656
Clock clock, Logging logging, LoadBalancingStrategy loadBalancingStrategy )
5757
{
58-
this( initialRouter, settings, connections, new ClusterRoutingTable( clock, initialRouter ), clock,
59-
logging.getLog( LOAD_BALANCER_LOG_NAME ), loadBalancingStrategy );
58+
this( connections, new ClusterRoutingTable( clock, initialRouter ),
59+
createRediscovery( initialRouter, settings, clock, logging ), loadBalancerLogger( logging ),
60+
loadBalancingStrategy );
6061
}
6162

62-
private LoadBalancer( BoltServerAddress initialRouter, RoutingSettings settings, ConnectionPool connections,
63-
RoutingTable routingTable, Clock clock, Logger log,
64-
LoadBalancingStrategy loadBalancingStrategy )
63+
// Used only in testing
64+
public LoadBalancer( ConnectionPool connections, RoutingTable routingTable, Rediscovery rediscovery, Logger log )
6565
{
66-
this( connections, routingTable, createRediscovery( initialRouter, settings, clock, log ), log,
67-
loadBalancingStrategy );
66+
this( connections, routingTable, rediscovery, log, new LeastConnectedLoadBalancingStrategy( connections ) );
6867
}
6968

70-
// Used only in testing
71-
public LoadBalancer( ConnectionPool connections, RoutingTable routingTable, Rediscovery rediscovery, Logger log,
69+
private LoadBalancer( ConnectionPool connections, RoutingTable routingTable, Rediscovery rediscovery, Logger log,
7270
LoadBalancingStrategy loadBalancingStrategy )
7371
{
7472
this.connections = connections;
@@ -186,13 +184,19 @@ private BoltServerAddress selectAddress( AccessMode mode, AddressSet servers )
186184
}
187185

188186
private static Rediscovery createRediscovery( BoltServerAddress initialRouter, RoutingSettings settings,
189-
Clock clock, Logger log )
187+
Clock clock, Logging logging )
190188
{
189+
Logger log = loadBalancerLogger( logging );
191190
ClusterCompositionProvider clusterComposition =
192191
new RoutingProcedureClusterCompositionProvider( clock, log, settings );
193192
return new Rediscovery( initialRouter, settings, clock, log, clusterComposition, new DnsResolver( log ) );
194193
}
195194

195+
private static Logger loadBalancerLogger( Logging logging )
196+
{
197+
return logging.getLog( LOAD_BALANCER_LOG_NAME );
198+
}
199+
196200
private static RuntimeException unknownMode( AccessMode mode )
197201
{
198202
return new IllegalArgumentException( "Mode '" + mode + "' is not supported" );

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ private Config( ConfigBuilder builder)
9494
this.loadBalancingStrategy = builder.loadBalancingStrategy;
9595
}
9696

97-
/**
98-
* Load Balancing Strategy
99-
*
100-
* @return load balancing strategy to use
101-
*/
102-
public LoadBalancingStrategy loadBalancingStrategy()
103-
{
104-
return loadBalancingStrategy;
105-
}
106-
10797
/**
10898
* Logging provider
10999
* @return the logging provider to use
@@ -186,6 +176,17 @@ public TrustStrategy trustStrategy()
186176
return trustStrategy;
187177
}
188178

179+
/**
180+
* Load balancing strategy
181+
*
182+
* @return the strategy to use
183+
*/
184+
@Experimental
185+
public LoadBalancingStrategy loadBalancingStrategy()
186+
{
187+
return loadBalancingStrategy;
188+
}
189+
189190
/**
190191
* Return a {@link ConfigBuilder} instance
191192
* @return a {@link ConfigBuilder} instance
@@ -245,11 +246,10 @@ public ConfigBuilder withLogging( Logging logging )
245246
}
246247

247248
/**
248-
* Provide an alternative load balancing implementation for the driver to use. By default we use
249+
* Provide an alternative load balancing strategy for the routing driver to use. By default we use
249250
* {@link LoadBalancingStrategy#LEAST_CONNECTED}.
250251
* <p>
251-
* We are experimnenting with different strategies before sticking to one. This could be removed in the next
252-
* minor version
252+
* <b>Note:</b> We are experimenting with different strategies. This could be removed in the next minor version.
253253
*
254254
* @param loadBalancingStrategy the strategy to use
255255
* @return this builder

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.HashSet;
3131
import java.util.List;
3232

33-
import org.neo4j.driver.internal.cluster.loadbalancing.LeastConnectedLoadBalancingStrategy;
3433
import org.neo4j.driver.internal.cluster.loadbalancing.LoadBalancer;
3534
import org.neo4j.driver.internal.net.BoltServerAddress;
3635
import org.neo4j.driver.internal.net.pooling.PoolSettings;
@@ -326,8 +325,7 @@ private static LoadBalancer newLoadBalancer( ClusterComposition clusterCompositi
326325
{
327326
Rediscovery rediscovery = mock( Rediscovery.class );
328327
when( rediscovery.lookupClusterComposition( routingTable, connectionPool ) ).thenReturn( clusterComposition );
329-
return new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER,
330-
new LeastConnectedLoadBalancingStrategy( connectionPool ) );
328+
return new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER );
331329
}
332330

333331
private interface ConnectionMethod

driver/src/test/java/org/neo4j/driver/internal/cluster/loadbalancing/LoadBalancerTest.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ public void ensureRoutingShouldUpdateRoutingTableAndPurgeConnectionPoolWhenStale
9393
when( routingTable.update( any( ClusterComposition.class ) ) ).thenReturn( set );
9494

9595
// when
96-
LoadBalancer balancer = new LoadBalancer( conns, routingTable, rediscovery, DEV_NULL_LOGGER,
97-
new LeastConnectedLoadBalancingStrategy( conns ) );
96+
LoadBalancer balancer = new LoadBalancer( conns, routingTable, rediscovery, DEV_NULL_LOGGER );
9897

9998
// then
10099
assertNotNull( balancer );
@@ -110,8 +109,7 @@ public void shouldRefreshRoutingTableOnInitialization() throws Exception
110109
// given & when
111110
final AtomicInteger refreshRoutingTableCounter = new AtomicInteger( 0 );
112111
LoadBalancer balancer = new LoadBalancer( mock( ConnectionPool.class ), mock( RoutingTable.class ),
113-
mock( Rediscovery.class ), DEV_NULL_LOGGER,
114-
new LeastConnectedLoadBalancingStrategy( mock( ConnectionPool.class ) ) )
112+
mock( Rediscovery.class ), DEV_NULL_LOGGER )
115113
{
116114
@Override
117115
synchronized void refreshRoutingTable()
@@ -165,8 +163,7 @@ public void shouldForgetAddressAndItsConnectionsOnServiceUnavailableWhileClosing
165163
RoutingTable routingTable = mock( RoutingTable.class );
166164
ConnectionPool connectionPool = mock( ConnectionPool.class );
167165
Rediscovery rediscovery = mock( Rediscovery.class );
168-
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER,
169-
new LeastConnectedLoadBalancingStrategy( connectionPool ) );
166+
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER );
170167
BoltServerAddress address = new BoltServerAddress( "host", 42 );
171168

172169
PooledConnection connection = newConnectionWithFailingSync( address );
@@ -200,8 +197,7 @@ public void shouldForgetAddressAndItsConnectionsOnServiceUnavailableWhileClosing
200197
PooledConnection connectionWithFailingSync = newConnectionWithFailingSync( address );
201198
when( connectionPool.acquire( any( BoltServerAddress.class ) ) ).thenReturn( connectionWithFailingSync );
202199
Rediscovery rediscovery = mock( Rediscovery.class );
203-
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER,
204-
new LeastConnectedLoadBalancingStrategy( connectionPool ) );
200+
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER );
205201

206202
Session session = newSession( loadBalancer );
207203
// begin transaction to make session obtain a connection
@@ -247,8 +243,7 @@ public void shouldThrowWhenRediscoveryReturnsNoSuitableServers()
247243
when( routingTable.readers() ).thenReturn( new AddressSet() );
248244
when( routingTable.writers() ).thenReturn( new AddressSet() );
249245

250-
LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER,
251-
new LeastConnectedLoadBalancingStrategy( connections ) );
246+
LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER );
252247

253248
try
254249
{
@@ -288,8 +283,7 @@ public void shouldSelectLeastConnectedAddress()
288283

289284
Rediscovery rediscovery = mock( Rediscovery.class );
290285

291-
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER,
292-
new LeastConnectedLoadBalancingStrategy( connectionPool ) );
286+
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER );
293287

294288
Set<BoltServerAddress> seenAddresses = new HashSet<>();
295289
for ( int i = 0; i < 10; i++ )
@@ -315,8 +309,7 @@ public void shouldRoundRobinWhenNoActiveConnections()
315309

316310
Rediscovery rediscovery = mock( Rediscovery.class );
317311

318-
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER,
319-
new LeastConnectedLoadBalancingStrategy( connectionPool ) );
312+
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER );
320313

321314
Set<BoltServerAddress> seenAddresses = new HashSet<>();
322315
for ( int i = 0; i < 10; i++ )
@@ -337,8 +330,7 @@ private void testRediscoveryWhenStale( AccessMode mode )
337330
RoutingTable routingTable = newStaleRoutingTableMock( mode );
338331
Rediscovery rediscovery = newRediscoveryMock();
339332

340-
LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER,
341-
new LeastConnectedLoadBalancingStrategy( connections ) );
333+
LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER );
342334
verify( rediscovery ).lookupClusterComposition( routingTable, connections );
343335

344336
assertNotNull( loadBalancer.acquireConnection( mode ) );
@@ -354,8 +346,7 @@ private void testNoRediscoveryWhenNotStale( AccessMode staleMode, AccessMode not
354346
RoutingTable routingTable = newStaleRoutingTableMock( staleMode );
355347
Rediscovery rediscovery = newRediscoveryMock();
356348

357-
LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER,
358-
new LeastConnectedLoadBalancingStrategy( connections ) );
349+
LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER );
359350
verify( rediscovery ).lookupClusterComposition( routingTable, connections );
360351

361352
assertNotNull( loadBalancer.acquireConnection( notStaleMode ) );
@@ -388,8 +379,7 @@ private LoadBalancer setupLoadBalancer( PooledConnection writerConn, PooledConne
388379
when( routingTable.readers() ).thenReturn( readerAddrs );
389380
when( routingTable.writers() ).thenReturn( writerAddrs );
390381

391-
return new LoadBalancer( connPool, routingTable, rediscovery, DEV_NULL_LOGGER,
392-
new LeastConnectedLoadBalancingStrategy( connPool ) );
382+
return new LoadBalancer( connPool, routingTable, rediscovery, DEV_NULL_LOGGER );
393383
}
394384

395385
private static Session newSession( LoadBalancer loadBalancer )

0 commit comments

Comments
 (0)