Search in sources :

Example 46 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project jedis by xetorthio.

the class ShardedJedisTest method checkKeyTags.

@Test
public void checkKeyTags() {
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
    shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
    ShardedJedis jedis = new ShardedJedis(shards, ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
    assertEquals(jedis.getKeyTag("foo"), "foo");
    assertEquals(jedis.getKeyTag("foo{bar}"), "bar");
    // default pattern is
    assertEquals(jedis.getKeyTag("foo{bar}}"), "bar");
    // non greedy
    // Key tag may appear
    assertEquals(jedis.getKeyTag("{bar}foo"), "bar");
    // anywhere
    // Key tag may appear
    assertEquals(jedis.getKeyTag("f{bar}oo"), "bar");
    // anywhere
    JedisShardInfo s1 = jedis.getShardInfo("abc{bar}");
    JedisShardInfo s2 = jedis.getShardInfo("foo{bar}");
    assertSame(s1, s2);
    List<String> keys = getKeysDifferentShard(jedis);
    JedisShardInfo s3 = jedis.getShardInfo(keys.get(0));
    JedisShardInfo s4 = jedis.getShardInfo(keys.get(1));
    assertNotSame(s3, s4);
    ShardedJedis jedis2 = new ShardedJedis(shards);
    assertEquals(jedis2.getKeyTag("foo"), "foo");
    assertNotSame(jedis2.getKeyTag("foo{bar}"), "bar");
    JedisShardInfo s5 = jedis2.getShardInfo(keys.get(0) + "{bar}");
    JedisShardInfo s6 = jedis2.getShardInfo(keys.get(1) + "{bar}");
    assertNotSame(s5, s6);
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Test(org.junit.Test)

Example 47 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project jedis by xetorthio.

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)

Example 48 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project jedis by xetorthio.

the class ShardedJedisPoolTest method checkResourceIsCloseable.

@Test
public void checkResourceIsCloseable() throws URISyntaxException {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    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")));
    ShardedJedisPool pool = new ShardedJedisPool(config, shards);
    ShardedJedis jedis = pool.getResource();
    try {
        jedis.set("hello", "jedis");
    } finally {
        jedis.close();
    }
    ShardedJedis jedis2 = pool.getResource();
    try {
        assertEquals(jedis, jedis2);
    } finally {
        jedis2.close();
    }
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) URI(java.net.URI) Test(org.junit.Test)

Example 49 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project jedis by xetorthio.

the class ShardedJedisTest method testAvoidLeaksUponDisconnect.

/**
   * Test for "Issue - BinaryShardedJedis.disconnect() may occur memory leak". You can find more
   * detailed information at https://github.com/xetorthio/jedis/issues/808
   * @throws InterruptedException
   */
@Test
public void testAvoidLeaksUponDisconnect() throws InterruptedException {
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(2);
    // 6379
    JedisShardInfo shard1 = new JedisShardInfo(redis1.getHost(), redis1.getPort());
    shard1.setPassword("foobared");
    shards.add(shard1);
    // 6380
    JedisShardInfo shard2 = new JedisShardInfo(redis2.getHost(), redis2.getPort());
    shard2.setPassword("foobared");
    shards.add(shard2);
    @SuppressWarnings("resource") ShardedJedis shardedJedis = new ShardedJedis(shards);
    // establish the connection for two redis servers
    shardedJedis.set("a", "bar");
    JedisShardInfo ak = shardedJedis.getShardInfo("a");
    assertEquals(shard2, ak);
    shardedJedis.set("b", "bar1");
    JedisShardInfo bk = shardedJedis.getShardInfo("b");
    assertEquals(shard1, bk);
    // We set a name to the instance so it's easy to find it
    Iterator<Jedis> it = shardedJedis.getAllShards().iterator();
    Jedis deadClient = it.next();
    deadClient.clientSetname("DEAD");
    ClientKillerUtil.killClient(deadClient, "DEAD");
    assertEquals(true, deadClient.isConnected());
    assertEquals(false, deadClient.getClient().getSocket().isClosed());
    // normal - not found
    assertEquals(false, deadClient.getClient().isBroken());
    shardedJedis.disconnect();
    assertEquals(false, deadClient.isConnected());
    assertEquals(true, deadClient.getClient().getSocket().isClosed());
    assertEquals(true, deadClient.getClient().isBroken());
    Jedis jedis2 = it.next();
    assertEquals(false, jedis2.isConnected());
    assertEquals(true, jedis2.getClient().getSocket().isClosed());
    assertEquals(false, jedis2.getClient().isBroken());
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) Jedis(redis.clients.jedis.Jedis) ShardedJedis(redis.clients.jedis.ShardedJedis) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Test(org.junit.Test)

Example 50 with ShardedJedis

use of redis.clients.jedis.ShardedJedis in project jedis by xetorthio.

the class ShardedJedisTest method checkCloseable.

@Test
public void checkCloseable() {
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
    shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
    shards.get(0).setPassword("foobared");
    shards.get(1).setPassword("foobared");
    ShardedJedis jedisShard = new ShardedJedis(shards);
    try {
        jedisShard.set("shard_closeable", "true");
    } finally {
        jedisShard.close();
    }
    for (Jedis jedis : jedisShard.getAllShards()) {
        assertTrue(!jedis.isConnected());
    }
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) Jedis(redis.clients.jedis.Jedis) ShardedJedis(redis.clients.jedis.ShardedJedis) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Test(org.junit.Test)

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