Search in sources :

Example 1 with JULogging

use of org.neo4j.driver.internal.logging.JULogging in project neo4j by neo4j.

the class BoltCausalClusteringIT method shouldPickANewServerToWriteToOnLeaderSwitch.

@Test
public void shouldPickANewServerToWriteToOnLeaderSwitch() throws Throwable {
    // given
    cluster = clusterRule.withNumberOfReadReplicas(0).startCluster();
    CoreClusterMember leader = cluster.awaitLeader();
    LeaderSwitcher leaderSwitcher = new LeaderSwitcher(cluster);
    Config config = Config.build().withLogging(new JULogging(Level.OFF)).toConfig();
    Set<BoltServerAddress> seenAddresses = new HashSet<>();
    try (Driver driver = GraphDatabase.driver(leader.routingURI(), AuthTokens.basic("neo4j", "neo4j"), config)) {
        boolean success = false;
        long deadline = System.currentTimeMillis() + (30 * 1000);
        while (!success) {
            if (System.currentTimeMillis() > deadline) {
                fail("Failed to write to the new leader in time. Addresses seen: " + seenAddresses);
            }
            try (Session session = driver.session(AccessMode.WRITE)) {
                BoltServerAddress boltServerAddress = ((RoutingNetworkSession) session).address();
                session.run("CREATE (p:Person)");
                seenAddresses.add(boltServerAddress);
                success = seenAddresses.size() >= 2;
            } catch (Exception e) {
                Thread.sleep(100);
            }
            /*
                 * Having the latch release here ensures that we've done at least one pass through the loop, which means
                 * we've completed a connection before the forced master switch.
                 */
            if (seenAddresses.size() >= 1) {
                leaderSwitcher.start();
            }
        }
    } finally {
        leaderSwitcher.stop();
        assertTrue(leaderSwitcher.hadLeaderSwitch());
        assertThat(seenAddresses.size(), greaterThanOrEqualTo(2));
    }
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Config(org.neo4j.driver.v1.Config) Driver(org.neo4j.driver.v1.Driver) TimeoutException(java.util.concurrent.TimeoutException) SessionExpiredException(org.neo4j.driver.v1.exceptions.SessionExpiredException) ServiceUnavailableException(org.neo4j.driver.v1.exceptions.ServiceUnavailableException) IOException(java.io.IOException) ClientException(org.neo4j.driver.v1.exceptions.ClientException) BoltServerAddress(org.neo4j.driver.internal.net.BoltServerAddress) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) JULogging(org.neo4j.driver.internal.logging.JULogging) HashSet(java.util.HashSet) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.Test)1 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)1 RoutingNetworkSession (org.neo4j.driver.internal.RoutingNetworkSession)1 JULogging (org.neo4j.driver.internal.logging.JULogging)1 BoltServerAddress (org.neo4j.driver.internal.net.BoltServerAddress)1 Config (org.neo4j.driver.v1.Config)1 Driver (org.neo4j.driver.v1.Driver)1 Session (org.neo4j.driver.v1.Session)1 ClientException (org.neo4j.driver.v1.exceptions.ClientException)1 ServiceUnavailableException (org.neo4j.driver.v1.exceptions.ServiceUnavailableException)1 SessionExpiredException (org.neo4j.driver.v1.exceptions.SessionExpiredException)1