use of com.thinkaurelius.titan.diskstorage.PermanentStorageException in project titan by thinkaurelius.
the class CassandraEmbeddedKeyColumnValueStore method getInternal.
static ByteBuffer getInternal(String keyspace, String columnFamily, ByteBuffer key, ByteBuffer column, org.apache.cassandra.db.ConsistencyLevel cl) throws StorageException {
QueryPath slicePath = new QueryPath(columnFamily);
SliceByNamesReadCommand namesCmd = new SliceByNamesReadCommand(keyspace, key.duplicate(), slicePath, Arrays.asList(column.duplicate()));
List<Row> rows = read(namesCmd, cl);
if (null == rows || 0 == rows.size())
return null;
if (1 < rows.size())
throw new PermanentStorageException("Received " + rows.size() + " rows from a single-key-column cassandra read");
assert 1 == rows.size();
Row r = rows.get(0);
if (null == r) {
log.warn("Null Row object retrieved from Cassandra StorageProxy");
return null;
}
ColumnFamily cf = r.cf;
if (null == cf)
return null;
if (cf.isMarkedForDelete())
return null;
IColumn c = cf.getColumn(column.duplicate());
if (null == c)
return null;
// These came up during testing
if (c.isMarkedForDelete())
return null;
return org.apache.cassandra.utils.ByteBufferUtil.clone(c.value());
}
use of com.thinkaurelius.titan.diskstorage.PermanentStorageException 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.PermanentStorageException 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.PermanentStorageException in project titan by thinkaurelius.
the class CassandraThriftStoreManager method createColumnFamily.
private void createColumnFamily(Cassandra.Client client, String ksName, String cfName, String comparator) throws StorageException {
CfDef createColumnFamily = new CfDef();
createColumnFamily.setName(cfName);
createColumnFamily.setKeyspace(ksName);
createColumnFamily.setComparator_type(comparator);
ImmutableMap.Builder<String, String> compressionOptions = new ImmutableMap.Builder<String, String>();
if (compressionEnabled) {
compressionOptions.put("sstable_compression", compressionClass).put("chunk_length_kb", Integer.toString(compressionChunkSizeKB));
}
createColumnFamily.setCompression_options(compressionOptions.build());
// Hard-coded caching settings
if (cfName.startsWith(Backend.EDGESTORE_NAME)) {
createColumnFamily.setCaching("keys_only");
} else if (cfName.startsWith(Backend.VERTEXINDEX_STORE_NAME)) {
createColumnFamily.setCaching("rows_only");
}
log.debug("Adding column family {} to keyspace {}...", cfName, ksName);
try {
client.system_add_column_family(createColumnFamily);
} catch (SchemaDisagreementException e) {
throw new TemporaryStorageException("Error in setting up column family", e);
} catch (Exception e) {
throw new PermanentStorageException(e);
}
log.debug("Added column family {} to keyspace {}.", cfName, ksName);
}
use of com.thinkaurelius.titan.diskstorage.PermanentStorageException in project titan by thinkaurelius.
the class CTConnectionFactory method waitForClusterSize.
public static void waitForClusterSize(Cassandra.Client thriftClient, int minSize) throws InterruptedException, StorageException {
log.debug("Checking Cassandra cluster size" + " (want at least {} nodes)...", minSize);
Map<String, List<String>> versions = null;
final long STARTUP_WAIT_MAX = 10000L;
final long STARTUP_WAIT_INCREMENT = 100L;
long start = System.currentTimeMillis();
long lastTry = 0;
long limit = start + STARTUP_WAIT_MAX;
long minSleep = STARTUP_WAIT_INCREMENT;
Integer curSize = null;
while (limit - System.currentTimeMillis() >= 0) {
// Block for a little while if we're looping too fast
long sinceLast = System.currentTimeMillis() - lastTry;
long willSleep = minSleep - sinceLast;
if (0 < willSleep) {
// log.debug("Cassandra cluster size={} " +
// "(want {}); rechecking in {} ms",
// new Object[]{ curSize, minSize, willSleep });
Thread.sleep(willSleep);
}
// Issue thrift query
try {
lastTry = System.currentTimeMillis();
versions = thriftClient.describe_schema_versions();
if (1 != versions.size())
continue;
String version = Iterators.getOnlyElement(versions.keySet().iterator());
curSize = versions.get(version).size();
if (curSize >= minSize) {
log.debug("Cassandra cluster verified at size {} (schema version {}) in about {} ms", new Object[] { curSize, version, System.currentTimeMillis() - start });
return;
}
} catch (Exception e) {
throw new PermanentStorageException("Failed to fetch Cassandra Thrift schema versions: " + ((e instanceof InvalidRequestException) ? ((InvalidRequestException) e).getWhy() : e.getMessage()));
}
}
throw new PermanentStorageException("Could not verify Cassandra cluster size");
}
Aggregations