Search in sources :

Example 11 with StandardTitanTx

use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.

the class VertexListTest method testLists.

@Test
public void testLists() {
    int num = 13;
    TitanGraph g = TitanFactory.open("inmemory");
    StandardTitanTx tx = (StandardTitanTx) g.newTransaction();
    VertexLongList vll = new VertexLongList(tx);
    VertexArrayList val = new VertexArrayList(tx);
    for (int i = 0; i < num; i++) {
        TitanVertex v = tx.addVertex();
        vll.add(v);
        val.add(v);
    }
    assertEquals(num, Iterables.size(vll));
    assertEquals(num, Iterables.size(val));
    vll.sort();
    val.sort();
    assertTrue(vll.isSorted());
    assertTrue(val.isSorted());
    for (Iterable<TitanVertex> iterable : new Iterable[] { val, vll }) {
        Iterator<TitanVertex> iter = iterable.iterator();
        TitanVertex previous = null;
        for (int i = 0; i < num; i++) {
            TitanVertex next = iter.next();
            if (previous != null)
                assertTrue(previous.longId() < next.longId());
            previous = next;
        }
        try {
            iter.next();
            fail();
        } catch (NoSuchElementException ex) {
        }
    }
    tx.commit();
    g.close();
}
Also used : TitanGraph(com.thinkaurelius.titan.core.TitanGraph) VertexLongList(com.thinkaurelius.titan.graphdb.query.vertex.VertexLongList) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) VertexArrayList(com.thinkaurelius.titan.graphdb.query.vertex.VertexArrayList) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 12 with StandardTitanTx

use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.

the class TitanSchemaVertex method getDefinition.

@Override
public TypeDefinitionMap getDefinition() {
    TypeDefinitionMap def = definition;
    if (def == null) {
        def = new TypeDefinitionMap();
        Iterable<TitanVertexProperty> ps;
        if (isLoaded()) {
            StandardTitanTx tx = tx();
            ps = (Iterable) RelationConstructor.readRelation(this, tx.getGraph().getSchemaCache().getSchemaRelations(longId(), BaseKey.SchemaDefinitionProperty, Direction.OUT), tx);
        } else {
            ps = query().type(BaseKey.SchemaDefinitionProperty).properties();
        }
        for (TitanVertexProperty property : ps) {
            TypeDefinitionDescription desc = property.valueOrNull(BaseKey.SchemaDefinitionDesc);
            Preconditions.checkArgument(desc != null && desc.getCategory().isProperty());
            def.setValue(desc.getCategory(), property.value());
        }
        assert def.size() > 0;
        definition = def;
    }
    assert def != null;
    return def;
}
Also used : TitanVertexProperty(com.thinkaurelius.titan.core.TitanVertexProperty) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx)

Example 13 with StandardTitanTx

use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.

the class TitanSchemaVertex method name.

@Override
public String name() {
    if (name == null) {
        TitanVertexProperty<String> p;
        if (isLoaded()) {
            StandardTitanTx tx = tx();
            p = (TitanVertexProperty) Iterables.getOnlyElement(RelationConstructor.readRelation(this, tx.getGraph().getSchemaCache().getSchemaRelations(longId(), BaseKey.SchemaName, Direction.OUT), tx), null);
        } else {
            p = Iterables.getOnlyElement(query().type(BaseKey.SchemaName).properties(), null);
        }
        Preconditions.checkState(p != null, "Could not find type for id: %s", longId());
        name = p.value();
    }
    assert name != null;
    return TitanSchemaCategory.getName(name);
}
Also used : StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx)

Example 14 with StandardTitanTx

use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.

the class TitanH1OutputFormat method getRecordWriter.

