Search in sources :

Example 1 with UnavailableException

use of com.datastax.driver.core.exceptions.UnavailableException in project atlasdb by palantir.

the class CqlKeyValueService method alterTableForCompaction.

private void alterTableForCompaction(TableReference tableRef, int gcGraceSeconds, float tombstoneThreshold) {
    log.trace("Altering table {} to have gc_grace_seconds={} and tombstone_threshold={}", tableRef, gcGraceSeconds, String.format("%.2f", tombstoneThreshold));
    String alterTableQuery = "ALTER TABLE " + getFullTableName(tableRef) + " WITH gc_grace_seconds = " + gcGraceSeconds + " and compaction = {'class':'" + CassandraConstants.LEVELED_COMPACTION_STRATEGY + "', 'tombstone_threshold':" + tombstoneThreshold + "};";
    BoundStatement alterTable = getPreparedStatement(tableRef, alterTableQuery, longRunningQuerySession).setConsistencyLevel(ConsistencyLevel.ALL).bind();
    ResultSet resultSet;
    try {
        resultSet = longRunningQuerySession.execute(alterTable);
    } catch (UnavailableException e) {
        throw new InsufficientConsistencyException("Alter table requires all Cassandra" + " nodes to be up and available.", e);
    } catch (Exception e) {
        throw Throwables.throwUncheckedException(e);
    }
    cqlKeyValueServices.logTracedQuery(alterTableQuery, resultSet, session, cqlStatementCache.normalQuery);
}
Also used : InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) ResultSet(com.datastax.driver.core.ResultSet) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException) BoundStatement(com.datastax.driver.core.BoundStatement) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) ExecutionException(java.util.concurrent.ExecutionException) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException)

Example 2 with UnavailableException

use of com.datastax.driver.core.exceptions.UnavailableException in project java-driver by datastax.

the class DowngradingRetry method read.

/**
 * Queries data, retrying if necessary with a downgraded CL.
 *
 * @param cl         the consistency level to apply.
 * @param retryCount the current retry count.
 * @throws DriverException if the current consistency level cannot be downgraded.
 */
private ResultSet read(ConsistencyLevel cl, int retryCount) {
    System.out.printf("Reading at %s (retry count: %d)%n", cl, retryCount);
    Statement stmt = new SimpleStatement("SELECT sensor_id, date, timestamp, value " + "FROM downgrading.sensor_data " + "WHERE " + "sensor_id = 756716f7-2e54-4715-9f00-91dcbea6cf50 AND " + "date = '2018-02-26' AND " + "timestamp > '2018-02-26+01:00'").setConsistencyLevel(cl);
    try {
        ResultSet rows = session.execute(stmt);
        System.out.println("Read succeeded at " + cl);
        return rows;
    } catch (DriverException e) {
        if (retryCount == maxRetries) {
            throw e;
        }
        e = unwrapNoHostAvailableException(e);
        System.out.println("Read failed: " + e);
        if (e instanceof UnavailableException) {
            // Downgrade to the number of replicas reported alive and retry.
            int aliveReplicas = ((UnavailableException) e).getAliveReplicas();
            ConsistencyLevel downgraded = downgrade(cl, aliveReplicas, e);
            return read(downgraded, retryCount + 1);
        } else if (e instanceof ReadTimeoutException) {
            ReadTimeoutException readTimeout = (ReadTimeoutException) e;
            int received = readTimeout.getReceivedAcknowledgements();
            int required = readTimeout.getRequiredAcknowledgements();
            // equal to the number of received acknowledgements.
            if (received < required) {
                ConsistencyLevel downgraded = downgrade(cl, received, e);
                return read(downgraded, retryCount + 1);
            }
            // and get the data back.
            if (!readTimeout.wasDataRetrieved()) {
                return read(cl, retryCount + 1);
            }
            // Otherwise, abort since the read timeout is unlikely to be solved by a retry.
            throw e;
        } else {
            // and hope to talk to a healthier coordinator.
            return read(cl, retryCount + 1);
        }
    }
}
Also used : ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) SimpleStatement(com.datastax.driver.core.SimpleStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) SimpleStatement(com.datastax.driver.core.SimpleStatement) ReadTimeoutException(com.datastax.driver.core.exceptions.ReadTimeoutException) ResultSet(com.datastax.driver.core.ResultSet) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException) DriverException(com.datastax.driver.core.exceptions.DriverException)

Example 3 with UnavailableException

use of com.datastax.driver.core.exceptions.UnavailableException in project nifi by apache.

the class PutCassandraQLTest method testProcessorUnavailableException.

@Test
public void testProcessorUnavailableException() {
    setUpStandardTestConfig();
    processor.setExceptionToThrow(new UnavailableException(new InetSocketAddress("localhost", 9042), ConsistencyLevel.ALL, 5, 2));
    testRunner.enqueue("UPDATE users SET cities = [ 'New York', 'Los Angeles' ] WHERE user_id = 'coast2coast';");
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(PutCassandraQL.REL_RETRY, 1);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException) Test(org.junit.Test)

