Search in sources :

Example 6 with StorageException

use of com.thinkaurelius.titan.diskstorage.StorageException 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 StorageException if any checked Thrift or UnknownHostException is thrown in the body of this method
 */
public void clearStorage() throws StorageException {
    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 TemporaryStorageException(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) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) Cassandra(org.apache.cassandra.thrift.Cassandra) NotFoundException(org.apache.cassandra.thrift.NotFoundException) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException) KsDef(org.apache.cassandra.thrift.KsDef) CfDef(org.apache.cassandra.thrift.CfDef) NotFoundException(org.apache.cassandra.thrift.NotFoundException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) TException(org.apache.thrift.TException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) SchemaDisagreementException(org.apache.cassandra.thrift.SchemaDisagreementException)

Example 7 with StorageException

use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.

the class CassandraThriftKeyColumnValueStore method getRangeSlices.

private List<KeySlice> getRangeSlices(KeyRange keyRange, @Nullable SliceQuery sliceQuery) throws StorageException {
    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<KeySlice>(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) SliceRange(org.apache.cassandra.thrift.SliceRange) ColumnParent(org.apache.cassandra.thrift.ColumnParent) ArrayList(java.util.ArrayList) SlicePredicate(org.apache.cassandra.thrift.SlicePredicate) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) TimedOutException(org.apache.cassandra.thrift.TimedOutException) NoSuchElementException(java.util.NoSuchElementException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) UnavailableException(org.apache.cassandra.thrift.UnavailableException) TException(org.apache.thrift.TException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) KeySlice(org.apache.cassandra.thrift.KeySlice)

Example 8 with StorageException

use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.

the class KCVSConfiguration method setConfigurationProperty.

/**
 * Sets a configuration property for this StoreManager.
 *
 * @param key   Key identifying the configuration property
 * @param value Value to be stored for the key
 * @throws StorageException
 */
public void setConfigurationProperty(final String key, final String value) throws StorageException {
    StaticBuffer column = string2StaticBuffer(key);
    StaticBuffer val = string2StaticBuffer(value);
    final List<Entry> additions = new ArrayList<Entry>(1);
    additions.add(new StaticBufferEntry(column, val));
    BackendOperation.execute(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            StoreTransaction txh = null;
            try {
                txh = manager.beginTransaction(new StoreTxConfig(ConsistencyLevel.KEY_CONSISTENT));
                store.mutate(rowKey, additions, NO_DELETIONS, txh);
                return true;
            } finally {
                if (txh != null)
                    txh.commit();
            }
        }

        @Override
        public String toString() {
            return "setConfiguration";
        }
    }, maxOperationWaitTime);
}
Also used : ArrayList(java.util.ArrayList) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) StorageException(com.thinkaurelius.titan.diskstorage.StorageException)

Example 9 with StorageException

use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.

the class CachedKeyColumnValueStore method getSlice.

@Override
public List<Entry> getSlice(final KeySliceQuery query, final StoreTransaction txh) throws StorageException {
    if (query.isStatic()) {
        try {
            if (log.isDebugEnabled())
                log.debug("Cache Retrieval on " + store.getName() + ". Attempts: {} | Misses: {}", CACHE_RETRIEVAL.getCount(), CACHE_MISS.getCount());
            CACHE_RETRIEVAL.inc();
            List<Entry> result = cache.get(query, new Callable<List<Entry>>() {

                @Override
                public List<Entry> call() throws StorageException {
                    CACHE_MISS.inc();
                    return store.getSlice(query, txh);
                }
            });
            if (result.isEmpty())
                cache.invalidate(query);
            return result;
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();
            if (cause != null && cause instanceof StorageException) {
                throw (StorageException) cause;
            } else {
                throw new TemporaryStorageException("Exception while accessing cache", e);
            }
        }
    } else {
        return store.getSlice(query, txh);
    }
}
Also used : TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException)

Example 10 with StorageException

use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.

the class BackendOperation method execute.

public static final <V> V execute(Callable<V> exe, long maxTimeMS) throws TitanException {
    long waitTime = BASE_REATTEMPT_TIME_MS;
    long maxTime = System.currentTimeMillis() + maxTimeMS;
    StorageException lastException = null;
    do {
        try {
            return exe.call();
        } catch (StorageException e) {
            if (e instanceof TemporaryStorageException)
                lastException = e;
            else
                // Its permanent
                throw new TitanException("Permanent exception during backend operation", e);
        } catch (Throwable e) {
            throw new TitanException("Unexpected exception during backend operation", e);
        }
        // Wait and retry
        Preconditions.checkNotNull(lastException);
        if (System.currentTimeMillis() + waitTime < maxTime) {
            log.info("Temporary storage exception during backend operation. Attempting backoff retry", exe.toString(), lastException);
            try {
                Thread.sleep(waitTime);
            } catch (InterruptedException r) {
                throw new TitanException("Interrupted while waiting to retry failed storage operation", r);
            }
        }
        // Exponential backoff
        waitTime *= 2;
    } while (System.currentTimeMillis() < maxTime);
    throw new TitanException("Could not successfully complete backend operation due to repeated temporary exceptions after " + maxTimeMS + " ms", lastException);
}
Also used : TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) TitanException(com.thinkaurelius.titan.core.TitanException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException)

Aggregations

StorageException (com.thinkaurelius.titan.diskstorage.StorageException)29 TemporaryStorageException (com.thinkaurelius.titan.diskstorage.TemporaryStorageException)22 PermanentStorageException (com.thinkaurelius.titan.diskstorage.PermanentStorageException)20 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)9 InvalidRequestException (org.apache.cassandra.thrift.InvalidRequestException)9 TException (org.apache.thrift.TException)9 CTConnection (com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection)8 ArrayList (java.util.ArrayList)7 Cassandra (org.apache.cassandra.thrift.Cassandra)7 TitanException (com.thinkaurelius.titan.core.TitanException)6 NotFoundException (org.apache.cassandra.thrift.NotFoundException)6 SchemaDisagreementException (org.apache.cassandra.thrift.SchemaDisagreementException)6 SlicePredicate (org.apache.cassandra.thrift.SlicePredicate)5 IOException (java.io.IOException)4 List (java.util.List)4 NoSuchElementException (java.util.NoSuchElementException)4 CfDef (org.apache.cassandra.thrift.CfDef)4 ColumnParent (org.apache.cassandra.thrift.ColumnParent)4 KsDef (org.apache.cassandra.thrift.KsDef)4 SliceRange (org.apache.cassandra.thrift.SliceRange)4