Search in sources :

Example 1 with JanusGraphTransaction

use of org.janusgraph.core.JanusGraphTransaction in project grakn by graknlabs.

the class JanusPreviousPropertyStep method flatMap.

@Override
protected Iterator<JanusGraphVertex> flatMap(Traverser.Admin<S> traverser) {
    JanusGraphTransaction tx = JanusGraphTraversalUtil.getTx(this.traversal);
    // Retrieve property value to look-up, that is identified in the traversal by the `stepLabel`
    Object value = getNullableScopeValue(Pop.first, stepLabel, traverser);
    return value != null ? verticesWithProperty(tx, value) : emptyIterator();
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction)

Example 2 with JanusGraphTransaction

use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.

the class JanusGraphPerformanceMemoryTest method testMemoryLeakage.

@Test
public void testMemoryLeakage() {
    long memoryBaseline = 0;
    SummaryStatistics stats = new SummaryStatistics();
    int numRuns = 25;
    for (int r = 0; r < numRuns; r++) {
        if (r == 1 || r == (numRuns - 1)) {
            memoryBaseline = MemoryAssess.getMemoryUse();
            stats.addValue(memoryBaseline);
        // System.out.println("Memory before run "+(r+1)+": " + memoryBaseline / 1024 + " KB");
        }
        for (int t = 0; t < 1000; t++) {
            graph.addVertex();
            graph.tx().rollback();
            JanusGraphTransaction tx = graph.newTransaction();
            tx.addVertex();
            tx.rollback();
        }
        if (r == 1 || r == (numRuns - 1)) {
            memoryBaseline = MemoryAssess.getMemoryUse();
            stats.addValue(memoryBaseline);
        // System.out.println("Memory after run " + (r + 1) + ": " + memoryBaseline / 1024 + " KB");
        }
        clopen();
    }
    System.out.println("Average: " + stats.getMean() + " Std. Dev: " + stats.getStandardDeviation());
    assertTrue(stats.getStandardDeviation() < stats.getMin());
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) SummaryStatistics(org.apache.commons.math.stat.descriptive.SummaryStatistics) Test(org.junit.Test)

Example 3 with JanusGraphTransaction

use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.

the class JanusGraphTest method testConsistencyEnforcement.

/* ==================================================================================
                            CONSISTENCY
     ==================================================================================*/
/**
 * Tests the correct application of ConsistencyModifiers across transactional boundaries
 */
