@Override public boolean apply(Host host) { return hosts.contains(host.getSocketAddress()); } };
/** * @return the previous host associated with this socket address, or {@code null} if there was no * such host. */ Host addIfAbsent(Host host) { return hosts.putIfAbsent(host.getSocketAddress(), host); }
boolean remove(Host host) { return hosts.remove(host.getSocketAddress()) != null; }
public static String hostMetricName(String prefix, Host host) { StringBuilder result = new StringBuilder(prefix); boolean first = true; for (byte b : host.getSocketAddress().getAddress().getAddress()) { if (first) { first = false; } else { result.append('_'); } result.append(b & 0xFF); } return result.toString(); } }
@Override public void onFailure(Throwable t) { if (t instanceof BusyPoolException) { logError(host.getSocketAddress(), t); } else { logger.error("Unexpected error while querying " + host.getAddress(), t); logError(host.getSocketAddress(), t); } findNextHostAndQuery(); } });
@Override public void run() { future.setException( new BusyPoolException(host.getSocketAddress(), timeout, unit)); } },
private String buildConnectionName(Host host) { return host.getSocketAddress().toString() + '-' + getIdGenerator(host).getAndIncrement(); }
/** * Creates new connections and associate them to the provided connection pool, but does not * start them. */ List<Connection> newConnections(HostConnectionPool pool, int count) { pool.host.convictionPolicy.signalConnectionsOpening(count); List<Connection> connections = Lists.newArrayListWithCapacity(count); for (int i = 0; i < count; i++) connections.add( new Connection( buildConnectionName(pool.host), pool.host.getSocketAddress(), this, pool)); return connections; }
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 }
private void onHostGone(Host host) { Connection current = connectionRef.get(); if (current != null && current.address.equals(host.getSocketAddress())) { logger.debug( "[Control connection] {} is down/removed and it was the control host, triggering reconnect", current.address); if (!current.isClosed()) current.closeAsync().force(); backgroundReconnect(0); } }
public HostAssert hasSocketAddress(InetSocketAddress address) { assertThat(actual.getSocketAddress()).isNotNull().isEqualTo(address); return this; }
@BeforeMethod(groups = "unit") public void setup() { MockitoAnnotations.initMocks(this); when(host1.getSocketAddress()).thenReturn(address1); when(host2.getSocketAddress()).thenReturn(address2); when(host3.getSocketAddress()).thenReturn(address3); when(wrappedPolicy.distance(any(Host.class))).thenReturn(HostDistance.LOCAL); }
/** Same as open, but associate the created connection to the provided connection pool. */ Connection open(HostConnectionPool pool) throws ConnectionException, InterruptedException, UnsupportedProtocolVersionException, ClusterNameMismatchException { pool.host.convictionPolicy.signalConnectionsOpening(1); Connection connection = new Connection(buildConnectionName(pool.host), pool.host.getSocketAddress(), this, pool); try { connection.initAsync().get(); return connection; } catch (ExecutionException e) { throw launderAsyncInitException(e); } }
public Host host(Cluster cluster, int dc, int node) { InetSocketAddress address = address(dc, node); for (Host host : cluster.getMetadata().getAllHosts()) { if (host.getSocketAddress().equals(address)) { return host; } } return null; }
@Test(groups = "short") public void should_throw_proper_unavailable_exception() { simulateError(1, unavailable); try { query(); fail("expected an UnavailableException"); } catch (UnavailableException e) { assertThat(e.getMessage()) .isEqualTo( "Not enough replicas available for query at consistency LOCAL_ONE (1 required but only 0 alive)"); assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_ONE); assertThat(e.getAliveReplicas()).isEqualTo(0); assertThat(e.getRequiredReplicas()).isEqualTo(1); assertThat(e.getAddress()).isEqualTo(host1.getSocketAddress()); assertThat(e.getHost()).isEqualTo(host1.getAddress()); } }
@Test(groups = "short") public void should_throw_proper_read_timeout_exception() { simulateError(1, read_request_timeout); try { query(); fail("expected a ReadTimeoutException"); } catch (ReadTimeoutException e) { assertThat(e.getMessage()) .isEqualTo( "Cassandra timeout during read query at consistency LOCAL_ONE (1 responses were required but only 0 replica responded)"); assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_ONE); assertThat(e.getReceivedAcknowledgements()).isEqualTo(0); assertThat(e.getRequiredAcknowledgements()).isEqualTo(1); assertThat(e.getAddress()).isEqualTo(host1.getSocketAddress()); assertThat(e.getHost()).isEqualTo(host1.getAddress()); } }
/** * @return a {@link Cluster} instance that connects only to the control host of the given cluster. */ public static Cluster buildControlCluster(Cluster cluster, CCMAccess ccm) { Host controlHost = cluster.manager.controlConnection.connectedHost(); List<InetSocketAddress> singleAddress = Collections.singletonList(controlHost.getSocketAddress()); return Cluster.builder() .addContactPoints(controlHost.getSocketAddress().getAddress()) .withPort(ccm.getBinaryPort()) .withLoadBalancingPolicy(new WhiteListPolicy(new RoundRobinPolicy(), singleAddress)) .build(); }
@Test(groups = "short") public void should_log_ignored_request_error() throws InterruptedException { simulateError(1, server_error); retryDecision = ignore(); query(); String line = appender.waitAndGet(5000); assertThat(line.trim()) .isEqualTo( expectedMessage( IGNORING_REQUEST_ERROR, defaultCL, 0, new ServerError(host1.getSocketAddress(), "Server Error").toString())); }
@Test(groups = "short") public void should_log_retried_request_error() throws InterruptedException { simulateError(1, server_error); retryDecision = tryNextHost(LOCAL_ONE); query(); String line = appender.waitAndGet(5000); assertThat(line.trim()) .isEqualTo( expectedMessage( RETRYING_ON_REQUEST_ERROR, "next host", LOCAL_ONE, defaultCL, 0, new ServerError(host1.getSocketAddress(), "Server Error").toString())); }
@Test(groups = "short") public void should_connect_with_credentials() { PlainTextAuthProvider authProvider = spy(new PlainTextAuthProvider("cassandra", "cassandra")); Cluster cluster = Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withAuthProvider(authProvider) .build(); cluster.connect(); verify(authProvider, atLeastOnce()) .newAuthenticator( findHost(cluster, 1).getSocketAddress(), "org.apache.cassandra.auth.PasswordAuthenticator"); assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount()) .isEqualTo(0); }