Search in sources :

Example 76 with Transaction

use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.

the class TransactionTerminationIT method terminateMasterTransactionThatWaitsForLockAcquiredBySlave.

@Test
public void terminateMasterTransactionThatWaitsForLockAcquiredBySlave() throws Exception {
    ClusterManager.ManagedCluster cluster = startCluster();
    String masterValue = "master";
    String slaveValue = "slave";
    HighlyAvailableGraphDatabase master = cluster.getMaster();
    HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
    createNode(cluster);
    CountDownLatch slaveTxStarted = new CountDownLatch(1);
    CountDownLatch slaveTxCommit = new CountDownLatch(1);
    Future<?> slaveTx = setPropertyInSeparateThreadAndWaitBeforeCommit("slaveTx", slave, slaveValue, slaveTxStarted, slaveTxCommit);
    await(slaveTxStarted);
    AtomicReference<Transaction> masterTxReference = new AtomicReference<>();
    CountDownLatch masterTxStarted = new CountDownLatch(1);
    Future<?> masterTx = setPropertyInSeparateThreadAndAttemptToCommit("masterTx", master, masterValue, masterTxStarted, masterTxReference);
    masterTxStarted.await();
    sleepForAWhile();
    terminate(masterTxReference);
    assertTxWasTerminated(masterTx);
    slaveTxCommit.countDown();
    assertNull(slaveTx.get());
    assertNodeExists(cluster, slaveValue);
}
Also used : Transaction(org.neo4j.graphdb.Transaction) HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterManager(org.neo4j.kernel.impl.ha.ClusterManager) Test(org.junit.Test)

Example 77 with Transaction

use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.

the class TransactionTerminationIT method setPropertyInSeparateThreadAndWaitBeforeCommit.

private static Future<?> setPropertyInSeparateThreadAndWaitBeforeCommit(String threadName, GraphDatabaseService db, Object value, CountDownLatch txStarted, CountDownLatch txCommit) {
    return executeInSeparateThread(threadName, () -> {
        try (Transaction tx = db.beginTx()) {
            Node node = findNode(db);
            node.setProperty(PROPERTY, value);
            txStarted.countDown();
            await(txCommit);
            tx.success();
        }
    });
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node)

Example 78 with Transaction

use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.

the class TransactionTerminationIT method assertNodeExists.

private static void assertNodeExists(GraphDatabaseService db, Object value) {
    try (Transaction tx = db.beginTx()) {
        Node node = findNode(db);
        assertTrue(node.hasProperty(PROPERTY));
        assertEquals(value, node.getProperty(PROPERTY));
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node)

Example 79 with Transaction

use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.

the class TransactionTerminationIT method terminateSlaveTransactionThatWaitsForLockOnMaster.

@Test
public void terminateSlaveTransactionThatWaitsForLockOnMaster() throws Exception {
    ClusterManager.ManagedCluster cluster = startCluster();
    String masterValue = "master";
    String slaveValue = "slave";
    HighlyAvailableGraphDatabase master = cluster.getMaster();
    HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
    createNode(cluster);
    CountDownLatch masterTxStarted = new CountDownLatch(1);
    CountDownLatch masterTxCommit = new CountDownLatch(1);
    Future<?> masterTx = setPropertyInSeparateThreadAndWaitBeforeCommit("masterTx", master, masterValue, masterTxStarted, masterTxCommit);
    await(masterTxStarted);
    AtomicReference<Transaction> slaveTxReference = new AtomicReference<>();
    CountDownLatch slaveTxStarted = new CountDownLatch(1);
    Future<?> slaveTx = setPropertyInSeparateThreadAndAttemptToCommit("slaveTx", slave, slaveValue, slaveTxStarted, slaveTxReference);
    slaveTxStarted.await();
    sleepForAWhile();
    terminate(slaveTxReference);
    assertTxWasTerminated(slaveTx);
    masterTxCommit.countDown();
    assertNull(masterTx.get());
    assertNodeExists(cluster, masterValue);
}
Also used : Transaction(org.neo4j.graphdb.Transaction) HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterManager(org.neo4j.kernel.impl.ha.ClusterManager) Test(org.junit.Test)

Example 80 with Transaction

use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.

the class TransactionTerminationIT method createNode.

private static void createNode(GraphDatabaseService db) {
    try (Transaction tx = db.beginTx()) {
        db.createNode(LABEL);
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction)

Aggregations

Transaction (org.neo4j.graphdb.Transaction)2409 Node (org.neo4j.graphdb.Node)1086 Test (org.junit.jupiter.api.Test)751 Test (org.junit.Test)607 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)352 Relationship (org.neo4j.graphdb.Relationship)307 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)302 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)241 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)177 Label (org.neo4j.graphdb.Label)154 Result (org.neo4j.graphdb.Result)142 HashMap (java.util.HashMap)105 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)104 MethodSource (org.junit.jupiter.params.provider.MethodSource)103 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)86 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)77 File (java.io.File)74 ArrayList (java.util.ArrayList)73 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)67 Path (java.nio.file.Path)64