Search in sources :

Example 1 with CTConnection

use of com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection in project titan by thinkaurelius.

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 com.thinkaurelius.titan.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;
        }
        List<CfDef> cfDefs = ksDef.getCf_defs();
        if (null == cfDefs) {
            log.debug(lp + "Received empty CfDef list for keyspace {}; not truncating CFs", keySpaceName);
            return;
        }
        for (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 (InvalidRequestException e) {
                log.warn("Failed to reset keyspace", e);
            } catch (TException e) {
                log.warn("Failed to reset keyspace", e);
            }
        }
        pool.returnObjectUnsafe(SYSTEM_KS, conn);
    }
}
Also used : CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) TException(org.apache.thrift.TException) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) TException(org.apache.thrift.TException)

Example 2 with CTConnection

use of com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection in project titan by thinkaurelius.

the class CassandraThriftStoreManager method ensureColumnFamilyExists.

private void ensureColumnFamilyExists(String ksName, String cfName, String comparator) throws BackendException {
    CTConnection conn = null;
    try {
        KsDef keyspaceDef = ensureKeyspaceExists(ksName);
        conn = pool.borrowObject(ksName);
        Cassandra.Client client = conn.getClient();
        log.debug("Looking up metadata on keyspace {}...", ksName);
        boolean foundColumnFamily = false;
        for (CfDef cfDef : keyspaceDef.getCf_defs()) {
            String curCfName = cfDef.getName();
            if (curCfName.equals(cfName))
                foundColumnFamily = true;
        }
        if (!foundColumnFamily) {
            createColumnFamily(client, ksName, cfName, comparator);
        } else {
            log.debug("Keyspace {} and ColumnFamily {} were found.", ksName, cfName);
        }
    } catch (SchemaDisagreementException e) {
        throw new TemporaryBackendException(e);
    } catch (Exception e) {
        throw new PermanentBackendException(e);
    } finally {
        pool.returnObjectUnsafe(ksName, conn);
    }
}
Also used : CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) TException(org.apache.thrift.TException)

Example 3 with CTConnection

use of com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection in project titan by thinkaurelius.

the class CassandraThriftKeyColumnValueStore method getRangeSlices.

private List<KeySlice> getRangeSlices(org.apache.cassandra.thrift.KeyRange keyRange, @Nullable SliceQuery sliceQuery) throws BackendException {
    SliceRange sliceRange = new SliceRange();
    if (sliceQuery == null) {
        sliceRange.setStart(ArrayUtils.EMPTY_BYTE_ARRAY).setFinish(ArrayUtils.EMPTY_BYTE_ARRAY).setCount(5);
    } else {
        sliceRange.setStart(sliceQuery.getSliceStart().asByteBuffer()).setFinish(sliceQuery.getSliceEnd().asByteBuffer()).setCount((sliceQuery.hasLimit()) ? sliceQuery.getLimit() : Integer.MAX_VALUE);
    }
    CTConnection connection = null;
    try {
        connection = pool.borrowObject(keyspace);
        List<KeySlice> slices = connection.getClient().get_range_slices(new ColumnParent(columnFamily), new SlicePredicate().setSlice_range(sliceRange), keyRange, ConsistencyLevel.QUORUM);
        for (KeySlice s : slices) {
            logger.debug("Key {}", ByteBufferUtil.toString(s.key, "-"));
        }
        /* Note: we need to fetch columns for each row as well to remove "range ghosts" */
        List<KeySlice> result = new ArrayList<>(slices.size());
        KeyIterationPredicate pred = new KeyIterationPredicate();
        for (KeySlice ks : slices) if (pred.apply(ks))
            result.add(ks);
        return result;
    } catch (Exception e) {
        throw convertException(e);
    } finally {
        if (connection != null)
            pool.returnObjectUnsafe(keyspace, connection);
    }
}
Also used : CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) TException(org.apache.thrift.TException)

Example 4 with CTConnection

use of com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection in project titan by thinkaurelius.

the class CassandraThriftKeyColumnValueStore method getNamesSlice.

