Search in sources :

Example 1 with CassandraClientConfig

use of com.palantir.atlasdb.keyvalue.cassandra.CassandraClientFactory.CassandraClientConfig in project atlasdb by palantir.

the class CassandraClientPoolingContainer method createClientPool.

/**
 * Pool size:
 *    Always keep {@link CassandraKeyValueServiceConfig#poolSize()} connections around, per host. Allow bursting
 *    up to {@link CassandraKeyValueServiceConfig#maxConnectionBurstSize()} connections per host under load.
 *
 * Borrowing from pool:
 *    On borrow, check if the connection is actually open. If it is not,
 *       immediately discard this connection from the pool, and try to take another.
 *    Borrow attempts against a fully in-use pool immediately throw a NoSuchElementException.
 *       {@code CassandraClientPool} when it sees this will:
 *          Follow an exponential backoff as a method of back pressure.
 *          Try 3 times against this host, and then give up and try against different hosts 3 additional times.
 *
 * In an asynchronous thread (using default values):
 *    Every 20-30 seconds, examine approximately a tenth of the connections in pool.
 *    Discard any connections in this tenth of the pool whose TCP connections are closed.
 *    Discard any connections in this tenth of the pool that have been idle for more than 10 minutes,
 *       while still keeping a minimum number of idle connections around for fast borrows.
 */
private GenericObjectPool<CassandraClient> createClientPool() {
    CassandraClientConfig clientConfig = CassandraClientConfig.of(config);
    CassandraClientFactory cassandraClientFactory = new CassandraClientFactory(metricsManager, proxy, clientConfig);
    GenericObjectPoolConfig<CassandraClient> poolConfig = new GenericObjectPoolConfig<>();
    poolConfig.setMinIdle(config.poolSize());
    poolConfig.setMaxIdle(config.maxConnectionBurstSize());
    poolConfig.setMaxTotal(config.maxConnectionBurstSize());
    // immediately throw when we try and borrow from a full pool; dealt with at higher level
    poolConfig.setBlockWhenExhausted(false);
    // this test is free/just checks a boolean and does not block; borrow is still fast
    poolConfig.setTestOnBorrow(true);
    poolConfig.setSoftMinEvictableIdleTimeMillis(TimeUnit.MILLISECONDS.convert(Duration.ofSeconds(config.idleConnectionTimeoutSeconds())));
    poolConfig.setMinEvictableIdleTimeMillis(Long.MAX_VALUE);
    // the randomness here is to prevent all of the pools for all of the hosts
    // evicting all at at once, which isn't great for C*.
    int timeBetweenEvictionsSeconds = config.timeBetweenConnectionEvictionRunsSeconds();
    int delta = ThreadLocalRandom.current().nextInt(Math.min(timeBetweenEvictionsSeconds / 2, 10));
    poolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.MILLISECONDS.convert(Duration.ofSeconds(timeBetweenEvictionsSeconds + delta)));
    poolConfig.setNumTestsPerEvictionRun(-(int) (1.0 / config.proportionConnectionsToCheckPerEvictionRun()));
    poolConfig.setTestWhileIdle(true);
    poolConfig.setJmxNamePrefix(proxy.getHostString());
    poolConfig.setEvictionPolicy(new DefaultEvictionPolicy<>());
    GenericObjectPool<CassandraClient> pool = new GenericObjectPool<>(cassandraClientFactory, poolConfig);
    pool.setSwallowedExceptionListener(exception -> log.info("Swallowed exception within object pool", exception));
    registerMetrics(pool);
    log.info("Creating a Cassandra client pool for {} with the configuration {}", SafeArg.of("cassandraHost", cassandraServer.cassandraHostName()), SafeArg.of("proxy", proxy), SafeArg.of("poolConfig", poolConfig));
    return pool;
}
Also used : CassandraClientConfig(com.palantir.atlasdb.keyvalue.cassandra.CassandraClientFactory.CassandraClientConfig) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Aggregations

CassandraClientConfig (com.palantir.atlasdb.keyvalue.cassandra.CassandraClientFactory.CassandraClientConfig)1 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1