Search in sources :

Example 76 with ShardedJedis

use of redis.clients.jedis.ShardedJedis 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 77 with ShardedJedis

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

the class ShardedJedisPoolTest method checkConnectionWithDefaultPort.

@Test
public void checkConnectionWithDefaultPort() {
    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 78 with ShardedJedis

use of redis.clients.jedis.ShardedJedis 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 79 with ShardedJedis

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

the class ShardedJedisPoolTest method checkFailedJedisServer.

@Test
public void checkFailedJedisServer() {
    ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards);
    ShardedJedis 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 80 with ShardedJedis

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

the class HashingBenchmark method main.

public static void main(String[] args) throws UnknownHostException, IOException {
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    JedisShardInfo shard = new JedisShardInfo(hnp1.getHost(), hnp1.getPort());
    shard.setPassword("foobared");
    shards.add(shard);
    shard = new JedisShardInfo(hnp2.getHost(), hnp2.getPort());
    shard.setPassword("foobared");
    shards.add(shard);
    ShardedJedis jedis = new ShardedJedis(shards);
    Collection<Jedis> allShards = jedis.getAllShards();
    for (Jedis j : allShards) {
        j.flushAll();
    }
    long begin = Calendar.getInstance().getTimeInMillis();
    for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
        String key = "foo" + n;
        jedis.set(key, "bar" + n);
        jedis.get(key);
    }
    long elapsed = Calendar.getInstance().getTimeInMillis() - begin;
    jedis.disconnect();
    System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
Also used : Jedis(redis.clients.jedis.Jedis) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedis(redis.clients.jedis.ShardedJedis) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo)

Aggregations

ShardedJedis (redis.clients.jedis.ShardedJedis)83 Test (org.junit.Test)56 JedisShardInfo (redis.clients.jedis.JedisShardInfo)36 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)35 ArrayList (java.util.ArrayList)33 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)33 Jedis (redis.clients.jedis.Jedis)21 URI (java.net.URI)6 ShardedJedisPipeline (redis.clients.jedis.ShardedJedisPipeline)6 Before (org.junit.Before)3 Serializable (java.io.Serializable)2 BinaryShardedJedis (redis.clients.jedis.BinaryShardedJedis)2 JedisException (redis.clients.jedis.exceptions.JedisException)1