Search in sources :

Example 16 with AllNodesFailedException

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

the class ShutdownIT method should_fail_requests_when_session_is_closed.

@Test
public void should_fail_requests_when_session_is_closed() throws Exception {
    // Given
    // Prime with a bit of delay to increase the chance that a query will be aborted in flight when
    // we force-close the session
    SIMULACRON_RULE.cluster().prime(when(QUERY_STRING).then(noRows()).delay(20, TimeUnit.MILLISECONDS));
    CqlSession session = SessionUtils.newSession(SIMULACRON_RULE);
    // When
    // Max out the in-flight requests on the connection (from a separate thread pool to get a bit of
    // contention), then force-close the session abruptly.
    Set<String> unexpectedErrors = new ConcurrentSkipListSet<>();
    ExecutorService requestExecutor = Executors.newFixedThreadPool(4);
    int maxConcurrentRequests = session.getContext().getConfig().getDefaultProfile().getInt(DefaultDriverOption.CONNECTION_MAX_REQUESTS);
    Semaphore semaphore = new Semaphore(maxConcurrentRequests);
    CountDownLatch gotSessionClosedError = new CountDownLatch(1);
    for (int i = 0; i < 4; i++) {
        requestExecutor.execute(() -> {
            try {
                while (!Thread.currentThread().isInterrupted()) {
                    semaphore.acquire();
                    session.executeAsync(QUERY_STRING).whenComplete((ignoredResult, error) -> {
                        semaphore.release();
                        // => AllNodesFailedException wrapping IllegalStateException
                        if (error instanceof IllegalStateException && "Session is closed".equals(error.getMessage())) {
                            gotSessionClosedError.countDown();
                        } else if (error instanceof AllNodesFailedException) {
                            AllNodesFailedException anfe = (AllNodesFailedException) error;
                            // acceptable.
                            if (anfe.getAllErrors().size() > 0) {
                                assertThat(anfe.getAllErrors()).hasSize(1);
                                error = anfe.getAllErrors().values().iterator().next().get(0);
                                if (!(error instanceof IllegalStateException) && !error.getMessage().endsWith("is closing")) {
                                    unexpectedErrors.add(error.toString());
                                }
                            }
                        } else if (error != null && !(error instanceof ClosedConnectionException)) {
                            unexpectedErrors.add(error.toString());
                        }
                    });
                }
            } catch (InterruptedException e) {
            // return
            }
        });
    }
    TimeUnit.MILLISECONDS.sleep(1000);
    session.forceCloseAsync();
    assertThat(gotSessionClosedError.await(10, TimeUnit.SECONDS)).as("Expected to get the 'Session is closed' error shortly after shutting down").isTrue();
    requestExecutor.shutdownNow();
    // Then
    assertThat(unexpectedErrors).isEmpty();
}
Also used : AllNodesFailedException(com.datastax.oss.driver.api.core.AllNodesFailedException) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ClosedConnectionException(com.datastax.oss.driver.api.core.connection.ClosedConnectionException) ExecutorService(java.util.concurrent.ExecutorService) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Aggregations

AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)16 Test (org.junit.Test)16 CqlSession (com.datastax.oss.driver.api.core.CqlSession)8 Node (com.datastax.oss.driver.api.core.metadata.Node)8 List (java.util.List)6 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)5 SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)4 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)4 Error (com.datastax.oss.protocol.internal.response.Error)4 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)3 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)2 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)2 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)2 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)2 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)2 Statement (com.datastax.oss.driver.api.core.cql.Statement)2 CassandraRequirement (com.datastax.oss.driver.api.testinfra.CassandraRequirement)2 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)2 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)2