Search in sources :

Example 1 with Selector

use of org.scale7.cassandra.pelops.Selector in project scale7-pelops by s7.

the class CommonsBackedPoolIntegrationTest method testGetConnectionMultiThreaded.

/**
     * Test that the pool operates as expected when multiple threads are hitting it.
     */
@Test
public void testGetConnectionMultiThreaded() {
    CommonsBackedPool.Policy config = new CommonsBackedPool.Policy();
    // disable the background thread
    config.setTimeBetweenScheduledMaintenanceTaskRunsMillis(-1);
    // one less than the number of worker threads
    config.setMaxActivePerNode(4);
    final CommonsBackedPool pool = configurePool(config);
    try {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        int taskCount = 1000;
        for (int i = 0; i < taskCount; i++) {
            executorService.submit(new Runnable() {

                @Override
                public void run() {
                    Selector selector = pool.createSelector();
                    selector.getColumnCount(COLUMN_FAMILY, "a", ConsistencyLevel.ONE);
                }
            });
        }
        executorService.shutdown();
        try {
            executorService.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            fail("Failed to run all submitted tasks within a minute");
        }
        PooledNode node = pool.getPooledNode("localhost");
        assertEquals("Task count did not match connections borrowed", taskCount, pool.getStatistics().getConnectionsBorrowedTotal());
        assertEquals("Task count did not match connections borrowed on node", taskCount, node.getConnectionsBorrowedTotal());
        assertEquals("Task count did not match connections released", taskCount, pool.getStatistics().getConnectionsReleasedTotal());
        assertEquals("Task count did not match connections released on node", taskCount, node.getConnectionsReleasedTotal());
        assertEquals("Connections created did not match max active", config.getMaxActivePerNode(), pool.getStatistics().getConnectionsCreated());
        assertEquals("Connections created did not match max active on node", config.getMaxActivePerNode(), node.getConnectionsCreated());
    } finally {
        pool.shutdown();
    }
}
Also used : OperandPolicy(org.scale7.cassandra.pelops.OperandPolicy) ExecutorService(java.util.concurrent.ExecutorService) Selector(org.scale7.cassandra.pelops.Selector) AbstractIntegrationTest(org.scale7.cassandra.pelops.support.AbstractIntegrationTest) Test(org.junit.Test)

Example 2 with Selector

use of org.scale7.cassandra.pelops.Selector in project scale7-pelops by s7.

the class Selector method getSuperColumnsFromRowsUtf8Keys.

/**
     * Retrieve super columns from a set of rows.
     * @param columnFamily                  The column family containing the rows
     * @param rowKeys                        The keys of the rows containing the super columns
     * @param colPredicate                   The super column selector predicate
     * @param cLevel                         The Cassandra consistency level with which to perform the operation
     * @return                               A map from row keys to the matching lists of super columns
     * @throws PelopsException if an error occurs
     */
public LinkedHashMap<String, List<SuperColumn>> getSuperColumnsFromRowsUtf8Keys(String columnFamily, List<String> rowKeys, SlicePredicate colPredicate, ConsistencyLevel cLevel) throws PelopsException {
    ColumnParent columnParent = newColumnParent(columnFamily);
    List<ByteBuffer> keys = Bytes.transformUTF8ToList(validateRowKeysUtf8(rowKeys));
    return transformUtf8(getColumnOrSuperColumnsFromRows(columnParent, keys, colPredicate, cLevel), rowKeys, keys, SUPER_COLUMN);
}
Also used : ColumnParent(org.apache.cassandra.thrift.ColumnParent) ByteBuffer(java.nio.ByteBuffer) Bytes.fromByteBuffer(org.scale7.cassandra.pelops.Bytes.fromByteBuffer)

Example 3 with Selector

use of org.scale7.cassandra.pelops.Selector in project scale7-pelops by s7.

the class Selector method getSuperColumnsFromRows.

