use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.
the class ConsistentKeyLockerTest method testWriteLockDiesOnPermanentStorageException.
/**
* Test that the first {@link PermanentStorageException} thrown by the
* locker's store causes it to attempt to delete outstanding lock writes and
* then emit the exception without retrying.
*
* @throws StorageException shouldn't happen
*/
@Test
public void testWriteLockDiesOnPermanentStorageException() throws StorageException {
PermanentStorageException errOnFire = new PermanentStorageException("Storage cluster is on fire");
expect(lockState.has(defaultTx, defaultLockID)).andReturn(false);
recordSuccessfulLocalLock();
StaticBuffer lockCol = recordExceptionLockWrite(1, TimeUnit.NANOSECONDS, null, errOnFire);
recordSuccessfulLockDelete(1, TimeUnit.NANOSECONDS, lockCol);
recordSuccessfulLocalUnlock();
ctrl.replay();
StorageException expected = null;
try {
// SUT
locker.writeLock(defaultLockID, defaultTx);
} catch (PermanentLockingException e) {
expected = e;
}
assertNotNull(expected);
assertEquals(errOnFire, expected.getCause());
}
use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.
the class CassandraEmbeddedKeyColumnValueStore method getKeySlice.
private List<Row> getKeySlice(Token start, Token end, @Nullable SliceQuery sliceQuery, int pageSize) throws StorageException {
IPartitioner<?> partitioner = StorageService.getPartitioner();
SliceRange columnSlice = new SliceRange();
if (sliceQuery == null) {
columnSlice.setStart(ArrayUtils.EMPTY_BYTE_ARRAY).setFinish(ArrayUtils.EMPTY_BYTE_ARRAY).setCount(5);
} else {
columnSlice.setStart(sliceQuery.getSliceStart().asByteBuffer()).setFinish(sliceQuery.getSliceEnd().asByteBuffer()).setCount(sliceQuery.hasLimit() ? sliceQuery.getLimit() : Integer.MAX_VALUE);
}
/* Note: we need to fetch columns for each row as well to remove "range ghosts" */
SlicePredicate predicate = new SlicePredicate().setSlice_range(columnSlice);
RowPosition startPosition = start.minKeyBound(partitioner);
RowPosition endPosition = end.minKeyBound(partitioner);
List<Row> rows;
try {
IDiskAtomFilter filter = ThriftValidation.asIFilter(predicate, Schema.instance.getComparator(keyspace, columnFamily));
rows = StorageProxy.getRangeSlice(new RangeSliceCommand(keyspace, new ColumnParent(columnFamily), filter, new Bounds<RowPosition>(startPosition, endPosition), null, pageSize), ConsistencyLevel.QUORUM);
} catch (Exception e) {
throw new PermanentStorageException(e);
}
return rows;
}
use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.
the class CassandraThriftKeyColumnValueStore method containsKey.
@Override
public boolean containsKey(StaticBuffer key, StoreTransaction txh) throws StorageException {
ColumnParent parent = new ColumnParent(columnFamily);
ConsistencyLevel consistency = getTx(txh).getReadConsistencyLevel().getThriftConsistency();
SlicePredicate predicate = new SlicePredicate();
SliceRange range = new SliceRange();
range.setCount(1);
byte[] empty = new byte[0];
range.setStart(empty);
range.setFinish(empty);
predicate.setSlice_range(range);
CTConnection conn = null;
try {
conn = pool.borrowObject(keyspace);
Cassandra.Client client = conn.getClient();
List<?> result = client.get_slice(key.asByteBuffer(), parent, predicate, consistency);
return 0 < result.size();
} catch (Exception e) {
throw convertException(e);
} finally {
pool.returnObjectUnsafe(keyspace, conn);
}
}
use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.
the class CassandraThriftKeyColumnValueStore method getNamesSlice.
public Map<ByteBuffer, List<Entry>> getNamesSlice(List<StaticBuffer> keys, SliceQuery query, StoreTransaction txh) throws StorageException {
Preconditions.checkArgument(query.getLimit() >= 0);
if (0 == query.getLimit())
return Collections.emptyMap();
ColumnParent parent = new ColumnParent(columnFamily);
/*
* Cassandra cannot handle columnStart = columnEnd.
* Cassandra's Thrift getSlice() throws InvalidRequestException
* if columnStart = columnEnd.
*/
if (ByteBufferUtil.compare(query.getSliceStart(), query.getSliceEnd()) >= 0) {
// Check for invalid arguments where columnEnd < columnStart
if (ByteBufferUtil.isSmallerThan(query.getSliceEnd(), query.getSliceStart())) {
throw new PermanentStorageException("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 Collections.emptyMap();
}
}
// true: columnStart < columnEnd
ConsistencyLevel consistency = getTx(txh).getReadConsistencyLevel().getThriftConsistency();
SlicePredicate predicate = new SlicePredicate();
SliceRange range = new SliceRange();
range.setCount(query.getLimit());
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();
List<ByteBuffer> requestKeys = new ArrayList<ByteBuffer>(keys.size());
{
for (StaticBuffer key : keys) {
requestKeys.add(key.asByteBuffer());
}
}
Map<ByteBuffer, List<ColumnOrSuperColumn>> rows = client.multiget_slice(requestKeys, 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<ByteBuffer, List<Entry>> results = new HashMap<ByteBuffer, List<Entry>>();
ByteBuffer sliceEndBB = query.getSliceEnd().asByteBuffer();
for (ByteBuffer key : rows.keySet()) {
results.put(key, excludeLastColumn(rows.get(key), sliceEndBB));
}
return results;
} catch (Exception e) {
throw convertException(e);
} finally {
pool.returnObjectUnsafe(keyspace, conn);
}
}
use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.
the class CassandraThriftStoreManager method ensureKeyspaceExists.
private KsDef ensureKeyspaceExists(String keyspaceName) throws NotFoundException, InvalidRequestException, TException, SchemaDisagreementException, StorageException {
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);
KsDef ksdef = new KsDef().setName(keyspaceName).setCf_defs(// cannot be null but can be empty
new LinkedList<CfDef>()).setStrategy_class("org.apache.cassandra.locator.SimpleStrategy").setStrategy_options(ImmutableMap.of("replication_factor", String.valueOf(replicationFactor)));
client.set_keyspace(SYSTEM_KS);
try {
client.system_add_keyspace(ksdef);
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 TemporaryStorageException(e);
} finally {
pool.returnObjectUnsafe(SYSTEM_KS, connection);
}
}
Aggregations