Search in sources :

Example 11 with NoHostAvailableException

use of com.datastax.driver.core.exceptions.NoHostAvailableException 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 12 with NoHostAvailableException

use of com.datastax.driver.core.exceptions.NoHostAvailableException 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 13 with NoHostAvailableException

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

the class ClusterInitTest method should_be_able_to_close_cluster_that_never_successfully_connected.

/**
 * Validates that a Cluster that was never able to successfully establish connection a session can be closed
 * properly.
 *
 * @test_category connection
 * @expected_result Cluster closes within 1 second.
 */
@Test(groups = "short")
public void should_be_able_to_close_cluster_that_never_successfully_connected() throws Exception {
    Cluster cluster = Cluster.builder().addContactPointsWithPorts(new InetSocketAddress("127.0.0.1", 65534)).withNettyOptions(nonQuietClusterCloseOptions).build();
    try {
        cluster.connect();
        fail("Should not have been able to connect.");
    } catch (NoHostAvailableException e) {
        // Expected.
        CloseFuture closeFuture = cluster.closeAsync();
        try {
            closeFuture.get(1, TimeUnit.SECONDS);
        } catch (TimeoutException e1) {
            fail("Close Future did not complete quickly.");
        }
    } finally {
        cluster.close();
    }
}
Also used : NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) InetSocketAddress(java.net.InetSocketAddress) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 14 with NoHostAvailableException

use of com.datastax.driver.core.exceptions.NoHostAvailableException in project presto by prestodb.

the class NativeCassandraSession method executeWithSession.

private <T> T executeWithSession(SessionCallable<T> sessionCallable) {
    ReconnectionPolicy reconnectionPolicy = cluster.getConfiguration().getPolicies().getReconnectionPolicy();
    ReconnectionSchedule schedule = reconnectionPolicy.newSchedule();
    long deadline = System.currentTimeMillis() + noHostAvailableRetryTimeout.toMillis();
    while (true) {
        try {
            return sessionCallable.executeWithSession(session.get());
        } catch (NoHostAvailableException e) {
            long timeLeft = deadline - System.currentTimeMillis();
            if (timeLeft <= 0) {
                throw e;
            } else {
                long delay = Math.min(schedule.nextDelayMs(), timeLeft);
                log.warn(e.getCustomMessage(10, true, true));
                log.warn("Reconnecting in %dms", delay);
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException interrupted) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException("interrupted", interrupted);
                }
            }
        }
    }
}
Also used : ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) ReconnectionSchedule(com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule)

Example 15 with NoHostAvailableException

use of com.datastax.driver.core.exceptions.NoHostAvailableException in project cassandra by apache.

the class ViewLongTest method testConflictResolution.

@Test
public void testConflictResolution() throws Throwable {
    final int writers = 96;
    final int insertsPerWriter = 50;
    final Map<Integer, Exception> failedWrites = new ConcurrentHashMap<>();
    createTable("CREATE TABLE %s (" + "a int," + "b int," + "c int," + "PRIMARY KEY (a, b))");
    createView("CREATE MATERIALIZED VIEW %s AS SELECT * FROM %s " + "WHERE c IS NOT NULL AND a IS NOT NULL AND b IS NOT NULL " + "PRIMARY KEY (c, a, b)");
    CyclicBarrier semaphore = new CyclicBarrier(writers);
    Thread[] threads = new Thread[writers];
    for (int i = 0; i < writers; i++) {
        final int writer = i;
        Thread t = NamedThreadFactory.createAnonymousThread(new WrappedRunnable() {

            public void runMayThrow() {
                try {
                    int writerOffset = writer * insertsPerWriter;
                    semaphore.await();
                    for (int i = 0; i < insertsPerWriter; i++) {
                        try {
                            executeNet("INSERT INTO %s (a, b, c) VALUES (?, ?, ?) USING TIMESTAMP 1", 1, 1, i + writerOffset);
                        } catch (NoHostAvailableException | WriteTimeoutException e) {
                            failedWrites.put(i + writerOffset, e);
                        }
                    }
                } catch (Throwable e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.start();
        threads[i] = t;
    }
    for (int i = 0; i < writers; i++) threads[i].join();
    for (int i = 0; i < writers * insertsPerWriter; i++) {
        if (executeNet("SELECT COUNT(*) FROM system.batches").one().getLong(0) == 0)
            break;
        try {
            // This will throw exceptions whenever there are exceptions trying to push the view values out, caused
            // by the view becoming overwhelmed.
            BatchlogManager.instance.startBatchlogReplay().get();
        } catch (Throwable ignore) {
        }
    }
    int value = executeNet("SELECT c FROM %s WHERE a = 1 AND b = 1").one().getInt("c");
    List<Row> rows = executeNet("SELECT c FROM " + keyspace() + "." + currentView()).all();
    boolean containsC = false;
    StringBuilder others = new StringBuilder();
    StringBuilder overlappingFailedWrites = new StringBuilder();
    for (Row row : rows) {
        int c = row.getInt("c");
        if (c == value)
            containsC = true;
        else {
            if (others.length() != 0)
                others.append(' ');
            others.append(c);
            if (failedWrites.containsKey(c)) {
                if (overlappingFailedWrites.length() != 0)
                    overlappingFailedWrites.append(' ');
                overlappingFailedWrites.append(c).append(':').append(failedWrites.get(c).getMessage());
            }
        }
    }
    if (rows.size() > 1) {
        throw new AssertionError(String.format("Expected 1 row, but found %d; %s c = %d, " + "and (%s) of which (%s) failed to insert", rows.size(), containsC ? "found row with" : "no rows contained", value, others, overlappingFailedWrites));
    } else if (rows.isEmpty()) {
        throw new AssertionError(String.format("Could not find row with c = %d", value));
    } else if (!containsC) {
        throw new AssertionError(String.format("Single row had c = %d, expected %d", rows.get(0).getInt("c"), value));
    }
}
Also used : WrappedRunnable(org.apache.cassandra.utils.WrappedRunnable) WriteTimeoutException(com.datastax.driver.core.exceptions.WriteTimeoutException) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Row(com.datastax.driver.core.Row) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)18 Test (org.junit.Test)8 ProcessException (org.apache.nifi.processor.exception.ProcessException)5 InetSocketAddress (java.net.InetSocketAddress)4 Session (com.datastax.driver.core.Session)3 HashMap (java.util.HashMap)3 TimeoutException (java.util.concurrent.TimeoutException)3 ToolResult (org.apache.cassandra.tools.ToolRunner.ToolResult)3 ComponentLog (org.apache.nifi.logging.ComponentLog)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)2 ResultSet (com.datastax.driver.core.ResultSet)2 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)2 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)2 QueryExecutionException (com.datastax.driver.core.exceptions.QueryExecutionException)2 QueryValidationException (com.datastax.driver.core.exceptions.QueryValidationException)2 ReadTimeoutException (com.datastax.driver.core.exceptions.ReadTimeoutException)2 Charset (java.nio.charset.Charset)2 FlowFile (org.apache.nifi.flowfile.FlowFile)2 ProcessSession (org.apache.nifi.processor.ProcessSession)2 CassandraConf (org.apache.storm.cassandra.client.CassandraConf)2