@Override protected Connection tryReconnect() throws ConnectionException { if (isShutdown) throw new ConnectionException(null, "Control connection was shut down"); try { return reconnectInternal(queryPlan(), false); } catch (NoHostAvailableException e) { throw new ConnectionException(null, e.getMessage()); } catch (UnsupportedProtocolVersionException e) { // reconnectInternal only propagate those if we've not decided on the protocol version // yet, // which should only happen on the initial connection and thus in connect() but never // here. throw new AssertionError(); } }
@Override public ConnectionException copy() { return new ConnectionException(address, getRawMessage(), this); }
final CloseFuture closeAsync() { CloseFuture future = closeFuture.get(); if (future != null) return future; phase.set(Phase.CLOSING); for (PendingBorrow pendingBorrow : pendingBorrows) { pendingBorrow.setException( new ConnectionException(host.getSocketAddress(), "Pool is shutdown")); } future = new CloseFuture.Forwarding(discardAvailableConnections()); return closeFuture.compareAndSet(null, future) ? future : closeFuture.get(); // We raced, it's ok, return the future that was actually set }
@Override public void onSuccess(List<Void> l) { // Some of the connections might have failed, keep only the successful ones ListIterator<Connection> it = connections.listIterator(); while (it.hasNext()) { if (it.next().isClosed()) it.remove(); } HostConnectionPool.this.connections.addAll(connections); open.set(connections.size()); if (isClosed()) { initFuture.setException( new ConnectionException( host.getSocketAddress(), "Pool was closed during initialization")); // we're not sure if closeAsync() saw the connections, so ensure they get closed forceClose(connections); } else { logger.debug( "Created connection pool to host {} ({} connections needed, {} successfully opened)", host, coreSize, connections.size()); phase.compareAndSet(Phase.INITIALIZING, Phase.READY); initFuture.set(null); } }
void setKeyspace(String keyspace) throws ConnectionException { if (keyspace == null) return; if (MoreObjects.equal(keyspace(), keyspace)) return; try { Uninterruptibles.getUninterruptibly(setKeyspaceAsync(keyspace)); } catch (ConnectionException e) { throw defunct(e); } catch (BusyConnectionException e) { logger.warn( "Tried to set the keyspace on busy {}. " + "This should not happen but is not critical (it will be retried)", this); throw new ConnectionException(address, "Tried to set the keyspace on busy connection"); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof OperationTimedOutException) { // Rethrow so that the caller doesn't try to use the connection, but do not defunct as we // don't want to mark down logger.warn( "Timeout while setting keyspace on {}. " + "This should not happen but is not critical (it will be retried)", this); throw new ConnectionException(address, "Timeout while setting keyspace on connection"); } else { throw defunct(new ConnectionException(address, "Error while setting keyspace", cause)); } } }
@Override public ListenableFuture<Void> apply(Throwable t) throws Exception { SettableFuture<Void> future = SettableFuture.create(); // Make sure the connection gets properly closed. if (t instanceof ClusterNameMismatchException || t instanceof UnsupportedProtocolVersionException) { // Just propagate closeAsync().force(); future.setException(t); } else { // Defunct to ensure that the error will be signaled (marking the host down) Throwable e = (t instanceof ConnectionException || t instanceof DriverException || t instanceof InterruptedException || t instanceof Error) ? t : new ConnectionException( Connection.this.address, String.format( "Unexpected error during transport initialization (%s)", t), t); future.setException(defunct(e)); } return future; } },
/** @jira_ticket JAVA-1139 */ @Test(groups = "unit") public void getMessage_should_return_message_if_address_is_resolved() { assertEquals( new ConnectionException(new InetSocketAddress("127.0.0.1", 9042), "Test message") .getMessage(), "[/127.0.0.1:9042] Test message"); } }
/** @jira_ticket JAVA-1139 */ @Test(groups = "unit") public void getMessage_should_return_message_if_address_is_unresolved() { assertEquals( new ConnectionException( InetSocketAddress.createUnresolved("127.0.0.1", 9042), "Test message") .getMessage(), "[127.0.0.1:9042] Test message"); }
/** @jira_ticket JAVA-1139 */ @Test(groups = "unit") public void getHost_should_return_null_if_address_is_null() { assertNull(new ConnectionException(null, "Test message").getHost()); }
/** @jira_ticket JAVA-1139 */ @Test(groups = "unit") public void getMessage_should_return_message_if_address_is_null() { assertEquals(new ConnectionException(null, "Test message").getMessage(), "Test message"); }
protected Connection tryReconnect() throws ConnectionException { tries += 1; logger.debug("in reconnection work, wait for tick from main thread"); try { barrier.await(60, TimeUnit.SECONDS); logger.debug("in reconnection work, got tick from main thread, proceeding"); } catch (Exception e) { fail("Error while waiting for tick", e); } switch (nextReconnect) { case SUCCEED: logger.debug("simulate reconnection success"); return null; case THROW_EXCEPTION: logger.debug("simulate reconnection error"); throw new ConnectionException( new InetSocketAddress(8888), "Simulated exception from mock reconnection"); default: throw new AssertionError(); } }
private ListenableFuture<Connection> enqueue(long timeout, TimeUnit unit, int maxQueueSize) { if (timeout == 0 || maxQueueSize == 0) { return Futures.immediateFailedFuture(new BusyPoolException(host.getSocketAddress(), 0)); } while (true) { int count = pendingBorrowCount.get(); if (count >= maxQueueSize) { return Futures.immediateFailedFuture( new BusyPoolException(host.getSocketAddress(), maxQueueSize)); } if (pendingBorrowCount.compareAndSet(count, count + 1)) { break; } } PendingBorrow pendingBorrow = new PendingBorrow(timeout, unit, timeoutsExecutor); pendingBorrows.add(pendingBorrow); // If we raced with shutdown, make sure the future will be completed. This has no effect if it // was properly // handled in closeAsync. if (phase.get() == Phase.CLOSING) { pendingBorrow.setException( new ConnectionException(host.getSocketAddress(), "Pool is shutdown")); } return pendingBorrow.future; }
new ConnectionException( connection.address, String.format("Got unexpected %s result response", rm.kind))); new ConnectionException( connection.address, String.format("Got unexpected %s response", response.type)));
ResponseHandler write( ResponseCallback callback, long statementReadTimeoutMillis, boolean startTimeout) throws ConnectionException, BusyConnectionException { ResponseHandler handler = new ResponseHandler(this, statementReadTimeoutMillis, callback); dispatcher.add(handler); Message.Request request = callback.request().setStreamId(handler.streamId); /* * We check for close/defunct *after* having set the handler because closing/defuncting * will set their flag and then error out handler if need. So, by doing the check after * having set the handler, we guarantee that even if we race with defunct/close, we may * never leave a handler that won't get an answer or be errored out. */ if (isDefunct.get()) { dispatcher.removeHandler(handler, true); throw new ConnectionException(address, "Write attempt on defunct connection"); } if (isClosed()) { dispatcher.removeHandler(handler, true); throw new ConnectionException(address, "Connection has been closed"); } logger.trace("{}, stream {}, writing request {}", this, request.getStreamId(), request); writer.incrementAndGet(); if (DISABLE_COALESCING) { channel.writeAndFlush(request).addListener(writeHandler(request, handler)); } else { flush(new FlushItem(channel, request, writeHandler(request, handler))); } if (startTimeout) handler.startTimeout(); return handler; }
/** * Opens a new connection to the node this factory points to. * * @return the newly created (and initialized) connection. * @throws ConnectionException if connection attempt fails. */ Connection open(Host host) throws ConnectionException, InterruptedException, UnsupportedProtocolVersionException, ClusterNameMismatchException { InetSocketAddress address = host.getSocketAddress(); if (isShutdown) throw new ConnectionException(address, "Connection factory is shut down"); host.convictionPolicy.signalConnectionsOpening(1); Connection connection = new Connection(buildConnectionName(host), address, this); // This method opens the connection synchronously, so wait until it's initialized try { connection.initAsync().get(); return connection; } catch (ExecutionException e) { throw launderAsyncInitException(e); } }
if (phase != Phase.READY) return Futures.immediateFailedFuture( new ConnectionException(host.getSocketAddress(), "Pool is " + phase)); new ConnectionException(host.getSocketAddress(), "Pool is shutdown"));
new ConnectionException( connection.address, String.format("Got unexpected %s response", response.type))); setException(
ListenableFuture<Void> initAsync() { if (factory.isShutdown) return Futures.immediateFailedFuture( new ConnectionException(address, "Connection factory is shut down"));
/** @jira_ticket JAVA-1139 */ @Test(groups = "unit") public void getHost_should_return_null_if_address_is_null() { assertNull(new ConnectionException(null, "Test message").getHost()); }
/** @jira_ticket JAVA-1139 */ @Test(groups = "unit") public void getMessage_should_return_message_if_address_is_unresolved() { assertEquals( new ConnectionException( InetSocketAddress.createUnresolved("127.0.0.1", 9042), "Test message") .getMessage(), "[127.0.0.1:9042] Test message"); }