use of org.neo4j.driver.v1.Values.parameters in project neo4j by neo4j.
the class BoltCausalClusteringIT method shouldSendRequestsToNewlyAddedReadReplicas.
@Test
public void shouldSendRequestsToNewlyAddedReadReplicas() throws Throwable {
// given
cluster = clusterRule.withNumberOfReadReplicas(1).withSharedCoreParams(stringMap(CausalClusteringSettings.cluster_routing_ttl.name(), "1s")).startCluster();
CoreClusterMember leader = cluster.awaitLeader();
Driver driver = GraphDatabase.driver(leader.routingURI(), AuthTokens.basic("neo4j", "neo4j"));
String bookmark = inExpirableSession(driver, (d) -> d.session(AccessMode.WRITE), (session) -> {
try (Transaction tx = session.beginTransaction()) {
tx.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Jim"));
tx.success();
}
return session.lastBookmark();
});
// when
Set<String> readReplicas = new HashSet<>();
for (ReadReplica readReplica : cluster.readReplicas()) {
readReplicas.add(readReplica.boltAdvertisedAddress());
}
for (int i = 10; i <= 13; i++) {
ReadReplica newReadReplica = cluster.addReadReplicaWithId(i);
readReplicas.add(newReadReplica.boltAdvertisedAddress());
newReadReplica.start();
}
assertEventually("Failed to send requests to all servers", () -> {
for (// don't care about cores
int i = 0; // don't care about cores
i < cluster.readReplicas().size(); // don't care about cores
i++) {
try (Session session = driver.session(AccessMode.READ)) {
BoltServerAddress boltServerAddress = ((RoutingNetworkSession) session).address();
executeReadQuery(bookmark, session);
readReplicas.remove(boltServerAddress.toString());
} catch (Throwable throwable) {
return false;
}
}
// have sent something to all replicas
return readReplicas.size() == 0;
}, is(true), 30, SECONDS);
}
use of org.neo4j.driver.v1.Values.parameters in project neo4j by neo4j.
the class BoltCausalClusteringIT method shouldUseBookmarkFromAWriteSessionInAReadSession.
@Test
public void shouldUseBookmarkFromAWriteSessionInAReadSession() throws Throwable {
// given
cluster = clusterRule.withNumberOfReadReplicas(1).startCluster();
CoreClusterMember leader = cluster.awaitLeader();
ReadReplica readReplica = cluster.getReadReplicaById(0);
readReplica.txPollingClient().stop();
Driver driver = GraphDatabase.driver(leader.directURI(), AuthTokens.basic("neo4j", "neo4j"));
String bookmark = inExpirableSession(driver, (d) -> d.session(AccessMode.WRITE), (session) -> {
try (Transaction tx = session.beginTransaction()) {
tx.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Jim"));
tx.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Alistair"));
tx.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Mark"));
tx.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Chris"));
tx.success();
}
return session.lastBookmark();
});
assertNotNull(bookmark);
readReplica.txPollingClient().start();
driver = GraphDatabase.driver(readReplica.directURI(), AuthTokens.basic("neo4j", "neo4j"));
try (Session session = driver.session(AccessMode.READ)) {
try (Transaction tx = session.beginTransaction(bookmark)) {
Record record = tx.run("MATCH (n:Person) RETURN COUNT(*) AS count").next();
tx.success();
assertEquals(4, record.get("count").asInt());
}
}
}
use of org.neo4j.driver.v1.Values.parameters in project neo4j by neo4j.
the class BoltCausalClusteringIT method shouldReadAndWriteToANewSessionCreatedAfterALeaderSwitch.
/*
Create a session with empty arg list (no AccessMode arg), in a driver that was initialized with a bolt+routing
URI, and ensure that it
a) works against the Leader for reads and writes before a leader switch, and
b) receives a SESSION EXPIRED after a leader switch, and
c) keeps working if a new session is created after that exception, again with no access mode specified.
*/
@Test
public void shouldReadAndWriteToANewSessionCreatedAfterALeaderSwitch() throws Exception {
// given
cluster = clusterRule.withNumberOfReadReplicas(1).startCluster();
CoreClusterMember leader = cluster.awaitLeader();
try (Driver driver = GraphDatabase.driver(leader.routingURI(), AuthTokens.basic("neo4j", "neo4j"))) {
inExpirableSession(driver, Driver::session, (session) -> {
// execute a write/read query
session.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Jim"));
Record record = session.run("MATCH (n:Person) RETURN COUNT(*) AS count").next();
assertEquals(1, record.get("count").asInt());
try {
switchLeader(leader);
session.run("CREATE (p:Person {name: {name} })").consume();
fail("Should have thrown an exception as the leader went away mid session");
} catch (SessionExpiredException sep) {
// then
assertEquals(String.format("Server at %s no longer accepts writes", leader.boltAdvertisedAddress()), sep.getMessage());
} catch (InterruptedException e) {
// ignored
}
return null;
});
inExpirableSession(driver, Driver::session, (session) -> {
// execute a write/read query
session.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Jim"));
Record record = session.run("MATCH (n:Person) RETURN COUNT(*) AS count").next();
assertEquals(2, record.get("count").asInt());
return null;
});
}
}
use of org.neo4j.driver.v1.Values.parameters in project neo4j by neo4j.
the class BoltCausalClusteringIT method sessionShouldExpireOnFailingReadQuery.
@Test
public void sessionShouldExpireOnFailingReadQuery() throws Exception {
// given
cluster = clusterRule.withNumberOfReadReplicas(1).startCluster();
CoreClusterMember coreServer = cluster.getCoreMemberById(0);
Driver driver = GraphDatabase.driver(coreServer.routingURI(), AuthTokens.basic("neo4j", "neo4j"));
inExpirableSession(driver, Driver::session, (session) -> {
session.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Jim"));
return null;
});
try (Session readSession = driver.session(AccessMode.READ)) {
// when
connectedServer(readSession).shutdown();
// then
readSession.run("MATCH (n) RETURN n LIMIT 1").consume();
fail("Should have thrown an exception as the read replica went away mid query");
} catch (SessionExpiredException sep) {
// then
assertThat(sep.getMessage(), containsString("is no longer available"));
} finally {
driver.close();
}
}
use of org.neo4j.driver.v1.Values.parameters in project neo4j by neo4j.
the class BoltCausalClusteringIT method bookmarksShouldWorkWithDriverPinnedToSingleServer.
// Ensure that Bookmarks work with single instances using a driver created using a bolt[not+routing] URI.
@Test
public void bookmarksShouldWorkWithDriverPinnedToSingleServer() throws Exception {
// given
cluster = clusterRule.withNumberOfReadReplicas(1).startCluster();
CoreClusterMember leader = cluster.awaitLeader();
try (Driver driver = GraphDatabase.driver(leader.directURI(), AuthTokens.basic("neo4j", "neo4j"))) {
String bookmark = inExpirableSession(driver, Driver::session, (session) -> {
try (Transaction tx = session.beginTransaction()) {
tx.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Alistair"));
tx.success();
}
return session.lastBookmark();
});
assertNotNull(bookmark);
try (Session session = driver.session();
Transaction tx = session.beginTransaction(bookmark)) {
Record record = tx.run("MATCH (n:Person) RETURN COUNT(*) AS count").next();
assertEquals(1, record.get("count").asInt());
tx.success();
}
}
}
Aggregations