Search in sources :

Example 41 with JedisShardInfo

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

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 42 with JedisShardInfo

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

the class ShardedJedisTest method testMD5Sharding.

@Test
public void testMD5Sharding() {
    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.MD5);
    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 43 with JedisShardInfo

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

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

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

the class SSLJedisTest method connectWithShardInfoAndEmptyTrustStore.

/**
   * Tests opening an SSL/TLS connection to redis with an empty certificate
   * trust store. This test should fail because there is no trust anchor for the
   * redis server certificate.
   * 
   * @throws Exception
   */
@Test
public void connectWithShardInfoAndEmptyTrustStore() throws Exception {
    final URI uri = URI.create("rediss://localhost:6390");
    final SSLSocketFactory sslSocketFactory = createTrustNoOneSslSocketFactory();
    JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, null, null);
    shardInfo.setPassword("foobared");
    Jedis jedis = new Jedis(shardInfo);
    try {
        jedis.get("foo");
        Assert.fail("The code did not throw the expected JedisConnectionException.");
    } catch (JedisConnectionException e) {
        Assert.assertEquals("Unexpected first inner exception.", SSLException.class, e.getCause().getClass());
        Assert.assertEquals("Unexpected second inner exception.", RuntimeException.class, e.getCause().getCause().getClass());
        Assert.assertEquals("Unexpected third inner exception.", InvalidAlgorithmParameterException.class, e.getCause().getCause().getCause().getClass());
    }
    try {
        jedis.close();
    } catch (Throwable e1) {
    // Expected.
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) JedisShardInfo(redis.clients.jedis.JedisShardInfo) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URI(java.net.URI) SSLException(javax.net.ssl.SSLException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) 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