Search in sources :

Example 1 with NoOpConnectionValidator

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

the class CommonsBackedPoolIntegrationTest method testsScheduledTaskNodeSuspension.

/**
     * Test that when a node is suspended all it's connections are terminated and that when it comes good it starts
     * returning connections again.
     */
@Test
public void testsScheduledTaskNodeSuspension() throws Exception {
    CommonsBackedPool.Policy config = new CommonsBackedPool.Policy();
    // disable the background thread
    config.setTimeBetweenScheduledMaintenanceTaskRunsMillis(-1);
    config.setMaxActivePerNode(1);
    final AtomicBoolean suspended = new AtomicBoolean(true);
    CommonsBackedPool pool = new CommonsBackedPool(AbstractIntegrationTest.cluster, AbstractIntegrationTest.KEYSPACE, config, new OperandPolicy(), new LeastLoadedNodeSelectionStrategy(), new CommonsBackedPool.INodeSuspensionStrategy() {

        @Override
        public boolean evaluate(CommonsBackedPool pool, PooledNode node) {
            if (suspended.get()) {
                // first run through we want to suspend the node
                suspended.set(false);
                node.setSuspensionState(new CommonsBackedPool.INodeSuspensionState() {

                    @Override
                    public boolean isSuspended() {
                        return true;
                    }
                });
                return true;
            } else {
                // second run through we want the node active
                node.setSuspensionState(new CommonsBackedPool.INodeSuspensionState() {

                    @Override
                    public boolean isSuspended() {
                        return false;
                    }
                });
                return false;
            }
        }
    }, new NoOpConnectionValidator());
    try {
        // node not yet suspended
        IThriftPool.IPooledConnection connection = pool.getConnection();
        connection.release();
        // suspend the node
        pool.runMaintenanceTasks();
        try {
            pool.getConnection();
            fail("No nodes should be available");
        } catch (NoConnectionsAvailableException e) {
        // expected
        }
        // activate the node
        pool.runMaintenanceTasks();
        // node is now active
        connection = pool.getConnection();
        connection.release();
    } finally {
        pool.shutdown();
    }
}
Also used : OperandPolicy(org.scale7.cassandra.pelops.OperandPolicy) NoConnectionsAvailableException(org.scale7.cassandra.pelops.exceptions.NoConnectionsAvailableException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OperandPolicy(org.scale7.cassandra.pelops.OperandPolicy) AbstractIntegrationTest(org.scale7.cassandra.pelops.support.AbstractIntegrationTest) Test(org.junit.Test)

Example 2 with NoOpConnectionValidator

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

the class CommonsBackedPoolFactoryBeanIntegrationTest method testAfterProperties.

/**
     * Tests the factory bean works as expected when operand or pool policy instances are provided.
     * @throws Exception if an error occurs
     */
@Test
public void testAfterProperties() throws Exception {
    OperandPolicy operandPolicy = new OperandPolicy();
    CommonsBackedPool.Policy policy = new CommonsBackedPool.Policy();
    LeastLoadedNodeSelectionStrategy nodeSelectionStrategy = new LeastLoadedNodeSelectionStrategy();
    NoOpNodeSuspensionStrategy nodeSuspensionStrategy = new NoOpNodeSuspensionStrategy();
    NoOpConnectionValidator connectionValidator = new NoOpConnectionValidator();
    CommonsBackedPoolFactoryBean factoryBean = new CommonsBackedPoolFactoryBean();
    factoryBean.setCluster(AbstractIntegrationTest.cluster);
    factoryBean.setKeyspace(AbstractIntegrationTest.KEYSPACE);
    factoryBean.setPolicy(policy);
    factoryBean.setOperandPolicy(operandPolicy);
    factoryBean.setNodeSelectionStrategy(nodeSelectionStrategy);
    factoryBean.setNodeSuspensionStrategy(nodeSuspensionStrategy);
    factoryBean.setConnectionValidator(connectionValidator);
    assertNull("The factory should not have created the pool at this point", factoryBean.getObject());
    try {
        factoryBean.afterPropertiesSet();
        CommonsBackedPool pool = (CommonsBackedPool) factoryBean.getObject();
        assertNotNull("The factory didn't initialize the pool", pool);
        assertTrue("The factory didn't use the provided operand policy instance", operandPolicy == pool.getOperandPolicy());
        assertTrue("The factory didn't use the provided config instance", policy == pool.getPolicy());
        assertTrue("The factory didn't use the provided config instance", cluster == pool.getCluster());
        assertTrue("The factory didn't use the provided node selection instance", nodeSelectionStrategy == pool.getNodeSelectionStrategy());
        assertTrue("The factory didn't use the provided node suspension instance", nodeSuspensionStrategy == pool.getNodeSuspensionStrategy());
        assertTrue("The factory didn't use the provided connection validator instance", connectionValidator == pool.getConnectionValidator());
    } finally {
        factoryBean.destroy();
    }
}
Also used : OperandPolicy(org.scale7.cassandra.pelops.OperandPolicy) NoOpConnectionValidator(org.scale7.cassandra.pelops.pool.NoOpConnectionValidator) LeastLoadedNodeSelectionStrategy(org.scale7.cassandra.pelops.pool.LeastLoadedNodeSelectionStrategy) CommonsBackedPool(org.scale7.cassandra.pelops.pool.CommonsBackedPool) NoOpNodeSuspensionStrategy(org.scale7.cassandra.pelops.pool.NoOpNodeSuspensionStrategy) OperandPolicy(org.scale7.cassandra.pelops.OperandPolicy) Test(org.junit.Test) AbstractIntegrationTest(org.scale7.cassandra.pelops.support.AbstractIntegrationTest)

Aggregations

Test (org.junit.Test)2 OperandPolicy (org.scale7.cassandra.pelops.OperandPolicy)2 AbstractIntegrationTest (org.scale7.cassandra.pelops.support.AbstractIntegrationTest)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 NoConnectionsAvailableException (org.scale7.cassandra.pelops.exceptions.NoConnectionsAvailableException)1 CommonsBackedPool (org.scale7.cassandra.pelops.pool.CommonsBackedPool)1 LeastLoadedNodeSelectionStrategy (org.scale7.cassandra.pelops.pool.LeastLoadedNodeSelectionStrategy)1 NoOpConnectionValidator (org.scale7.cassandra.pelops.pool.NoOpConnectionValidator)1 NoOpNodeSuspensionStrategy (org.scale7.cassandra.pelops.pool.NoOpNodeSuspensionStrategy)1