@Override
public RecordWriter<NullWritable, VertexWritable> getRecordWriter(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
    synchronized (this) {
        if (null == graph) {
            Configuration hadoopConf = taskAttemptContext.getConfiguration();
            ModifiableHadoopConfiguration mhc = ModifiableHadoopConfiguration.of(TitanHadoopConfiguration.MAPRED_NS, hadoopConf);
            graph = (StandardTitanGraph) TitanFactory.open(mhc.getTitanGraphConf());
        }
    }
    // returned by VertexProgram.getComputeKeys()
    if (null == persistableKeys) {
        try {
            persistableKeys = VertexProgram.createVertexProgram(graph, ConfUtil.makeApacheConfiguration(taskAttemptContext.getConfiguration())).getElementComputeKeys();
            log.debug("Set persistableKeys={}", Joiner.on(",").join(persistableKeys));
        } catch (Exception e) {
            log.debug("Unable to detect or instantiate vertex program", e);
            persistableKeys = ImmutableSet.of();
        }
    }
    StandardTitanTx tx = transactions.computeIfAbsent(taskAttemptContext.getTaskAttemptID(), id -> (StandardTitanTx) graph.newTransaction());
    return new TitanH1RecordWriter(taskAttemptContext, tx, persistableKeys);
}
Also used : ModifiableHadoopConfiguration(com.thinkaurelius.titan.hadoop.config.ModifiableHadoopConfiguration) TitanHadoopConfiguration(com.thinkaurelius.titan.hadoop.config.TitanHadoopConfiguration) Configuration(org.apache.hadoop.conf.Configuration) ModifiableHadoopConfiguration(com.thinkaurelius.titan.hadoop.config.ModifiableHadoopConfiguration) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) IOException(java.io.IOException)

Example 15 with StandardTitanTx

use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.

the class StandardTransactionLogProcessor method fixSecondaryFailure.

