Search in sources :

Example 1 with OperandPolicy

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

the class CommonsBackedPoolIntegrationTest method testInitWithDownedNode.

/**
     * Test initialization with static node list that contains an offline node.
     * https://github.com/s7/scale7-pelops/issues#issue/24
     */
@Test
public void testInitWithDownedNode() throws Exception {
    final int timeout = 2000;
    // allowed timeout deviation in percentage
    final int allowedDeviation = 10;
    Cluster cluster = new Cluster(new String[] { RPC_LISTEN_ADDRESS, "192.0.2.0" }, new IConnection.Config(RPC_PORT, true, timeout), false);
    CommonsBackedPool.Policy config = new CommonsBackedPool.Policy();
    // disable the background thread
    config.setTimeBetweenScheduledMaintenanceTaskRunsMillis(-1);
    config.setMaxActivePerNode(1);
    long startMillis = System.currentTimeMillis();
    CommonsBackedPool pool = new CommonsBackedPool(cluster, AbstractIntegrationTest.KEYSPACE, config, new OperandPolicy(), new LeastLoadedNodeSelectionStrategy(), new NoOpNodeSuspensionStrategy(), new DescribeVersionConnectionValidator());
    double totalMillis = System.currentTimeMillis() - startMillis;
    String reason = String.format("actual timeout should be within %d%% of the configured", allowedDeviation);
    assertThat(reason, totalMillis, closeTo(timeout, (allowedDeviation / 100.0) * timeout));
    try {
        pool.createSelector();
    } finally {
        pool.shutdown();
    }
}
Also used : OperandPolicy(org.scale7.cassandra.pelops.OperandPolicy) Cluster(org.scale7.cassandra.pelops.Cluster) IConnection(org.scale7.cassandra.pelops.IConnection) OperandPolicy(org.scale7.cassandra.pelops.OperandPolicy) AbstractIntegrationTest(org.scale7.cassandra.pelops.support.AbstractIntegrationTest) Test(org.junit.Test)

Example 2 with OperandPolicy

use of org.scale7.cassandra.pelops.OperandPolicy 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 3 with OperandPolicy

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

the class CommonsBackedPoolIntegrationTest method testsScheduledTaskConnectionValidation.

