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);
}
}
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);
}
}
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);
}
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);
}
}
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);
}
Aggregations