Search in sources :

Example 1 with ReadTimeoutException

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

Example 2 with ReadTimeoutException

use of com.datastax.driver.core.exceptions.ReadTimeoutException in project java-driver by datastax.

the class ClusterWidePercentileTrackerTest method should_track_all_measurements_for_cluster.

@Test(groups = "unit")
public void should_track_all_measurements_for_cluster() {
    // given - a cluster wide percentile tracker.
    Cluster cluster0 = mock(Cluster.class);
    ClusterWidePercentileTracker tracker = builder().withInterval(1, TimeUnit.SECONDS).withMinRecordedValues(100).build();
    tracker.onRegister(cluster0);
    List<Host> hosts = Lists.newArrayList(mock(Host.class), mock(Host.class), mock(Host.class));
    List<Statement> statements = Lists.newArrayList(mock(Statement.class), mock(Statement.class));
    List<Exception> exceptions = Lists.newArrayList(new Exception(), null, new ReadTimeoutException(ConsistencyLevel.ANY, 1, 1, true), null, null);
    // when - recording latencies over a linear progression with varying hosts, statements and exceptions.
    for (int i = 0; i < 100; i++) {
        tracker.update(hosts.get(i % hosts.size()), statements.get(i % statements.size()), exceptions.get(i % exceptions.size()), TimeUnit.NANOSECONDS.convert(i + 1, TimeUnit.MILLISECONDS));
    }
    Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
    // then - the resulting tracker's percentiles should represent that linear progression. (x percentile == x)
    for (int i = 1; i <= 99; i++) {
        long latencyAtPct = tracker.getLatencyAtPercentile(null, null, null, i);
        assertThat(latencyAtPct).isEqualTo(i);
    }
}
Also used : ReadTimeoutException(com.datastax.driver.core.exceptions.ReadTimeoutException) ReadTimeoutException(com.datastax.driver.core.exceptions.ReadTimeoutException) Test(org.testng.annotations.Test)

Example 3 with ReadTimeoutException

use of com.datastax.driver.core.exceptions.ReadTimeoutException in project nifi by apache.

the class QueryCassandraTest method testProcessorNoInputFlowFileAndExceptions.

@Test
public void testProcessorNoInputFlowFileAndExceptions() {
    setUpStandardProcessorConfig();
    // Test no input flowfile
    testRunner.setIncomingConnection(false);
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_SUCCESS, 1);
    testRunner.clearTransferState();
    // Test exceptions
    processor.setExceptionToThrow(new NoHostAvailableException(new HashMap<InetSocketAddress, Throwable>()));
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_RETRY, 1);
    testRunner.clearTransferState();
    processor.setExceptionToThrow(new ReadTimeoutException(new InetSocketAddress("localhost", 9042), ConsistencyLevel.ANY, 0, 1, false));
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_RETRY, 1);
    testRunner.clearTransferState();
    processor.setExceptionToThrow(new InvalidQueryException(new InetSocketAddress("localhost", 9042), "invalid query"));
    testRunner.run(1, true, true);
    // No files transferred to failure if there was no incoming connection
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_FAILURE, 0);
    testRunner.clearTransferState();
    processor.setExceptionToThrow(new ProcessException());
    testRunner.run(1, true, true);
    // No files transferred to failure if there was no incoming connection
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_FAILURE, 0);
    testRunner.clearTransferState();
    processor.setExceptionToThrow(null);
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) HashMap(java.util.HashMap) ReadTimeoutException(com.datastax.driver.core.exceptions.ReadTimeoutException) InetSocketAddress(java.net.InetSocketAddress) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) Test(org.junit.Test)

Example 4 with ReadTimeoutException

use of com.datastax.driver.core.exceptions.ReadTimeoutException in project nifi by apache.

the class QueryCassandraTest method testProcessorEmptyFlowFileAndExceptions.