private void fixSecondaryFailure(final StandardTransactionId txId, final TxEntry entry) {
    logRecoveryMsg("Attempting to repair partially failed transaction [%s]", txId);
    if (entry.entry == null) {
        logRecoveryMsg("Trying to repair expired or unpersisted transaction [%s] (Ignore in startup)", txId);
        return;
    }
    boolean userLogFailure = true;
    boolean secIndexFailure = true;
    final Predicate<String> isFailedIndex;
    final TransactionLogHeader.Entry commitEntry = entry.entry;
    final TransactionLogHeader.SecondaryFailures secFail = entry.failures;
    if (secFail != null) {
        userLogFailure = secFail.userLogFailure;
        secIndexFailure = !secFail.failedIndexes.isEmpty();
        isFailedIndex = new Predicate<String>() {

            @Override
            public boolean apply(@Nullable String s) {
                return secFail.failedIndexes.contains(s);
            }
        };
    } else {
        isFailedIndex = Predicates.alwaysTrue();
    }
    // I) Restore external indexes
    if (secIndexFailure) {
        //1) Collect all elements (vertices and relations) and the indexes for which they need to be restored
        final SetMultimap<String, IndexRestore> indexRestores = HashMultimap.create();
        BackendOperation.execute(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                StandardTitanTx tx = (StandardTitanTx) graph.newTransaction();
                try {
                    for (TransactionLogHeader.Modification modification : commitEntry.getContentAsModifications(serializer)) {
                        InternalRelation rel = ModificationDeserializer.parseRelation(modification, tx);
                        //Collect affected vertex indexes
                        for (MixedIndexType index : getMixedIndexes(rel.getType())) {
                            if (index.getElement() == ElementCategory.VERTEX && isFailedIndex.apply(index.getBackingIndexName())) {
                                assert rel.isProperty();
                                indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.getVertex(0).longId(), ElementCategory.VERTEX, getIndexId(index)));
                            }
                        }
                        //See if relation itself is affected
                        for (RelationType relType : rel.getPropertyKeysDirect()) {
                            for (MixedIndexType index : getMixedIndexes(relType)) {
                                if (index.getElement().isInstance(rel) && isFailedIndex.apply(index.getBackingIndexName())) {
                                    assert rel.id() instanceof RelationIdentifier;
                                    indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.id(), ElementCategory.getByClazz(rel.getClass()), getIndexId(index)));
                                }
                            }
                        }
                    }
                } finally {
                    if (tx.isOpen())
                        tx.rollback();
                }
                return true;
            }
        }, readTime);
        //2) Restore elements per backing index
        for (final String indexName : indexRestores.keySet()) {
            final StandardTitanTx tx = (StandardTitanTx) graph.newTransaction();
            try {
                BackendTransaction btx = tx.getTxHandle();
                final IndexTransaction indexTx = btx.getIndexTransaction(indexName);
                BackendOperation.execute(new Callable<Boolean>() {

                    @Override
                    public Boolean call() throws Exception {
                        Map<String, Map<String, List<IndexEntry>>> restoredDocs = Maps.newHashMap();
                        for (IndexRestore restore : indexRestores.get(indexName)) {
                            TitanSchemaVertex indexV = (TitanSchemaVertex) tx.getVertex(restore.indexId);
                            MixedIndexType index = (MixedIndexType) indexV.asIndexType();
                            TitanElement element = restore.retrieve(tx);
                            if (element != null) {
                                graph.getIndexSerializer().reindexElement(element, index, restoredDocs);
                            } else {
                                //Element is deleted
                                graph.getIndexSerializer().removeElement(restore.elementId, index, restoredDocs);
                            }
                        }
                        indexTx.restore(restoredDocs);
                        indexTx.commit();
                        return true;
                    }

                    @Override
                    public String toString() {
                        return "IndexMutation";
                    }
                }, persistenceTime);
            } finally {
                if (tx.isOpen())
                    tx.rollback();
            }
        }
    }
    // II) Restore log messages
    final String logTxIdentifier = (String) commitEntry.getMetadata().get(LogTxMeta.LOG_ID);
    if (userLogFailure && logTxIdentifier != null) {
        TransactionLogHeader txHeader = new TransactionLogHeader(txCounter.incrementAndGet(), times.getTime(), times);
        final StaticBuffer userLogContent = txHeader.serializeUserLog(serializer, commitEntry, txId);
        BackendOperation.execute(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                final Log userLog = graph.getBackend().getUserLog(logTxIdentifier);
                Future<Message> env = userLog.add(userLogContent);
                if (env.isDone()) {
                    env.get();
                }
                return true;
            }
        }, persistenceTime);
    }
}
Also used : IndexTransaction(com.thinkaurelius.titan.diskstorage.indexing.IndexTransaction) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) RelationType(com.thinkaurelius.titan.core.RelationType) TitanElement(com.thinkaurelius.titan.core.TitanElement) List(java.util.List) TransactionLogHeader(com.thinkaurelius.titan.graphdb.database.log.TransactionLogHeader) MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) RelationIdentifier(com.thinkaurelius.titan.graphdb.relations.RelationIdentifier) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) TitanException(com.thinkaurelius.titan.core.TitanException) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) Map(java.util.Map)

Aggregations

StandardTitanTx (com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx)20 Test (org.junit.Test)5 RelationType (com.thinkaurelius.titan.core.RelationType)3 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)3 WriteConfiguration (com.thinkaurelius.titan.diskstorage.configuration.WriteConfiguration)3 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)3 IOException (java.io.IOException)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)1 TitanElement (com.thinkaurelius.titan.core.TitanElement)1 TitanException (com.thinkaurelius.titan.core.TitanException)1 TitanGraph (com.thinkaurelius.titan.core.TitanGraph)1 TitanRelation (com.thinkaurelius.titan.core.TitanRelation)1 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)1 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)1 EntryList (com.thinkaurelius.titan.diskstorage.EntryList)1 ModifiableConfiguration (com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration)1 IndexTransaction (com.thinkaurelius.titan.diskstorage.indexing.IndexTransaction)1