Example 4 with UnavailableException

use of com.datastax.driver.core.exceptions.UnavailableException in project java-driver by datastax.

the class DowngradingRetry method write.

/**
 * Inserts data, retrying if necessary with a downgraded CL.
 *
 * @param cl         the consistency level to apply.
 * @param retryCount the current retry count.
 * @throws DriverException if the current consistency level cannot be downgraded.
 */
private void write(ConsistencyLevel cl, int retryCount) {
    System.out.printf("Writing at %s (retry count: %d)%n", cl, retryCount);
    BatchStatement batch = new BatchStatement(UNLOGGED);
    batch.add(new SimpleStatement("INSERT INTO downgrading.sensor_data " + "(sensor_id, date, timestamp, value) " + "VALUES (" + "756716f7-2e54-4715-9f00-91dcbea6cf50," + "'2018-02-26'," + "'2018-02-26T13:53:46.345+01:00'," + "2.34)"));
    batch.add(new SimpleStatement("INSERT INTO downgrading.sensor_data " + "(sensor_id, date, timestamp, value) " + "VALUES (" + "756716f7-2e54-4715-9f00-91dcbea6cf50," + "'2018-02-26'," + "'2018-02-26T13:54:27.488+01:00'," + "2.47)"));
    batch.add(new SimpleStatement("INSERT INTO downgrading.sensor_data " + "(sensor_id, date, timestamp, value) " + "VALUES (" + "756716f7-2e54-4715-9f00-91dcbea6cf50," + "'2018-02-26'," + "'2018-02-26T13:56:33.739+01:00'," + "2.52)"));
    batch.setConsistencyLevel(cl);
    try {
        session.execute(batch);
        System.out.println("Write succeeded at " + cl);
    } catch (DriverException e) {
        if (retryCount == maxRetries) {
            throw e;
        }
        e = unwrapNoHostAvailableException(e);
        System.out.println("Write failed: " + e);
        if (e instanceof UnavailableException) {
            // With an UnavailableException, we know that the write wasn't even attempted.
            // Downgrade to the number of replicas reported alive and retry.
            int aliveReplicas = ((UnavailableException) e).getAliveReplicas();
            ConsistencyLevel downgraded = downgrade(cl, aliveReplicas, e);
            write(downgraded, retryCount + 1);
        } else if (e instanceof WriteTimeoutException) {
            WriteType writeType = ((WriteTimeoutException) e).getWriteType();
            int acknowledgements = ((WriteTimeoutException) e).getReceivedAcknowledgements();
            switch(writeType) {
                case SIMPLE:
                case BATCH:
                    // a retry would ever succeed.
                    if (acknowledgements == 0) {
                        throw e;
                    }
                    break;
                case UNLOGGED_BATCH:
                    // For unlogged batches, the write might have been persisted only partially,
                    // so we can't simply ignore the exception: instead, we need to retry with
                    // consistency level equal to the number of acknowledged writes.
                    ConsistencyLevel downgraded = downgrade(cl, acknowledgements, e);
                    write(downgraded, retryCount + 1);
                    break;
                case BATCH_LOG:
                    // Rare edge case: the peers that were chosen by the coordinator
                    // to receive the distributed batch log failed to respond.
                    // Simply retry with same consistency level.
                    write(cl, retryCount + 1);
                    break;
                default:
                    // Other write types are uncommon and should not be retried.
                    throw e;
            }
        } else {
            // Unexpected error: just retry with same consistency level
            // and hope to talk to a healthier coordinator.
            write(cl, retryCount + 1);
        }
    }
}
Also used : ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) WriteTimeoutException(com.datastax.driver.core.exceptions.WriteTimeoutException) SimpleStatement(com.datastax.driver.core.SimpleStatement) WriteType(com.datastax.driver.core.WriteType) BatchStatement(com.datastax.driver.core.BatchStatement) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException) DriverException(com.datastax.driver.core.exceptions.DriverException)

Aggregations

UnavailableException (com.datastax.driver.core.exceptions.UnavailableException)4 BatchStatement (com.datastax.driver.core.BatchStatement)2 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)2 ResultSet (com.datastax.driver.core.ResultSet)2 SimpleStatement (com.datastax.driver.core.SimpleStatement)2 DriverException (com.datastax.driver.core.exceptions.DriverException)2 BoundStatement (com.datastax.driver.core.BoundStatement)1 Statement (com.datastax.driver.core.Statement)1 WriteType (com.datastax.driver.core.WriteType)1 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)1 NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)1 ReadTimeoutException (com.datastax.driver.core.exceptions.ReadTimeoutException)1 WriteTimeoutException (com.datastax.driver.core.exceptions.WriteTimeoutException)1 InsufficientConsistencyException (com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException)1 KeyAlreadyExistsException (com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)1 InetSocketAddress (java.net.InetSocketAddress)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.Test)1