Search in sources :

Example 1 with StandardTitanGraph

use of com.thinkaurelius.titan.graphdb.database.StandardTitanGraph in project titan by thinkaurelius.

the class ManagementUtil method awaitIndexUpdate.

private static void awaitIndexUpdate(TitanGraph g, String indexName, String relationTypeName, long time, TemporalUnit unit) {
    Preconditions.checkArgument(g != null && g.isOpen(), "Need to provide valid, open graph instance");
    Preconditions.checkArgument(time > 0 && unit != null, "Need to provide valid time interval");
    Preconditions.checkArgument(StringUtils.isNotBlank(indexName), "Need to provide an index name");
    StandardTitanGraph graph = (StandardTitanGraph) g;
    TimestampProvider times = graph.getConfiguration().getTimestampProvider();
    Instant end = times.getTime().plus(Duration.of(time, unit));
    boolean isStable = false;
    while (times.getTime().isBefore(end)) {
        TitanManagement mgmt = graph.openManagement();
        try {
            if (StringUtils.isNotBlank(relationTypeName)) {
                RelationTypeIndex idx = mgmt.getRelationIndex(mgmt.getRelationType(relationTypeName), indexName);
                Preconditions.checkArgument(idx != null, "Index could not be found: %s @ %s", indexName, relationTypeName);
                isStable = idx.getIndexStatus().isStable();
            } else {
                TitanGraphIndex idx = mgmt.getGraphIndex(indexName);
                Preconditions.checkArgument(idx != null, "Index could not be found: %s", indexName);
                isStable = true;
                for (PropertyKey key : idx.getFieldKeys()) {
                    if (!idx.getIndexStatus(key).isStable())
                        isStable = false;
                }
            }
        } finally {
            mgmt.rollback();
        }
        if (isStable)
            break;
        try {
            times.sleepFor(Duration.ofMillis(500));
        } catch (InterruptedException e) {
        }
    }
    if (!isStable)
        throw new TitanException("Index did not stabilize within the given amount of time. For sufficiently long " + "wait periods this is most likely caused by a failed/incorrectly shut down Titan instance or a lingering transaction.");
}
Also used : StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) TimestampProvider(com.thinkaurelius.titan.diskstorage.util.time.TimestampProvider) Instant(java.time.Instant) TitanException(com.thinkaurelius.titan.core.TitanException) TitanManagement(com.thinkaurelius.titan.core.schema.TitanManagement) RelationTypeIndex(com.thinkaurelius.titan.core.schema.RelationTypeIndex) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Example 2 with StandardTitanGraph

use of com.thinkaurelius.titan.graphdb.database.StandardTitanGraph in project titan by thinkaurelius.

the class TitanLocalQueryOptimizerStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!traversal.getGraph().isPresent())
        return;
    Graph graph = traversal.getGraph().get();
    //If this is a compute graph then we can't apply local traversal optimisation at this stage.
    StandardTitanGraph titanGraph = graph instanceof StandardTitanTx ? ((StandardTitanTx) graph).getGraph() : (StandardTitanGraph) graph;
    final boolean useMultiQuery = traversal.getEngine().isStandard() && titanGraph.getConfiguration().useMultiQuery();
    /*
                ====== VERTEX STEP ======
         */
    TraversalHelper.getStepsOfClass(VertexStep.class, traversal).forEach(originalStep -> {
        TitanVertexStep vstep = new TitanVertexStep(originalStep);
        TraversalHelper.replaceStep(originalStep, vstep, traversal);
        if (TitanTraversalUtil.isEdgeReturnStep(vstep)) {
            HasStepFolder.foldInHasContainer(vstep, traversal);
        }
        assert TitanTraversalUtil.isEdgeReturnStep(vstep) || TitanTraversalUtil.isVertexReturnStep(vstep);
        Step nextStep = TitanTraversalUtil.getNextNonIdentityStep(vstep);
        if (nextStep instanceof RangeGlobalStep) {
            int limit = QueryUtil.convertLimit(((RangeGlobalStep) nextStep).getHighRange());
            vstep.setLimit(QueryUtil.mergeLimits(limit, vstep.getLimit()));
        }
        if (useMultiQuery) {
            vstep.setUseMultiQuery(true);
        }
    });
    /*
                ====== PROPERTIES STEP ======
         */
    TraversalHelper.getStepsOfClass(PropertiesStep.class, traversal).forEach(originalStep -> {
        TitanPropertiesStep vstep = new TitanPropertiesStep(originalStep);
        TraversalHelper.replaceStep(originalStep, vstep, traversal);
        if (vstep.getReturnType().forProperties()) {
            HasStepFolder.foldInHasContainer(vstep, traversal);
        }
        if (useMultiQuery) {
            vstep.setUseMultiQuery(true);
        }
    });
    /*
                ====== EITHER INSIDE LOCAL ======
         */
    TraversalHelper.getStepsOfClass(LocalStep.class, traversal).forEach(localStep -> {
        Traversal.Admin localTraversal = ((LocalStep<?, ?>) localStep).getLocalChildren().get(0);
        Step localStart = localTraversal.getStartStep();
        if (localStart instanceof VertexStep) {
            TitanVertexStep vstep = new TitanVertexStep((VertexStep) localStart);
            TraversalHelper.replaceStep(localStart, vstep, localTraversal);
            if (TitanTraversalUtil.isEdgeReturnStep(vstep)) {
                HasStepFolder.foldInHasContainer(vstep, localTraversal);
                HasStepFolder.foldInOrder(vstep, localTraversal, traversal, false);
            }
            HasStepFolder.foldInRange(vstep, localTraversal);
            unfoldLocalTraversal(traversal, localStep, localTraversal, vstep, useMultiQuery);
        }
        if (localStart instanceof PropertiesStep) {
            TitanPropertiesStep vstep = new TitanPropertiesStep((PropertiesStep) localStart);
            TraversalHelper.replaceStep(localStart, vstep, localTraversal);
            if (vstep.getReturnType().forProperties()) {
                HasStepFolder.foldInHasContainer(vstep, localTraversal);
                HasStepFolder.foldInOrder(vstep, localTraversal, traversal, false);
            }
            HasStepFolder.foldInRange(vstep, localTraversal);
            unfoldLocalTraversal(traversal, localStep, localTraversal, vstep, useMultiQuery);
        }
    });
}
Also used : PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) Graph(org.apache.tinkerpop.gremlin.structure.Graph) StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep)

Example 3 with StandardTitanGraph

use of com.thinkaurelius.titan.graphdb.database.StandardTitanGraph in project titan by thinkaurelius.

the class EdgeSerializerTest method testValueOrdering.

@Test
public void testValueOrdering() {
    StandardTitanGraph graph = (StandardTitanGraph) StorageSetup.getInMemoryGraph();
    TitanManagement mgmt = graph.openManagement();
    EdgeLabel father = mgmt.makeEdgeLabel("father").multiplicity(Multiplicity.MANY2ONE).make();
    for (int i = 1; i <= 5; i++) mgmt.makePropertyKey("key" + i).dataType(Integer.class).make();
    mgmt.commit();
    TitanVertex v1 = graph.addVertex(), v2 = graph.addVertex();
    TitanEdge e1 = v1.addEdge("father", v2);
    for (int i = 1; i <= 5; i++) e1.property("key" + i, i);
    graph.tx().commit();
    e1.remove();
    graph.tx().commit();
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) TitanManagement(com.thinkaurelius.titan.core.schema.TitanManagement) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Test(org.junit.Test)

Example 4 with StandardTitanGraph

use of com.thinkaurelius.titan.graphdb.database.StandardTitanGraph in project titan by thinkaurelius.

the class TitanCleanup method clear.

