Skip to content

Commit a234ca0

Browse files
committed
Better handling of cluster startup failures
And couple small cleanups.
1 parent 2c8821c commit a234ca0

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

driver/src/test/java/org/neo4j/driver/v1/util/cc/Cluster.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Path getPath()
7272
return path;
7373
}
7474

75-
public void cleanUp()
75+
public void deleteData()
7676
{
7777
leaderTx( new Consumer<Session>()
7878
{
@@ -142,8 +142,8 @@ private Set<ClusterMember> membersWithRole( ClusterMemberRole role )
142142
try ( Driver driver = createDriver( members, password );
143143
Session session = driver.session( AccessMode.READ ) )
144144
{
145-
StatementResult result = session.run( "call dbms.cluster.overview()" );
146-
for ( Record record : result.list() )
145+
List<Record> records = findClusterOverview( session );
146+
for ( Record record : records )
147147
{
148148
if ( role == extractRole( record ) )
149149
{
@@ -185,8 +185,8 @@ private static Set<ClusterMember> waitForMembers( Set<ClusterMember> members, St
185185

186186
try ( Session session = driver.session( AccessMode.READ ) )
187187
{
188-
StatementResult result = session.run( "call dbms.cluster.overview()" );
189-
for ( Record record : result.list() )
188+
List<Record> records = findClusterOverview( session );
189+
for ( Record record : records )
190190
{
191191
URI boltUri = extractBoltUri( record );
192192

@@ -230,16 +230,16 @@ private static Driver createDriver( Set<ClusterMember> members, String password
230230
throw new IllegalStateException( "No core members found among: " + members );
231231
}
232232

233-
private static Driver createDriver( URI boltUri, String password )
233+
private static List<Record> findClusterOverview( Session session )
234234
{
235-
return GraphDatabase.driver( boltUri, AuthTokens.basic( ADMIN_USER, password ), driverConfig() );
235+
StatementResult result = session.run( "call dbms.cluster.overview" );
236+
return result.list();
236237
}
237238

238239
private static boolean isCoreMember( Session session )
239240
{
240241
Record record = single( session.run( "call dbms.cluster.role" ).list() );
241-
String roleName = record.get( "role" ).asString();
242-
ClusterMemberRole role = ClusterMemberRole.valueOf( roleName.toUpperCase() );
242+
ClusterMemberRole role = extractRole( record );
243243
return role != ClusterMemberRole.READ_REPLICA;
244244
}
245245

@@ -277,6 +277,11 @@ private static ClusterMember findByBoltUri( URI boltUri, Set<ClusterMember> memb
277277
return null;
278278
}
279279

280+
private static Driver createDriver( URI boltUri, String password )
281+
{
282+
return GraphDatabase.driver( boltUri, AuthTokens.basic( ADMIN_USER, password ), driverConfig() );
283+
}
284+
280285
private static Config driverConfig()
281286
{
282287
// try to build config for a very lightweight driver

driver/src/test/java/org/neo4j/driver/v1/util/cc/ClusterControl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private ClusterControl()
3535
{
3636
}
3737

38-
static boolean boltKitAvailable()
38+
static boolean boltKitClusterAvailable()
3939
{
4040
try
4141
{

driver/src/test/java/org/neo4j/driver/v1/util/cc/ClusterMember.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
public class ClusterMember
2727
{
28+
private static final String SIMPLE_SCHEME = "bolt://";
29+
private static final String ROUTING_SCHEME = "bolt+routing://";
30+
2831
private final URI boltUri;
2932
private final Path path;
3033

@@ -41,7 +44,7 @@ public URI getBoltUri()
4144

4245
public URI getRoutingUri()
4346
{
44-
return URI.create( boltUri.toString().replace( "bolt://", "bolt+routing://" ) );
47+
return URI.create( boltUri.toString().replace( SIMPLE_SCHEME, ROUTING_SCHEME ) );
4548
}
4649

4750
public Path getPath()

driver/src/test/java/org/neo4j/driver/v1/util/cc/ClusterRule.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.neo4j.driver.v1.AuthToken;
3232
import org.neo4j.driver.v1.AuthTokens;
3333

34-
import static java.lang.System.err;
3534
import static org.junit.Assume.assumeTrue;
3635

3736
public class ClusterRule extends ExternalResource
@@ -40,6 +39,12 @@ public class ClusterRule extends ExternalResource
4039
private static final String PASSWORD = "test";
4140
private static final int INITIAL_PORT = 20_000;
4241

42+
// todo: neo4j version should come from environment variables
43+
private static final String NEO4J_VERSION = "3.1.0-RC1";
44+
// todo: should be possible to configure (dynamically add/remove) cores and read replicas
45+
private static final int CORE_COUNT = 3;
46+
private static final int READ_REPLICA_COUNT = 2;
47+
4348
public Cluster getCluster()
4449
{
4550
return SharedCluster.get();
@@ -53,17 +58,34 @@ public AuthToken getDefaultAuthToken()
5358
@Override
5459
protected void before() throws Throwable
5560
{
56-
assumeTrue( "BoltKit unavailable", ClusterControl.boltKitAvailable() );
61+
assumeTrue( "BoltKit cluster support unavailable", ClusterControl.boltKitClusterAvailable() );
5762

5863
if ( !SharedCluster.exists() )
5964
{
65+
deleteClusterDir();
66+
67+
SharedCluster.install( NEO4J_VERSION, CORE_COUNT, READ_REPLICA_COUNT, PASSWORD, INITIAL_PORT, CLUSTER_DIR );
68+
6069
try
6170
{
62-
delete( CLUSTER_DIR );
63-
// todo: get version from env
64-
SharedCluster.install( "3.1.0-M13-beta3", 3, 2, PASSWORD, INITIAL_PORT, CLUSTER_DIR );
6571
SharedCluster.start();
6672
}
73+
catch ( Throwable startError )
74+
{
75+
try
76+
{
77+
SharedCluster.kill();
78+
}
79+
catch ( Throwable killError )
80+
{
81+
startError.addSuppressed( killError );
82+
}
83+
finally
84+
{
85+
SharedCluster.remove();
86+
}
87+
throw startError;
88+
}
6789
finally
6890
{
6991
addShutdownHookToStopCluster();
@@ -74,7 +96,7 @@ protected void before() throws Throwable
7496
@Override
7597
protected void after()
7698
{
77-
getCluster().cleanUp();
99+
getCluster().deleteData();
78100
}
79101

80102
private static void addShutdownHookToStopCluster()
@@ -87,17 +109,21 @@ public void run()
87109
try
88110
{
89111
SharedCluster.kill();
90-
delete( CLUSTER_DIR );
91112
}
92113
catch ( Throwable t )
93114
{
94-
err.println( "Cluster stopping shutdown hook failed" );
115+
System.err.println( "Cluster stopping shutdown hook failed" );
95116
t.printStackTrace();
96117
}
97118
}
98119
} );
99120
}
100121

122+
private static void deleteClusterDir()
123+
{
124+
delete( CLUSTER_DIR );
125+
}
126+
101127
private static void delete( final Path path )
102128
{
103129
try

driver/src/test/java/org/neo4j/driver/v1/util/cc/SharedCluster.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ static Cluster get()
4040
return clusterInstance;
4141
}
4242

43+
static void remove()
44+
{
45+
assertClusterExists();
46+
clusterInstance = null;
47+
}
48+
4349
static boolean exists()
4450
{
4551
return clusterInstance != null;

0 commit comments

Comments
 (0)