Search in sources :

Example 16 with ShardedJedisPool

use of redis.clients.jedis.ShardedJedisPool in project cachecloud by sohutv.

the class ShardedJedisPoolTest method startWithUrlString.

@Test
public void startWithUrlString() {
    Jedis j = new Jedis("localhost", 6380);
    j.auth("foobared");
    j.set("foo", "bar");
    j = new Jedis("localhost", 6379);
    j.auth("foobared");
    j.set("foo", "bar");
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add(new JedisShardInfo("redis://:foobared@localhost:6380"));
    shards.add(new JedisShardInfo("redis://:foobared@localhost:6379"));
    GenericObjectPoolConfig redisConfig = new GenericObjectPoolConfig();
    ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards);
    Jedis[] jedises = pool.getResource().getAllShards().toArray(new Jedis[2]);
    Jedis jedis = jedises[0];
    assertEquals("PONG", jedis.ping());
    assertEquals("bar", jedis.get("foo"));
    jedis = jedises[1];
    assertEquals("PONG", jedis.ping());
    assertEquals("bar", jedis.get("foo"));
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Test(org.junit.Test)

Example 17 with ShardedJedisPool

use of redis.clients.jedis.ShardedJedisPool in project cachecloud by sohutv.

the class ShardedJedisPoolTest method shouldReturnActiveShardsWhenOneGoesOffline.

@Test
public void shouldReturnActiveShardsWhenOneGoesOffline() {
    GenericObjectPoolConfig redisConfig = new GenericObjectPoolConfig();
    redisConfig.setTestOnBorrow(false);
    ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards);
    ShardedJedis jedis = pool.getResource();
    // fill the shards
    for (int i = 0; i < 1000; i++) {
        jedis.set("a-test-" + i, "0");
    }
    jedis.close();
    // check quantity for each shard
    Jedis j = new Jedis(shards.get(0));
    j.connect();
    Long c1 = j.dbSize();
    j.disconnect();
    j = new Jedis(shards.get(1));
    j.connect();
    Long c2 = j.dbSize();
    j.disconnect();
    // shutdown shard 2 and check thay the pool returns an instance with c1
    // items on one shard
    // alter shard 1 and recreate pool
    pool.destroy();
    shards.set(1, new JedisShardInfo("localhost", 1234));
    pool = new ShardedJedisPool(redisConfig, shards);
    jedis = pool.getResource();
    Long actual = Long.valueOf(0);
    Long fails = Long.valueOf(0);
    for (int i = 0; i < 1000; i++) {
        try {
            jedis.get("a-test-" + i);
            actual++;
        } catch (RuntimeException e) {
            fails++;
        }
    }
    jedis.close();
    pool.destroy();
    assertEquals(actual, c1);
    assertEquals(fails, c2);
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Test(org.junit.Test)

Example 18 with ShardedJedisPool

use of redis.clients.jedis.ShardedJedisPool in project cachecloud by sohutv.

the class ShardedJedisPoolTest method checkPoolRepairedWhenJedisIsBroken.

@Test
public void checkPoolRepairedWhenJedisIsBroken() {
    ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards);
    ShardedJedis jedis = pool.getResource();
    jedis.disconnect();
    jedis.close();
    jedis = pool.getResource();
    jedis.incr("foo");
    jedis.close();
    pool.destroy();
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) Test(org.junit.Test)

Example 19 with ShardedJedisPool

use of redis.clients.jedis.ShardedJedisPool in project cachecloud by sohutv.

the class ShardedJedisPoolTest method checkConnections.

@Test
public void checkConnections() {
    ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards);
    ShardedJedis jedis = pool.getResource();
    jedis.set("foo", "bar");
    assertEquals("bar", jedis.get("foo"));
    jedis.close();
    pool.destroy();
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) Test(org.junit.Test)

Example 20 with ShardedJedisPool

use of redis.clients.jedis.ShardedJedisPool in project cachecloud by sohutv.

the class ShardedJedisPoolTest method startWithUrl.

@Test
public void startWithUrl() throws URISyntaxException {
    Jedis j = new Jedis("localhost", 6380);
    j.auth("foobared");
    j.set("foo", "bar");
    j = new Jedis("localhost", 6379);
    j.auth("foobared");
    j.set("foo", "bar");
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380")));
    shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379")));
    GenericObjectPoolConfig redisConfig = new GenericObjectPoolConfig();
    ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards);
    Jedis[] jedises = pool.getResource().getAllShards().toArray(new Jedis[2]);
    Jedis jedis = jedises[0];
    assertEquals("PONG", jedis.ping());
    assertEquals("bar", jedis.get("foo"));
    jedis = jedises[1];
    assertEquals("PONG", jedis.ping());
    assertEquals("bar", jedis.get("foo"));
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) URI(java.net.URI) Test(org.junit.Test)

Aggregations

ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)27 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)26 Test (org.junit.Test)26 ShardedJedis (redis.clients.jedis.ShardedJedis)26 JedisShardInfo (redis.clients.jedis.JedisShardInfo)10 ArrayList (java.util.ArrayList)8 URI (java.net.URI)6 Jedis (redis.clients.jedis.Jedis)6 ShardedJedisPipeline (redis.clients.jedis.ShardedJedisPipeline)2 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)1