use of com.palantir.atlasdb.keyvalue.cassandra.CassandraClientPoolingContainer in project atlasdb by palantir.
the class CassandraService method addPool.
public void addPool(InetSocketAddress server) {
int currentPoolNumber = cassandraHosts.indexOf(server) + 1;
currentPools.put(server, new CassandraClientPoolingContainer(metricsManager, server, config, currentPoolNumber, poolMetrics));
}
use of com.palantir.atlasdb.keyvalue.cassandra.CassandraClientPoolingContainer in project atlasdb by palantir.
the class WeightedHostsTest method testWeightedHostsWithMaxActivityPool.
@Test
public void testWeightedHostsWithMaxActivityPool() {
InetSocketAddress highActivityHost = new InetSocketAddress(2);
Map<InetSocketAddress, CassandraClientPoolingContainer> pools = ImmutableMap.of(new InetSocketAddress(0), createMockClientPoolingContainerWithUtilization(5), new InetSocketAddress(1), createMockClientPoolingContainerWithUtilization(5), highActivityHost, createMockClientPoolingContainerWithUtilization(20));
NavigableMap<Integer, InetSocketAddress> result = WeightedHosts.create(pools).hosts;
int smallestWeight = result.firstEntry().getKey();
InetSocketAddress hostWithSmallestWeight = result.firstEntry().getValue();
int prevKey = 0;
for (Map.Entry<Integer, InetSocketAddress> entry : result.entrySet()) {
int currWeight = entry.getKey() - prevKey;
prevKey = entry.getKey();
if (currWeight < smallestWeight) {
smallestWeight = currWeight;
hostWithSmallestWeight = entry.getValue();
}
}
assertThat(hostWithSmallestWeight).isEqualTo(highActivityHost);
}
use of com.palantir.atlasdb.keyvalue.cassandra.CassandraClientPoolingContainer in project atlasdb by palantir.
the class WeightedHostsTest method testWeightedHostsWithLowActivityPool.
@Test
public void testWeightedHostsWithLowActivityPool() {
InetSocketAddress lowActivityHost = new InetSocketAddress(2);
Map<InetSocketAddress, CassandraClientPoolingContainer> pools = ImmutableMap.of(new InetSocketAddress(0), createMockClientPoolingContainerWithUtilization(10), new InetSocketAddress(1), createMockClientPoolingContainerWithUtilization(10), lowActivityHost, createMockClientPoolingContainerWithUtilization(0));
NavigableMap<Integer, InetSocketAddress> result = WeightedHosts.create(pools).hosts;
int largestWeight = result.firstEntry().getKey();
InetSocketAddress hostWithLargestWeight = result.firstEntry().getValue();
int prevKey = 0;
for (Map.Entry<Integer, InetSocketAddress> entry : result.entrySet()) {
int currWeight = entry.getKey() - prevKey;
prevKey = entry.getKey();
if (currWeight > largestWeight) {
largestWeight = currWeight;
hostWithLargestWeight = entry.getValue();
}
}
assertThat(hostWithLargestWeight).isEqualTo(lowActivityHost);
}
use of com.palantir.atlasdb.keyvalue.cassandra.CassandraClientPoolingContainer in project atlasdb by palantir.
the class WeightedHostsTest method testWeightedHostsWithNonZeroWeights.
@Test
public void testWeightedHostsWithNonZeroWeights() {
Map<InetSocketAddress, CassandraClientPoolingContainer> pools = ImmutableMap.of(new InetSocketAddress(0), createMockClientPoolingContainerWithUtilization(5), new InetSocketAddress(1), createMockClientPoolingContainerWithUtilization(10), new InetSocketAddress(2), createMockClientPoolingContainerWithUtilization(15));
NavigableMap<Integer, InetSocketAddress> result = WeightedHosts.create(pools).hosts;
int prevKey = 0;
for (Map.Entry<Integer, InetSocketAddress> entry : result.entrySet()) {
assertThat(entry.getKey()).isGreaterThan(prevKey);
prevKey = entry.getKey();
}
}
use of com.palantir.atlasdb.keyvalue.cassandra.CassandraClientPoolingContainer in project atlasdb by palantir.
the class CassandraService method getRandomGoodHostForPredicate.
public Optional<CassandraClientPoolingContainer> getRandomGoodHostForPredicate(Predicate<CassandraServer> predicate, Set<CassandraServer> triedNodes) {
Map<CassandraServer, CassandraClientPoolingContainer> pools = currentPools;
Set<CassandraServer> hostsMatchingPredicate = pools.keySet().stream().filter(predicate).collect(Collectors.toSet());
Map<String, Long> triedDatacenters = triedNodes.stream().map(hostToDatacenter::get).filter(Objects::nonNull).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Optional<Long> maximumAttemptsPerDatacenter = triedDatacenters.values().stream().max(Long::compareTo);
Set<String> maximallyAttemptedDatacenters = KeyedStream.stream(triedDatacenters).filter(attempts -> Objects.equals(attempts, maximumAttemptsPerDatacenter.orElseThrow(() -> new SafeIllegalStateException("Unexpectedly could not find the max attempts per datacenter")))).keys().collect(Collectors.toSet());
Set<CassandraServer> hostsInPermittedDatacenters = hostsMatchingPredicate.stream().filter(pool -> {
String datacenter = hostToDatacenter.get(pool);
return datacenter == null || !maximallyAttemptedDatacenters.contains(datacenter);
}).collect(Collectors.toSet());
Set<CassandraServer> filteredHosts = hostsInPermittedDatacenters.isEmpty() ? hostsMatchingPredicate : hostsInPermittedDatacenters;
if (filteredHosts.isEmpty()) {
log.info("No hosts match the provided predicate.");
return Optional.empty();
}
Set<CassandraServer> 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<CassandraServer> randomLivingHost = getRandomHostByActiveConnections(livingHosts);
return randomLivingHost.map(pools::get);
}
Aggregations