Search in sources :

Example 1 with ConnectionException

use of com.netflix.astyanax.connectionpool.exceptions.ConnectionException in project titan by thinkaurelius.

the class AstyanaxKeyColumnValueStore method getKeys.

@Override
public KeyIterator getKeys(KeyRangeQuery query, StoreTransaction txh) throws BackendException {
    // this query could only be done when byte-ordering partitioner is used
    // because Cassandra operates on tokens internally which means that even contiguous
    // range of keys (e.g. time slice) with random partitioner could produce disjoint set of tokens
    // returning ambiguous results to the user.
    Partitioner partitioner = storeManager.getPartitioner();
    if (partitioner != Partitioner.BYTEORDER)
        throw new PermanentBackendException("getKeys(KeyRangeQuery could only be used with byte-ordering partitioner.");
    ByteBuffer start = query.getKeyStart().asByteBuffer(), end = query.getKeyEnd().asByteBuffer();
    RowSliceQuery rowSlice = keyspace.prepareQuery(columnFamily).setConsistencyLevel(getTx(txh).getReadConsistencyLevel().getAstyanax()).withRetryPolicy(retryPolicy.duplicate()).getKeyRange(start, end, null, null, Integer.MAX_VALUE);
    // Astyanax is bad at builder pattern :(
    rowSlice.withColumnRange(query.getSliceStart().asByteBuffer(), query.getSliceEnd().asByteBuffer(), false, query.getLimit());
    // Omit final the query's keyend from the result, if present in result
    final Rows<ByteBuffer, ByteBuffer> r;
    try {
        r = ((OperationResult<Rows<ByteBuffer, ByteBuffer>>) rowSlice.execute()).getResult();
    } catch (ConnectionException e) {
        throw new TemporaryBackendException(e);
    }
    Iterator<Row<ByteBuffer, ByteBuffer>> i = Iterators.filter(r.iterator(), new KeySkipPredicate(query.getKeyEnd().asByteBuffer()));
    return new RowIterator(i, query);
}
Also used : RowSliceQuery(com.netflix.astyanax.query.RowSliceQuery) ByteBuffer(java.nio.ByteBuffer) Partitioner(com.thinkaurelius.titan.diskstorage.cassandra.AbstractCassandraStoreManager.Partitioner) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException)

Example 2 with ConnectionException

use of com.netflix.astyanax.connectionpool.exceptions.ConnectionException in project titan by thinkaurelius.

the class AstyanaxStoreManager method getCompressionOptions.

@Override
public Map<String, String> getCompressionOptions(String cf) throws BackendException {
    try {
        Keyspace k = keyspaceContext.getClient();
        KeyspaceDefinition kdef = k.describeKeyspace();
        if (null == kdef) {
            throw new PermanentBackendException("Keyspace " + k.getKeyspaceName() + " is undefined");
        }
        ColumnFamilyDefinition cfdef = kdef.getColumnFamily(cf);
        if (null == cfdef) {
            throw new PermanentBackendException("Column family " + cf + " is undefined");
        }
        return cfdef.getCompressionOptions();
    } catch (ConnectionException e) {
        throw new PermanentBackendException(e);
    }
}
Also used : KeyspaceDefinition(com.netflix.astyanax.ddl.KeyspaceDefinition) ColumnFamilyDefinition(com.netflix.astyanax.ddl.ColumnFamilyDefinition) PermanentBackendException(com.thinkaurelius.titan.diskstorage.PermanentBackendException) Keyspace(com.netflix.astyanax.Keyspace) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException)

Example 3 with ConnectionException

use of com.netflix.astyanax.connectionpool.exceptions.ConnectionException in project titan by thinkaurelius.

the class AstyanaxStoreManager method ensureKeyspaceExists.

private void ensureKeyspaceExists(Cluster cl) throws BackendException {
    KeyspaceDefinition ksDef;
    try {
        ksDef = cl.describeKeyspace(keySpaceName);
        if (null != ksDef && ksDef.getName().equals(keySpaceName)) {
            log.debug("Found keyspace {}", keySpaceName);
            return;
        }
    } catch (ConnectionException e) {
        log.debug("Failed to describe keyspace {}", keySpaceName);
    }
    log.debug("Creating keyspace {}...", keySpaceName);
    try {
        ksDef = cl.makeKeyspaceDefinition().setName(keySpaceName).setStrategyClass(storageConfig.get(REPLICATION_STRATEGY)).setStrategyOptions(strategyOptions);
        cl.addKeyspace(ksDef);
        log.debug("Created keyspace {}", keySpaceName);
    } catch (ConnectionException e) {
        log.debug("Failed to create keyspace {}", keySpaceName);
        throw new TemporaryBackendException(e);
    }
}
Also used : KeyspaceDefinition(com.netflix.astyanax.ddl.KeyspaceDefinition) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException)

Example 4 with ConnectionException

use of com.netflix.astyanax.connectionpool.exceptions.ConnectionException in project titan by thinkaurelius.

the class AstyanaxKeyColumnValueStore method getKeys.

