Search in sources :

Example 16 with BackendException

use of org.janusgraph.diskstorage.BackendException in project janusgraph by JanusGraph.

the class CassandraThriftStoreManager method mutateMany.

@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> mutations, StoreTransaction txh) throws BackendException {
    Preconditions.checkNotNull(mutations);
    final MaskedTimestamp commitTime = new MaskedTimestamp(txh);
    ConsistencyLevel consistency = getTx(txh).getWriteConsistencyLevel().getThrift();
    // Generate Thrift-compatible batch_mutate() datastructure
    // key -> cf -> cassmutation
    int size = 0;
    for (final Map<StaticBuffer, KCVMutation> mutation : mutations.values()) {
        size += mutation.size();
    }
    final Map<ByteBuffer, Map<String, List<org.apache.cassandra.thrift.Mutation>>> batch = new HashMap<>(size);
    for (final Map.Entry<String, Map<StaticBuffer, KCVMutation>> keyMutation : mutations.entrySet()) {
        final String columnFamily = keyMutation.getKey();
        for (final Map.Entry<StaticBuffer, KCVMutation> mutEntry : keyMutation.getValue().entrySet()) {
            ByteBuffer keyBB = mutEntry.getKey().asByteBuffer();
            // Get or create the single Cassandra Mutation object responsible for this key
            // Most mutations only modify the edgeStore and indexStore
            final Map<String, List<org.apache.cassandra.thrift.Mutation>> cfmutation = batch.computeIfAbsent(keyBB, k -> new HashMap<>(3));
            final KCVMutation mutation = mutEntry.getValue();
            final List<org.apache.cassandra.thrift.Mutation> thriftMutation = new ArrayList<>(mutations.size());
            if (mutation.hasDeletions()) {
                for (final StaticBuffer buf : mutation.getDeletions()) {
                    final Deletion d = new Deletion();
                    final SlicePredicate sp = new SlicePredicate();
                    sp.addToColumn_names(buf.as(StaticBuffer.BB_FACTORY));
                    d.setPredicate(sp);
                    d.setTimestamp(commitTime.getDeletionTime(times));
                    org.apache.cassandra.thrift.Mutation m = new org.apache.cassandra.thrift.Mutation();
                    m.setDeletion(d);
                    thriftMutation.add(m);
                }
            }
            if (mutation.hasAdditions()) {
                for (final Entry ent : mutation.getAdditions()) {
                    final ColumnOrSuperColumn columnOrSuperColumn = new ColumnOrSuperColumn();
                    final Column column = new Column(ent.getColumnAs(StaticBuffer.BB_FACTORY));
                    column.setValue(ent.getValueAs(StaticBuffer.BB_FACTORY));
                    column.setTimestamp(commitTime.getAdditionTime(times));
                    final Integer ttl = (Integer) ent.getMetaData().get(EntryMetaData.TTL);
                    if (null != ttl && ttl > 0) {
                        column.setTtl(ttl);
                    }
                    columnOrSuperColumn.setColumn(column);
                    org.apache.cassandra.thrift.Mutation m = new org.apache.cassandra.thrift.Mutation();
                    m.setColumn_or_supercolumn(columnOrSuperColumn);
                    thriftMutation.add(m);
                }
            }
            cfmutation.put(columnFamily, thriftMutation);
        }
    }
    CTConnection conn = null;
    try {
        conn = pool.borrowObject(keySpaceName);
        Cassandra.Client client = conn.getClient();
        if (atomicBatch) {
            client.atomic_batch_mutate(batch, consistency);
        } else {
            client.batch_mutate(batch, consistency);
        }
    } catch (Exception ex) {
        throw CassandraThriftKeyColumnValueStore.convertException(ex);
    } finally {
        pool.returnObjectUnsafe(keySpaceName, conn);
    }
    sleepAfterWrite(txh, commitTime);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) org.apache.cassandra.thrift(org.apache.cassandra.thrift) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ByteBuffer(java.nio.ByteBuffer) KCVMutation(org.janusgraph.diskstorage.keycolumnvalue.KCVMutation) BackendException(org.janusgraph.diskstorage.BackendException) TException(org.apache.thrift.TException) CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) KCVMutation(org.janusgraph.diskstorage.keycolumnvalue.KCVMutation) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 17 with BackendException

use of org.janusgraph.diskstorage.BackendException in project janusgraph by JanusGraph.

the class CassandraThriftStoreManager method exists.

