use of com.datastax.oss.driver.api.core.DriverException 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 = SimpleStatement.newInstance("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 = unwrapAllNodesFailedException(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).getAlive();
ConsistencyLevel downgraded = downgrade(cl, aliveReplicas, e);
return read(downgraded, retryCount + 1);
} else if (e instanceof ReadTimeoutException) {
ReadTimeoutException readTimeout = (ReadTimeoutException) e;
int received = readTimeout.getReceived();
int required = readTimeout.getBlockFor();
// 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.wasDataPresent()) {
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.oss.driver.api.core.DriverException 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 = BatchStatement.newInstance(UNLOGGED).add(SimpleStatement.newInstance("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)")).add(SimpleStatement.newInstance("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)")).add(SimpleStatement.newInstance("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)")).setConsistencyLevel(cl);
try {
session.execute(batch);
System.out.println("Write succeeded at " + cl);
} catch (DriverException e) {
if (retryCount == maxRetries) {
throw e;
}
e = unwrapAllNodesFailedException(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).getAlive();
ConsistencyLevel downgraded = downgrade(cl, aliveReplicas, e);
write(downgraded, retryCount + 1);
} else if (e instanceof WriteTimeoutException) {
DefaultWriteType writeType = (DefaultWriteType) ((WriteTimeoutException) e).getWriteType();
int acknowledgements = ((WriteTimeoutException) e).getReceived();
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);
}
}
}
use of com.datastax.oss.driver.api.core.DriverException in project java-driver by datastax.
the class GraphRequestHandler method setFinalError.
private void setFinalError(GraphStatement<?> statement, Throwable error, Node node, int execution) {
DriverExecutionProfile executionProfile = Conversions.resolveExecutionProfile(statement, context);
if (error instanceof DriverException) {
((DriverException) error).setExecutionInfo(new DefaultExecutionInfo(statement, node, startedSpeculativeExecutionsCount.get(), execution, errors, null, null, true, session, context, executionProfile));
}
if (result.completeExceptionally(error)) {
cancelScheduledTasks();
if (!(requestTracker instanceof NoopRequestTracker)) {
long latencyNanos = System.nanoTime() - startTimeNanos;
requestTracker.onError(statement, error, latencyNanos, executionProfile, node, logPrefix);
}
if (error instanceof DriverTimeoutException) {
throttler.signalTimeout(this);
sessionMetricUpdater.incrementCounter(DseSessionMetric.GRAPH_CLIENT_TIMEOUTS, executionProfile.getName());
} else if (!(error instanceof RequestThrottlingException)) {
throttler.signalError(this, error);
}
}
}
use of com.datastax.oss.driver.api.core.DriverException in project java-driver by datastax.
the class CqlRequestHandler method setFinalError.
private void setFinalError(Statement<?> statement, Throwable error, Node node, int execution) {
DriverExecutionProfile executionProfile = Conversions.resolveExecutionProfile(statement, context);
if (error instanceof DriverException) {
((DriverException) error).setExecutionInfo(new DefaultExecutionInfo(statement, node, startedSpeculativeExecutionsCount.get(), execution, errors, null, null, true, session, context, executionProfile));
}
if (result.completeExceptionally(error)) {
cancelScheduledTasks();
if (!(requestTracker instanceof NoopRequestTracker)) {
long latencyNanos = System.nanoTime() - startTimeNanos;
requestTracker.onError(statement, error, latencyNanos, executionProfile, node, logPrefix);
}
if (error instanceof DriverTimeoutException) {
throttler.signalTimeout(this);
sessionMetricUpdater.incrementCounter(DefaultSessionMetric.CQL_CLIENT_TIMEOUTS, executionProfile.getName());
} else if (!(error instanceof RequestThrottlingException)) {
throttler.signalError(this, error);
}
}
}
Aggregations