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;
}
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();
}
}
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));
}
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();
}
}
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();
}
Aggregations