use of com.datastax.oss.driver.api.core.servererrors.UnavailableException in project java-driver by datastax.
the class ReactiveRetryIT method should_retry_at_application_level.
@Test
public void should_retry_at_application_level() {
// Given
CqlSession session = spy(SESSION_RULE.session());
BoundCluster cluster = SIMULACRON_RULE.cluster();
cluster.node(0).prime(when(QUERY_STRING).then(unavailable(ConsistencyLevel.ONE, 1, 0)));
cluster.node(1).prime(when(QUERY_STRING).then(unavailable(ConsistencyLevel.ONE, 1, 0)));
cluster.node(2).prime(when(QUERY_STRING).then(rows().row("col1", "Yay!")));
// When
ReactiveRow row = Flowable.defer(() -> session.executeReactive(QUERY_STRING)).retry((retry, error) -> {
assertThat(error).isInstanceOf(UnavailableException.class);
UnavailableException ue = (UnavailableException) error;
Node coordinator = ue.getCoordinator();
if (retry == 1) {
assertCoordinator(0, coordinator);
return true;
} else if (retry == 2) {
assertCoordinator(1, coordinator);
return true;
} else {
fail("Unexpected retry attempt");
return false;
}
}).blockingLast();
// Then
assertThat(row.getString(0)).isEqualTo("Yay!");
verify(session, times(3)).executeReactive(QUERY_STRING);
assertUnavailableMetric(0, 1L);
assertUnavailableMetric(1, 1L);
assertUnavailableMetric(2, 0L);
}
use of com.datastax.oss.driver.api.core.servererrors.UnavailableException in project java-driver by datastax.
the class AllNodesFailedExceptionTest method should_create_instance_from_map_of_first_errors.
@SuppressWarnings("deprecation")
@Test
public void should_create_instance_from_map_of_first_errors() {
// given
UnavailableException e1 = new UnavailableException(node1, QUORUM, 2, 1);
ReadTimeoutException e2 = new ReadTimeoutException(node2, QUORUM, 2, 1, false);
Map<Node, Throwable> errors = ImmutableMap.of(node1, e1, node2, e2);
// when
AllNodesFailedException e = AllNodesFailedException.fromErrors(errors);
// then
assertThat(e).hasMessage("All 2 node(s) tried for the query failed " + "(showing first 2 nodes, use getAllErrors() for more): " + "node1: [%s], node2: [%s]", e1, e2);
assertThat(e.getAllErrors()).hasEntrySatisfying(node1, list -> assertThat(list).containsExactly(e1));
assertThat(e.getAllErrors()).hasEntrySatisfying(node2, list -> assertThat(list).containsExactly(e2));
assertThat(e.getErrors()).containsEntry(node1, e1);
assertThat(e.getErrors()).containsEntry(node2, e2);
assertThat(e).hasSuppressedException(e1).hasSuppressedException(e2);
}
use of com.datastax.oss.driver.api.core.servererrors.UnavailableException in project java-driver by datastax.
the class ReactiveResultSetSubscriptionTest method should_report_error_on_intermediary_page.
@Test
public void should_report_error_on_intermediary_page() {
CompletableFuture<AsyncResultSet> future1 = new CompletableFuture<>();
CompletableFuture<AsyncResultSet> future2 = new CompletableFuture<>();
MockAsyncResultSet page1 = new MockAsyncResultSet(3, future2);
TestSubscriber<ReactiveRow> mainSubscriber = new TestSubscriber<>();
TestSubscriber<ColumnDefinitions> colDefsSubscriber = new TestSubscriber<>();
TestSubscriber<ExecutionInfo> execInfosSubscriber = new TestSubscriber<>();
TestSubscriber<Boolean> wasAppliedSubscriber = new TestSubscriber<>();
ReactiveResultSetSubscription<AsyncResultSet> subscription = new ReactiveResultSetSubscription<>(mainSubscriber, colDefsSubscriber, execInfosSubscriber, wasAppliedSubscriber);
mainSubscriber.onSubscribe(subscription);
subscription.start(() -> future1);
future1.complete(page1);
future2.completeExceptionally(new UnavailableException(null, null, 0, 0));
mainSubscriber.awaitTermination();
assertThat(mainSubscriber.getElements()).extracting("row").isEqualTo(page1.currentPage());
assertThat(mainSubscriber.getError()).isNotNull().isInstanceOf(UnavailableException.class);
// colDefsSubscriber completed normally when page1 arrived
assertThat(colDefsSubscriber.getError()).isNull();
assertThat(colDefsSubscriber.getElements()).hasSize(1).containsExactly(page1.getColumnDefinitions());
// execInfosSubscriber completed with error, but should have emitted 1 item for page1
assertThat(execInfosSubscriber.getElements()).hasSize(1).containsExactly(page1.getExecutionInfo());
assertThat(execInfosSubscriber.getError()).isNotNull().isInstanceOf(UnavailableException.class);
// colDefsSubscriber completed normally when page1 arrived
assertThat(wasAppliedSubscriber.getElements()).hasSize(1).containsExactly(true);
assertThat(wasAppliedSubscriber.getError()).isNull();
}
use of com.datastax.oss.driver.api.core.servererrors.UnavailableException in project java-driver by datastax.
the class ReactiveResultSetSubscriptionTest method should_report_error_on_first_page.
@Test
public void should_report_error_on_first_page() {
CompletableFuture<AsyncResultSet> future1 = new CompletableFuture<>();
TestSubscriber<ReactiveRow> mainSubscriber = new TestSubscriber<>();
TestSubscriber<ColumnDefinitions> colDefsSubscriber = new TestSubscriber<>();
TestSubscriber<ExecutionInfo> execInfosSubscriber = new TestSubscriber<>();
TestSubscriber<Boolean> wasAppliedSubscriber = new TestSubscriber<>();
ReactiveResultSetSubscription<AsyncResultSet> subscription = new ReactiveResultSetSubscription<>(mainSubscriber, colDefsSubscriber, execInfosSubscriber, wasAppliedSubscriber);
mainSubscriber.onSubscribe(subscription);
subscription.start(() -> future1);
future1.completeExceptionally(new UnavailableException(null, null, 0, 0));
mainSubscriber.awaitTermination();
assertThat(mainSubscriber.getError()).isNotNull().isInstanceOf(UnavailableException.class);
assertThat(colDefsSubscriber.getError()).isNotNull().isInstanceOf(UnavailableException.class);
assertThat(execInfosSubscriber.getError()).isNotNull().isInstanceOf(UnavailableException.class);
assertThat(wasAppliedSubscriber.getError()).isNotNull().isInstanceOf(UnavailableException.class);
}
use of com.datastax.oss.driver.api.core.servererrors.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 = 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);
}
}
}
Aggregations