Refine search
@Test(groups = "unit") public void should_initialize_to_v2_defaults_if_v2_or_below() { PoolingOptions options = new PoolingOptions(); options.setProtocolVersion(ProtocolVersion.V1); assertThat(options.getCoreConnectionsPerHost(LOCAL)).isEqualTo(2); assertThat(options.getMaxConnectionsPerHost(LOCAL)).isEqualTo(8); assertThat(options.getCoreConnectionsPerHost(REMOTE)).isEqualTo(1); assertThat(options.getMaxConnectionsPerHost(REMOTE)).isEqualTo(2); assertThat(options.getNewConnectionThreshold(LOCAL)).isEqualTo(100); assertThat(options.getNewConnectionThreshold(REMOTE)).isEqualTo(100); assertThat(options.getMaxRequestsPerConnection(LOCAL)).isEqualTo(128); assertThat(options.getMaxRequestsPerConnection(REMOTE)).isEqualTo(128); }
INIT_COUNT.incrementAndGet(); cluster = Cluster.builder().withCredentials(username, password) .withPort(Integer.valueOf(port)).addContactPoints(hosts).build(); } else { cluster = Cluster.builder().withPort(Integer.valueOf(port)) .addContactPoints(hosts).build(); MAX_CONNECTIONS_PROPERTY); if (maxConnections != null) { cluster.getConfiguration().getPoolingOptions() .setMaxConnectionsPerHost(HostDistance.LOCAL, Integer.valueOf(maxConnections)); CORE_CONNECTIONS_PROPERTY); if (coreConnections != null) { cluster.getConfiguration().getPoolingOptions() .setCoreConnectionsPerHost(HostDistance.LOCAL, Integer.valueOf(coreConnections)); CONNECT_TIMEOUT_MILLIS_PROPERTY); if (connectTimoutMillis != null) { cluster.getConfiguration().getSocketOptions() .setConnectTimeoutMillis(Integer.valueOf(connectTimoutMillis));
@Test(groups = "unit") public void should_reject_negative_connection_options_even_when_protocol_version_unknown() { PoolingOptions options = new PoolingOptions(); options.setCoreConnectionsPerHost(LOCAL, -1); fail("expected an IllegalArgumentException"); } catch (IllegalArgumentException e) { options.setMaxConnectionsPerHost(LOCAL, -1); fail("expected an IllegalArgumentException"); } catch (IllegalArgumentException e) { options.setConnectionsPerHost(LOCAL, -1, 1); fail("expected an IllegalArgumentException"); } catch (IllegalArgumentException e) { options.setConnectionsPerHost(LOCAL, -2, -1); fail("expected an IllegalArgumentException"); } catch (IllegalArgumentException e) { options.setNewConnectionThreshold(LOCAL, -1); fail("expected an IllegalArgumentException"); } catch (IllegalArgumentException e) { options.setMaxRequestsPerConnection(LOCAL, -1); fail("expected an IllegalArgumentException"); } catch (IllegalArgumentException e) {
CassandraConf cassandraConf = new CassandraConf(topoConf); Cluster.Builder cluster = Cluster.builder() .withoutJMXReporting() .withoutMetrics() .addContactPoints(cassandraConf.getNodes()) .withPort(cassandraConf.getPort()) .withRetryPolicy(cassandraConf.getRetryPolicy()) cassandraConf.getReconnectionPolicyMaxMs())) .withLoadBalancingPolicy(cassandraConf.getLoadBalancingPolicy()); cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis((int) cassandraConf.getSocketReadTimeoutMillis()); cluster.getConfiguration().getSocketOptions().setConnectTimeoutMillis((int) cassandraConf.getSocketConnectTimeoutMillis()); cluster.withQueryOptions(options); PoolingOptions poolOps = new PoolingOptions(); poolOps.setMaxQueueSize(cassandraConf.getPoolMaxQueueSize()); poolOps.setHeartbeatIntervalSeconds(cassandraConf.getHeartbeatIntervalSeconds()); poolOps.setIdleTimeoutSeconds(cassandraConf.getIdleTimeoutSeconds()); poolOps.setMaxRequestsPerConnection(HostDistance.LOCAL, cassandraConf.getMaxRequestPerConnectionLocal()); poolOps.setMaxRequestsPerConnection(HostDistance.REMOTE, cassandraConf.getMaxRequestPerConnectionRemote()); cluster.withPoolingOptions(poolOps);
final PoolingOptions poolingOptions = new PoolingOptions() .setCoreConnectionsPerHost(HostDistance.LOCAL, cassandraConfig.getConnections()) .setMaxConnectionsPerHost(HostDistance.LOCAL, cassandraConfig.getConnections()) .setIdleTimeoutSeconds( cassandraConfig.getPoolTimeout() / 1000 ) .setPoolTimeoutMillis( cassandraConfig.getPoolTimeout()); .setMetadataEnabled(true); // choose whether to have the driver store metadata such as schema info Cluster.Builder datastaxCluster = Cluster.builder() .withClusterName(cassandraConfig.getClusterName()) .addContactPoints(cassandraConfig.getHosts().split(",")) .withMaxSchemaAgreementWaitSeconds(45) .withCompression(ProtocolOptions.Compression.LZ4) .withLoadBalancingPolicy(loadBalancingPolicy)
void ensureCoreConnections() { if (isClosed()) return; if (!host.convictionPolicy.canReconnectNow()) return; // Note: this process is a bit racy, but it doesn't matter since we're still guaranteed to not // create // more connection than maximum (and if we create more than core connection due to a race but // this isn't // justified by the load, the connection in excess will be quickly trashed anyway) int opened = open.get(); for (int i = opened; i < options().getCoreConnectionsPerHost(hostDistance); i++) { // We don't respect MAX_SIMULTANEOUS_CREATION here because it's only to // protect against creating connection in excess of core too quickly scheduledForCreation.incrementAndGet(); manager.blockingExecutor().submit(newConnectionTask); } }
int coreSize = options().getCoreConnectionsPerHost(hostDistance); if (coreSize == 0) { maybeSpawnNewConnection(); } else if (scheduledForCreation.compareAndSet(0, coreSize)) { for (int i = 0; i < coreSize; i++) { Connection leastBusy = null; for (Connection connection : connections) { int inFlight = connection.inFlight.get(); if (inFlight < minInFlight) { minInFlight = inFlight; } else { while (true) { int inFlight = leastBusy.inFlight.get(); options().getMaxRequestsPerConnection(hostDistance))) { return enqueue(timeout, unit, maxQueueSize); int connectionCount = open.get() + scheduledForCreation.get(); if (connectionCount < options().getCoreConnectionsPerHost(hostDistance)) { maybeSpawnNewConnection(); } else if (connectionCount < options().getMaxConnectionsPerHost(hostDistance)) { (connectionCount - 1) * options().getMaxRequestsPerConnection(hostDistance) + options().getNewConnectionThreshold(hostDistance); if (totalInFlightCount > currentCapacity) maybeSpawnNewConnection();
@Test(groups = "long") public void should_resurrect_trashed_connection_within_idle_timeout() throws Exception { Cluster cluster = createClusterBuilder() .withPoolingOptions(new PoolingOptions().setIdleTimeoutSeconds(20)) .build(); List<MockRequest> allRequests = newArrayList(); try { Connection connection2 = pool.connections.get(1); assertThat(connection1.inFlight.get()).isEqualTo(101); assertThat(connection2.inFlight.get()).isEqualTo(0); assertThat(connection1.inFlight.get()).isEqualTo(50); assertThat(connection2.inFlight.get()).isEqualTo(0); } finally { MockRequest.completeAll(allRequests); cluster.close();
private boolean trashConnection(Connection connection) { if (!connection.state.compareAndSet(OPEN, TRASHED)) return true; // First, make sure we don't go below core connections for (; ; ) { int opened = open.get(); if (opened <= options().getCoreConnectionsPerHost(hostDistance)) { connection.state.set(OPEN); return false; } if (open.compareAndSet(opened, opened - 1)) break; } logger.trace("Trashing {}", connection); connection.maxIdleTime = System.currentTimeMillis() + options().getIdleTimeoutSeconds() * 1000; doTrashConnection(connection); return true; }
@Test(groups = "long") public void should_handle_race_between_response_and_cancellation() { final Scassandra scassandra = TestUtils.createScassandraServer(); Cluster.builder() .addContactPoint(TestUtils.ipOfNode(1)) .withPort(scassandra.getBinaryPort()) .withPoolingOptions( new PoolingOptions() .setCoreConnectionsPerHost(HostDistance.LOCAL, 1) .setMaxConnectionsPerHost(HostDistance.LOCAL, 1) .setHeartbeatIntervalSeconds(0)) .build(); Session session = cluster.connect(); assertThat(connection.inFlight.get()).isEqualTo(0); } finally { if (cluster != null) cluster.close(); scassandra.stop();
@Test(groups = "long") public void should_send_heartbeat_when_requests_being_written_but_nothing_received() throws Exception { Cluster cluster = Cluster.builder() .addContactPoints(hostAddress.getAddress()) .withPort(scassandra.getBinaryPort()) .withPoolingOptions( new PoolingOptions() .setHeartbeatIntervalSeconds(3) .setConnectionsPerHost(HostDistance.LOCAL, 1, 1)) .build(); try { cluster.init(); SessionManager session = (SessionManager) cluster.connect(); Host host = TestUtils.findHost(cluster, 1); Connection connection = session.pools.get(host).connections.get(0); assertNoLineMatches(log, heartbeatSentPattern); int inFlight = connection.inFlight.get(); assertThat(inFlight).isGreaterThan(0); assertThat(connection.inFlight.get()).isGreaterThan(inFlight); log = logs.getNext();
private boolean trashConnection(PooledConnection connection) { // First, make sure we don't go below core connections for(;;) { int opened = open.get(); if (opened <= options().getCoreConnectionsPerHost(hostDistance)) return false; if (open.compareAndSet(opened, opened - 1)) break; } doTrashConnection(connection); return true; }
/** If we have more active connections than needed, trash some of them */ private void shrinkIfBelowCapacity() { int currentLoad = maxTotalInFlight.getAndSet(totalInFlight.get()); int maxRequestsPerConnection = options().getMaxRequestsPerConnection(hostDistance); int needed = currentLoad / maxRequestsPerConnection + 1; if (currentLoad % maxRequestsPerConnection > options().getNewConnectionThreshold(hostDistance)) needed += 1; needed = Math.max(needed, options().getCoreConnectionsPerHost(hostDistance)); int actual = open.get(); int toTrash = Math.max(0, actual - needed); logger.trace( "Current inFlight = {}, {} connections needed, {} connections available, trashing {}", currentLoad, needed, actual, toTrash); if (toTrash <= 0) return; for (Connection connection : connections) if (trashConnection(connection)) { toTrash -= 1; if (toTrash == 0) return; } }
private void createCluster(int core, int max) { PoolingOptions poolingOptions = new PoolingOptions().setConnectionsPerHost(LOCAL, core, max); SocketOptions socketOptions = new SocketOptions().setReadTimeoutMillis(1000); cluster = Cluster.builder() .addContactPoints(scassandra.address(1).getAddress()) .withPort(scassandra.getBinaryPort()) .withQueryOptions(nonDebouncingQueryOptions()) .withPoolingOptions(poolingOptions) .withSocketOptions(socketOptions) .withReconnectionPolicy(new ConstantReconnectionPolicy(1000)) .build(); cluster.connect(); }
@Test(groups = "short") public void should_not_hang_when_executing_sync_queries() { primingClient.prime( .withReconnectionPolicy(new ConstantReconnectionPolicy(10000)) .build(); cluster.getConfiguration().getPoolingOptions().setPoolTimeoutMillis(500); try { Session session = cluster.connect(); try { session.execute("server_error query"); cluster.close();
@Test(groups = "long") public void should_not_send_heartbeat_when_disabled() throws InterruptedException { Cluster cluster = Cluster.builder() .addContactPoints(hostAddress.getAddress()) .withPort(scassandra.getBinaryPort()) .withPoolingOptions(new PoolingOptions().setHeartbeatIntervalSeconds(0)) .build(); cluster.init(); assertThat(logs.get()).doesNotContain("sending heartbeat"); } finally { cluster.close();
@Test(groups = "unit") public void should_set_core_and_max_connections_simultaneously() { PoolingOptions options = new PoolingOptions(); options.setProtocolVersion(ProtocolVersion.V2); options.setConnectionsPerHost(LOCAL, 10, 15); assertThat(options.getCoreConnectionsPerHost(LOCAL)).isEqualTo(10); assertThat(options.getMaxConnectionsPerHost(LOCAL)).isEqualTo(15); }
@Test(groups = "long") public void sessions_should_not_leak_connections() { Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withPoolingOptions( new PoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, 1)) .withNettyOptions(channelMonitor.nettyOptions()) .build(); stressCluster.init(); assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1);
@Test(groups = "short") public void should_release_connection_before_completing_future() throws Exception { Cluster cluster = null; Cluster.builder() .addContactPoints(hostAddress.getAddress()) .withPort(scassandra.getBinaryPort()) .withPoolingOptions( new PoolingOptions() .setCoreConnectionsPerHost(HostDistance.LOCAL, 1) .setMaxConnectionsPerHost(HostDistance.LOCAL, 1)) .build(); final Session session = cluster.connect("ks"); if (cluster != null) cluster.close();
/** * Ensures that authentication is possible even if the server is busy during SASL handshake. * * @jira_ticket JAVA-1429 */ @Test(groups = "short") @CCMConfig(dirtiesContext = true) public void should_connect_with_slow_server() { Cluster cluster = Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withAuthProvider(new SlowAuthProvider()) .withPoolingOptions(new PoolingOptions().setHeartbeatIntervalSeconds(1)) .build(); cluster.connect(); }