@Override
public boolean exists() throws BackendException {
    CTConnection connection = null;
    try {
        connection = pool.borrowObject(SYSTEM_KS);
        final Cassandra.Client client = connection.getClient();
        try {
            // Side effect: throws Exception if keyspaceName doesn't exist
            // Don't remove
            client.set_keyspace(keySpaceName);
            client.set_keyspace(SYSTEM_KS);
            return true;
        } catch (InvalidRequestException e) {
            return false;
        }
    } catch (Exception e) {
        throw new TemporaryBackendException(e);
    } finally {
        pool.returnObjectUnsafe(SYSTEM_KS, connection);
    }
}
Also used : CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) BackendException(org.janusgraph.diskstorage.BackendException) TException(org.apache.thrift.TException)

Example 18 with BackendException

use of org.janusgraph.diskstorage.BackendException in project janusgraph by JanusGraph.

the class ManagementSystem method updateIndex.

/* --------------
    Schema Update
     --------------- */
@Override
public IndexJobFuture updateIndex(Index index, SchemaAction updateAction) {
    Preconditions.checkArgument(index != null, "Need to provide an index");
    Preconditions.checkArgument(updateAction != null, "Need to provide update action");
    JanusGraphSchemaVertex schemaVertex = getSchemaVertex(index);
    Set<JanusGraphSchemaVertex> dependentTypes;
    Set<PropertyKeyVertex> keySubset = ImmutableSet.of();
    if (index instanceof RelationTypeIndex) {
        dependentTypes = ImmutableSet.of((JanusGraphSchemaVertex) ((InternalRelationType) schemaVertex).getBaseType());
        if (!updateAction.isApplicableStatus(schemaVertex.getStatus()))
            return null;
    } else if (index instanceof JanusGraphIndex) {
        IndexType indexType = schemaVertex.asIndexType();
        dependentTypes = Sets.newHashSet();
        if (indexType.isCompositeIndex()) {
            if (!updateAction.isApplicableStatus(schemaVertex.getStatus()))
                return null;
            for (PropertyKey key : ((JanusGraphIndex) index).getFieldKeys()) {
                dependentTypes.add((PropertyKeyVertex) key);
            }
        } else {
            keySubset = Sets.newHashSet();
            MixedIndexType mixedIndexType = (MixedIndexType) indexType;
            Set<SchemaStatus> applicableStatus = updateAction.getApplicableStatus();
            for (ParameterIndexField field : mixedIndexType.getFieldKeys()) {
                if (applicableStatus.contains(field.getStatus()))
                    keySubset.add((PropertyKeyVertex) field.getFieldKey());
            }
            if (keySubset.isEmpty())
                return null;
            dependentTypes.addAll(keySubset);
        }
    } else
        throw new UnsupportedOperationException("Updates not supported for index: " + index);
    IndexIdentifier indexId = new IndexIdentifier(index);
    StandardScanner.Builder builder;
    IndexJobFuture future;
    switch(updateAction) {
        case REGISTER_INDEX:
            setStatus(schemaVertex, SchemaStatus.INSTALLED, keySubset);
            updatedTypes.add(schemaVertex);
            updatedTypes.addAll(dependentTypes);
            setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.REGISTERED, keySubset));
            future = new EmptyIndexJobFuture();
            break;
        case REINDEX:
            builder = graph.getBackend().buildEdgeScanJob();
            builder.setFinishJob(indexId.getIndexJobFinisher(graph, SchemaAction.ENABLE_INDEX));
            builder.setJobId(indexId);
            builder.setJob(VertexJobConverter.convert(graph, new IndexRepairJob(indexId.indexName, indexId.relationTypeName)));
            try {
                future = builder.execute();
            } catch (BackendException e) {
                throw new JanusGraphException(e);
            }
            break;
        case ENABLE_INDEX:
            setStatus(schemaVertex, SchemaStatus.ENABLED, keySubset);
            updatedTypes.add(schemaVertex);
            if (!keySubset.isEmpty())
                updatedTypes.addAll(dependentTypes);
            future = new EmptyIndexJobFuture();
            break;
        case DISABLE_INDEX:
            setStatus(schemaVertex, SchemaStatus.INSTALLED, keySubset);
            updatedTypes.add(schemaVertex);
            if (!keySubset.isEmpty())
                updatedTypes.addAll(dependentTypes);
            setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.DISABLED, keySubset));
            future = new EmptyIndexJobFuture();
            break;
        case REMOVE_INDEX:
            if (index instanceof RelationTypeIndex) {
                builder = graph.getBackend().buildEdgeScanJob();
            } else {
                JanusGraphIndex graphIndex = (JanusGraphIndex) index;
                if (graphIndex.isMixedIndex())
                    throw new UnsupportedOperationException("External mixed indexes must be removed in the indexing system directly.");
                builder = graph.getBackend().buildGraphIndexScanJob();
            }
            builder.setFinishJob(indexId.getIndexJobFinisher());
            builder.setJobId(indexId);
            builder.setJob(new IndexRemoveJob(graph, indexId.indexName, indexId.relationTypeName));
            try {
                future = builder.execute();
            } catch (BackendException e) {
                throw new JanusGraphException(e);
            }
            break;
        default:
            throw new UnsupportedOperationException("Update action not supported: " + updateAction);
    }
    return future;
}
Also used : Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) JanusGraphException(org.janusgraph.core.JanusGraphException) IndexRepairJob(org.janusgraph.graphdb.olap.job.IndexRepairJob) ParameterIndexField(org.janusgraph.graphdb.types.ParameterIndexField) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) BackendException(org.janusgraph.diskstorage.BackendException) StandardScanner(org.janusgraph.diskstorage.keycolumnvalue.scan.StandardScanner) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) PropertyKeyVertex(org.janusgraph.graphdb.types.vertices.PropertyKeyVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) IndexType(org.janusgraph.graphdb.types.IndexType) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) PropertyKey(org.janusgraph.core.PropertyKey) IndexRemoveJob(org.janusgraph.graphdb.olap.job.IndexRemoveJob)

