use of org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection in project janusgraph by JanusGraph.
the class ThriftConnectionTest method testConnectionDropped.
@Test
public void testConnectionDropped() throws Exception {
CTConnectionFactory connectionFactory = spy(factoryConfig.build());
CTConnection mockConnection = spy(connectionFactory.makeObject("janusgraph"));
when(mockConnection.getConfig()).thenReturn(factoryConfig);
when(mockConnection.isOpen()).thenReturn(true);
TTransport mockClient = spy(mockConnection.getTransport());
assertTrue(connectionFactory.validateObject(null, mockConnection));
when(mockClient.readAll(new byte[0], 0, 0)).thenThrow(new TTransportException("Broken Pipe"));
assertTrue(mockClient.isOpen());
}
use of org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection in project janusgraph by JanusGraph.
the class CassandraThriftKeyColumnValueStore method getRangeSlices.
private List<KeySlice> getRangeSlices(org.apache.cassandra.thrift.KeyRange keyRange, @Nullable SliceQuery sliceQuery) throws BackendException {
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<>(slices.size());
KeyIterationPredicate predicate = new KeyIterationPredicate();
for (KeySlice ks : slices) if (predicate.apply(ks))
result.add(ks);
return result;
} catch (Exception e) {
throw convertException(e);
} finally {
if (connection != null)
pool.returnObjectUnsafe(keyspace, connection);
}
}
use of org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection in project janusgraph by JanusGraph.
the class CassandraThriftKeyColumnValueStore method getNamesSlice.
public Map<StaticBuffer, EntryList> getNamesSlice(List<StaticBuffer> keys, SliceQuery query, StoreTransaction txh) throws BackendException {
ColumnParent parent = new ColumnParent(columnFamily);
/*
* Cassandra cannot handle columnStart = columnEnd.
* Cassandra's Thrift getSlice() throws InvalidRequestException
* if columnStart = columnEnd.
*/
if (query.getSliceStart().compareTo(query.getSliceEnd()) >= 0) {
// Check for invalid arguments where columnEnd < columnStart
if (query.getSliceEnd().compareTo(query.getSliceStart()) < 0) {
throw new PermanentBackendException("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 KCVSUtil.emptyResults(keys);
}
}
assert query.getSliceStart().compareTo(query.getSliceEnd()) < 0;
ConsistencyLevel consistency = getTx(txh).getReadConsistencyLevel().getThrift();
SlicePredicate predicate = new SlicePredicate();
SliceRange range = new SliceRange();
// Add one for potentially removed last column
range.setCount(query.getLimit() + (query.hasLimit() ? 1 : 0));
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();
final Map<ByteBuffer, List<ColumnOrSuperColumn>> rows = client.multiget_slice(convert(keys), 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
*/
return rows.entrySet().stream().collect(Collectors.toMap(e -> StaticArrayBuffer.of(e.getKey()), e -> makeEntryList(e.getValue(), entryGetter, query.getSliceEnd(), query.getLimit())));
} catch (Exception e) {
throw convertException(e);
} finally {
pool.returnObjectUnsafe(keyspace, conn);
}
}
use of org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection in project janusgraph by JanusGraph.
the class CassandraThriftStoreManager method ensureColumnFamilyExists.
private void ensureColumnFamilyExists(String ksName, String cfName) throws BackendException {
CTConnection conn = null;
try {
KsDef keyspaceDef = ensureKeyspaceExists(ksName);
conn = pool.borrowObject(ksName);
Cassandra.Client client = conn.getClient();
log.debug("Looking up metadata on keyspace {}...", ksName);
boolean foundColumnFamily = false;
for (CfDef cfDef : keyspaceDef.getCf_defs()) {
String curCfName = cfDef.getName();
if (curCfName.equals(cfName))
foundColumnFamily = true;
}
if (!foundColumnFamily) {
createColumnFamily(client, ksName, cfName, "org.apache.cassandra.db.marshal.BytesType");
} else {
log.debug("Keyspace {} and ColumnFamily {} were found.", ksName, cfName);
}
} catch (SchemaDisagreementException e) {
throw new TemporaryBackendException(e);
} catch (Exception e) {
throw new PermanentBackendException(e);
} finally {
pool.returnObjectUnsafe(ksName, conn);
}
}
use of org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection in project janusgraph by JanusGraph.
the class CassandraThriftStoreManager method getCompressionOptions.
@Override
public Map<String, String> getCompressionOptions(String cf) throws BackendException {
CTConnection conn = null;
Map<String, String> result = null;
try {
conn = pool.borrowObject(keySpaceName);
Cassandra.Client client = conn.getClient();
KsDef ksDef = client.describe_keyspace(keySpaceName);
for (CfDef cfDef : ksDef.getCf_defs()) {
if (null != cfDef && cfDef.getName().equals(cf)) {
result = cfDef.getCompression_options();
break;
}
}
return result;
} catch (InvalidRequestException e) {
log.debug("Keyspace {} does not exist", keySpaceName);
return null;
} catch (Exception e) {
throw new TemporaryBackendException(e);
} finally {
pool.returnObjectUnsafe(keySpaceName, conn);
}
}
Aggregations