Search in sources :

Example 21 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig 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.
 *
 * @param poolNumber number of the pool for metric registration.
 */
private GenericObjectPool<CassandraClient> createClientPool(int poolNumber) {
    CassandraClientFactory cassandraClientFactory = new CassandraClientFactory(qosClient, host, config);
    GenericObjectPoolConfig 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);
    poolConfig.setMaxWaitMillis(config.socketTimeoutMillis());
    // this test is free/just checks a boolean and does not block; borrow is still fast
    poolConfig.setTestOnBorrow(true);
    poolConfig.setMinEvictableIdleTimeMillis(TimeUnit.MILLISECONDS.convert(config.idleConnectionTimeoutSeconds(), TimeUnit.SECONDS));
    // 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(timeBetweenEvictionsSeconds + delta, TimeUnit.SECONDS));
    poolConfig.setNumTestsPerEvictionRun(-(int) (1.0 / config.proportionConnectionsToCheckPerEvictionRun()));
    poolConfig.setTestWhileIdle(true);
    poolConfig.setJmxNamePrefix(CassandraLogHelper.host(host));
    GenericObjectPool<CassandraClient> pool = new GenericObjectPool<>(cassandraClientFactory, poolConfig);
    registerMetrics(pool, poolNumber);
    return pool;
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 22 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project Hotchpotch by carryxyh.

the class RedisSentinelService method init.

public void init() {
    Set<String> sentinelset = new HashSet<>(Arrays.asList(sentinels.split(",")));
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxIdle(this.getMaxIdle());
    config.setMinIdle(this.getMinIdle());
    config.setMaxTotal(this.getMaxTotal());
    // 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException
    config.setMaxWaitMillis(this.getMaxWaitMillis());
    // 获取连接时关闭触发ping
    config.setTestOnBorrow(this.isTestOnBorrow());
    // 释放连接时关闭触发ping
    config.setTestOnReturn(this.isTestOnReturn());
    // 表示有一个idle object evitor线程对idle object进行扫描,如果validate失败,此object会被从pool中drop掉
    config.setTestWhileIdle(this.isTestWhileIdle());
    // 每隔30秒定期检查空闲连接
    config.setTimeBetweenEvictionRunsMillis(this.getTimeBetweenEvictionRunsMillis());
    // 空闲连接扫描时,每次最多扫描的连接数, -1 全部扫描
    config.setNumTestsPerEvictionRun(this.getNumTestsPerEvictionRun());
    // 连接在池中保持空闲而不被空闲连接回收器线程回收的最小时间值
    config.setMinEvictableIdleTimeMillis(this.getMinEvictableIdleTimeMillis());
    pool = new JedisSentinelPool(masterName, sentinelset, config, Protocol.DEFAULT_TIMEOUT, null, database);
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            pool.destroy();
        }
    });
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) HashSet(java.util.HashSet)

Example 23 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project jetcache by alibaba.

the class JetCacheConfig method pool.

@Bean
public Pool<Jedis> pool() {
    GenericObjectPoolConfig pc = new GenericObjectPoolConfig();
    pc.setMinIdle(2);
    pc.setMaxIdle(10);
    pc.setMaxTotal(10);
    return new JedisPool(pc, "localhost", 6379);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) Bean(org.springframework.context.annotation.Bean)

Example 24 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project jetcache by alibaba.

the class RedisCacheTest method testSentinel.

@Test
public void testSentinel() throws Exception {
    GenericObjectPoolConfig pc = new GenericObjectPoolConfig();
    pc.setMinIdle(2);
    pc.setMaxIdle(10);
    pc.setMaxTotal(10);
    Set<String> sentinels = new HashSet<>();
    sentinels.add("127.0.0.1:26379");
    sentinels.add("127.0.0.1:26380");
    sentinels.add("127.0.0.1:26381");
    JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels, pc);
    testWithPool(pool);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) HashSet(java.util.HashSet) RefreshCacheTest(com.alicp.jetcache.RefreshCacheTest) Test(org.junit.Test) LoadingCacheTest(com.alicp.jetcache.LoadingCacheTest) AbstractExternalCacheTest(com.alicp.jetcache.test.external.AbstractExternalCacheTest)

Example 25 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project jetcache by alibaba.

the class RedisCacheTest method readFromSlaveTest.

@Test
public void readFromSlaveTest() throws Exception {
    GenericObjectPoolConfig pc = new GenericObjectPoolConfig();
    pc.setMinIdle(2);
    pc.setMaxIdle(10);
    pc.setMaxTotal(10);
    JedisPool pool1 = new JedisPool(pc, "localhost", 6379);
    JedisPool pool2 = new JedisPool(pc, "localhost", 6380);
    JedisPool pool3 = new JedisPool(pc, "localhost", 6381);
    RedisCacheBuilder builder = RedisCacheBuilder.createRedisCacheBuilder();
    builder.setJedisPool(pool1);
    builder.setReadFromSlave(true);
    builder.setJedisSlavePools(pool2, pool3);
    builder.setSlaveReadWeights(1, 1);
    builder.setKeyConvertor(FastjsonKeyConvertor.INSTANCE);
    builder.setValueEncoder(JavaValueEncoder.INSTANCE);
    builder.setValueDecoder(JavaValueDecoder.INSTANCE);
    builder.setKeyPrefix(new Random().nextInt() + "");
    builder.setExpireAfterWriteInMillis(500);
    readFromSlaveTestAsserts(pool1, builder);
    builder.setSlaveReadWeights(null);
    readFromSlaveTestAsserts(pool1, builder);
}
Also used : Random(java.util.Random) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) RefreshCacheTest(com.alicp.jetcache.RefreshCacheTest) Test(org.junit.Test) LoadingCacheTest(com.alicp.jetcache.LoadingCacheTest) AbstractExternalCacheTest(com.alicp.jetcache.test.external.AbstractExternalCacheTest)

Aggregations

GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)127 Test (org.junit.Test)77 Jedis (redis.clients.jedis.Jedis)41 ShardedJedis (redis.clients.jedis.ShardedJedis)40 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)40 JedisPool (redis.clients.jedis.JedisPool)31 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)19 ArrayList (java.util.ArrayList)15 JedisShardInfo (redis.clients.jedis.JedisShardInfo)15 URI (java.net.URI)11 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)9 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 BaseTest (com.sohu.tv.test.base.BaseTest)6 IOException (java.io.IOException)6 Transaction (redis.clients.jedis.Transaction)6 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)5 PooledObject (org.apache.commons.pool2.PooledObject)5 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Test (org.testng.annotations.Test)5