Skip to content

Commit

Permalink
Merge pull request #613 from zhenlineo/2.0-driver-config
Browse files Browse the repository at this point in the history
Clean up driver configuration.
  • Loading branch information
zhenlineo authored Jul 22, 2019
2 parents e2e4943 + e07c4da commit 97b1532
Show file tree
Hide file tree
Showing 54 changed files with 160 additions and 629 deletions.
278 changes: 41 additions & 237 deletions driver/src/main/java/org/neo4j/driver/Config.java

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion driver/src/main/java/org/neo4j/driver/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.internal.SessionConfig;
import org.neo4j.driver.reactive.RxSession;
import org.neo4j.driver.types.TypeSystem;
import org.neo4j.driver.util.Experimental;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.internal;
package org.neo4j.driver;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.neo4j.driver.AccessMode;
import org.neo4j.driver.Session;
import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.reactive.RxSession;

Expand Down Expand Up @@ -64,7 +62,7 @@ public static Builder builder()
*
* @return a session config for a general purpose session.
*/
public static SessionConfig empty()
public static SessionConfig defaultConfig()
{
return EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.neo4j.driver.internal.cluster.loadbalancing.LeastConnectedLoadBalancingStrategy;
import org.neo4j.driver.internal.cluster.loadbalancing.LoadBalancer;
import org.neo4j.driver.internal.cluster.loadbalancing.LoadBalancingStrategy;
import org.neo4j.driver.internal.cluster.loadbalancing.RoundRobinLoadBalancingStrategy;
import org.neo4j.driver.internal.logging.NettyLogging;
import org.neo4j.driver.internal.metrics.InternalMetricsProvider;
import org.neo4j.driver.internal.metrics.MetricsProvider;
Expand Down Expand Up @@ -218,26 +217,12 @@ protected InternalDriver createDriver( SecurityPlan securityPlan, SessionFactory
protected LoadBalancer createLoadBalancer( BoltServerAddress address, ConnectionPool connectionPool,
EventExecutorGroup eventExecutorGroup, Config config, RoutingSettings routingSettings )
{
LoadBalancingStrategy loadBalancingStrategy = createLoadBalancingStrategy( config, connectionPool );
LoadBalancingStrategy loadBalancingStrategy = new LeastConnectedLoadBalancingStrategy( connectionPool, config.logging() );
ServerAddressResolver resolver = createResolver( config );
return new LoadBalancer( address, routingSettings, connectionPool, eventExecutorGroup, createClock(),
config.logging(), loadBalancingStrategy, resolver );
}

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

private static ServerAddressResolver createResolver( Config config )
{
ServerAddressResolver configuredResolver = config.resolver();
Expand Down Expand Up @@ -323,17 +308,6 @@ private static SecurityPlan createSecurityPlanImpl( BoltServerAddress address, C
boolean hostnameVerificationEnabled = trustStrategy.isHostnameVerificationEnabled();
switch ( trustStrategy.strategy() )
{
case TRUST_ON_FIRST_USE:
logger.warn(
"Option `TRUST_ON_FIRST_USE` has been deprecated and will be removed in a future " +
"version of the driver. Please switch to use `TRUST_ALL_CERTIFICATES` instead." );
return SecurityPlan.forTrustOnFirstUse( trustStrategy.certFile(), hostnameVerificationEnabled, address, logger );
case TRUST_SIGNED_CERTIFICATES:
logger.warn(
"Option `TRUST_SIGNED_CERTIFICATE` has been deprecated and will be removed in a future " +
"version of the driver. Please switch to use `TRUST_CUSTOM_CA_SIGNED_CERTIFICATES` instead." );
// intentional fallthrough

case TRUST_CUSTOM_CA_SIGNED_CERTIFICATES:
return SecurityPlan.forCustomCASignedCertificates( trustStrategy.certFile(), hostnameVerificationEnabled );
case TRUST_SYSTEM_CA_SIGNED_CERTIFICATES:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.neo4j.driver.Logging;
import org.neo4j.driver.Metrics;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.internal.async.InternalAsyncSession;
import org.neo4j.driver.internal.async.NetworkSession;
Expand Down Expand Up @@ -59,7 +60,7 @@ public class InternalDriver implements Driver
@Override
public Session session()
{
return new InternalSession( newSession( SessionConfig.empty() ) );
return new InternalSession( newSession( SessionConfig.defaultConfig() ) );
}

@Override
Expand All @@ -71,7 +72,7 @@ public Session session( SessionConfig sessionConfig )
@Override
public RxSession rxSession()
{
return new InternalRxSession( newSession( SessionConfig.empty() ) );
return new InternalRxSession( newSession( SessionConfig.defaultConfig() ) );
}

@Override
Expand All @@ -83,7 +84,7 @@ public RxSession rxSession( SessionConfig sessionConfig )
@Override
public AsyncSession asyncSession()
{
return new InternalAsyncSession( newSession( SessionConfig.empty() ) );
return new InternalAsyncSession( newSession( SessionConfig.defaultConfig() ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.concurrent.CompletionStage;

import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.internal.async.NetworkSession;

public interface SessionFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.Config;
import org.neo4j.driver.Logging;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.internal.async.NetworkSession;
import org.neo4j.driver.internal.async.LeakLoggingNetworkSession;
import org.neo4j.driver.internal.retry.RetryLogic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public class RoutingProcedureClusterCompositionProvider implements ClusterCompos
private final Clock clock;
private final RoutingProcedureRunner routingProcedureRunner;

public RoutingProcedureClusterCompositionProvider( Clock clock, RoutingSettings settings )
public RoutingProcedureClusterCompositionProvider( Clock clock, RoutingContext routingContext )
{
this( clock, new RoutingProcedureRunner( settings.routingContext() ) );
this( clock, new RoutingProcedureRunner( routingContext ) );
}

RoutingProcedureClusterCompositionProvider( Clock clock, RoutingProcedureRunner routingProcedureRunner )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,30 @@

public class RoutingSettings
{
public static final RoutingSettings DEFAULT = new RoutingSettings( 1, SECONDS.toMillis( 5 ) );
public static final long STALE_ROUTING_TABLE_PURGE_DELAY_MS = SECONDS.toMillis( 30 );
public static final RoutingSettings DEFAULT = new RoutingSettings( 1, SECONDS.toMillis( 5 ), STALE_ROUTING_TABLE_PURGE_DELAY_MS );

private final int maxRoutingFailures;
private final long retryTimeoutDelay;
private final RoutingContext routingContext;
private final long routingTablePurgeDelayMs;

public RoutingSettings( int maxRoutingFailures, long retryTimeoutDelay )
public RoutingSettings( int maxRoutingFailures, long retryTimeoutDelay, long routingTablePurgeDelayMs )
{
this( maxRoutingFailures, retryTimeoutDelay, RoutingContext.EMPTY );
this( maxRoutingFailures, retryTimeoutDelay, routingTablePurgeDelayMs, RoutingContext.EMPTY );
}

public RoutingSettings( int maxRoutingFailures, long retryTimeoutDelay, RoutingContext routingContext )
public RoutingSettings( int maxRoutingFailures, long retryTimeoutDelay, long routingTablePurgeDelayMs, RoutingContext routingContext )
{
this.maxRoutingFailures = maxRoutingFailures;
this.retryTimeoutDelay = retryTimeoutDelay;
this.routingContext = routingContext;
this.routingTablePurgeDelayMs = routingTablePurgeDelayMs;
}

public RoutingSettings withRoutingContext( RoutingContext newRoutingContext )
{
return new RoutingSettings( maxRoutingFailures, retryTimeoutDelay, newRoutingContext );
return new RoutingSettings( maxRoutingFailures, retryTimeoutDelay, routingTablePurgeDelayMs, newRoutingContext );
}

public int maxRoutingFailures()
Expand All @@ -59,4 +62,9 @@ public RoutingContext routingContext()
{
return routingContext;
}

public long routingTablePurgeDelayMs()
{
return routingTablePurgeDelayMs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.neo4j.driver.internal.cluster;

import java.time.Duration;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand All @@ -41,19 +40,18 @@ public class RoutingTableHandler implements RoutingErrorHandler
private final ConnectionPool connectionPool;
private final Rediscovery rediscovery;
private final Logger log;
private final long routingTablePurgeDelayMs;

// This defines how long we shall wait before trimming a routing table from routing tables after it is stale.
// TODO make this a configuration option
public static final Duration STALE_ROUTING_TABLE_PURGE_TIMEOUT = Duration.ofSeconds( 30 );

public RoutingTableHandler( RoutingTable routingTable, Rediscovery rediscovery, ConnectionPool connectionPool, RoutingTableRegistry routingTableRegistry, Logger log )
public RoutingTableHandler( RoutingTable routingTable, Rediscovery rediscovery, ConnectionPool connectionPool, RoutingTableRegistry routingTableRegistry,
Logger log, long routingTablePurgeDelayMs )
{
this.routingTable = routingTable;
this.databaseName = routingTable.database();
this.rediscovery = rediscovery;
this.connectionPool = connectionPool;
this.routingTableRegistry = routingTableRegistry;
this.log = log;
this.routingTablePurgeDelayMs = routingTablePurgeDelayMs;
}

@Override
Expand Down Expand Up @@ -145,7 +143,7 @@ public Set<BoltServerAddress> servers()
// This method cannot be synchronized as it will be visited by all routing table handler's threads concurrently
public boolean isRoutingTableAged()
{
return refreshRoutingTableFuture == null && routingTable.hasBeenStaleFor( STALE_ROUTING_TABLE_PURGE_TIMEOUT.toMillis() );
return refreshRoutingTableFuture == null && routingTable.hasBeenStaleFor( routingTablePurgeDelayMs );
}

// for testing only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class RoutingTableRegistryImpl implements RoutingTableRegistry
private final RoutingTableHandlerFactory factory;
private final Logger logger;

public RoutingTableRegistryImpl( ConnectionPool connectionPool, Rediscovery rediscovery, Clock clock, Logger logger )
public RoutingTableRegistryImpl( ConnectionPool connectionPool, Rediscovery rediscovery, Clock clock, Logger logger, long routingTablePurgeDelayMs )
{
this( new ConcurrentHashMap<>(), new RoutingTableHandlerFactory( connectionPool, rediscovery, clock, logger ), logger );
this( new ConcurrentHashMap<>(), new RoutingTableHandlerFactory( connectionPool, rediscovery, clock, logger, routingTablePurgeDelayMs ), logger );
}

RoutingTableRegistryImpl( ConcurrentMap<String,RoutingTableHandler> routingTableHandlers, RoutingTableHandlerFactory factory, Logger logger )
Expand Down Expand Up @@ -109,19 +109,21 @@ static class RoutingTableHandlerFactory
private final Rediscovery rediscovery;
private final Logger log;
private final Clock clock;
private final long routingTablePurgeDelayMs;

RoutingTableHandlerFactory( ConnectionPool connectionPool, Rediscovery rediscovery, Clock clock, Logger log )
RoutingTableHandlerFactory( ConnectionPool connectionPool, Rediscovery rediscovery, Clock clock, Logger log, long routingTablePurgeDelayMs )
{
this.connectionPool = connectionPool;
this.rediscovery = rediscovery;
this.clock = clock;
this.log = log;
this.routingTablePurgeDelayMs = routingTablePurgeDelayMs;
}

RoutingTableHandler newInstance( String databaseName, RoutingTableRegistry allTables )
{
ClusterRoutingTable routingTable = new ClusterRoutingTable( databaseName, clock );
return new RoutingTableHandler( routingTable, rediscovery, connectionPool, allTables, log );
return new RoutingTableHandler( routingTable, rediscovery, connectionPool, allTables, log, routingTablePurgeDelayMs );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ private static RoutingTableRegistry createRoutingTables( ConnectionPool connecti
{
Logger log = loadBalancerLogger( logging );
Rediscovery rediscovery = createRediscovery( eventExecutorGroup, initialRouter, resolver, settings, clock, log );
return new RoutingTableRegistryImpl( connectionPool, rediscovery, clock, log );
return new RoutingTableRegistryImpl( connectionPool, rediscovery, clock, log, settings.routingTablePurgeDelayMs() );
}

private static Rediscovery createRediscovery( EventExecutorGroup eventExecutorGroup, BoltServerAddress initialRouter, ServerAddressResolver resolver,
RoutingSettings settings, Clock clock, Logger log )
{
ClusterCompositionProvider clusterCompositionProvider = new RoutingProcedureClusterCompositionProvider( clock, settings );
ClusterCompositionProvider clusterCompositionProvider = new RoutingProcedureClusterCompositionProvider( clock, settings.routingContext() );
return new RediscoveryImpl( initialRouter, settings, clusterCompositionProvider, eventExecutorGroup, resolver, log );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

import org.neo4j.driver.internal.BoltServerAddress;
import org.neo4j.driver.Logger;

import static org.neo4j.driver.internal.util.CertificateTool.loadX509Cert;

/**
Expand Down Expand Up @@ -73,16 +69,6 @@ public static SecurityPlan forSystemCASignedCertificates( boolean requiresHostna
return new SecurityPlan( true, SSLContext.getDefault(), true, requiresHostnameVerification );
}

@Deprecated
public static SecurityPlan forTrustOnFirstUse( File knownHosts, boolean requiresHostnameVerification, BoltServerAddress address, Logger logger )
throws IOException, KeyManagementException, NoSuchAlgorithmException
{
SSLContext sslContext = SSLContext.getInstance( "TLS" );
sslContext.init( new KeyManager[0], new TrustManager[]{new TrustOnFirstUseTrustManager( address, knownHosts, logger )}, null );

return new SecurityPlan( true, sslContext, false, requiresHostnameVerification );
}

public static SecurityPlan insecure()
{
return new SecurityPlan( false, null, true, false );
Expand Down
17 changes: 0 additions & 17 deletions driver/src/test/java/org/neo4j/driver/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.File;
import java.util.concurrent.TimeUnit;

import org.neo4j.driver.Config;
import org.neo4j.driver.net.ServerAddressResolver;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -48,22 +47,6 @@ void shouldDefaultToKnownCerts()
assertEquals( authConfig.strategy(), Config.TrustStrategy.Strategy.TRUST_ALL_CERTIFICATES );
}

@SuppressWarnings( "deprecation" )
@Test
void shouldChangeToNewKnownCerts()
{
// Given
File knownCerts = new File( "new_known_hosts" );
Config config = Config.builder().withTrustStrategy( Config.TrustStrategy.trustOnFirstUse( knownCerts ) ).build();

// When
Config.TrustStrategy authConfig = config.trustStrategy();

// Then
assertEquals( authConfig.strategy(), Config.TrustStrategy.Strategy.TRUST_ON_FIRST_USE );
assertEquals( knownCerts.getAbsolutePath(), authConfig.certFile().getAbsolutePath() );
}

@Test
void shouldChangeToTrustedCert()
{
Expand Down
16 changes: 0 additions & 16 deletions driver/src/test/java/org/neo4j/driver/GraphDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URI;
Expand All @@ -44,7 +43,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.neo4j.driver.Config.TrustStrategy.trustOnFirstUse;
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
import static org.neo4j.driver.internal.util.Matchers.clusterDriver;
import static org.neo4j.driver.internal.util.Matchers.directDriver;
Expand Down Expand Up @@ -90,20 +88,6 @@ void boltPlusDiscoverySchemeShouldInstantiateClusterDriver() throws Exception
assertThat( server.exitStatus(), equalTo( 0 ) );
}

@Test
@SuppressWarnings( "deprecation" )
void boltPlusDiscoverySchemeShouldNotSupportTrustOnFirstUse()
{
URI uri = URI.create( "neo4j://127.0.0.1:9001" );

Config config = Config.builder()
.withEncryption()
.withTrustStrategy( trustOnFirstUse( new File( "./known_hosts" ) ) )
.build();

assertThrows( IllegalArgumentException.class, () -> GraphDatabase.driver( uri, config ) );
}

@Test
void throwsWhenBoltSchemeUsedWithRoutingParams()
{
Expand Down
Loading

0 comments on commit 97b1532

Please sign in to comment.