Search in sources :

Example 36 with Transaction

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

the class SlaveWritingAfterStoreCopyTest method createOneNode.

private long createOneNode(HighlyAvailableGraphDatabase db) {
    long id;
    try (Transaction tx = db.beginTx()) {
        id = db.createNode().getId();
        tx.success();
    }
    return id;
}
Also used : Transaction(org.neo4j.graphdb.Transaction)

Example 37 with Transaction

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

the class ClusterLocksIT method takeExclusiveLockOnSameNodeAfterSwitch.

private void takeExclusiveLockOnSameNodeAfterSwitch(Label testLabel, HighlyAvailableGraphDatabase master, HighlyAvailableGraphDatabase db) throws EntityNotFoundException {
    try (Transaction transaction = db.beginTx()) {
        Node node = getNode(master, testLabel);
        transaction.acquireWriteLock(node);
        node.setProperty("key", "value");
        transaction.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node)

Example 38 with Transaction

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

the class TransactionThroughMasterSwitchStressIT method oneRound.

private void oneRound() throws Throwable {
    // GIVEN a cluster and a node
    final String key = "key";
    ManagedCluster cluster = clusterRule.startCluster();
    final GraphDatabaseService master = cluster.getMaster();
    final long nodeId = createNode(master);
    cluster.sync();
    // and a bunch of workers contending on that node, each changing it
    Workers<Runnable> transactors = new Workers<>("Transactors");
    final AtomicInteger successes = new AtomicInteger();
    final AtomicBoolean end = new AtomicBoolean();
    for (int i = 0; i < 10; i++) {
        transactors.start(new Runnable() {

            @Override
            public void run() {
                Random random = ThreadLocalRandom.current();
                while (!end.get()) {
                    boolean committed = true;
                    try (Transaction tx = master.beginTx()) {
                        Node node = master.getNodeById(nodeId);
                        // Acquiring lock, read int property value, increment, set incremented int property
                        // should not break under any circumstances.
                        tx.acquireWriteLock(node);
                        node.setProperty(key, (Integer) node.getProperty(key, 0) + 1);
                        // Throw in relationship for good measure
                        node.createRelationshipTo(master.createNode(), TEST);
                        Thread.sleep(random.nextInt(1_000));
                        tx.success();
                    } catch (Throwable e) {
                        // It's OK
                        committed = false;
                    }
                    if (committed) {
                        successes.incrementAndGet();
                    }
                }
            }
        });
    }
    // WHEN entering a period of induced cluster instabilities
    reelectTheSameMasterMakingItGoToPendingAndBack(cluster);
    // ... letting transactions run a bit after the role switch as well.
    long targetSuccesses = successes.get() + 20;
    while (successes.get() < targetSuccesses) {
        Thread.sleep(100);
    }
    end.set(true);
    transactors.awaitAndThrowOnError(RuntimeException.class);
    // THEN verify that the count is equal to the number of successful transactions
    assertEquals(successes.get(), getNodePropertyValue(master, nodeId, key));
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Transaction(org.neo4j.graphdb.Transaction) Workers(org.neo4j.unsafe.impl.batchimport.cache.idmapping.string.Workers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedCluster(org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster)

Example 39 with Transaction

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

the class TransactionThroughMasterSwitchStressIT method createNode.

private long createNode(GraphDatabaseService db) {
    try (Transaction tx = db.beginTx()) {
        Node node = db.createNode();
        tx.success();
        return node.getId();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node)

Example 40 with Transaction

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

the class IndexOperationsIT method index_objects_can_be_reused_after_role_switch.

@Test
public void index_objects_can_be_reused_after_role_switch() throws Throwable {
    // GIVEN
    // -- an existing index
    String key = "key", value = "value";
    HighlyAvailableGraphDatabase master = cluster.getMaster();
    long nodeId = createNode(master, key, value, true);
    cluster.sync();
    // -- get Index and IndexManager references to all dbs
    Map<HighlyAvailableGraphDatabase, IndexManager> indexManagers = new HashMap<>();
    Map<HighlyAvailableGraphDatabase, Index<Node>> indexes = new HashMap<>();
    for (HighlyAvailableGraphDatabase db : cluster.getAllMembers()) {
        try (Transaction transaction = db.beginTx()) {
            indexManagers.put(db, db.index());
            indexes.put(db, db.index().forNodes(key));
            transaction.success();
        }
    }
    // WHEN
    // -- there's a master switch
    RepairKit repair = cluster.shutdown(master);
    indexManagers.remove(master);
    indexes.remove(master);
    cluster.await(ClusterManager.masterAvailable(master));
    cluster.await(ClusterManager.masterSeesSlavesAsAvailable(1));
    // -- the index instances should still be viable to use
    for (Map.Entry<HighlyAvailableGraphDatabase, IndexManager> entry : indexManagers.entrySet()) {
        HighlyAvailableGraphDatabase db = entry.getKey();
        try (Transaction transaction = db.beginTx()) {
            IndexManager indexManager = entry.getValue();
            assertTrue(indexManager.existsForNodes(key));
            assertEquals(nodeId, indexManager.forNodes(key).get(key, value).getSingle().getId());
        }
    }
    for (Map.Entry<HighlyAvailableGraphDatabase, Index<Node>> entry : indexes.entrySet()) {
        HighlyAvailableGraphDatabase db = entry.getKey();
        try (Transaction transaction = db.beginTx()) {
            Index<Node> index = entry.getValue();
            assertEquals(nodeId, index.get(key, value).getSingle().getId());
        }
    }
    repair.repair();
}
Also used : HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) Index(org.neo4j.graphdb.index.Index) IndexManager(org.neo4j.graphdb.index.IndexManager) Transaction(org.neo4j.graphdb.Transaction) HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) RepairKit(org.neo4j.kernel.impl.ha.ClusterManager.RepairKit) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

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