Search in sources :

Example 1 with Transaction

use of edu.princeton.cs.algs4.Transaction 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);
}
Also used : ReadReplica(org.neo4j.causalclustering.discovery.ReadReplica) Transaction(org.neo4j.driver.v1.Transaction) CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) HashSet(java.util.HashSet) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) BoltServerAddress(org.neo4j.driver.internal.net.BoltServerAddress) Test(org.junit.Test)

Example 2 with Transaction

use of edu.princeton.cs.algs4.Transaction 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());
        }
    }
}
Also used : ReadReplica(org.neo4j.causalclustering.discovery.ReadReplica) Transaction(org.neo4j.driver.v1.Transaction) CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) Record(org.neo4j.driver.v1.Record) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Example 3 with Transaction

use of edu.princeton.cs.algs4.Transaction in project neo4j by neo4j.

the class BoltCausalClusteringIT method shouldHandleLeaderSwitch.

@Test
public void shouldHandleLeaderSwitch() throws Exception {
    // given
    cluster = clusterRule.startCluster();
    CoreClusterMember leader = cluster.awaitLeader();
    try (Driver driver = GraphDatabase.driver(leader.routingURI(), AuthTokens.basic("neo4j", "neo4j"))) {
        // when
        try (Session session = driver.session()) {
            try (Transaction tx = session.beginTransaction()) {
                switchLeader(leader);
                tx.run("CREATE (person:Person {name: {name}, title: {title}})", parameters("name", "Webber", "title", "Mr"));
                tx.success();
            } catch (SessionExpiredException ignored) {
            // expected
            }
        }
        String bookmark = inExpirableSession(driver, Driver::session, s -> {
            try (Transaction tx = s.beginTransaction()) {
                tx.run("CREATE (person:Person {name: {name}, title: {title}})", parameters("name", "Webber", "title", "Mr"));
                tx.success();
            } catch (SessionExpiredException ignored) {
            }
            return s.lastBookmark();
        });
        // then
        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(1, record.get("count").asInt());
            }
        }
    }
}
Also used : Transaction(org.neo4j.driver.v1.Transaction) CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) SessionExpiredException(org.neo4j.driver.v1.exceptions.SessionExpiredException) Record(org.neo4j.driver.v1.Record) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Example 4 with Transaction

use of edu.princeton.cs.algs4.Transaction in project google-cloud-java by GoogleCloudPlatform.

the class SessionImplTest method multiUseReadOnlyTransactionReturnsEmptyTransactionMetadata.

@Test
public void multiUseReadOnlyTransactionReturnsEmptyTransactionMetadata() {
    Transaction txnMetadata = Transaction.newBuilder().setId(ByteString.copyFromUtf8("x")).build();
    PartialResultSet resultSet = PartialResultSet.newBuilder().setMetadata(newMetadata(Type.struct(Type.StructField.of("C", Type.string())))).build();
    Mockito.when(rpc.beginTransaction(Mockito.<BeginTransactionRequest>any(), Mockito.eq(options))).thenReturn(txnMetadata);
    mockRead(resultSet);
    ReadOnlyTransaction txn = session.readOnlyTransaction(TimestampBound.strong());
    expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.INTERNAL));
    txn.readRow("Dummy", Key.of(), Arrays.asList("C"));
}
Also used : Transaction(com.google.spanner.v1.Transaction) PartialResultSet(com.google.spanner.v1.PartialResultSet) Test(org.junit.Test)

Example 5 with Transaction

use of edu.princeton.cs.algs4.Transaction in project algorithms-sedgewick-wayne by reneargento.

the class Exercise13 method main.

public static void main(String[] args) {
    Date date = new Date(8, 3, 2016);
    Exercise13 transaction = new Exercise13("Rene", date, 500);
    StdOut.println(transaction);
    StdOut.println("Expected: Rene spent 500.0 on 8/3/2016");
}
Also used : Date(edu.princeton.cs.algs4.Date)

Aggregations

Test (org.junit.Test)8 Transaction (org.neo4j.driver.v1.Transaction)8 Session (org.neo4j.driver.v1.Session)7 Record (org.neo4j.driver.v1.Record)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)5 RoutingNetworkSession (org.neo4j.driver.internal.RoutingNetworkSession)5 Driver (org.neo4j.driver.v1.Driver)5 Date (edu.princeton.cs.algs4.Date)4 Transaction (edu.princeton.cs.algs4.Transaction)4 PartialResultSet (com.google.spanner.v1.PartialResultSet)3 Transaction (com.google.spanner.v1.Transaction)3 In (edu.princeton.cs.algs4.In)2 Queue (edu.princeton.cs.algs4.Queue)2 ReadReplica (org.neo4j.causalclustering.discovery.ReadReplica)2 Exercise21_ComparableTransactions (chapter2.section1.Exercise21_ComparableTransactions)1 Stack (com.jimmysun.algorithms.chapter1_3.Stack)1 Transaction (com.jimmysun.algorithms.chapter2_1.Transaction)1 MinPQ (edu.princeton.cs.algs4.MinPQ)1 HashSet (java.util.HashSet)1