Search in sources :

Example 66 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.

the class JedisPoolTest method checkResourceIsCloseable.

@Test
public void checkResourceIsCloseable() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    Jedis jedis = pool.getResource();
    try {
        jedis.set("hello", "jedis");
    } finally {
        jedis.close();
    }
    Jedis jedis2 = pool.getResource();
    try {
        assertEquals(jedis, jedis2);
    } finally {
        jedis2.close();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) Test(org.junit.Test)

Example 67 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.

the class JedisPoolTest method returnResourceShouldResetState.

@Test
public void returnResourceShouldResetState() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    Jedis jedis = pool.getResource();
    try {
        jedis.set("hello", "jedis");
        Transaction t = jedis.multi();
        t.set("hello", "world");
    } finally {
        jedis.close();
    }
    Jedis jedis2 = pool.getResource();
    try {
        assertTrue(jedis == jedis2);
        assertEquals("jedis", jedis2.get("hello"));
    } finally {
        jedis2.close();
    }
    pool.destroy();
    assertTrue(pool.isClosed());
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) Test(org.junit.Test)

Example 68 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project athenz by yahoo.

the class DataSourceFactory method create.

static PoolableDataSource create(ConnectionFactory connectionFactory) {
    // setup our pool config object
    GenericObjectPoolConfig config = setupPoolConfig();
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // Set max lifetime of a connection in milli-secs, after which it will
    // always fail activation, passivation, and validation.
    // Value of -1 means infinite life time. The default value
    // defined in this class is 10 minutes.
    long connTtlMillis = retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TTL, MAX_TTL_CONN_MS);
    poolableConnectionFactory.setMaxConnLifetimeMillis(connTtlMillis);
    if (LOG.isInfoEnabled()) {
        LOG.info("Setting Time-To-Live interval for live connections ({}) msecs", connTtlMillis);
    }
    // set the validation query for our jdbc connector
    final String validationQuery = System.getProperty(ATHENZ_PROP_DBPOOL_VALIDATION_QUERY, MYSQL_VALIDATION_QUERY);
    poolableConnectionFactory.setValidationQuery(validationQuery);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);
    return new AthenzDataSource(connectionPool);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 69 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project athenz by yahoo.

the class DataSourceFactory method setupPoolConfig.

public static GenericObjectPoolConfig setupPoolConfig() {
    // setup config vars for the object pool
    // ie. min and max idle instances, and max total instances of arbitrary objects
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    // The maximum number of active connections that can be allocated from
    // this pool at the same time, or negative for no limit. Default: 8
    config.setMaxTotal(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TOTAL, GenericObjectPoolConfig.DEFAULT_MAX_TOTAL));
    if (config.getMaxTotal() == 0) {
        // -1 means no limit
        config.setMaxTotal(-1);
    }
    // The maximum number of connections that can remain idle in the pool,
    // without extra ones being released, or negative for no limit. Default 8
    config.setMaxIdle(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_IDLE, GenericObjectPoolConfig.DEFAULT_MAX_IDLE));
    if (config.getMaxIdle() == 0) {
        // -1 means no limit
        config.setMaxIdle(-1);
    }
    // The minimum number of connections that can remain idle in the pool,
    // without extra ones being created, or zero to create none. Default 0
    config.setMinIdle(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MIN_IDLE, GenericObjectPoolConfig.DEFAULT_MIN_IDLE));
    // The maximum number of milliseconds that the pool will wait (when
    // there are no available connections) for a connection to be returned
    // before throwing an exception, or -1 to wait indefinitely. Default -1
    config.setMaxWaitMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_WAIT, BaseObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS));
    // setup the configuration to cleanup idle connections
    // 
    // Minimum time an object can be idle in the pool before being eligible
    // for eviction by the idle object evictor.
    // The default value is 30 minutes (1000 * 60 * 30).
    config.setMinEvictableIdleTimeMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_EVICT_IDLE_TIMEOUT, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS));
    // Number of milliseconds to sleep between runs of idle object evictor thread.
    // Not using DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS since it is -1
    // meaning it will not run the evictor thread and instead we're using
    // the default min value for evictable idle connections (Default 30 minutes)
    config.setTimeBetweenEvictionRunsMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_EVICT_IDLE_INTERVAL, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS));
    if (LOG.isDebugEnabled()) {
        LOG.debug("Config settings for idle object eviction: time interval between eviction thread runs ({} millis), " + "minimum timeout for idle objects ({} millis)", config.getTimeBetweenEvictionRunsMillis(), config.getMinEvictableIdleTimeMillis());
    }
    // Validate objects by the idle object evictor. If invalid, gets dropped
    // from the pool.
    config.setTestWhileIdle(true);
    // Validate object before borrowing from pool and returning to the pool.
    // If invalid, gets dropped from the pool and an attempt to borrow
    // another one will occur.
    config.setTestOnBorrow(true);
    config.setTestOnReturn(true);
    return config;
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig)

Example 70 with GenericObjectPoolConfig

use of com.frameworkset.commons.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)

Aggregations

GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)222 Test (org.junit.Test)82 Jedis (redis.clients.jedis.Jedis)44 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)43 ShardedJedis (redis.clients.jedis.ShardedJedis)41 JedisPool (redis.clients.jedis.JedisPool)37 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)32 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)21 Bean (org.springframework.context.annotation.Bean)20 ArrayList (java.util.ArrayList)17 JedisShardInfo (redis.clients.jedis.JedisShardInfo)17 Test (org.junit.jupiter.api.Test)15 URI (java.net.URI)12 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)12 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)12 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)11 IOException (java.io.IOException)10 Test (org.testng.annotations.Test)10 PooledObject (org.apache.commons.pool2.PooledObject)9 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)9