/**
     * Retrieve super columns from a set of rows.
     * @param columnFamily                  The column family containing the rows
     * @param rowKeys                        The keys of the rows containing the super columns
     * @param colPredicate                   The super column selector predicate
     * @param cLevel                         The Cassandra consistency level with which to perform the operation
     * @return                               A map from row keys to the matching lists of super columns
     * @throws PelopsException if an error occurs
     */
public LinkedHashMap<Bytes, List<SuperColumn>> getSuperColumnsFromRows(String columnFamily, List<Bytes> rowKeys, SlicePredicate colPredicate, ConsistencyLevel cLevel) throws PelopsException {
    ColumnParent columnParent = newColumnParent(columnFamily);
    List<ByteBuffer> keys = Bytes.transformBytesToList(validateRowKeys(rowKeys));
    return transform(getColumnOrSuperColumnsFromRows(columnParent, keys, colPredicate, cLevel), rowKeys, SUPER_COLUMN);
}
Also used : ColumnParent(org.apache.cassandra.thrift.ColumnParent) ByteBuffer(java.nio.ByteBuffer) Bytes.fromByteBuffer(org.scale7.cassandra.pelops.Bytes.fromByteBuffer)

Example 4 with Selector

use of org.scale7.cassandra.pelops.Selector in project scale7-pelops by s7.

the class Pelops method addPool.

/**
	 * Add a new Thrift connection pool for a specific cluster and keyspace. The name given to the pool is later used
	 * when creating operands such as <code>Mutator</code> and <code>Selector</code>.
	 * @param poolName				A name used to reference the pool e.g. "MainDatabase" or "LucandraIndexes"
	 * @param cluster				The Cassandra cluster that network connections will be made to
	 * @param keyspace				The keyspace in the Cassandra cluster against which pool operations will apply
	 */
public static void addPool(String poolName, Cluster cluster, String keyspace) {
    IThriftPool pool = new CommonsBackedPool(cluster, keyspace);
    addPool(poolName, pool);
}
Also used : IThriftPool(org.scale7.cassandra.pelops.pool.IThriftPool) CommonsBackedPool(org.scale7.cassandra.pelops.pool.CommonsBackedPool)

Example 5 with Selector

use of org.scale7.cassandra.pelops.Selector in project scale7-pelops by s7.

the class Pelops method addPool.

/**
	 * Add a new Thrift connection pool for a specific cluster and keyspace. The name given to the pool is later used
	 * when creating operands such as <code>Mutator</code> and <code>Selector</code>.
	 * @param poolName				A name used to reference the pool e.g. "MainDatabase" or "LucandraIndexes"
     * @param cluster				The Cassandra cluster that network connections will be made to
     * @param keyspace				The keyspace in the Cassandra cluster against which pool operations will apply
     * @param policy                The configuration used by the pool
     * @param operandPolicy         The configuration used by the {@link org.scale7.cassandra.pelops.Operand}
     */
public static void addPool(String poolName, Cluster cluster, String keyspace, CommonsBackedPool.Policy policy, OperandPolicy operandPolicy) {
    IThriftPool pool = new CommonsBackedPool(cluster, keyspace, policy, operandPolicy);
    addPool(poolName, pool);
}
Also used : IThriftPool(org.scale7.cassandra.pelops.pool.IThriftPool) CommonsBackedPool(org.scale7.cassandra.pelops.pool.CommonsBackedPool)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 ColumnParent (org.apache.cassandra.thrift.ColumnParent)2 Bytes.fromByteBuffer (org.scale7.cassandra.pelops.Bytes.fromByteBuffer)2 CommonsBackedPool (org.scale7.cassandra.pelops.pool.CommonsBackedPool)2 IThriftPool (org.scale7.cassandra.pelops.pool.IThriftPool)2 ExecutorService (java.util.concurrent.ExecutorService)1 Test (org.junit.Test)1 OperandPolicy (org.scale7.cassandra.pelops.OperandPolicy)1 Selector (org.scale7.cassandra.pelops.Selector)1 AbstractIntegrationTest (org.scale7.cassandra.pelops.support.AbstractIntegrationTest)1