Search in sources :

Example 1 with ClosedConnectionException

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

the class InFlightHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object in, ChannelPromise promise) throws Exception {
    if (in == DriverChannel.GRACEFUL_CLOSE_MESSAGE) {
        LOG.debug("[{}] Received graceful close request", logPrefix);
        startGracefulShutdown(ctx);
    } else if (in == DriverChannel.FORCEFUL_CLOSE_MESSAGE) {
        LOG.debug("[{}] Received forceful close request, aborting pending queries", logPrefix);
        abortAllInFlight(new ClosedConnectionException("Channel was force-closed"));
        ctx.channel().close();
    } else if (in instanceof HeartbeatException) {
        abortAllInFlight(new ClosedConnectionException("Heartbeat query failed", ((HeartbeatException) in)));
        ctx.close();
    } else if (in instanceof RequestMessage) {
        write(ctx, (RequestMessage) in, promise);
    } else if (in instanceof ResponseCallback) {
        cancel(ctx, (ResponseCallback) in, promise);
    } else {
        promise.setFailure(new IllegalArgumentException("Unsupported message type " + in.getClass().getName()));
    }
}
Also used : ClosedConnectionException(com.datastax.oss.driver.api.core.connection.ClosedConnectionException) HeartbeatException(com.datastax.oss.driver.api.core.connection.HeartbeatException) RequestMessage(com.datastax.oss.driver.internal.core.channel.DriverChannel.RequestMessage)

Example 2 with ClosedConnectionException

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

the class InFlightHandler method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    // If the channel was closed normally (normal or forced shutdown), inFlight is already empty by
    // the time we get here. So if it's not, it means the channel closed unexpectedly (e.g. the
    // connection was dropped).
    abortAllInFlight(new ClosedConnectionException("Lost connection to remote peer"));
    super.channelInactive(ctx);
}
Also used : ClosedConnectionException(com.datastax.oss.driver.api.core.connection.ClosedConnectionException)

Example 3 with ClosedConnectionException

use of com.datastax.oss.driver.api.core.connection.ClosedConnectionException 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

ClosedConnectionException (com.datastax.oss.driver.api.core.connection.ClosedConnectionException)3 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)1 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 HeartbeatException (com.datastax.oss.driver.api.core.connection.HeartbeatException)1 RequestMessage (com.datastax.oss.driver.internal.core.channel.DriverChannel.RequestMessage)1 ConcurrentSkipListSet (java.util.concurrent.ConcurrentSkipListSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Semaphore (java.util.concurrent.Semaphore)1 Test (org.junit.Test)1