Search in sources :

Example 6 with CTConnection

use of org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection in project janusgraph by JanusGraph.

the class CassandraThriftStoreManager method getLocalKeyPartition.

@Override
public List<KeyRange> getLocalKeyPartition() throws BackendException {
    CTConnection conn = null;
    IPartitioner partitioner = getCassandraPartitioner();
    if (!(partitioner instanceof AbstractByteOrderedPartitioner))
        throw new UnsupportedOperationException("getLocalKeyPartition() only supported by byte ordered partitioner.");
    Token.TokenFactory tokenFactory = partitioner.getTokenFactory();
    try {
        // Resist the temptation to describe SYSTEM_KS.  It has no ring.
        // Instead, we'll create our own keyspace (or check that it exists), then describe it.
        ensureKeyspaceExists(keySpaceName);
        conn = pool.borrowObject(keySpaceName);
        final List<TokenRange> ranges = conn.getClient().describe_ring(keySpaceName);
        final List<KeyRange> keyRanges = new ArrayList<>(ranges.size());
        for (TokenRange range : ranges) {
            if (!NetworkUtil.hasLocalAddress(range.endpoints))
                continue;
            keyRanges.add(CassandraHelper.transformRange(tokenFactory.fromString(range.start_token), tokenFactory.fromString(range.end_token)));
        }
        return keyRanges;
    } catch (Exception e) {
        throw CassandraThriftKeyColumnValueStore.convertException(e);
    } finally {
        pool.returnObjectUnsafe(keySpaceName, conn);
    }
}
Also used : CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) AbstractByteOrderedPartitioner(org.apache.cassandra.dht.AbstractByteOrderedPartitioner) KeyRange(org.janusgraph.diskstorage.keycolumnvalue.KeyRange) ArrayList(java.util.ArrayList) Token(org.apache.cassandra.dht.Token) BackendException(org.janusgraph.diskstorage.BackendException) TException(org.apache.thrift.TException) IPartitioner(org.apache.cassandra.dht.IPartitioner)

Example 7 with CTConnection

use of org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection 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 8 with CTConnection

use of org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection 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 9 with CTConnection

use of org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection in project janusgraph by JanusGraph.

the class CassandraThriftStoreManager method clearStorage.

/**
 * Connect to Cassandra via Thrift on the specified host and port and attempt to truncate the named keyspace.
 * <p/>
 * This is a utility method intended mainly for testing. It is
 * equivalent to issuing 'truncate <cf>' for each of the column families in keyspace using
 * the cassandra-cli tool.
 * <p/>
 * Using truncate is better for a number of reasons, most significantly because it doesn't
 * involve any schema modifications which can take time to propagate across the cluster such
 * leaves nodes in the inconsistent state and could result in read/write failures.
 * Any schema modifications are discouraged until there is no traffic to Keyspace or ColumnFamilies.
 *
 * @throws org.janusgraph.diskstorage.BackendException if any checked Thrift or UnknownHostException is thrown in the body of this method
 */
public void clearStorage() throws BackendException {
    openStores.clear();
    // "log prefix"
    final String lp = "ClearStorage: ";
    /*
         * log4j is capable of automatically writing the name of a method that
         * generated a log message, but the docs warn that "generating caller
         * location information is extremely slow and should be avoided unless
         * execution speed is not an issue."
         */
    CTConnection conn = null;
    try {
        conn = pool.borrowObject(SYSTEM_KS);
        Cassandra.Client client = conn.getClient();
        KsDef ksDef;
        try {
            client.set_keyspace(keySpaceName);
            ksDef = client.describe_keyspace(keySpaceName);
        } catch (NotFoundException e) {
            log.debug(lp + "Keyspace {} does not exist, not attempting to truncate.", keySpaceName);
            return;
        } catch (InvalidRequestException e) {
            log.debug(lp + "InvalidRequestException when attempting to describe keyspace {}, not attempting to truncate.", keySpaceName);
            return;
        }
        if (null == ksDef) {
            log.debug(lp + "Received null KsDef for keyspace {}; not truncating its CFs", keySpaceName);
            return;
        }
        if (this.storageConfig.get(GraphDatabaseConfiguration.DROP_ON_CLEAR)) {
            client.system_drop_keyspace(keySpaceName);
            pool.clear();
        } else {
            final List<CfDef> columnFamilyDefinitions = ksDef.getCf_defs();
            if (null == columnFamilyDefinitions) {
                log.debug(lp + "Received empty CfDef list for keyspace {}; not truncating CFs", keySpaceName);
                return;
            }
            for (final CfDef cfDef : ksDef.getCf_defs()) {
                client.truncate(cfDef.name);
                log.info(lp + "Truncated CF {} in keyspace {}", cfDef.name, keySpaceName);
            }
        /*
                 * Clearing the CTConnectionPool is unnecessary. This method
                 * removes no keyspaces. All open Cassandra connections will
                 * remain valid.
                 */
        }
    } catch (Exception e) {
        throw new TemporaryBackendException(e);
    } finally {
        if (conn != null && conn.getClient() != null) {
            try {
                conn.getClient().set_keyspace(SYSTEM_KS);
            } catch (TException e) {
                log.warn("Failed to reset keyspace", e);
            }
        }
        pool.returnObjectUnsafe(SYSTEM_KS, conn);
    }
}
Also used : CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) TException(org.apache.thrift.TException) BackendException(org.janusgraph.diskstorage.BackendException) TException(org.apache.thrift.TException)

Example 10 with CTConnection

use of org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection in project janusgraph by JanusGraph.

the class CassandraThriftStoreManager method ensureKeyspaceExists.

private KsDef ensureKeyspaceExists(String keyspaceName) throws BackendException {
    CTConnection connection = null;
    try {
        connection = pool.borrowObject(SYSTEM_KS);
        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);
            log.debug("Found existing keyspace {}", keyspaceName);
        } catch (InvalidRequestException e) {
            // Keyspace didn't exist; create it
            log.debug("Creating keyspace {}...", keyspaceName);
            final KsDef ksdef = new KsDef().setName(keyspaceName).setCf_defs(// cannot be null but can be empty
            new LinkedList<>()).setStrategy_class(storageConfig.get(REPLICATION_STRATEGY)).setStrategy_options(strategyOptions);
            client.set_keyspace(SYSTEM_KS);
            try {
                client.system_add_keyspace(ksdef);
                retrySetKeyspace(keyspaceName, client);
                log.debug("Created keyspace {}", keyspaceName);
            } catch (InvalidRequestException ire) {
                log.error("system_add_keyspace failed for keyspace=" + keyspaceName, ire);
                throw ire;
            }
        }
        return client.describe_keyspace(keyspaceName);
    } catch (Exception e) {
        throw new TemporaryBackendException(e);
    } finally {
        pool.returnObjectUnsafe(SYSTEM_KS, connection);
    }
}
Also used : CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) LinkedList(java.util.LinkedList) BackendException(org.janusgraph.diskstorage.BackendException) TException(org.apache.thrift.TException)

Aggregations

CTConnection (org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection)10 TException (org.apache.thrift.TException)9 BackendException (org.janusgraph.diskstorage.BackendException)7 ImmutableMap (com.google.common.collect.ImmutableMap)2 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 org.apache.cassandra.thrift (org.apache.cassandra.thrift)2 Preconditions (com.google.common.base.Preconditions)1 Predicate (com.google.common.base.Predicate)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 java.util (java.util)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1