/**
     * Clears out the entire graph. This will delete ALL of the data stored in this graph and the data will NOT be
     * recoverable. This method is intended only for development and testing use.
     *
     * @param graph
     * @throws IllegalArgumentException if the graph has not been shut down
     * @throws com.thinkaurelius.titan.core.TitanException if clearing the storage is unsuccessful
     */
public static final void clear(TitanGraph graph) {
    Preconditions.checkNotNull(graph);
    Preconditions.checkArgument(graph instanceof StandardTitanGraph, "Invalid graph instance detected: %s", graph.getClass());
    StandardTitanGraph g = (StandardTitanGraph) graph;
    Preconditions.checkArgument(!g.isOpen(), "Graph needs to be shut down before it can be cleared.");
    final GraphDatabaseConfiguration config = g.getConfiguration();
    BackendOperation.execute(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            config.getBackend().clearStorage();
            return true;
        }

        @Override
        public String toString() {
            return "ClearBackend";
        }
    }, Duration.ofSeconds(20));
}
Also used : StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) GraphDatabaseConfiguration(com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration)

Example 5 with StandardTitanGraph

use of com.thinkaurelius.titan.graphdb.database.StandardTitanGraph in project titan by thinkaurelius.

the class TitanGraphTest method testIndexUpdateSyncWithMultipleInstances.

@Category({ BrittleTests.class })
@Test
public void testIndexUpdateSyncWithMultipleInstances() 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));
    StandardTitanGraph graph2 = (StandardTitanGraph) TitanFactory.open(config);
    TitanTransaction tx2;
    PropertyKey name = 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();
    TitanTransaction tx3 = graph2.newTransaction();
    tx3.addVertex("name", "v2");
    tx3.commit();
    newTx();
    tx.addVertex("name", "v3");
    tx.commit();
    //Wait for the index to register in graph2
    Thread.sleep(2000);
    finishSchema();
    try {
        mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX);
        //Open tx2 should not make this possible
        fail();
    } catch (IllegalArgumentException e) {
    }
    finishSchema();
    //Release transaction and wait a little for registration which should make enabling possible
    tx2.commit();
    mgmt.rollback();
    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();
    tx2 = graph2.newTransaction();
    //Should be added to index but index not yet enabled
    tx2.addVertex("name", "v4");
    tx2.commit();
    newTx();
    evaluateQuery(tx.query().has("name", "v1"), ElementCategory.VERTEX, 0, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("name", "v2"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("name", "v3"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("name", "v4"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
    Thread.sleep(2000);
    tx2 = graph2.newTransaction();
    evaluateQuery(tx2.query().has("name", "v1"), ElementCategory.VERTEX, 0, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx2.query().has("name", "v2"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx2.query().has("name", "v3"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx2.query().has("name", "v4"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
    tx2.commit();
    //Finally test retrieving and closing open instances
    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());
    graph2.close();
}
Also used : StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Category(org.junit.experimental.categories.Category) RelationCategory(com.thinkaurelius.titan.graphdb.internal.RelationCategory) ElementCategory(com.thinkaurelius.titan.graphdb.internal.ElementCategory) Test(org.junit.Test)

Aggregations

StandardTitanGraph (com.thinkaurelius.titan.graphdb.database.StandardTitanGraph)5 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)2 TitanManagement (com.thinkaurelius.titan.core.schema.TitanManagement)2 Test (org.junit.Test)2 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)1 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)1 TitanException (com.thinkaurelius.titan.core.TitanException)1 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)1 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)1 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)1 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)1 TimestampProvider (com.thinkaurelius.titan.diskstorage.util.time.TimestampProvider)1 GraphDatabaseConfiguration (com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration)1 ElementCategory (com.thinkaurelius.titan.graphdb.internal.ElementCategory)1 RelationCategory (com.thinkaurelius.titan.graphdb.internal.RelationCategory)1 StandardTitanTx (com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx)1 Instant (java.time.Instant)1 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)1 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)1 LocalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep)1