@Override
public KeyIterator getKeys(@Nullable SliceQuery sliceQuery, StoreTransaction txh) throws BackendException {
    if (storeManager.getPartitioner() != Partitioner.RANDOM)
        throw new PermanentBackendException("This operation is only allowed when random partitioner (md5 or murmur3) is used.");
    AllRowsQuery allRowsQuery = keyspace.prepareQuery(columnFamily).getAllRows();
    if (sliceQuery != null) {
        allRowsQuery.withColumnRange(sliceQuery.getSliceStart().asByteBuffer(), sliceQuery.getSliceEnd().asByteBuffer(), false, sliceQuery.getLimit());
    }
    Rows<ByteBuffer, ByteBuffer> result;
    try {
        /* Note: we need to fetch columns for each row as well to remove "range ghosts" */
        OperationResult op = // pre-fetch that many rows at a time
        allRowsQuery.setRowLimit(storeManager.getPageSize()).setConcurrencyLevel(// one execution thread for fetching portion of rows
        1).setExceptionCallback(new ExceptionCallback() {

            private int retries = 0;

            @Override
            public boolean onException(ConnectionException e) {
                try {
                    // make 3 re-tries
                    return retries > 2;
                } finally {
                    retries++;
                }
            }
        }).execute();
        result = ((OperationResult<Rows<ByteBuffer, ByteBuffer>>) op).getResult();
    } catch (ConnectionException e) {
        throw new PermanentBackendException(e);
    }
    return new RowIterator(result.iterator(), sliceQuery);
}
Also used : AllRowsQuery(com.netflix.astyanax.query.AllRowsQuery) OperationResult(com.netflix.astyanax.connectionpool.OperationResult) ExceptionCallback(com.netflix.astyanax.ExceptionCallback) ByteBuffer(java.nio.ByteBuffer) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException)

Example 5 with ConnectionException

use of com.netflix.astyanax.connectionpool.exceptions.ConnectionException in project zuul by Netflix.

the class HystrixCassandraPut method run.

@Override
protected Void run() throws Exception {
    try {
        MutationBatch m = keyspace.prepareMutationBatch().setConsistencyLevel(ConsistencyLevel.CL_QUORUM);
        // Setting columns in a standard column
        ColumnListMutation<String> cm = m.withRow(columnFamily, rowKey);
        for (String key : attributes.keySet()) {
            Object o = attributes.get(key);
            if (o != null) {
                // unfortunately the 'putColumn' method does not nicely figure out what type the Object is so we need to do it manually
                if (o instanceof String) {
                    cm.putColumn(key, (String) o, ttlSeconds);
                } else if (o instanceof Boolean) {
                    cm.putColumn(key, (Boolean) o, ttlSeconds);
                } else if (o instanceof Integer) {
                    cm.putColumn(key, (Integer) o, ttlSeconds);
                } else if (o instanceof Long) {
                    cm.putColumn(key, (Long) o, ttlSeconds);
                } else if (o instanceof Double) {
                    cm.putColumn(key, (Double) o, ttlSeconds);
                } else if (o instanceof Date) {
                    cm.putColumn(key, (Date) o, ttlSeconds);
                } else if (o instanceof byte[]) {
                    cm.putColumn(key, (byte[]) o, ttlSeconds);
                } else if (o instanceof ByteBuffer) {
                    cm.putColumn(key, (ByteBuffer) o, ttlSeconds);
                } else {
                    throw new IllegalArgumentException("Unsupported object instance type: " + o.getClass().getSimpleName());
                }
            }
        }
        m.execute();
        return null;
    } catch (ConnectionException e) {
        throw e;
    }
}
Also used : MutationBatch(com.netflix.astyanax.MutationBatch) ByteBuffer(java.nio.ByteBuffer) Date(java.util.Date) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException)

Aggregations

ConnectionException (com.netflix.astyanax.connectionpool.exceptions.ConnectionException)9 ByteBuffer (java.nio.ByteBuffer)5 ColumnFamilyDefinition (com.netflix.astyanax.ddl.ColumnFamilyDefinition)3 KeyspaceDefinition (com.netflix.astyanax.ddl.KeyspaceDefinition)3 TemporaryBackendException (com.thinkaurelius.titan.diskstorage.TemporaryBackendException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Cluster (com.netflix.astyanax.Cluster)2 Keyspace (com.netflix.astyanax.Keyspace)2 MutationBatch (com.netflix.astyanax.MutationBatch)2 RowSliceQuery (com.netflix.astyanax.query.RowSliceQuery)2 PermanentBackendException (com.thinkaurelius.titan.diskstorage.PermanentBackendException)2 ExceptionCallback (com.netflix.astyanax.ExceptionCallback)1 OperationResult (com.netflix.astyanax.connectionpool.OperationResult)1 AllRowsQuery (com.netflix.astyanax.query.AllRowsQuery)1 Entry (com.thinkaurelius.titan.diskstorage.Entry)1 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)1 Partitioner (com.thinkaurelius.titan.diskstorage.cassandra.AbstractCassandraStoreManager.Partitioner)1 KCVMutation (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1