Search in sources :

Example 1 with CTConnection

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());
}
Also used : CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) TTransportException(org.apache.thrift.transport.TTransportException) TTransport(org.apache.thrift.transport.TTransport) CTConnectionFactory(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnectionFactory) Test(org.junit.Test)

Example 2 with CTConnection

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);
    }
}
Also used : CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) TException(org.apache.thrift.TException)

Example 3 with CTConnection

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);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) java.util(java.util) ConsistencyLevel(org.apache.cassandra.thrift.ConsistencyLevel) LoggerFactory(org.slf4j.LoggerFactory) CassandraHelper.convert(org.janusgraph.diskstorage.cassandra.utils.CassandraHelper.convert) ByteBuffer(java.nio.ByteBuffer) CassandraHelper.makeEntryIterator(org.janusgraph.diskstorage.cassandra.utils.CassandraHelper.makeEntryIterator) org.janusgraph.diskstorage.util(org.janusgraph.diskstorage.util) CassandraHelper.makeEntryList(org.janusgraph.diskstorage.cassandra.utils.CassandraHelper.makeEntryList) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) org.apache.cassandra.thrift(org.apache.cassandra.thrift) Nullable(javax.annotation.Nullable) org.janusgraph.diskstorage.keycolumnvalue(org.janusgraph.diskstorage.keycolumnvalue) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) CTConnectionPool(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnectionPool) TException(org.apache.thrift.TException) org.apache.cassandra.dht(org.apache.cassandra.dht) Collectors(java.util.stream.Collectors) Predicate(com.google.common.base.Predicate) CassandraTransaction.getTx(org.janusgraph.diskstorage.cassandra.CassandraTransaction.getTx) Preconditions(com.google.common.base.Preconditions) Pattern(java.util.regex.Pattern) org.janusgraph.diskstorage(org.janusgraph.diskstorage) ArrayUtils(org.apache.commons.lang.ArrayUtils) ByteBuffer(java.nio.ByteBuffer) TException(org.apache.thrift.TException) ConsistencyLevel(org.apache.cassandra.thrift.ConsistencyLevel) CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) CassandraHelper.makeEntryList(org.janusgraph.diskstorage.cassandra.utils.CassandraHelper.makeEntryList) ImmutableList(com.google.common.collect.ImmutableList)

Example 4 with CTConnection

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);
    }
}
Also used : CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) BackendException(org.janusgraph.diskstorage.BackendException) TException(org.apache.thrift.TException)

Example 5 with CTConnection

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);
    }
}
Also used : CTConnection(org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection) BackendException(org.janusgraph.diskstorage.BackendException) TException(org.apache.thrift.TException)

Aggregations

CTConnection (org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnection)10 TException (org.apache.thrift.TException)9 BackendException (org.janusgraph.diskstorage.BackendException)7 ImmutableMap (com.google.common.collect.ImmutableMap)2 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 org.apache.cassandra.thrift (org.apache.cassandra.thrift)2 Preconditions (com.google.common.base.Preconditions)1 Predicate (com.google.common.base.Predicate)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 java.util (java.util)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1