Search in sources :

Example 1 with ReadTimeoutException

use of com.datastax.oss.driver.api.core.servererrors.ReadTimeoutException 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);
}
Also used : ReadTimeoutException(com.datastax.oss.driver.api.core.servererrors.ReadTimeoutException) Node(com.datastax.oss.driver.api.core.metadata.Node) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) Test(org.junit.Test)

Example 2 with ReadTimeoutException

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

Example 3 with ReadTimeoutException

use of com.datastax.oss.driver.api.core.servererrors.ReadTimeoutException 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)

Aggregations

ReadTimeoutException (com.datastax.oss.driver.api.core.servererrors.ReadTimeoutException)3 UnavailableException (com.datastax.oss.driver.api.core.servererrors.UnavailableException)3 Test (org.junit.Test)2 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)1 DefaultConsistencyLevel (com.datastax.oss.driver.api.core.DefaultConsistencyLevel)1 DriverException (com.datastax.oss.driver.api.core.DriverException)1 BatchStatement (com.datastax.oss.driver.api.core.cql.BatchStatement)1 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 Statement (com.datastax.oss.driver.api.core.cql.Statement)1 Node (com.datastax.oss.driver.api.core.metadata.Node)1 Entry (java.util.Map.Entry)1