@Test public void shouldReturnAbsentIfPredicateMatchesNoServers() { CassandraService cassandra = clientPoolWithServers(ImmutableSet.of(HOST_1)); Optional<CassandraClientPoolingContainer> container = cassandra.getRandomGoodHostForPredicate(address -> false); assertThat(container).isNotPresent(); }
public Optional<CassandraClientPoolingContainer> getRandomGoodHostForPredicate( Predicate<InetSocketAddress> predicate) { Map<InetSocketAddress, CassandraClientPoolingContainer> pools = currentPools; Set<InetSocketAddress> filteredHosts = pools.keySet().stream() .filter(predicate) .collect(Collectors.toSet()); if (filteredHosts.isEmpty()) { log.info("No hosts match the provided predicate."); return Optional.empty(); } Set<InetSocketAddress> livingHosts = blacklist.filterBlacklistedHostsFrom(filteredHosts); if (livingHosts.isEmpty()) { log.info("There are no known live hosts in the connection pool matching the predicate. We're choosing" + " one at random in a last-ditch attempt at forward progress."); livingHosts = filteredHosts; } Optional<InetSocketAddress> randomLivingHost = getRandomHostByActiveConnections(livingHosts); return randomLivingHost.flatMap(host -> Optional.ofNullable(pools.get(host))); }
CassandraService implements AutoCloseable { public Optional<CassandraClientPoolingContainer> getRandomGoodHostForPredicate( Predicate<InetSocketAddress> predicate) { Map<InetSocketAddress, CassandraClientPoolingContainer> pools = currentPools; Set<InetSocketAddress> filteredHosts = pools.keySet().stream() .filter(predicate) .collect(Collectors.toSet()); if (filteredHosts.isEmpty()) { log.info("No hosts match the provided predicate."); return Optional.empty(); } Set<InetSocketAddress> livingHosts = blacklist.filterBlacklistedHostsFrom(filteredHosts); if (livingHosts.isEmpty()) { log.info("There are no known live hosts in the connection pool matching the predicate. We're choosing" + " one at random in a last-ditch attempt at forward progress."); livingHosts = filteredHosts; } Optional<InetSocketAddress> randomLivingHost = getRandomHostByActiveConnections(livingHosts); return randomLivingHost.flatMap(host -> Optional.ofNullable(pools.get(host))); } }
CassandraService implements AutoCloseable { public Optional<CassandraClientPoolingContainer> getRandomGoodHostForPredicate( Predicate<InetSocketAddress> predicate) { Map<InetSocketAddress, CassandraClientPoolingContainer> pools = currentPools; Set<InetSocketAddress> filteredHosts = pools.keySet().stream() .filter(predicate) .collect(Collectors.toSet()); if (filteredHosts.isEmpty()) { log.info("No hosts match the provided predicate."); return Optional.empty(); } Set<InetSocketAddress> livingHosts = blacklist.filterBlacklistedHostsFrom(filteredHosts); if (livingHosts.isEmpty()) { log.info("There are no known live hosts in the connection pool matching the predicate. We're choosing" + " one at random in a last-ditch attempt at forward progress."); livingHosts = filteredHosts; } Optional<InetSocketAddress> randomLivingHost = getRandomHostByActiveConnections(livingHosts); return randomLivingHost.flatMap(host -> Optional.ofNullable(pools.get(host))); } CassandraService(
            MetricsManager metricsManager,
            CassandraKeyValueServiceConfig config,
            Blacklist blacklist,
            CassandraClientPoolMetrics poolMetrics); }
CassandraService implements AutoCloseable { public Optional<CassandraClientPoolingContainer> getRandomGoodHostForPredicate( Predicate<InetSocketAddress> predicate) { Map<InetSocketAddress, CassandraClientPoolingContainer> pools = currentPools; Set<InetSocketAddress> filteredHosts = pools.keySet().stream() .filter(predicate) .collect(Collectors.toSet()); if (filteredHosts.isEmpty()) { log.info("No hosts match the provided predicate."); return Optional.empty(); } Set<InetSocketAddress> livingHosts = blacklist.filterBlacklistedHostsFrom(filteredHosts); if (livingHosts.isEmpty()) { log.info("There are no known live hosts in the connection pool matching the predicate. We're choosing" + " one at random in a last-ditch attempt at forward progress."); livingHosts = filteredHosts; } Optional<InetSocketAddress> randomLivingHost = getRandomHostByActiveConnections(livingHosts); return randomLivingHost.flatMap(host -> Optional.ofNullable(pools.get(host))); } CassandraService(
            MetricsManager metricsManager,
            CassandraKeyValueServiceConfig config,
            Blacklist blacklist,
            CassandraClientPoolMetrics poolMetrics); @Override void close(); Set<InetSocketAddress> refreshTokenRangesAndGetServers(); Set<InetSocketAddress> getLocalHosts(); Optional<CassandraClientPoolingContainer> getRandomGoodHostForPredicate(
            Predicate<InetSocketAddress> predicate); CassandraClientPoolingContainer getRandomGoodHost(); RangeMap<LightweightOppToken, List<InetSocketAddress>> getTokenMap(); Map<InetSocketAddress, CassandraClientPoolingContainer> getPools(); void debugLogStateOfPool(); InetSocketAddress getRandomHostForKey(byte[] key); void addPool(InetSocketAddress server); void removePool(InetSocketAddress removedServerAddress); void cacheInitialCassandraHosts(); void clearInitialCassandraHosts(); }
CassandraService implements AutoCloseable { public Optional<CassandraClientPoolingContainer> getRandomGoodHostForPredicate( Predicate<InetSocketAddress> predicate) { Map<InetSocketAddress, CassandraClientPoolingContainer> pools = currentPools; Set<InetSocketAddress> filteredHosts = pools.keySet().stream() .filter(predicate) .collect(Collectors.toSet()); if (filteredHosts.isEmpty()) { log.info("No hosts match the provided predicate."); return Optional.empty(); } Set<InetSocketAddress> livingHosts = blacklist.filterBlacklistedHostsFrom(filteredHosts); if (livingHosts.isEmpty()) { log.info("There are no known live hosts in the connection pool matching the predicate. We're choosing" + " one at random in a last-ditch attempt at forward progress."); livingHosts = filteredHosts; } Optional<InetSocketAddress> randomLivingHost = getRandomHostByActiveConnections(livingHosts); return randomLivingHost.flatMap(host -> Optional.ofNullable(pools.get(host))); } CassandraService(
            MetricsManager metricsManager,
            CassandraKeyValueServiceConfig config,
            Blacklist blacklist,
            CassandraClientPoolMetrics poolMetrics); @Override void close(); Set<InetSocketAddress> refreshTokenRangesAndGetServers(); Set<InetSocketAddress> getLocalHosts(); Optional<CassandraClientPoolingContainer> getRandomGoodHostForPredicate(
            Predicate<InetSocketAddress> predicate); CassandraClientPoolingContainer getRandomGoodHost(); RangeMap<LightweightOppToken, List<InetSocketAddress>> getTokenMap(); Map<InetSocketAddress, CassandraClientPoolingContainer> getPools(); void debugLogStateOfPool(); InetSocketAddress getRandomHostForKey(byte[] key); void addPool(InetSocketAddress server); void removePool(InetSocketAddress removedServerAddress); void cacheInitialCassandraHosts(); void clearInitialCassandraHosts();  }