Search in sources :

Example 21 with JedisShardInfo

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

the class ShardedJedisTest method tryShardingWithMurmure.

@Test
public void tryShardingWithMurmure() {
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    JedisShardInfo si = new JedisShardInfo(redis1.getHost(), redis1.getPort());
    si.setPassword("foobared");
    shards.add(si);
    si = new JedisShardInfo(redis2.getHost(), redis2.getPort());
    si.setPassword("foobared");
    shards.add(si);
    ShardedJedis jedis = new ShardedJedis(shards, Hashing.MURMUR_HASH);
    jedis.set("a", "bar");
    JedisShardInfo s1 = jedis.getShardInfo("a");
    jedis.set("b", "bar1");
    JedisShardInfo s2 = jedis.getShardInfo("b");
    jedis.disconnect();
    Jedis j = new Jedis(s1.getHost(), s1.getPort());
    j.auth("foobared");
    assertEquals("bar", j.get("a"));
    j.disconnect();
    j = new Jedis(s2.getHost(), s2.getPort());
    j.auth("foobared");
    assertEquals("bar1", j.get("b"));
    j.disconnect();
}
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 22 with JedisShardInfo

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

the class ShardedJedisTest method trySharding.

@Test
public void trySharding() {
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    JedisShardInfo si = new JedisShardInfo(redis1.getHost(), redis1.getPort());
    si.setPassword("foobared");
    shards.add(si);
    si = new JedisShardInfo(redis2.getHost(), redis2.getPort());
    si.setPassword("foobared");
    shards.add(si);
    ShardedJedis jedis = new ShardedJedis(shards);
    jedis.set("a", "bar");
    JedisShardInfo s1 = jedis.getShardInfo("a");
    jedis.set("b", "bar1");
    JedisShardInfo s2 = jedis.getShardInfo("b");
    jedis.disconnect();
    Jedis j = new Jedis(s1.getHost(), s1.getPort());
    j.auth("foobared");
    assertEquals("bar", j.get("a"));
    j.disconnect();
    j = new Jedis(s2.getHost(), s2.getPort());
    j.auth("foobared");
    assertEquals("bar1", j.get("b"));
    j.disconnect();
}
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 23 with JedisShardInfo

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

the class ShardedJedisTest method testMurmurSharding.

@Test
public void testMurmurSharding() {
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
    shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
    shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
    shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
    Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards, Hashing.MURMUR_HASH);
    int shard_6379 = 0;
    int shard_6380 = 0;
    int shard_6381 = 0;
    for (int i = 0; i < 1000; i++) {
        JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
        switch(jedisShardInfo.getPort()) {
            case 6379:
                shard_6379++;
                break;
            case 6380:
                shard_6380++;
                break;
            case 6381:
                shard_6381++;
                break;
            default:
                fail("Attempting to use a non-defined shard!!:" + jedisShardInfo);
                break;
        }
    }
    assertTrue(shard_6379 > 300 && shard_6379 < 400);
    assertTrue(shard_6380 > 300 && shard_6380 < 400);
    assertTrue(shard_6381 > 300 && shard_6381 < 400);
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) Jedis(redis.clients.jedis.Jedis) Sharded(redis.clients.util.Sharded) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Test(org.junit.Test)

Example 24 with JedisShardInfo

use of redis.clients.jedis.JedisShardInfo in project pinpoint by naver.

the class JedisConstructorInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    try {
        if (!validate(target, args)) {
            return;
        }
        final StringBuilder endPoint = new StringBuilder();
        // first arg is host
        if (args[0] instanceof String) {
            endPoint.append(args[0]);
            // second arg is port
            if (args.length >= 2 && args[1] instanceof Integer) {
                endPoint.append(":").append(args[1]);
            } else {
                // set default port
                endPoint.append(":").append(6379);
            }
        } else if (args[0] instanceof URI) {
            final URI uri = (URI) args[0];
            endPoint.append(uri.getHost());
            endPoint.append(":");
            endPoint.append(uri.getPort());
        } else if (args[0] instanceof JedisShardInfo) {
            final JedisShardInfo info = (JedisShardInfo) args[0];
            endPoint.append(info.getHost());
            endPoint.append(":");
            endPoint.append(info.getPort());
        }
        ((EndPointAccessor) target)._$PINPOINT$_setEndPoint(endPoint.toString());
    } catch (Throwable t) {
        if (logger.isWarnEnabled()) {
            logger.warn("Failed to BEFORE process. {}", t.getMessage(), t);
        }
    }
}
Also used : EndPointAccessor(com.navercorp.pinpoint.plugin.redis.EndPointAccessor) JedisShardInfo(redis.clients.jedis.JedisShardInfo) URI(java.net.URI)

Example 25 with JedisShardInfo

use of redis.clients.jedis.JedisShardInfo 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)

Aggregations

JedisShardInfo (redis.clients.jedis.JedisShardInfo)51 Test (org.junit.Test)40 ShardedJedis (redis.clients.jedis.ShardedJedis)38 ArrayList (java.util.ArrayList)36 Jedis (redis.clients.jedis.Jedis)36 URI (java.net.URI)13 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)10 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)10 Sharded (redis.clients.util.Sharded)8 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)6 Before (org.junit.Before)6 SSLParameters (javax.net.ssl.SSLParameters)5 ShardedJedisPipeline (redis.clients.jedis.ShardedJedisPipeline)4 HostnameVerifier (javax.net.ssl.HostnameVerifier)3 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)3 JedisConnectionFactory (org.springframework.data.redis.connection.jedis.JedisConnectionFactory)2 BinaryJedis (redis.clients.jedis.BinaryJedis)2 EndPointAccessor (com.navercorp.pinpoint.plugin.redis.EndPointAccessor)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 CertificateException (java.security.cert.CertificateException)1