Example 19 with BackendException

use of org.janusgraph.diskstorage.BackendException in project janusgraph by JanusGraph.

the class KCVSConfiguration method close.

@Override
public void close() {
    try {
        store.close();
        txProvider.close();
        IOUtils.closeQuietly(serializer);
    } catch (BackendException e) {
        throw new JanusGraphException("Could not close configuration store", e);
    }
}
Also used : JanusGraphException(org.janusgraph.core.JanusGraphException) BackendException(org.janusgraph.diskstorage.BackendException)

Example 20 with BackendException

use of org.janusgraph.diskstorage.BackendException in project janusgraph by JanusGraph.

the class KCVSConfiguration method set.

public <O> void set(String key, O value, O expectedValue, final boolean checkExpectedValue) {
    final StaticBuffer column = string2StaticBuffer(key);
    final List<Entry> additions;
    final List<StaticBuffer> deletions;
    if (value != null) {
        // Addition
        additions = new ArrayList<>(1);
        deletions = KeyColumnValueStore.NO_DELETIONS;
        StaticBuffer val = object2StaticBuffer(value);
        additions.add(StaticArrayEntry.of(column, val));
    } else {
        // Deletion
        additions = KeyColumnValueStore.NO_ADDITIONS;
        deletions = Lists.newArrayList(column);
    }
    final StaticBuffer expectedValueBuffer;
    if (checkExpectedValue && expectedValue != null) {
        expectedValueBuffer = object2StaticBuffer(expectedValue);
    } else {
        expectedValueBuffer = null;
    }
    BackendOperation.execute(new BackendOperation.Transactional<Boolean>() {

        @Override
        public Boolean call(StoreTransaction txh) throws BackendException {
            if (checkExpectedValue)
                store.acquireLock(rowKey, column, expectedValueBuffer, txh);
            store.mutate(rowKey, additions, deletions, txh);
            return true;
        }

        @Override
        public String toString() {
            return "setConfiguration";
        }
    }, txProvider, times, maxOperationWaitTime);
}
Also used : StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) Entry(org.janusgraph.diskstorage.Entry) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) BackendOperation(org.janusgraph.diskstorage.util.BackendOperation) BackendException(org.janusgraph.diskstorage.BackendException)

Aggregations

BackendException (org.janusgraph.diskstorage.BackendException)32 PermanentBackendException (org.janusgraph.diskstorage.PermanentBackendException)12 ArrayList (java.util.ArrayList)9 TemporaryBackendException (org.janusgraph.diskstorage.TemporaryBackendException)9 IOException (java.io.IOException)7 List (java.util.List)7 Map (java.util.Map)7 TException (org.apache.thrift.TException)7 JanusGraphException (org.janusgraph.core.JanusGraphException)7 CTConnection (org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection)7 UncheckedIOException (java.io.UncheckedIOException)6 HashMap (java.util.HashMap)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 Duration (java.time.Duration)4 BaseTransactionConfig (org.janusgraph.diskstorage.BaseTransactionConfig)4 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)4 BackendOperation (org.janusgraph.diskstorage.util.BackendOperation)4 GraphDatabaseConfiguration (org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration)4 SolrServerException (org.apache.solr.client.solrj.SolrServerException)3 KeeperException (org.apache.zookeeper.KeeperException)3