/** 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; } }
@Test(groups = "unit") public void should_leave_connection_options_unset_until_protocol_version_known() { PoolingOptions options = new PoolingOptions(); assertThat(options.getCoreConnectionsPerHost(LOCAL)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getCoreConnectionsPerHost(REMOTE)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getMaxConnectionsPerHost(LOCAL)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getMaxConnectionsPerHost(REMOTE)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getNewConnectionThreshold(LOCAL)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getNewConnectionThreshold(REMOTE)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getMaxRequestsPerConnection(LOCAL)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getMaxRequestsPerConnection(REMOTE)).isEqualTo(PoolingOptions.UNSET); }
@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); }
@Test(groups = "unit") public void should_initialize_to_v3_defaults_if_v3_or_above() { PoolingOptions options = new PoolingOptions(); options.setProtocolVersion(ProtocolVersion.V3); assertThat(options.getCoreConnectionsPerHost(LOCAL)).isEqualTo(1); assertThat(options.getMaxConnectionsPerHost(LOCAL)).isEqualTo(1); assertThat(options.getCoreConnectionsPerHost(REMOTE)).isEqualTo(1); assertThat(options.getMaxConnectionsPerHost(REMOTE)).isEqualTo(1); assertThat(options.getNewConnectionThreshold(LOCAL)).isEqualTo(800); assertThat(options.getNewConnectionThreshold(REMOTE)).isEqualTo(200); assertThat(options.getMaxRequestsPerConnection(LOCAL)).isEqualTo(1024); assertThat(options.getMaxRequestsPerConnection(REMOTE)).isEqualTo(256); }
+ options().getNewConnectionThreshold(hostDistance); if (totalInFlightCount > currentCapacity) maybeSpawnNewConnection();
void setNewConnectionThreshold(PoolingOptions poolingOptions) { if (newConnectionThreshold != null) { int currentNewConnectionThreshold = poolingOptions.getNewConnectionThreshold(getHostDistance()); if (currentNewConnectionThreshold < newConnectionThreshold) { poolingOptions.setNewConnectionThreshold(getHostDistance(), newConnectionThreshold); } } }
PoolingOptions setNewConnectionThreshold(PoolingOptions poolingOptions) { if (newConnectionThreshold != null) { int currentNewConnectionThreshold = poolingOptions.getNewConnectionThreshold(getHostDistance()); if (currentNewConnectionThreshold < newConnectionThreshold) { poolingOptions.setNewConnectionThreshold(getHostDistance(), newConnectionThreshold); } } return poolingOptions; }
/** * 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; } }
/** * 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; } }
/** * 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; } }
@Test(groups = "unit") public void should_leave_connection_options_unset_until_protocol_version_known() { PoolingOptions options = new PoolingOptions(); assertThat(options.getCoreConnectionsPerHost(LOCAL)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getCoreConnectionsPerHost(REMOTE)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getMaxConnectionsPerHost(LOCAL)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getMaxConnectionsPerHost(REMOTE)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getNewConnectionThreshold(LOCAL)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getNewConnectionThreshold(REMOTE)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getMaxRequestsPerConnection(LOCAL)).isEqualTo(PoolingOptions.UNSET); assertThat(options.getMaxRequestsPerConnection(REMOTE)).isEqualTo(PoolingOptions.UNSET); }
pooling.put("max_connections_per_host_remote", poolingOptions.getMaxConnectionsPerHost(HostDistance.REMOTE)); pooling.put("new_connection_threshold_local", poolingOptions.getNewConnectionThreshold(HostDistance.LOCAL)); pooling.put("new_connection_threshold_remote", poolingOptions.getNewConnectionThreshold(HostDistance.REMOTE));
@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); }
@Test(groups = "unit") public void should_initialize_to_v3_defaults_if_v3_or_above() { PoolingOptions options = new PoolingOptions(); options.setProtocolVersion(ProtocolVersion.V3); assertThat(options.getCoreConnectionsPerHost(LOCAL)).isEqualTo(1); assertThat(options.getMaxConnectionsPerHost(LOCAL)).isEqualTo(1); assertThat(options.getCoreConnectionsPerHost(REMOTE)).isEqualTo(1); assertThat(options.getMaxConnectionsPerHost(REMOTE)).isEqualTo(1); assertThat(options.getNewConnectionThreshold(LOCAL)).isEqualTo(800); assertThat(options.getNewConnectionThreshold(REMOTE)).isEqualTo(200); assertThat(options.getMaxRequestsPerConnection(LOCAL)).isEqualTo(1024); assertThat(options.getMaxRequestsPerConnection(REMOTE)).isEqualTo(256); }
pooling.put("max_connections_per_host_remote", poolingOptions.getMaxConnectionsPerHost(HostDistance.REMOTE)); pooling.put("new_connection_threshold_local", poolingOptions.getNewConnectionThreshold(HostDistance.LOCAL)); pooling.put("new_connection_threshold_remote", poolingOptions.getNewConnectionThreshold(HostDistance.REMOTE));
+ options().getNewConnectionThreshold(hostDistance); if (totalInFlightCount > currentCapacity) maybeSpawnNewConnection();
+ options().getNewConnectionThreshold(hostDistance); if (totalInFlightCount > currentCapacity) maybeSpawnNewConnection();
+ options().getNewConnectionThreshold(hostDistance); if (totalInFlightCount > currentCapacity) maybeSpawnNewConnection();
assertEquals(8, options.getMaxRequestsPerConnection(HostDistance.LOCAL)); assertEquals(9, options.getMaxRequestsPerConnection(HostDistance.REMOTE)); assertEquals(10, options.getNewConnectionThreshold(HostDistance.LOCAL)); assertEquals(11, options.getNewConnectionThreshold(HostDistance.REMOTE));