@Test
public void testConsistencyEnforcement() {
    PropertyKey uid = makeVertexIndexedUniqueKey("uid", Integer.class);
    PropertyKey name = makeKey("name", String.class);
    mgmt.setConsistency(uid, ConsistencyModifier.LOCK);
    mgmt.setConsistency(name, ConsistencyModifier.LOCK);
    mgmt.setConsistency(mgmt.getGraphIndex("uid"), ConsistencyModifier.LOCK);
    EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.SIMPLE).make();
    EdgeLabel spouse = mgmt.makeEdgeLabel("spouse").multiplicity(Multiplicity.ONE2ONE).make();
    EdgeLabel connect = mgmt.makeEdgeLabel("connect").multiplicity(Multiplicity.MULTI).make();
    EdgeLabel related = mgmt.makeEdgeLabel("related").multiplicity(Multiplicity.MULTI).make();
    mgmt.setConsistency(knows, ConsistencyModifier.LOCK);
    mgmt.setConsistency(spouse, ConsistencyModifier.LOCK);
    mgmt.setConsistency(related, ConsistencyModifier.FORK);
    finishSchema();
    name = tx.getPropertyKey("name");
    connect = tx.getEdgeLabel("connect");
    related = tx.getEdgeLabel("related");
    JanusGraphVertex v1 = tx.addVertex("uid", 1);
    JanusGraphVertex v2 = tx.addVertex("uid", 2);
    JanusGraphVertex v3 = tx.addVertex("uid", 3);
    Edge e1 = v1.addEdge(connect.name(), v2, name.name(), "e1");
    Edge e2 = v1.addEdge(related.name(), v2, name.name(), "e2");
    newTx();
    v1 = getV(tx, v1);
    /*
         ==== check fork, no fork behavior
         */
    long e1id = getId(e1);
    long e2id = getId(e2);
    e1 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
    assertEquals("e1", e1.value("name"));
    assertEquals(e1id, getId(e1));
    e2 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
    assertEquals("e2", e2.value("name"));
    assertEquals(e2id, getId(e2));
    // Update edges - one should simply update, the other fork
    e1.property("name", "e1.2");
    e2.property("name", "e2.2");
    newTx();
    v1 = getV(tx, v1);
    e1 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
    assertEquals("e1.2", e1.value("name"));
    // should have same id
    assertEquals(e1id, getId(e1));
    e2 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
    assertEquals("e2.2", e2.value("name"));
    // should have different id since forked
    assertNotEquals(e2id, getId(e2));
    clopen();
    /*
         === check cross transaction
         */
    final Random random = new Random();
    final long[] vertexIds = { getId(v1), getId(v2), getId(v3) };
    // 1) Index uniqueness
    executeLockConflictingTransactionJobs(graph, new TransactionJob() {

        private int pos = 0;

        @Override
        public void run(JanusGraphTransaction tx) {
            JanusGraphVertex u = getV(tx, vertexIds[pos++]);
            u.property(VertexProperty.Cardinality.single, "uid", 5);
        }
    });
    // 2) Property out-uniqueness
    executeLockConflictingTransactionJobs(graph, tx -> {
        final JanusGraphVertex u = getV(tx, vertexIds[0]);
        u.property(VertexProperty.Cardinality.single, "name", "v" + random.nextInt(10));
    });
    // 3) knows simpleness
    executeLockConflictingTransactionJobs(graph, tx -> {
        final JanusGraphVertex u1 = getV(tx, vertexIds[0]), u2 = getV(tx, vertexIds[1]);
        u1.addEdge("knows", u2);
    });
    // 4) knows one2one (in 2 separate configurations)
    executeLockConflictingTransactionJobs(graph, new TransactionJob() {

        private int pos = 1;

        @Override
        public void run(JanusGraphTransaction tx) {
            final JanusGraphVertex u1 = getV(tx, vertexIds[0]), u2 = getV(tx, vertexIds[pos++]);
            u1.addEdge("spouse", u2);
        }
    });
    executeLockConflictingTransactionJobs(graph, new TransactionJob() {

        private int pos = 1;

        @Override
        public void run(JanusGraphTransaction tx) {
            final JanusGraphVertex u1 = getV(tx, vertexIds[pos++]), u2 = getV(tx, vertexIds[0]);
            u1.addEdge("spouse", u2);
        }
    });
    // ######### TRY INVALID CONSISTENCY
    try {
        // Fork does not apply to constrained types
        mgmt.setConsistency(mgmt.getPropertyKey("name"), ConsistencyModifier.FORK);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) Random(java.util.Random) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 4 with JanusGraphTransaction

use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.

the class JanusGraphTest method testIndexShouldRegisterWhenWeRemoveAnInstance.