@Test
public void testProcessorEmptyFlowFileAndExceptions() {
    setUpStandardProcessorConfig();
    // Run with empty flowfile
    testRunner.setIncomingConnection(true);
    processor.setExceptionToThrow(null);
    testRunner.enqueue("".getBytes());
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_SUCCESS, 1);
    testRunner.clearTransferState();
    // Test exceptions
    processor.setExceptionToThrow(new NoHostAvailableException(new HashMap<InetSocketAddress, Throwable>()));
    testRunner.enqueue("".getBytes());
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_RETRY, 1);
    testRunner.clearTransferState();
    processor.setExceptionToThrow(new ReadTimeoutException(new InetSocketAddress("localhost", 9042), ConsistencyLevel.ANY, 0, 1, false));
    testRunner.enqueue("".getBytes());
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_RETRY, 1);
    testRunner.clearTransferState();
    processor.setExceptionToThrow(new InvalidQueryException(new InetSocketAddress("localhost", 9042), "invalid query"));
    testRunner.enqueue("".getBytes());
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_FAILURE, 1);
    testRunner.clearTransferState();
    processor.setExceptionToThrow(new ProcessException());
    testRunner.enqueue("".getBytes());
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_FAILURE, 1);
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) HashMap(java.util.HashMap) ReadTimeoutException(com.datastax.driver.core.exceptions.ReadTimeoutException) InetSocketAddress(java.net.InetSocketAddress) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) Test(org.junit.Test)

Example 5 with ReadTimeoutException

use of com.datastax.driver.core.exceptions.ReadTimeoutException in project java-driver by datastax.

the class CustomPercentileTrackerTest method should_return_negative_value_when_key_cant_be_computed.

@Test(groups = "unit")
public void should_return_negative_value_when_key_cant_be_computed() {
    // given - A custom tracker that returns null for a specific host and keys by host otherwise.
    final Cluster cluster0 = mock(Cluster.class);
    final Host host0 = mock(Host.class);
    final Host host1 = mock(Host.class);
    PercentileTracker tracker = new PercentileTracker(1000, 3, 100, 50) {

        @Override
        protected Object computeKey(Host host, Statement statement, Exception exception) {
            if (host == host0) {
                return host;
            } else {
                return null;
            }
        }
    };
    tracker.onRegister(cluster0);
    List<Statement> statements = Lists.newArrayList(mock(Statement.class), mock(Statement.class));
    List<Exception> exceptions = Lists.newArrayList(new Exception(), null, new ReadTimeoutException(ConsistencyLevel.ANY, 1, 1, true), null, null);
    // when - recording latencies over a linear progression with varying hosts, statements and exceptions.
    for (int i = 0; i < 100; i++) {
        tracker.update(host0, statements.get(i % statements.size()), exceptions.get(i % exceptions.size()), TimeUnit.NANOSECONDS.convert((i + 1) * 2, TimeUnit.MILLISECONDS));
        tracker.update(host1, statements.get(i % statements.size()), exceptions.get(i % exceptions.size()), TimeUnit.NANOSECONDS.convert(i + 1, TimeUnit.MILLISECONDS));
    }
    Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
    // host1 should return -1 since it has no tracker since it has no key.
    for (int i = 1; i <= 99; i++) {
        assertThat(tracker.getLatencyAtPercentile(host0, null, null, i)).isEqualTo(i * 2);
        assertThat(tracker.getLatencyAtPercentile(host1, null, null, i)).isEqualTo(-1);
    }
}
Also used : ReadTimeoutException(com.datastax.driver.core.exceptions.ReadTimeoutException) ReadTimeoutException(com.datastax.driver.core.exceptions.ReadTimeoutException) Test(org.testng.annotations.Test)

Aggregations

ReadTimeoutException (com.datastax.driver.core.exceptions.ReadTimeoutException)6 Test (org.testng.annotations.Test)3 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)2 NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)2 InetSocketAddress (java.net.InetSocketAddress)2 HashMap (java.util.HashMap)2 ProcessException (org.apache.nifi.processor.exception.ProcessException)2 Test (org.junit.Test)2 BatchStatement (com.datastax.driver.core.BatchStatement)1 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 ResultSet (com.datastax.driver.core.ResultSet)1 SimpleStatement (com.datastax.driver.core.SimpleStatement)1 Statement (com.datastax.driver.core.Statement)1 DriverException (com.datastax.driver.core.exceptions.DriverException)1 UnavailableException (com.datastax.driver.core.exceptions.UnavailableException)1