use of org.scale7.cassandra.pelops.pool.CommonsBackedPool 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();
}
}
use of org.scale7.cassandra.pelops.pool.CommonsBackedPool 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();
}
}
use of org.scale7.cassandra.pelops.pool.CommonsBackedPool 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();
}
}
use of org.scale7.cassandra.pelops.pool.CommonsBackedPool 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();
}
}
use of org.scale7.cassandra.pelops.pool.CommonsBackedPool 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();
}
}
Aggregations