@Category({ BrittleTests.class })
@Test
public void testIndexShouldRegisterWhenWeRemoveAnInstance() throws InterruptedException {
    clopen(option(LOG_SEND_DELAY, MANAGEMENT_LOG), Duration.ofMillis(0), option(KCVSLog.LOG_READ_LAG_TIME, MANAGEMENT_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, MANAGEMENT_LOG), Duration.ofMillis(250));
    StandardJanusGraph graph2 = (StandardJanusGraph) JanusGraphFactory.open(config);
    JanusGraphTransaction tx2;
    mgmt.makePropertyKey("name").dataType(String.class).make();
    finishSchema();
    tx.addVertex("name", "v1");
    newTx();
    evaluateQuery(tx.query().has("name", "v1"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    tx2 = graph2.newTransaction();
    evaluateQuery(tx2.query().has("name", "v1"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    // Leave tx2 open to delay acknowledgement
    mgmt.buildIndex("theIndex", Vertex.class).addKey(mgmt.getPropertyKey("name")).buildCompositeIndex();
    mgmt.commit();
    JanusGraphTransaction tx3 = graph2.newTransaction();
    tx3.addVertex("name", "v2");
    tx3.commit();
    newTx();
    tx.addVertex("name", "v3");
    tx.commit();
    finishSchema();
    try {
        mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX);
        // Open tx2 should not make this possible
        fail();
    } catch (IllegalArgumentException e) {
    }
    finishSchema();
    // close second graph instance, so index can move to REGISTERED
    Set<String> openInstances = mgmt.getOpenInstances();
    assertEquals(2, openInstances.size());
    assertTrue(openInstances.contains(graph.getConfiguration().getUniqueGraphId() + "(current)"));
    assertTrue(openInstances.contains(graph2.getConfiguration().getUniqueGraphId()));
    try {
        mgmt.forceCloseInstance(graph.getConfiguration().getUniqueGraphId());
        // Cannot close current instance
        fail();
    } catch (IllegalArgumentException e) {
    }
    mgmt.forceCloseInstance(graph2.getConfiguration().getUniqueGraphId());
    mgmt.commit();
    assertTrue(ManagementSystem.awaitGraphIndexStatus(graph, "theIndex").status(SchemaStatus.REGISTERED).timeout(TestGraphConfigs.getSchemaConvergenceTime(ChronoUnit.SECONDS), ChronoUnit.SECONDS).call().getSucceeded());
    finishSchema();
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX);
    finishSchema();
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) Category(org.junit.experimental.categories.Category) RelationCategory(org.janusgraph.graphdb.internal.RelationCategory) ElementCategory(org.janusgraph.graphdb.internal.ElementCategory) Test(org.junit.Test)

Example 5 with JanusGraphTransaction

use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.

the class JanusGraphTest method executeLockConflictingTransactionJobs.

/**
 * Executes a transaction job in two parallel transactions under the assumptions that the two transactions
 * should conflict and the one committed later should throw a locking exception.
 *
 * @param graph
 * @param job
 */
private void executeLockConflictingTransactionJobs(JanusGraph graph, TransactionJob job) {
    JanusGraphTransaction tx1 = graph.newTransaction();
    JanusGraphTransaction tx2 = graph.newTransaction();
    job.run(tx1);
    job.run(tx2);
    /*
         * Under pessimistic locking, tx1 should abort and tx2 should commit.
         * Under optimistic locking, tx1 may commit and tx2 may abort.
         */
    if (isLockingOptimistic()) {
        tx1.commit();
        try {
            tx2.commit();
            fail("Storage backend does not abort conflicting transactions");
        } catch (JanusGraphException ignored) {
        }
    } else {
        try {
            tx1.commit();
            fail("Storage backend does not abort conflicting transactions");
        } catch (JanusGraphException ignored) {
        }
        tx2.commit();
    }
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) JanusGraphException(org.janusgraph.core.JanusGraphException)

Aggregations

JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)20 Test (org.junit.Test)10 PropertyKey (org.janusgraph.core.PropertyKey)8 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)7 EdgeLabel (org.janusgraph.core.EdgeLabel)4 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)4 JanusGraphException (org.janusgraph.core.JanusGraphException)4 SchemaViolationException (org.janusgraph.core.SchemaViolationException)4 StandardJanusGraph (org.janusgraph.graphdb.database.StandardJanusGraph)4 Random (java.util.Random)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 JanusGraphConfigurationException (org.janusgraph.core.JanusGraphConfigurationException)3 Geoshape (org.janusgraph.core.attribute.Geoshape)3 BackendException (org.janusgraph.diskstorage.BackendException)3 ElementCategory (org.janusgraph.graphdb.internal.ElementCategory)3 RelationCategory (org.janusgraph.graphdb.internal.RelationCategory)3 Category (org.junit.experimental.categories.Category)3 Instant (java.time.Instant)2 CountDownLatch (java.util.concurrent.CountDownLatch)2