Skip to content

Introduce neo4j scheme as a direct alias for bolt+routing #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class DriverFactory
{
public static final String BOLT_URI_SCHEME = "bolt";
public static final String BOLT_ROUTING_URI_SCHEME = "bolt+routing";
public static final String NEO4J_URI_SCHEME = "neo4j";

public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings routingSettings,
RetrySettings retrySettings, Config config )
Expand Down Expand Up @@ -138,6 +139,7 @@ private InternalDriver createDriver( URI uri, SecurityPlan securityPlan, BoltSer
assertNoRoutingContext( uri, routingSettings );
return createDirectDriver( securityPlan, address, connectionPool, retryLogic, metrics, config );
case BOLT_ROUTING_URI_SCHEME:
case NEO4J_URI_SCHEME:
return createRoutingDriver( securityPlan, address, connectionPool, eventExecutorGroup, routingSettings, retryLogic, metrics, config );
default:
throw new ClientException( format( "Unsupported URI scheme: %s", scheme ) );
Expand Down Expand Up @@ -168,7 +170,7 @@ protected InternalDriver createDirectDriver( SecurityPlan securityPlan, BoltServ
}

/**
* Creates new a new driver for "bolt+routing" scheme.
* Creates new a new driver for "bolt+routing" or "neo4j" scheme.
* <p>
* <b>This method is protected only for testing</b>
*/
Expand Down
2 changes: 1 addition & 1 deletion driver/src/main/java/org/neo4j/driver/v1/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public ConfigBuilder withMaxConnectionLifetime( long value, TimeUnit unit )
* Configure maximum amount of connections in the connection pool towards a single database. This setting
* limits total amount of connections in the pool when used in direct driver, created for URI with 'bolt'
* scheme. It will limit amount of connections per cluster member when used with routing driver, created for
* URI with 'bolt+routing' scheme.
* URI with 'bolt+routing' or 'neo4j' scheme.
* <p>
* Acquisition will be attempted for at most configured timeout
* {@link #withConnectionAcquisitionTimeout(long, TimeUnit)} when limit is reached.
Expand Down
2 changes: 1 addition & 1 deletion driver/src/main/java/org/neo4j/driver/v1/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* <td>Direct driver: connects directly to the host and port specified in the URI.</td>
* </tr>
* <tr>
* <td><code>bolt+routing</code></td>
* <td><code>bolt+routing</code> or <code>neo4j</code></td>
* <td>Routing driver: can automatically discover members of a Causal Cluster and route {@link Session sessions} based on {@link AccessMode}.</td>
* </tr>
* </tbody>
Expand Down
14 changes: 10 additions & 4 deletions driver/src/main/java/org/neo4j/driver/v1/GraphDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,27 @@
package org.neo4j.driver.v1;

import java.net.URI;
import java.util.Arrays;
import java.util.List;

import org.neo4j.driver.internal.DriverFactory;
import org.neo4j.driver.internal.cluster.RoutingSettings;
import org.neo4j.driver.internal.retry.RetrySettings;
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;

import static org.neo4j.driver.internal.DriverFactory.BOLT_ROUTING_URI_SCHEME;
import static org.neo4j.driver.internal.DriverFactory.NEO4J_URI_SCHEME;

/**
* Creates {@link Driver drivers}, optionally letting you {@link #driver(URI, Config)} to configure them.
*
* @see Driver
* @since 1.0
*/
public class GraphDatabase
{
private static final String LOGGER_NAME = GraphDatabase.class.getSimpleName();
private static final List<String> VALID_ROUTING_URIS = Arrays.asList( BOLT_ROUTING_URI_SCHEME, NEO4J_URI_SCHEME );

/**
* Return a driver for a Neo4j instance with the default configuration settings
Expand Down Expand Up @@ -137,12 +142,12 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
}

/**
* Try to create a bolt+routing driver from the <b>first</b> available address.
* Try to create a routing driver from the <b>first</b> available address.
* This is wrapper for the {@link #driver} method that finds the <b>first</b>
* server to respond positively.
*
* @param routingUris an {@link Iterable} of server {@link URI}s for Neo4j instances. All given URIs should
* have 'bolt+routing' scheme.
* have 'bolt+routing' or 'neo4j' scheme.
* @param authToken authentication to use, see {@link AuthTokens}
* @param config user defined configuration
* @return a new driver instance
Expand Down Expand Up @@ -171,10 +176,11 @@ private static void assertRoutingUris( Iterable<URI> uris )
{
for ( URI uri : uris )
{
if ( !BOLT_ROUTING_URI_SCHEME.equals( uri.getScheme() ) )
if ( !VALID_ROUTING_URIS.contains( uri.getScheme() ) )
{
throw new IllegalArgumentException(
"Illegal URI scheme, expected '" + BOLT_ROUTING_URI_SCHEME + "' in '" + uri + "'" );
String.format(
"Illegal URI scheme, expected URI scheme '%s' to be among '%s'", uri.getScheme(), VALID_ROUTING_URIS.toString() ) );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class DriverFactoryTest
{
private static Stream<String> testUris()
{
return Stream.of( "bolt://localhost:7687", "bolt+routing://localhost:7687" );
return Stream.of( "bolt://localhost:7687", "bolt+routing://localhost:7687", "neo4j://localhost:7687" );
}

@ParameterizedTest
Expand Down
34 changes: 24 additions & 10 deletions driver/src/test/java/org/neo4j/driver/v1/GraphDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.neo4j.driver.v1;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -69,15 +71,16 @@ void boltSchemeShouldInstantiateDirectDriver() throws Exception
assertThat( server.exitStatus(), equalTo( 0 ) );
}

@Test
void boltPlusDiscoverySchemeShouldInstantiateClusterDriver() throws Exception
@ParameterizedTest
@ValueSource( strings = {"bolt+routing://127.0.0.1:9001", "neo4j://127.0.0.1:9001"} )
void boltPlusDiscoverySchemeShouldInstantiateClusterDriver( String uri ) throws Exception
{
// Given
StubServer server = StubServer.start( "discover_servers.script", 9001 );
URI uri = URI.create( "bolt+routing://127.0.0.1:9001" );
URI driverUri = URI.create( uri );

// When
Driver driver = GraphDatabase.driver( uri, INSECURE_CONFIG );
Driver driver = GraphDatabase.driver( driverUri, INSECURE_CONFIG );

// Then
assertThat( driver, is( clusterDriver() ) );
Expand Down Expand Up @@ -107,8 +110,9 @@ void throwsWhenBoltSchemeUsedWithRoutingParams()
assertThrows( IllegalArgumentException.class, () -> GraphDatabase.driver( "bolt://localhost:7687/?policy=my_policy" ) );
}

@Test
void shouldLogWhenUnableToCreateRoutingDriver() throws Exception
@ParameterizedTest
@ValueSource( strings = {"bolt+routing", "neo4j"} )
void shouldLogWhenUnableToCreateRoutingDriver( String scheme ) throws Exception
{
StubServer server1 = StubServer.start( "non_discovery_server.script", 9001 );
StubServer server2 = StubServer.start( "non_discovery_server.script", 9002 );
Expand All @@ -123,21 +127,31 @@ void shouldLogWhenUnableToCreateRoutingDriver() throws Exception
.build();

List<URI> routingUris = asList(
URI.create( "bolt+routing://localhost:9001" ),
URI.create( "bolt+routing://localhost:9002" ) );
URI.create( scheme + "://localhost:9001" ),
URI.create( scheme + "://localhost:9002" ) );

assertThrows( ServiceUnavailableException.class, () -> GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config ) );

verify( logger ).warn( eq( "Unable to create routing driver for URI: bolt+routing://localhost:9001" ),
verify( logger ).warn( eq( String.format( "Unable to create routing driver for URI: %s://localhost:9001", scheme ) ),
any( Throwable.class ) );

verify( logger ).warn( eq( "Unable to create routing driver for URI: bolt+routing://localhost:9002" ),
verify( logger ).warn( eq( String.format( "Unable to create routing driver for URI: %s://localhost:9002", scheme ) ),
any( Throwable.class ) );

assertEquals( 0, server1.exitStatus() );
assertEquals( 0, server2.exitStatus() );
}

@Test
void shouldFailWhenUnsupportedSchemeIsProvidedToRoutingDriver()
{
IllegalArgumentException exc =
assertThrows( IllegalArgumentException.class, () ->
GraphDatabase.routingDriver( asList( URI.create( "xscheme://localhost:7687" ) ), AuthTokens.none(), Config.defaultConfig() ) );

assertThat( exc.getMessage(), equalTo( "Illegal URI scheme, expected URI scheme 'xscheme' to be among '[bolt+routing, neo4j]'" ) );
}

@Test
void shouldRespondToInterruptsWhenConnectingToUnresponsiveServer() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.neo4j.driver.v1.util.TestUtil;

import static org.neo4j.driver.internal.DriverFactory.BOLT_ROUTING_URI_SCHEME;
import static org.neo4j.driver.internal.DriverFactory.NEO4J_URI_SCHEME;
import static org.neo4j.driver.v1.Config.defaultConfig;

public class LocalOrRemoteClusterExtension implements BeforeAllCallback, AfterEachCallback, AfterAllCallback
Expand Down Expand Up @@ -118,9 +119,9 @@ private static void assertValidSystemPropertiesDefined()
"Both cluster uri and 'neo4j' user password system properties should be set. " +
"Uri: '" + uri + "', Password: '" + password + "'" );
}
if ( uri != null && !BOLT_ROUTING_URI_SCHEME.equals( uri.getScheme() ) )
if ( uri != null && !BOLT_ROUTING_URI_SCHEME.equals( uri.getScheme() ) && !NEO4J_URI_SCHEME.equals( uri.getScheme() ) )
{
throw new IllegalStateException( "Cluster uri should have bolt+routing scheme: '" + uri + "'" );
throw new IllegalStateException( "Cluster uri should have bolt+routing or neo4j scheme: '" + uri + "'" );
}
}

Expand Down