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);
}
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);
}
}
}
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);
}
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);
}
}
}
Aggregations