Skip to content

Migrate IT test to Testkit #1094

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 1 commit into from
Dec 2, 2021
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
210 changes: 0 additions & 210 deletions driver/src/test/java/org/neo4j/driver/stress/CausalClusteringIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CountDownLatch;
Expand All @@ -55,26 +53,20 @@
import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.async.ResultCursor;
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.exceptions.Neo4jException;
import org.neo4j.driver.exceptions.ServiceUnavailableException;
import org.neo4j.driver.exceptions.SessionExpiredException;
import org.neo4j.driver.integration.NestedQueries;
import org.neo4j.driver.internal.BoltServerAddress;
import org.neo4j.driver.internal.cluster.RoutingSettings;
import org.neo4j.driver.internal.retry.RetrySettings;
import org.neo4j.driver.internal.security.SecurityPlanImpl;
import org.neo4j.driver.internal.util.FailingConnectionDriverFactory;
import org.neo4j.driver.internal.util.FakeClock;
import org.neo4j.driver.internal.util.ServerVersion;
import org.neo4j.driver.internal.util.ThrowingMessageEncoder;
import org.neo4j.driver.internal.util.io.ChannelTrackingDriverFactory;
import org.neo4j.driver.summary.ResultSummary;
import org.neo4j.driver.util.cc.Cluster;
import org.neo4j.driver.util.cc.ClusterExtension;
import org.neo4j.driver.util.cc.ClusterMember;
import org.neo4j.driver.util.cc.ClusterMemberRole;
import org.neo4j.driver.util.cc.ClusterMemberRoleDiscoveryFactory;
import org.neo4j.driver.util.cc.ClusterMemberRoleDiscoveryFactory.ClusterMemberRoleDiscovery;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
Expand All @@ -96,7 +88,6 @@
import static org.neo4j.driver.internal.InternalBookmark.parse;
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
import static org.neo4j.driver.internal.util.Matchers.connectionAcquisitionTimeoutError;
import static org.neo4j.driver.internal.util.Neo4jFeature.BOLT_V3;
import static org.neo4j.driver.util.DaemonThreadFactory.daemon;
import static org.neo4j.driver.util.TestUtil.await;
import static org.neo4j.driver.util.TestUtil.awaitAllFutures;
Expand Down Expand Up @@ -481,61 +472,6 @@ RoutingSettings.DEFAULT, RetrySettings.DEFAULT, configWithoutLogging(), Security
}
}

@Test
void shouldRediscoverWhenConnectionsToAllCoresBreak()
{
Cluster cluster = clusterRule.getCluster();

ChannelTrackingDriverFactory driverFactory = new ChannelTrackingDriverFactory();
try ( Driver driver = driverFactory.newInstance( cluster.getRoutingUri(), clusterRule.getDefaultAuthToken(),
RoutingSettings.DEFAULT, RetrySettings.DEFAULT, configWithoutLogging(), SecurityPlanImpl.insecure() ) )
{
String database = "neo4j";
try ( Session session = driver.session( builder().withDatabase( database ).build() ) )
{
createNode( session, "Person", "name", "Vision" );

// force driver to connect to every cluster member
for ( int i = 0; i < cluster.members().size(); i++ )
{
assertEquals( 1, countNodes( session, "Person", "name", "Vision" ) );
}
}

// now driver should have connections towards every cluster member
// make all those connections throw and seem broken
makeAllChannelsFailToRunQueries( driverFactory, ServerVersion.version( driver ) );

// observe that connection towards writer is broken
try ( Session session = driver.session( builder().withDatabase( database ).withDefaultAccessMode( AccessMode.WRITE ).build() ) )
{
SessionExpiredException e = assertThrows( SessionExpiredException.class,
() -> runCreateNode( session, "Person", "name", "Vision" ).consume() );
assertEquals( "Disconnected", e.getCause().getMessage() );
}

// probe connections to all readers
int readersCount = cluster.followers().size() + cluster.readReplicas().size();
for ( int i = 0; i < readersCount; i++ )
{
try ( Session session = driver.session( builder().withDatabase( database ).withDefaultAccessMode( AccessMode.READ ).build() ) )
{
runCountNodes( session, "Person", "name", "Vision" );
}
catch ( Throwable ignore )
{
}
}

try ( Session session = driver.session( builder().withDatabase( database ).build() ) )
{
updateNode( session, "Person", "name", "Vision", "Thanos" );
assertEquals( 0, countNodes( session, "Person", "name", "Vision" ) );
assertEquals( 1, countNodes( session, "Person", "name", "Thanos" ) );
}
}
}

