Search in sources :

Example 6 with UnavailableException

use of com.datastax.oss.driver.api.core.servererrors.UnavailableException in project java-driver by datastax.

the class AllNodesFailedExceptionTest method should_create_instance_from_list_of_all_errors.

@SuppressWarnings("deprecation")
@Test
public void should_create_instance_from_list_of_all_errors() {
    // given
    UnavailableException e1a = new UnavailableException(node1, QUORUM, 2, 1);
    ReadTimeoutException e1b = new ReadTimeoutException(node1, QUORUM, 2, 1, false);
    ReadTimeoutException e2a = new ReadTimeoutException(node2, QUORUM, 2, 1, false);
    List<Entry<Node, Throwable>> errors = ImmutableList.of(entry(node1, e1a), entry(node1, e1b), entry(node2, e2a));
    // 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, %s], node2: [%s]", e1a, e1b, e2a);
    assertThat(e.getAllErrors()).hasEntrySatisfying(node1, list -> assertThat(list).containsExactly(e1a, e1b));
    assertThat(e.getAllErrors()).hasEntrySatisfying(node2, list -> assertThat(list).containsExactly(e2a));
    assertThat(e.getErrors()).containsEntry(node1, e1a);
    assertThat(e.getErrors()).containsEntry(node2, e2a);
    assertThat(e).hasSuppressedException(e1a).hasSuppressedException(e1b).hasSuppressedException(e2a);
}
Also used : Entry(java.util.Map.Entry) ReadTimeoutException(com.datastax.oss.driver.api.core.servererrors.ReadTimeoutException) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) Test(org.junit.Test)

Example 7 with UnavailableException

use of com.datastax.oss.driver.api.core.servererrors.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 = 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);
        }
    }
}
Also used : DefaultConsistencyLevel(com.datastax.oss.driver.api.core.DefaultConsistencyLevel) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) WriteTimeoutException(com.datastax.oss.driver.api.core.servererrors.WriteTimeoutException) BatchStatement(com.datastax.oss.driver.api.core.cql.BatchStatement) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) DriverException(com.datastax.oss.driver.api.core.DriverException) DefaultWriteType(com.datastax.oss.driver.api.core.servererrors.DefaultWriteType)

Example 8 with UnavailableException

use of com.datastax.oss.driver.api.core.servererrors.UnavailableException in project java-driver by datastax.

the class ExceptionIT method should_expose_execution_info_on_exceptions.

@Test
public void should_expose_execution_info_on_exceptions() {
    // Given
    SIMULACRON_RULE.cluster().node(0).prime(when(QUERY_STRING).then(unavailable(com.datastax.oss.simulacron.common.codec.ConsistencyLevel.ONE, 1, 0)));
    SIMULACRON_RULE.cluster().node(1).prime(when(QUERY_STRING).then(PrimeDsl.invalid("Mock error message")));
    // Then
    assertThatThrownBy(() -> SESSION_RULE.session().execute(QUERY_STRING)).isInstanceOf(InvalidQueryException.class).satisfies(exception -> {
        ExecutionInfo info = ((InvalidQueryException) exception).getExecutionInfo();
        assertThat(info).isNotNull();
        assertThat(info.getCoordinator().getEndPoint().resolve()).isEqualTo(SIMULACRON_RULE.cluster().node(1).inetSocketAddress());
        assertThat(((SimpleStatement) info.getRequest()).getQuery()).isEqualTo(QUERY_STRING);
        // specex disabled => the initial execution completed the response
        assertThat(info.getSpeculativeExecutionCount()).isEqualTo(0);
        assertThat(info.getSuccessfulExecutionIndex()).isEqualTo(0);
        assertThat(info.getTracingId()).isNull();
        assertThat(info.getPagingState()).isNull();
        assertThat(info.getIncomingPayload()).isEmpty();
        assertThat(info.getWarnings()).isEmpty();
        assertThat(info.isSchemaInAgreement()).isTrue();
        assertThat(info.getResponseSizeInBytes()).isEqualTo(info.getCompressedResponseSizeInBytes()).isEqualTo(-1);
        List<Map.Entry<Node, Throwable>> errors = info.getErrors();
        assertThat(errors).hasSize(1);
        Map.Entry<Node, Throwable> entry0 = errors.get(0);
        assertThat(entry0.getKey().getEndPoint().resolve()).isEqualTo(SIMULACRON_RULE.cluster().node(0).inetSocketAddress());
        Throwable node0Exception = entry0.getValue();
        assertThat(node0Exception).isInstanceOf(UnavailableException.class);
        // ExecutionInfo is not exposed for retried errors
        assertThat(((UnavailableException) node0Exception).getExecutionInfo()).isNull();
    });
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Node(com.datastax.oss.driver.api.core.metadata.Node) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Map(java.util.Map) InvalidQueryException(com.datastax.oss.driver.api.core.servererrors.InvalidQueryException) Test(org.junit.Test)

Aggregations

UnavailableException (com.datastax.oss.driver.api.core.servererrors.UnavailableException)8 Test (org.junit.Test)6 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)3 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)3 Node (com.datastax.oss.driver.api.core.metadata.Node)3 ReadTimeoutException (com.datastax.oss.driver.api.core.servererrors.ReadTimeoutException)3 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)2 DefaultConsistencyLevel (com.datastax.oss.driver.api.core.DefaultConsistencyLevel)2 DriverException (com.datastax.oss.driver.api.core.DriverException)2 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)2 BatchStatement (com.datastax.oss.driver.api.core.cql.BatchStatement)2 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Metric (com.codahale.metrics.Metric)1 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverContext (com.datastax.oss.driver.api.core.context.DriverContext)1 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 Statement (com.datastax.oss.driver.api.core.cql.Statement)1