Skip to content

Commit

Permalink
Minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
lutovich committed Jul 5, 2017
1 parent 5ea5cd1 commit fcc578d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,19 @@ protected LoadBalancer createLoadBalancer( BoltServerAddress address, Connection
RoutingSettings routingSettings )
{
return new LoadBalancer( address, routingSettings, connectionPool, createClock(), config.logging(),
loadBalancingStrategy( config, connectionPool ) );
createLoadBalancingStrategy( config, connectionPool ) );
}

private LoadBalancingStrategy loadBalancingStrategy( Config config,
ConnectionPool connectionPool )
private LoadBalancingStrategy createLoadBalancingStrategy( Config config, ConnectionPool connectionPool )
{
if ( config.loadBalancingStrategy() == Config.LoadBalancingStrategy.ROUND_ROBIN )
switch ( config.loadBalancingStrategy() )
{
case ROUND_ROBIN:
return new RoundRobinLoadBalancingStrategy();
}
else
{
case LEAST_CONNECTED:
return new LeastConnectedLoadBalancingStrategy( connectionPool );
default:
throw new IllegalArgumentException( "Unknown load balancing strategy: " + config.loadBalancingStrategy() );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,18 @@ public class LoadBalancer implements ConnectionProvider, RoutingErrorHandler, Au
public LoadBalancer( BoltServerAddress initialRouter, RoutingSettings settings, ConnectionPool connections,
Clock clock, Logging logging, LoadBalancingStrategy loadBalancingStrategy )
{
this( initialRouter, settings, connections, new ClusterRoutingTable( clock, initialRouter ), clock,
logging.getLog( LOAD_BALANCER_LOG_NAME ), loadBalancingStrategy );
this( connections, new ClusterRoutingTable( clock, initialRouter ),
createRediscovery( initialRouter, settings, clock, logging ), loadBalancerLogger( logging ),
loadBalancingStrategy );
}

private LoadBalancer( BoltServerAddress initialRouter, RoutingSettings settings, ConnectionPool connections,
RoutingTable routingTable, Clock clock, Logger log,
LoadBalancingStrategy loadBalancingStrategy )
// Used only in testing
public LoadBalancer( ConnectionPool connections, RoutingTable routingTable, Rediscovery rediscovery, Logger log )
{
this( connections, routingTable, createRediscovery( initialRouter, settings, clock, log ), log,
loadBalancingStrategy );
this( connections, routingTable, rediscovery, log, new LeastConnectedLoadBalancingStrategy( connections ) );
}

// Used only in testing
public LoadBalancer( ConnectionPool connections, RoutingTable routingTable, Rediscovery rediscovery, Logger log,
private LoadBalancer( ConnectionPool connections, RoutingTable routingTable, Rediscovery rediscovery, Logger log,
LoadBalancingStrategy loadBalancingStrategy )
{
this.connections = connections;
Expand Down Expand Up @@ -186,13 +184,19 @@ private BoltServerAddress selectAddress( AccessMode mode, AddressSet servers )
}

private static Rediscovery createRediscovery( BoltServerAddress initialRouter, RoutingSettings settings,
Clock clock, Logger log )
Clock clock, Logging logging )
{
Logger log = loadBalancerLogger( logging );
ClusterCompositionProvider clusterComposition =
new RoutingProcedureClusterCompositionProvider( clock, log, settings );
return new Rediscovery( initialRouter, settings, clock, log, clusterComposition, new DnsResolver( log ) );
}

private static Logger loadBalancerLogger( Logging logging )
{
return logging.getLog( LOAD_BALANCER_LOG_NAME );
}

private static RuntimeException unknownMode( AccessMode mode )
{
return new IllegalArgumentException( "Mode '" + mode + "' is not supported" );
Expand Down
26 changes: 13 additions & 13 deletions driver/src/main/java/org/neo4j/driver/v1/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@ private Config( ConfigBuilder builder)
this.loadBalancingStrategy = builder.loadBalancingStrategy;
}

/**
* Load Balancing Strategy
*
* @return load balancing strategy to use
*/
public LoadBalancingStrategy loadBalancingStrategy()
{
return loadBalancingStrategy;
}

/**
* Logging provider
* @return the logging provider to use
Expand Down Expand Up @@ -186,6 +176,17 @@ public TrustStrategy trustStrategy()
return trustStrategy;
}

/**
* Load balancing strategy
*
* @return the strategy to use
*/
@Experimental
public LoadBalancingStrategy loadBalancingStrategy()
{
return loadBalancingStrategy;
}

/**
* Return a {@link ConfigBuilder} instance
* @return a {@link ConfigBuilder} instance
Expand Down Expand Up @@ -245,11 +246,10 @@ public ConfigBuilder withLogging( Logging logging )
}

/**
* Provide an alternative load balancing implementation for the driver to use. By default we use
* Provide an alternative load balancing strategy for the routing driver to use. By default we use
* {@link LoadBalancingStrategy#LEAST_CONNECTED}.
* <p>
* We are experimnenting with different strategies before sticking to one. This could be removed in the next
* minor version
* <b>Note:</b> We are experimenting with different strategies. This could be removed in the next minor version.
*
* @param loadBalancingStrategy the strategy to use
* @return this builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.HashSet;
import java.util.List;

import org.neo4j.driver.internal.cluster.loadbalancing.LeastConnectedLoadBalancingStrategy;
import org.neo4j.driver.internal.cluster.loadbalancing.LoadBalancer;
import org.neo4j.driver.internal.net.BoltServerAddress;
import org.neo4j.driver.internal.net.pooling.PoolSettings;
Expand Down Expand Up @@ -326,8 +325,7 @@ private static LoadBalancer newLoadBalancer( ClusterComposition clusterCompositi
{
Rediscovery rediscovery = mock( Rediscovery.class );
when( rediscovery.lookupClusterComposition( routingTable, connectionPool ) ).thenReturn( clusterComposition );
return new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( connectionPool ) );
return new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER );
}

private interface ConnectionMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public void ensureRoutingShouldUpdateRoutingTableAndPurgeConnectionPoolWhenStale
when( routingTable.update( any( ClusterComposition.class ) ) ).thenReturn( set );

// when
LoadBalancer balancer = new LoadBalancer( conns, routingTable, rediscovery, DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( conns ) );
LoadBalancer balancer = new LoadBalancer( conns, routingTable, rediscovery, DEV_NULL_LOGGER );

// then
assertNotNull( balancer );
Expand All @@ -110,8 +109,7 @@ public void shouldRefreshRoutingTableOnInitialization() throws Exception
// given & when
final AtomicInteger refreshRoutingTableCounter = new AtomicInteger( 0 );
LoadBalancer balancer = new LoadBalancer( mock( ConnectionPool.class ), mock( RoutingTable.class ),
mock( Rediscovery.class ), DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( mock( ConnectionPool.class ) ) )
mock( Rediscovery.class ), DEV_NULL_LOGGER )
{
@Override
synchronized void refreshRoutingTable()
Expand Down Expand Up @@ -165,8 +163,7 @@ public void shouldForgetAddressAndItsConnectionsOnServiceUnavailableWhileClosing
RoutingTable routingTable = mock( RoutingTable.class );
ConnectionPool connectionPool = mock( ConnectionPool.class );
Rediscovery rediscovery = mock( Rediscovery.class );
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( connectionPool ) );
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER );
BoltServerAddress address = new BoltServerAddress( "host", 42 );

PooledConnection connection = newConnectionWithFailingSync( address );
Expand Down Expand Up @@ -200,8 +197,7 @@ public void shouldForgetAddressAndItsConnectionsOnServiceUnavailableWhileClosing
PooledConnection connectionWithFailingSync = newConnectionWithFailingSync( address );
when( connectionPool.acquire( any( BoltServerAddress.class ) ) ).thenReturn( connectionWithFailingSync );
Rediscovery rediscovery = mock( Rediscovery.class );
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( connectionPool ) );
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER );

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

LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( connections ) );
LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER );

try
{
Expand Down Expand Up @@ -288,8 +283,7 @@ public void shouldSelectLeastConnectedAddress()

Rediscovery rediscovery = mock( Rediscovery.class );

LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( connectionPool ) );
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER );

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

Rediscovery rediscovery = mock( Rediscovery.class );

LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( connectionPool ) );
LoadBalancer loadBalancer = new LoadBalancer( connectionPool, routingTable, rediscovery, DEV_NULL_LOGGER );

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

LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( connections ) );
LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER );
verify( rediscovery ).lookupClusterComposition( routingTable, connections );

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

LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( connections ) );
LoadBalancer loadBalancer = new LoadBalancer( connections, routingTable, rediscovery, DEV_NULL_LOGGER );
verify( rediscovery ).lookupClusterComposition( routingTable, connections );

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

return new LoadBalancer( connPool, routingTable, rediscovery, DEV_NULL_LOGGER,
new LeastConnectedLoadBalancingStrategy( connPool ) );
return new LoadBalancer( connPool, routingTable, rediscovery, DEV_NULL_LOGGER );
}

private static Session newSession( LoadBalancer loadBalancer )
Expand Down

0 comments on commit fcc578d

Please sign in to comment.