/**
     * 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 testsScheduledTaskConnectionValidation() throws Exception {
    CommonsBackedPool.Policy config = new CommonsBackedPool.Policy();
    // disable the background thread
    config.setTimeBetweenScheduledMaintenanceTaskRunsMillis(-1);
    config.setMaxActivePerNode(1);
    final AtomicBoolean invoked = new AtomicBoolean(false);
    CommonsBackedPool pool = new CommonsBackedPool(AbstractIntegrationTest.cluster, AbstractIntegrationTest.KEYSPACE, config, new OperandPolicy(), new LeastLoadedNodeSelectionStrategy(), new NoOpNodeSuspensionStrategy(), new CommonsBackedPool.IConnectionValidator() {

        @Override
        public boolean validate(CommonsBackedPool.PooledConnection connection) {
            invoked.set(true);
            return true;
        }
    });
    try {
        pool.runMaintenanceTasks();
        assertTrue("Connection validation was not invoked", invoked.get());
    } finally {
        pool.shutdown();
    }
}
Also used : OperandPolicy(org.scale7.cassandra.pelops.OperandPolicy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OperandPolicy(org.scale7.cassandra.pelops.OperandPolicy) AbstractIntegrationTest(org.scale7.cassandra.pelops.support.AbstractIntegrationTest) Test(org.junit.Test)

Example 4 with OperandPolicy

use of org.scale7.cassandra.pelops.OperandPolicy 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)

Example 5 with OperandPolicy

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

the class Operand method tryOperation.

protected <ReturnType> ReturnType tryOperation(IOperation<ReturnType> operation, OperandPolicy operandPolicy) throws PelopsException {
    Set<String> avoidNodes = null;
    Exception lastException = null;
    int retries = 0;
    do {
        // Get a connection to a Cassandra node
        IPooledConnection conn = null;
        try {
            conn = thrift.getConnectionExcept(avoidNodes);
        } catch (Exception e) {
            // the pool is responsible for blocking and waiting for a connection, so don't retry
            throw operandPolicy.getExceptionTranslator().translate(e);
        }
        try {
            // Return result!
            return operation.execute(conn);
        } catch (Exception e) {
            // Should we try again?
            if (e instanceof TimedOutException || e instanceof TTransportException || e instanceof UnavailableException) {
                logger.warn("Operation failed as result of network exception. Connection to node {} is being marked as corrupt " + "(and will probably be be destroyed). Cause of failure is {}", conn.getNode().getAddress(), e);
                // This connection is "broken" by network timeout or other problem.
                conn.corrupted();
                // to avoid create the set for every request create the set here
                if (avoidNodes == null)
                    avoidNodes = new HashSet<String>(10);
                avoidNodes.add(conn.getNode().getAddress());
                retries++;
                lastException = e;
            } else if (e instanceof NotFoundException) {
                // Re-throw application-level exceptions immediately.
                throw operandPolicy.getExceptionTranslator().translate(e);
            } else {
                // This connection is "broken" by network timeout or other problem.
                conn.corrupted();
                // Re-throw application-level exceptions immediately.
                throw operandPolicy.getExceptionTranslator().translate(e);
            }
        } finally {
            conn.release();
        }
    } while (retries < operandPolicy.getMaxOpRetries());
    throw operandPolicy.getExceptionTranslator().translate(lastException);
}
Also used : TimedOutException(org.apache.cassandra.thrift.TimedOutException) UnavailableException(org.apache.cassandra.thrift.UnavailableException) TTransportException(org.apache.thrift.transport.TTransportException) NotFoundException(org.apache.cassandra.thrift.NotFoundException) TimedOutException(org.apache.cassandra.thrift.TimedOutException) PelopsException(org.scale7.cassandra.pelops.exceptions.PelopsException) UnavailableException(org.apache.cassandra.thrift.UnavailableException) TTransportException(org.apache.thrift.transport.TTransportException) NotFoundException(org.apache.cassandra.thrift.NotFoundException) IPooledConnection(org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection)

Aggregations

Test (org.junit.Test)6 AbstractIntegrationTest (org.scale7.cassandra.pelops.support.AbstractIntegrationTest)6 OperandPolicy (org.scale7.cassandra.pelops.OperandPolicy)4 IThriftPool (org.scale7.cassandra.pelops.pool.IThriftPool)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CommonsBackedPool (org.scale7.cassandra.pelops.pool.CommonsBackedPool)2 Column (org.apache.cassandra.thrift.Column)1 Mutation (org.apache.cassandra.thrift.Mutation)1 NotFoundException (org.apache.cassandra.thrift.NotFoundException)1 TimedOutException (org.apache.cassandra.thrift.TimedOutException)1 UnavailableException (org.apache.cassandra.thrift.UnavailableException)1 TTransportException (org.apache.thrift.transport.TTransportException)1 Cluster (org.scale7.cassandra.pelops.Cluster)1 IConnection (org.scale7.cassandra.pelops.IConnection)1 NoConnectionsAvailableException (org.scale7.cassandra.pelops.exceptions.NoConnectionsAvailableException)1 PelopsException (org.scale7.cassandra.pelops.exceptions.PelopsException)1 DebuggingPool (org.scale7.cassandra.pelops.pool.DebuggingPool)1 IPooledConnection (org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection)1 LeastLoadedNodeSelectionStrategy (org.scale7.cassandra.pelops.pool.LeastLoadedNodeSelectionStrategy)1 NoOpConnectionValidator (org.scale7.cassandra.pelops.pool.NoOpConnectionValidator)1