Skip to content

Commit

Permalink
fixing port conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
lassewesth committed Oct 10, 2017
1 parent 48c9026 commit 0dcf28b
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 39 deletions.
Expand Up @@ -162,7 +162,7 @@ public static IntFunction<String> constant( final String value )
private final boolean consistencyCheck;
private final int firstInstanceId;
private LifeSupport life;
private int baseBoltPort;
private boolean boltEnabled;

private ClusterManager( Builder builder )
{
Expand All @@ -176,7 +176,7 @@ private ClusterManager( Builder builder )
this.availabilityChecks = builder.availabilityChecks;
this.consistencyCheck = builder.consistencyCheck;
this.firstInstanceId = builder.firstInstanceId;
this.baseBoltPort = builder.baseBoltPort;
this.boltEnabled = builder.boltEnabled;
}

private Map<String,IntFunction<String>> withDefaults( Map<String,IntFunction<String>> commonConfig )
Expand Down Expand Up @@ -622,10 +622,9 @@ public interface ClusterBuilder<SELF>
SELF withInstanceConfig( Map<String,IntFunction<String>> commonConfig );

/**
* Enables bolt across the cluster, which is off by default. The port argument specifies the base port to
* use for calculating per instance ports.
* Enables bolt across the cluster, which is off by default.
*/
SELF withBolt( int port );
SELF withBoltEnabled();