@Test
void shouldKeepOperatingWhenConnectionsBreak() throws Exception
{
Expand Down Expand Up @@ -672,52 +608,6 @@ private <T> T inExpirableSession( Driver driver, Function<Driver,Session> acquir
throw new TimeoutException( "Transaction did not succeed in time" );
}

private void ensureNodeVisible( Cluster cluster, String name, Bookmark bookmark )
{
for ( ClusterMember member : cluster.members() )
{
int count = countNodesUsingDirectDriver( member, name, bookmark );
assertEquals( 1, count );
}
}

private int countNodesUsingDirectDriver( ClusterMember member, final String name, Bookmark bookmark )
{
Driver driver = clusterRule.getCluster().getDirectDriver( member );
try ( Session session = driver.session( builder().withBookmarks( bookmark ).build() ) )
{
return session.readTransaction( tx ->
{
Result result = tx.run( "MATCH (:Person {name: $name}) RETURN count(*)",
parameters( "name", name ) );
return result.single().get( 0 ).asInt();
} );
}
}

private void awaitLeaderToStepDown( Set<ClusterMember> cores )
{
long deadline = System.currentTimeMillis() + DEFAULT_TIMEOUT_MS;
ClusterOverview overview = null;
do
{
for ( ClusterMember core : cores )
{
overview = fetchClusterOverview( core );
if ( overview != null )
{
break;
}
}
}
while ( !isSingleFollowerWithReadReplicas( overview ) && System.currentTimeMillis() <= deadline );

if ( System.currentTimeMillis() > deadline )
{
throw new IllegalStateException( "Leader did not step down in " + DEFAULT_TIMEOUT_MS + "ms. Last seen cluster overview: " + overview );
}
}

private Driver createDriver( URI boltUri )
{
return createDriver( boltUri, configWithoutLogging() );
Expand All @@ -733,45 +623,6 @@ private Driver discoverDriver( List<URI> routingUris )
return GraphDatabase.routingDriver( routingUris, clusterRule.getDefaultAuthToken(), configWithoutLogging() );
}

private static ClusterOverview fetchClusterOverview( ClusterMember member )
{
int leaderCount = 0;
int followerCount = 0;
int readReplicaCount = 0;

Driver driver = clusterRule.getCluster().getDirectDriver( member );
try
{
final ClusterMemberRoleDiscovery discovery = ClusterMemberRoleDiscoveryFactory.newInstance( ServerVersion.version( driver ) );
final Map<BoltServerAddress,ClusterMemberRole> clusterOverview = discovery.findClusterOverview( driver );
for ( BoltServerAddress address : clusterOverview.keySet() )
{
ClusterMemberRole role = clusterOverview.get( address );
if ( role == ClusterMemberRole.LEADER )
{
leaderCount++;
}
else if ( role == ClusterMemberRole.FOLLOWER )
{
followerCount++;
}
else if ( role == ClusterMemberRole.READ_REPLICA )
{
readReplicaCount++;
}
else
{
throw new AssertionError( "Unknown role: " + role );
}
}
return new ClusterOverview( leaderCount, followerCount, readReplicaCount );
}
catch ( Neo4jException ignore )
{
return null;
}
}

private static void createNodesInDifferentThreads( int count, final Driver driver ) throws Exception
{
final CountDownLatch beforeRunLatch = new CountDownLatch( count );
Expand Down Expand Up @@ -860,16 +711,6 @@ private static void createNode( Session session, String label, String property,
} );
}

private static void updateNode( Session session, String label, String property, String oldValue, String newValue )
{
session.writeTransaction( tx ->
{
tx.run( "MATCH (n: " + label + '{' + property + ": $oldValue}) SET n." + property + " = $newValue",
parameters( "oldValue", oldValue, "newValue", newValue ) );
return null;
} );
}

private static int countNodes( Session session, String label, String property, String value )
{
return session.readTransaction( tx -> runCountNodes( tx, label, property, value ) );
Expand Down Expand Up @@ -915,33 +756,6 @@ private static ExecutorService newExecutor()
return Executors.newCachedThreadPool( daemon( CausalClusteringIT.class.getSimpleName() + "-thread-" ) );
}

private static boolean isSingleFollowerWithReadReplicas( ClusterOverview overview )
{
if ( overview == null )
{
return false;
}
return overview.leaderCount == 0 &&
overview.followerCount == 1 &&
overview.readReplicaCount == ClusterExtension.READ_REPLICA_COUNT;
}

private static void makeAllChannelsFailToRunQueries( ChannelTrackingDriverFactory driverFactory, ServerVersion dbVersion )
{
for ( Channel channel : driverFactory.channels() )
{
RuntimeException error = new ServiceUnavailableException( "Disconnected" );
if ( BOLT_V3.availableIn( dbVersion ) )
{
channel.pipeline().addLast( ThrowingMessageEncoder.forRunWithMetadataMessage( error ) );
}
else
{
channel.pipeline().addLast( ThrowingMessageEncoder.forRunMessage( error ) );
}
}
}

private static class RecordAndSummary
{
final Record record;
Expand All @@ -953,28 +767,4 @@ private static class RecordAndSummary
this.summary = summary;
}
}

private static class ClusterOverview
{
final int leaderCount;
final int followerCount;
final int readReplicaCount;

ClusterOverview( int leaderCount, int followerCount, int readReplicaCount )
{
this.leaderCount = leaderCount;
this.followerCount = followerCount;
this.readReplicaCount = readReplicaCount;
}

@Override
public String toString()
{
return "ClusterOverview{" +
"leaderCount=" + leaderCount +
", followerCount=" + followerCount +
", readReplicaCount=" + readReplicaCount +
'}';
}
}
}
5 changes: 0 additions & 5 deletions driver/src/test/java/org/neo4j/driver/util/Neo4jSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ public Map<String, String> propertiesMap()
return settings;
}

public Neo4jSettings updateWith( Neo4jSettings other )
{
return updateWith( other.settings, other.excludes );
}

public Neo4jSettings updateWith( String key, String value )
{
return updateWith( map(key, value), excludes );
Expand Down
10 changes: 0 additions & 10 deletions driver/src/test/java/org/neo4j/driver/util/cc/ClusterControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,9 @@ static void stopCluster( Path path )
executeCommand( "neoctrl-cluster", "stop", path.toString() );
}

static void stopClusterMember( Path path )
{
executeCommand( "neoctrl-stop", path.toString() );
}

static void killCluster( Path path )
{
executeCommand( "neoctrl-cluster", "stop", "--kill", path.toString() );
}

static void killClusterMember( Path path )
{
executeCommand( "neoctrl-stop", "--kill", path.toString() );
}

}