public Map<StaticBuffer, EntryList> getNamesSlice(List<StaticBuffer> keys, SliceQuery query, StoreTransaction txh) throws BackendException {
    ColumnParent parent = new ColumnParent(columnFamily);
    /*
         * Cassandra cannot handle columnStart = columnEnd.
		 * Cassandra's Thrift getSlice() throws InvalidRequestException
		 * if columnStart = columnEnd.
		 */
    if (query.getSliceStart().compareTo(query.getSliceEnd()) >= 0) {
        // Check for invalid arguments where columnEnd < columnStart
        if (query.getSliceEnd().compareTo(query.getSliceStart()) < 0) {
            throw new PermanentBackendException("columnStart=" + query.getSliceStart() + " is greater than columnEnd=" + query.getSliceEnd() + ". " + "columnStart must be less than or equal to columnEnd");
        }
        if (0 != query.getSliceStart().length() && 0 != query.getSliceEnd().length()) {
            logger.debug("Return empty list due to columnEnd==columnStart and neither empty");
            return KCVSUtil.emptyResults(keys);
        }
    }
    assert query.getSliceStart().compareTo(query.getSliceEnd()) < 0;
    ConsistencyLevel consistency = getTx(txh).getReadConsistencyLevel().getThrift();
    SlicePredicate predicate = new SlicePredicate();
    SliceRange range = new SliceRange();
    //Add one for potentially removed last column
    range.setCount(query.getLimit() + (query.hasLimit() ? 1 : 0));
    range.setStart(query.getSliceStart().asByteBuffer());
    range.setFinish(query.getSliceEnd().asByteBuffer());
    predicate.setSlice_range(range);
    CTConnection conn = null;
    try {
        conn = pool.borrowObject(keyspace);
        Cassandra.Client client = conn.getClient();
        Map<ByteBuffer, List<ColumnOrSuperColumn>> rows = client.multiget_slice(CassandraHelper.convert(keys), parent, predicate, consistency);
        /*
			 * The final size of the "result" List may be at most rows.size().
			 * However, "result" could also be up to two elements smaller than
			 * rows.size(), depending on startInclusive and endInclusive
			 */
        Map<StaticBuffer, EntryList> results = new HashMap<StaticBuffer, EntryList>();
        for (ByteBuffer key : rows.keySet()) {
            results.put(StaticArrayBuffer.of(key), CassandraHelper.makeEntryList(rows.get(key), entryGetter, query.getSliceEnd(), query.getLimit()));
        }
        return results;
    } catch (Exception e) {
        throw convertException(e);
    } finally {
        pool.returnObjectUnsafe(keyspace, conn);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) TException(org.apache.thrift.TException) ConsistencyLevel(org.apache.cassandra.thrift.ConsistencyLevel) CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) ImmutableList(com.google.common.collect.ImmutableList)

Example 5 with CTConnection

use of com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection in project titan by thinkaurelius.

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);
        List<TokenRange> ranges = conn.getClient().describe_ring(keySpaceName);
        List<KeyRange> keyRanges = new ArrayList<KeyRange>(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(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) AbstractByteOrderedPartitioner(org.apache.cassandra.dht.AbstractByteOrderedPartitioner) KeyRange(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange) ArrayList(java.util.ArrayList) Token(org.apache.cassandra.dht.Token) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) TException(org.apache.thrift.TException) IPartitioner(org.apache.cassandra.dht.IPartitioner)

Aggregations

CTConnection (com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection)8 TException (org.apache.thrift.TException)8 BackendException (com.thinkaurelius.titan.diskstorage.BackendException)6 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 KCVMutation (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation)1 KeyRange (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AbstractByteOrderedPartitioner (org.apache.cassandra.dht.AbstractByteOrderedPartitioner)1 IPartitioner (org.apache.cassandra.dht.IPartitioner)1 Token (org.apache.cassandra.dht.Token)1 org.apache.cassandra.thrift (org.apache.cassandra.thrift)1 ConsistencyLevel (org.apache.cassandra.thrift.ConsistencyLevel)1