/**
* Like {@link #withInstanceConfig(Map)}, but for individual settings, conveniently using
Expand Down Expand Up @@ -683,7 +682,7 @@ public static class Builder implements ClusterBuilder<Builder>
private List<Predicate<ManagedCluster>> availabilityChecks = Collections.emptyList();
private boolean consistencyCheck;
private int firstInstanceId = FIRST_SERVER_ID;
private int baseBoltPort = -1; // -1 stands for no bolt enabled, anything > 0 means bolt enabled
private boolean boltEnabled;

public Builder( File root )
{
Expand Down Expand Up @@ -742,9 +741,9 @@ public Builder withInstanceConfig( Map<String,IntFunction<String>> commonConfig
return this;
}

public Builder withBolt( int basePort )
public Builder withBoltEnabled()
{
this.baseBoltPort = basePort;
this.boltEnabled = true;
return this;
}

Expand Down Expand Up @@ -1144,10 +1143,10 @@ private HighlyAvailableGraphDatabase startMemberNow( InstanceId serverId )
builder.setConfig( conf.getKey(), conf.getValue().apply( serverId.toIntegerIndex() ) );
}

if ( baseBoltPort > 0 )
if ( boltEnabled )
{
String listenAddress = "127.0.0.1";
int boltPort = baseBoltPort + serverId.toIntegerIndex();
int boltPort = PortAuthority.allocatePort();
AdvertisedSocketAddress advertisedSocketAddress = socketAddressForServer( listenAddress, boltPort );
String advertisedAddress = advertisedSocketAddress.getHostname();
String boltAdvertisedAddress = advertisedAddress + ":" + boltPort;
Expand Down
Expand Up @@ -129,9 +129,9 @@ public ClusterRule withInstanceConfig( Map<String,IntFunction<String>> commonCon
}

@Override
public ClusterRule withBolt( int port )
public ClusterRule withBoltEnabled()
{
return set( clusterManagerBuilder.withBolt( port ) );
return set( clusterManagerBuilder.withBoltEnabled() );
}

@Override
Expand Down
Expand Up @@ -50,6 +50,7 @@
import org.neo4j.kernel.ha.HaSettings;
import org.neo4j.kernel.ha.HighlyAvailableGraphDatabase;
import org.neo4j.kernel.impl.api.KernelTransactions;
import org.neo4j.kernel.impl.enterprise.configuration.OnlineBackupSettings;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory;
import org.neo4j.kernel.impl.ha.ClusterManager;
import org.neo4j.kernel.impl.locking.LockClientStoppedException;
Expand Down Expand Up @@ -92,7 +93,6 @@ public class TransactionTerminationIT
private final CleanupRule cleanupRule = new CleanupRule();
private final ClusterRule clusterRule = new ClusterRule( getClass() )
.withCluster( clusterOfSize( 3 ) )
.withSharedSetting( HaSettings.ha_server, ":6001-6005" )
.withSharedSetting( HaSettings.tx_push_factor, "2" )
.withSharedSetting( HaSettings.lock_read_timeout, "1m" );

Expand All @@ -113,6 +113,7 @@ public void terminateSingleInstanceRestTransactionThatWaitsForLock() throws Exce
ServerControls server = cleanupRule.add( TestServerBuilders.newInProcessBuilder()
.withConfig( GraphDatabaseSettings.auth_enabled, Settings.FALSE )
.withConfig( GraphDatabaseFacadeFactory.Configuration.lock_manager, lockManagerName )
.withConfig( OnlineBackupSettings.online_backup_enabled, Settings.FALSE )
.newServer() );

GraphDatabaseService db = server.graph();
Expand Down
47 changes: 30 additions & 17 deletions integrationtests/src/test/java/org/neo4j/bolt/BoltFailuresIT.java
Expand Up @@ -50,11 +50,13 @@
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.io.IOUtils;
import org.neo4j.kernel.impl.enterprise.configuration.OnlineBackupSettings;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.logging.AssertableLogProvider;
import org.neo4j.logging.AssertableLogProvider.LogMatcher;
import org.neo4j.logging.LogProvider;
import org.neo4j.ports.allocation.PortAuthority;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.test.TestEnterpriseGraphDatabaseFactory;
import org.neo4j.test.rule.TestDirectory;
Expand Down Expand Up @@ -110,12 +112,14 @@ public void throwsWhenWorkerCreationFails()

BoltKernelExtension extension = new BoltKernelExtensionWithWorkerFactory( workerFactory );

db = startDbWithBolt( new GraphDatabaseFactoryWithCustomBoltKernelExtension( extension ) );
int port = PortAuthority.allocatePort();

db = startDbWithBolt( new GraphDatabaseFactoryWithCustomBoltKernelExtension( extension ), port );

try
{
// attempt to create a driver when server is unavailable
driver = createDriver();
driver = createDriver( port );
fail( "Exception expected" );
}
catch ( Exception e )
Expand All @@ -131,11 +135,13 @@ public void throwsWhenMonitoredWorkerCreationFails()
sessionMonitor.throwInSessionStarted();
Monitors monitors = newMonitorsSpy( sessionMonitor );

db = startDbWithBolt( new GraphDatabaseFactory().setMonitors( monitors ) );
int port = PortAuthority.allocatePort();

db = startDbWithBolt( new GraphDatabaseFactory().setMonitors( monitors ), port );
try
{
// attempt to create a driver when server is unavailable
driver = createDriver();
driver = createDriver( port );
fail( "Exception expected" );
}
catch ( Exception e )
Expand Down Expand Up @@ -184,7 +190,8 @@ public void throwsWhenRunMessageProcessingFailsToComplete()
public void boltServerLogsRealErrorWhenDriverIsClosedWithRunningTransactions() throws Exception
{
AssertableLogProvider internalLogProvider = new AssertableLogProvider();
db = startTestDb( internalLogProvider );
int port = PortAuthority.allocatePort();
db = startTestDb( internalLogProvider, port );

// create a dummy node
db.execute( "CREATE (:Node)" ).close();
Expand All @@ -194,7 +201,7 @@ public void boltServerLogsRealErrorWhenDriverIsClosedWithRunningTransactions() t
Node node = single( db.findNodes( label( "Node" ) ) );
tx.acquireWriteLock( node );

Driver driver = createDriver();
Driver driver = createDriver( port );

// try to execute write query for the same node through the driver
Future<?> writeThroughDriverFuture = updateAllNodesAsync( driver );
Expand Down Expand Up @@ -223,11 +230,13 @@ private void throwsWhenInitMessageFails( Consumer<ThrowingSessionMonitor> monito
monitorSetup.accept( sessionMonitor );
Monitors monitors = newMonitorsSpy( sessionMonitor );

db = startTestDb( monitors );
int port = PortAuthority.allocatePort();

db = startTestDb( monitors, port );

try
{
driver = GraphDatabase.driver( "bolt://localhost", Config.build().withoutEncryption().toConfig() );
driver = GraphDatabase.driver( "bolt://localhost:" + port, Config.build().withoutEncryption().toConfig() );
if ( shouldBeAbleToBeginTransaction )
{
try ( Session session = driver.session();
Expand All @@ -252,8 +261,10 @@ private void throwsWhenRunMessageFails( Consumer<ThrowingSessionMonitor> monitor
ThrowingSessionMonitor sessionMonitor = new ThrowingSessionMonitor();
Monitors monitors = newMonitorsSpy( sessionMonitor );

db = startTestDb( monitors );
driver = createDriver();
int port = PortAuthority.allocatePort();

db = startTestDb( monitors, port );
driver = createDriver( port );

// open a session and start a transaction, this will force driver to obtain
// a network connection and bind it to the transaction
Expand All @@ -276,22 +287,24 @@ private void throwsWhenRunMessageFails( Consumer<ThrowingSessionMonitor> monitor
}
}

private GraphDatabaseService startTestDb( Monitors monitors )
private GraphDatabaseService startTestDb( Monitors monitors, int port )
{
return startDbWithBolt( newDbFactory().setMonitors( monitors ) );
return startDbWithBolt( newDbFactory().setMonitors( monitors ), port );
}

private GraphDatabaseService startTestDb( LogProvider internalLogProvider )
private GraphDatabaseService startTestDb( LogProvider internalLogProvider, int port )
{
return startDbWithBolt( newDbFactory().setInternalLogProvider( internalLogProvider ) );
return startDbWithBolt( newDbFactory().setInternalLogProvider( internalLogProvider ), port );
}

private GraphDatabaseService startDbWithBolt( GraphDatabaseFactory dbFactory )
private GraphDatabaseService startDbWithBolt( GraphDatabaseFactory dbFactory, int port )
{
return dbFactory.newEmbeddedDatabaseBuilder( dir.graphDbDir() )
.setConfig( boltConnector( "0" ).type, BOLT.name() )
.setConfig( boltConnector( "0" ).enabled, TRUE )
.setConfig( boltConnector( "0" ).listen_address, "localhost:" + port )
.setConfig( GraphDatabaseSettings.auth_enabled, FALSE )
.setConfig( OnlineBackupSettings.online_backup_enabled, FALSE )
.newGraphDatabase();
}

Expand Down Expand Up @@ -349,9 +362,9 @@ private static TestEnterpriseGraphDatabaseFactory newDbFactory()
return new TestEnterpriseGraphDatabaseFactory();
}

private static Driver createDriver()
private static Driver createDriver( int port )
{
return GraphDatabase.driver( "bolt://localhost", Config.build().withoutEncryption().toConfig() );
return GraphDatabase.driver( "bolt://localhost:" + port, Config.build().withoutEncryption().toConfig() );
}

private static Monitors newMonitorsSpy( ThrowingSessionMonitor sessionMonitor )
Expand Down
Expand Up @@ -38,6 +38,8 @@
import org.neo4j.kernel.configuration.BoltConnector;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.configuration.ConnectorPortRegister;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.impl.enterprise.configuration.OnlineBackupSettings;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.DatabaseRule;
Expand Down Expand Up @@ -85,7 +87,7 @@ protected void configure( GraphDatabaseBuilder builder )
builder.setConfig( new BoltConnector( CONNECTOR_KEY ).listen_address, "localhost:0" );
builder.setConfig( new BoltConnector( CONNECTOR_KEY ).encryption_level, DISABLED.toString() );
}
}.startLazily();
}.withSetting( OnlineBackupSettings.online_backup_enabled, Settings.FALSE ).startLazily();

private Driver driver;

Expand Down
Expand Up @@ -58,7 +58,9 @@
import org.neo4j.harness.junit.EnterpriseNeo4jRule;
import org.neo4j.harness.junit.Neo4jRule;
import org.neo4j.io.IOUtils;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.impl.api.KernelTransactions;
import org.neo4j.kernel.impl.enterprise.configuration.OnlineBackupSettings;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.test.ThreadTestUtils;
import org.neo4j.test.rule.VerboseTimeout;
Expand Down Expand Up @@ -101,7 +103,7 @@ public class SessionResetIT
private static final String[] STRESS_IT_QUERIES = {SHORT_QUERY_1, SHORT_QUERY_2, LONG_QUERY};

private final VerboseTimeout timeout = VerboseTimeout.builder().withTimeout( 3, MINUTES ).build();
private final Neo4jRule db = new EnterpriseNeo4jRule();
private final Neo4jRule db = new EnterpriseNeo4jRule().withConfig( OnlineBackupSettings.online_backup_enabled, Settings.FALSE );

@Rule
public final RuleChain ruleChain = RuleChain.outerRule( timeout ).around( db );
Expand Down
2 changes: 1 addition & 1 deletion integrationtests/src/test/java/org/neo4j/ha/BoltHAIT.java
Expand Up @@ -46,7 +46,7 @@
public class BoltHAIT
{
@Rule
public final ClusterRule clusterRule = new ClusterRule( getClass() ).withBolt( 8000 ).withCluster( clusterOfSize( 3 ) );
public final ClusterRule clusterRule = new ClusterRule( getClass() ).withBoltEnabled().withCluster( clusterOfSize( 3 ) );

@Test
public void shouldContinueServingBoltRequestsBetweenInternalRestarts() throws Throwable
Expand Down
Expand Up @@ -36,9 +36,11 @@
import org.neo4j.graphdb.factory.EnterpriseGraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.enterprise.api.security.EnterpriseSecurityContext;
import org.neo4j.kernel.ha.HighlyAvailableGraphDatabase;
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
import org.neo4j.kernel.impl.enterprise.configuration.OnlineBackupSettings;
import org.neo4j.kernel.impl.ha.ClusterManager;
import org.neo4j.kernel.impl.storemigration.LogFiles;
import org.neo4j.test.ha.ClusterRule;
Expand Down Expand Up @@ -203,7 +205,10 @@ private static File dbWithOutLogs()
GraphDatabaseService db = null;
try
{
db = new EnterpriseGraphDatabaseFactory().newEmbeddedDatabase( seedDir );
db = new EnterpriseGraphDatabaseFactory()
.newEmbeddedDatabaseBuilder( seedDir )
.setConfig( OnlineBackupSettings.online_backup_enabled, Settings.FALSE )
.newGraphDatabase();
createSomeData( db );
}
finally
Expand Down
Expand Up @@ -24,7 +24,9 @@

import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.harness.junit.Neo4jRule;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.configuration.ssl.LegacySslPolicyConfig;
import org.neo4j.kernel.impl.enterprise.configuration.OnlineBackupSettings;
import org.neo4j.server.configuration.ServerSettings;

import static org.junit.Assert.assertEquals;
Expand All @@ -43,7 +45,8 @@ public class BatchEndpointIT
.withConfig( GraphDatabaseSettings.logs_directory,
getRelativePath( getSharedTestTemporaryFolder(), GraphDatabaseSettings.logs_directory ) )
.withConfig( ServerSettings.http_logging_enabled, "true" )
.withConfig( GraphDatabaseSettings.auth_enabled, "false" );
.withConfig( GraphDatabaseSettings.auth_enabled, "false" )
.withConfig( OnlineBackupSettings.online_backup_enabled, Settings.FALSE );

@Test
public void requestsShouldNotFailWhenHttpLoggingIsOn()
Expand Down
Expand Up @@ -33,7 +33,10 @@
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.harness.junit.Neo4jRule;
import org.neo4j.kernel.configuration.BoltConnector;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.configuration.ssl.LegacySslPolicyConfig;
import org.neo4j.kernel.impl.enterprise.configuration.OnlineBackupSettings;
import org.neo4j.ports.allocation.PortAuthority;
import org.neo4j.server.configuration.ServerSettings;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -58,16 +61,17 @@ public BoltQueryLoggingIT() throws IOException
.withConfig( GraphDatabaseSettings.log_queries, "true")
.withConfig( new BoltConnector( "bolt" ).type, "BOLT" )
.withConfig( new BoltConnector( "bolt" ).enabled, "true" )
.withConfig( new BoltConnector( "bolt" ).address, "localhost:8776" )
.withConfig( new BoltConnector( "bolt" ).encryption_level, "DISABLED" );
.withConfig( new BoltConnector( "bolt" ).address, "localhost:" + PortAuthority.allocatePort() )
.withConfig( new BoltConnector( "bolt" ).encryption_level, "DISABLED" )
.withConfig( OnlineBackupSettings.online_backup_enabled, Settings.FALSE );
}

@Test
public void shouldLogQueriesViaBolt() throws IOException
{
// *** GIVEN ***

Socket socket = new Socket( "localhost", 8776 );
Socket socket = new Socket( "localhost", neo4j.boltURI().getPort() );
DataInputStream dataIn = new DataInputStream( socket.getInputStream() );
DataOutputStream dataOut = new DataOutputStream( socket.getOutputStream() );

Expand Down Expand Up @@ -119,7 +123,7 @@ public void shouldLogQueriesViaBolt() throws IOException
assertTrue( line.contains( "ms: bolt-session\tbolt\tneo4j\tMyClient/1.0" ) );
assertTrue( line.contains( "client/127.0.0.1:" ) );
assertTrue( line.contains( "client/127.0.0.1:" ) );
assertTrue( line.contains( "server/127.0.0.1:8776" ) );
assertTrue( line.contains( "server/127.0.0.1:" + neo4j.boltURI().getPort() ) );
assertTrue( line.contains( " - RETURN 1 AS num - {}" ) );
}

Expand Down

0 comments on commit 0dcf28b

Please sign in to comment.