Search in sources :

Example 1 with PelopsException

use of org.scale7.cassandra.pelops.exceptions.PelopsException 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;
}
Also used : NoConnectionsAvailableException(org.scale7.cassandra.pelops.exceptions.NoConnectionsAvailableException) TTransportException(org.apache.thrift.transport.TTransportException) PelopsException(org.scale7.cassandra.pelops.exceptions.PelopsException) TTransportException(org.apache.thrift.transport.TTransportException) NoConnectionsAvailableException(org.scale7.cassandra.pelops.exceptions.NoConnectionsAvailableException) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException) SocketException(java.net.SocketException) NoSuchElementException(java.util.NoSuchElementException) TException(org.apache.thrift.TException) PelopsException(org.scale7.cassandra.pelops.exceptions.PelopsException) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with PelopsException

use of org.scale7.cassandra.pelops.exceptions.PelopsException 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);
}
Also used : ColumnPath(org.apache.cassandra.thrift.ColumnPath) IPooledConnection(org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection)

Example 3 with PelopsException

use of org.scale7.cassandra.pelops.exceptions.PelopsException 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 4 with PelopsException

use of org.scale7.cassandra.pelops.exceptions.PelopsException 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 5 with PelopsException

use of org.scale7.cassandra.pelops.exceptions.PelopsException 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

ByteBuffer (java.nio.ByteBuffer)2 ColumnParent (org.apache.cassandra.thrift.ColumnParent)2 TTransportException (org.apache.thrift.transport.TTransportException)2 Bytes.fromByteBuffer (org.scale7.cassandra.pelops.Bytes.fromByteBuffer)2 PelopsException (org.scale7.cassandra.pelops.exceptions.PelopsException)2 IPooledConnection (org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection)2 SocketException (java.net.SocketException)1 NoSuchElementException (java.util.NoSuchElementException)1 ColumnPath (org.apache.cassandra.thrift.ColumnPath)1 InvalidRequestException (org.apache.cassandra.thrift.InvalidRequestException)1 NotFoundException (org.apache.cassandra.thrift.NotFoundException)1 TimedOutException (org.apache.cassandra.thrift.TimedOutException)1 UnavailableException (org.apache.cassandra.thrift.UnavailableException)1 TException (org.apache.thrift.TException)1 NoConnectionsAvailableException (org.scale7.cassandra.pelops.exceptions.NoConnectionsAvailableException)1