use of org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection in project scale7-pelops by s7.
the class CommonsBackedPool method getConnectionExcept.
@Override
public IPooledConnection getConnectionExcept(Set<String> avoidNodes) throws NoConnectionsAvailableException {
PooledNode node = null;
IPooledConnection connection = null;
long timeout = -1;
while (connection == null) {
if (timeout == -1) {
// first run through calc the timeout for the next loop
// (this makes debugging easier)
int maxWait = getPolicy().getMaxWaitForConnection();
timeout = maxWait > 0 ? System.currentTimeMillis() + maxWait : Long.MAX_VALUE;
} else if (timeout < System.currentTimeMillis()) {
logger.debug("Max wait time for connection exceeded");
break;
}
node = nodeSelectionStrategy.select(this, nodes.keySet(), avoidNodes);
// if the strategy was unable to choose a node (all suspended?) then sleep for a bit and loop
if (node == null) {
logger.debug("The node selection strategy was unable to choose a node, sleeping before trying again...");
try {
Thread.sleep(DEFAULT_WAIT_PERIOD);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
continue;
}
try {
logger.debug("Attempting to borrow free connection for node '{}'", node.getAddress());
// note that if no connections are currently available for this node then the pool will sleep for
// DEFAULT_WAIT_PERIOD milliseconds
connection = pool.borrowObject(node.getAddress());
} catch (IllegalStateException e) {
throw new PelopsException("The pool has been shutdown", e);
} catch (Exception e) {
if (e instanceof NoSuchElementException) {
logger.debug("No free connections available for node '{}'. Trying another node...", node.getAddress());
} else if (e instanceof TTransportException) {
logger.warn(String.format("A TTransportException was thrown while attempting to create a connection to '%s'. " + "This node will be suspended for %sms. Trying another node...", node.getAddress(), this.policy.getNodeDownSuspensionMillis()));
node.suspendForMillis(this.policy.getNodeDownSuspensionMillis());
} else
logger.warn(String.format("An exception was thrown while attempting to create a connection to '%s'. " + "Trying another node...", node.getAddress()), e);
// try and avoid this node on the next trip through the loop
if (avoidNodes == null)
avoidNodes = new HashSet<String>(10);
avoidNodes.add(node.getAddress());
}
}
if (node == null) {
logger.error("Failed to get a connection within the configured wait time because there are no available nodes. " + "This possibly indicates that either the suspension strategy is too aggressive or that your " + "cluster is in a bad way.");
throw new NoConnectionsAvailableException("Failed to get a connection within the configured max wait time.");
}
if (connection == null) {
logger.error("Failed to get a connection within the maximum allowed wait time. " + "Try increasing the either the number of allowed connections or the max wait time.");
throw new NoConnectionsAvailableException("Failed to get a connection within the configured max wait time.");
}
logger.debug("Borrowing connection '{}'", connection);
statistics.connectionsActive.incrementAndGet();
reportConnectionBorrowed(connection.getNode().getAddress());
return connection;
}
use of org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection in project scale7-pelops by s7.
the class RowDeletor method deleteRow.
/**
* Delete a row with a specified key from a specified column family. The function succeeds even if
* the row does not exist.
* @param columnFamily The column family from which to delete the row
* @param rowKey The key of the row
* @param cLevel The Cassandra consistency level to be used
* @throws PelopsException
*/
public void deleteRow(String columnFamily, final Bytes rowKey, final ConsistencyLevel cLevel) throws PelopsException {
final ColumnPath path = new ColumnPath(columnFamily);
IOperation<Void> operation = new IOperation<Void>() {
@Override
public Void execute(IPooledConnection conn) throws Exception {
conn.getAPI().remove(nullSafeGet(rowKey), path, timestamp, cLevel);
return null;
}
};
tryOperation(operation);
}
use of org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection 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);
}
use of org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection in project scale7-pelops by s7.
the class DebuggingPool method getConnection.
@Override
public IPooledConnection getConnection() throws NoConnectionsAvailableException {
Cluster.Node[] nodes = cluster.getNodes();
int index = nodes.length == 1 ? 0 : random.nextInt(nodes.length);
logger.debug("Using node '{}'", nodes[index]);
if (connection != null && connection.isOpen())
return connection;
try {
connection = new PooledConnection(nodes[index], keyspace);
connection.open();
} catch (Exception e) {
throw new NoConnectionsAvailableException();
}
return connection;
}
Aggregations