From bbb867781deb9e8a0f073694d3ab3434426c7405 Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Thu, 28 Nov 2013 08:13:57 -0500 Subject: [PATCH] upgrade to commons-pool 2 --- Makefile | 3 - pom.xml | 10 +- .../clients/jedis/BasicRedisPipeline.java | 2 - .../clients/jedis/BinaryJedisCommands.java | 32 +- .../clients/jedis/BinaryShardedJedis.java | 104 ++--- .../redis/clients/jedis/JedisFactory.java | 129 +++--- .../java/redis/clients/jedis/JedisPool.java | 73 ++-- .../redis/clients/jedis/JedisPoolConfig.java | 123 +----- .../clients/jedis/JedisSentinelPool.java | 31 +- .../redis/clients/jedis/ShardedJedisPool.java | 130 +++--- src/main/java/redis/clients/util/Pool.java | 114 +++--- .../clients/jedis/tests/JedisPoolTest.java | 197 +++++---- .../jedis/tests/JedisSentinelPoolTest.java | 67 +-- .../jedis/tests/JedisSentinelTest.java | 6 +- .../jedis/tests/ShardedJedisPipelineTest.java | 148 +++---- .../jedis/tests/ShardedJedisPoolTest.java | 257 ++++++------ .../jedis/tests/benchmark/PoolBenchmark.java | 76 ++-- .../commands/AllKindOfValuesCommandsTest.java | 13 +- .../commands/TransactionCommandsTest.java | 381 +++++++++--------- 19 files changed, 901 insertions(+), 995 deletions(-) diff --git a/Makefile b/Makefile index 761d033..3606f06 100644 --- a/Makefile +++ b/Makefile @@ -57,7 +57,6 @@ sentinel monitor mymaster 127.0.0.1 6379 1 sentinel auth-pass mymaster foobared sentinel down-after-milliseconds mymaster 3000 sentinel failover-timeout mymaster 900000 -sentinel can-failover mymaster yes sentinel parallel-syncs mymaster 1 pidfile /tmp/sentinel1.pid logfile /tmp/sentinel1.log @@ -69,7 +68,6 @@ daemonize yes sentinel monitor mymaster 127.0.0.1 6381 2 sentinel auth-pass mymaster foobared sentinel down-after-milliseconds mymaster 3000 -sentinel can-failover mymaster yes sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 900000 pidfile /tmp/sentinel2.pid @@ -82,7 +80,6 @@ daemonize yes sentinel monitor mymaster 127.0.0.1 6381 2 sentinel auth-pass mymaster foobared sentinel down-after-milliseconds mymaster 3000 -sentinel can-failover mymaster yes sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 900000 pidfile /tmp/sentinel3.pid diff --git a/pom.xml b/pom.xml index bb63109..8649428 100644 --- a/pom.xml +++ b/pom.xml @@ -59,9 +59,9 @@ test - commons-pool - commons-pool - 1.6 + org.apache.commons + commons-pool2 + 2.0 jar compile @@ -115,11 +115,11 @@ org.apache.maven.plugins maven-release-plugin - 2.4.1 + 2.4.2 maven-deploy-plugin - 2.7 + 2.8.1 internal.repo::default::file://${project.build.directory}/mvn-repo diff --git a/src/main/java/redis/clients/jedis/BasicRedisPipeline.java b/src/main/java/redis/clients/jedis/BasicRedisPipeline.java index 97658d8..a667ce9 100644 --- a/src/main/java/redis/clients/jedis/BasicRedisPipeline.java +++ b/src/main/java/redis/clients/jedis/BasicRedisPipeline.java @@ -1,8 +1,6 @@ package redis.clients.jedis; -import java.util.List; - /** * Pipelined responses for all of the low level, non key related commands */ diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java index 35e1879..aed3011 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java @@ -5,8 +5,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import redis.clients.jedis.BinaryClient.LIST_POSITION; - /** * Common interface for sharded and non-sharded BinaryJedis */ @@ -116,7 +114,7 @@ public interface BinaryJedisCommands { Long strlen(byte[] key); Long zadd(byte[] key, double score, byte[] member); - + Long zadd(byte[] key, Map scoreMembers); Set zrange(byte[] key, long start, long end); @@ -159,45 +157,45 @@ public interface BinaryJedisCommands { Set zrevrangeByScore(byte[] key, byte[] max, byte[] min); Set zrangeByScore(byte[] key, byte[] min, byte[] max, int offset, - int count); + int count); Set zrevrangeByScore(byte[] key, double max, double min, - int offset, int count); + int offset, int count); Set zrangeByScoreWithScores(byte[] key, double min, double max); Set zrevrangeByScoreWithScores(byte[] key, double max, double min); Set zrangeByScoreWithScores(byte[] key, double min, double max, - int offset, int count); - + int offset, int count); + Set zrevrangeByScore(byte[] key, byte[] max, byte[] min, - int offset, int count); + int offset, int count); Set zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max); - + Set zrevrangeByScoreWithScores(byte[] key, byte[] max, byte[] min); Set zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max, - int offset, int count); + int offset, int count); Set zrevrangeByScoreWithScores(byte[] key, double max, double min, - int offset, int count); - + int offset, int count); + Set zrevrangeByScoreWithScores(byte[] key, byte[] max, byte[] min, - int offset, int count); + int offset, int count); Long zremrangeByRank(byte[] key, long start, long end); Long zremrangeByScore(byte[] key, double start, double end); - + Long zremrangeByScore(byte[] key, byte[] start, byte[] end); Long linsert(byte[] key, Client.LIST_POSITION where, byte[] pivot, - byte[] value); - + byte[] value); + Long lpushx(byte[] key, byte[]... arg); - + Long rpushx(byte[] key, byte[]... arg); List blpop(byte[] arg); diff --git a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java index 2723aa1..da934a4 100644 --- a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java @@ -1,16 +1,15 @@ package redis.clients.jedis; -import redis.clients.jedis.BinaryClient.LIST_POSITION; -import redis.clients.util.Hashing; -import redis.clients.util.Sharded; - -import java.io.IOException; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; +import redis.clients.jedis.BinaryClient.LIST_POSITION; +import redis.clients.util.Hashing; +import redis.clients.util.Sharded; + public class BinaryShardedJedis extends Sharded implements BinaryJedisCommands { public BinaryShardedJedis(List shards) { @@ -31,10 +30,10 @@ public class BinaryShardedJedis extends Sharded } public void disconnect() { - for (Jedis jedis : getAllShards()) { - jedis.quit(); - jedis.disconnect(); - } + for (Jedis jedis : getAllShards()) { + jedis.quit(); + jedis.disconnect(); + } } protected Jedis create(JedisShardInfo shard) { @@ -102,10 +101,10 @@ public class BinaryShardedJedis extends Sharded } public Long del(byte[] key) { - Jedis j = getShard(key); - return j.del(key); + Jedis j = getShard(key); + return j.del(key); } - + public Long incrBy(byte[] key, long integer) { Jedis j = getShard(key); return j.incrBy(key, integer); @@ -197,23 +196,23 @@ public class BinaryShardedJedis extends Sharded } public Long strlen(final byte[] key) { - Jedis j = getShard(key); - return j.strlen(key); + Jedis j = getShard(key); + return j.strlen(key); } public Long lpushx(byte[] key, byte[]... string) { - Jedis j = getShard(key); - return j.lpushx(key, string); + Jedis j = getShard(key); + return j.lpushx(key, string); } public Long persist(final byte[] key) { - Jedis j = getShard(key); - return j.persist(key); + Jedis j = getShard(key); + return j.persist(key); } public Long rpushx(byte[] key, byte[]... string) { - Jedis j = getShard(key); - return j.rpushx(key, string); + Jedis j = getShard(key); + return j.rpushx(key, string); } public Long llen(byte[] key) { @@ -365,7 +364,7 @@ public class BinaryShardedJedis extends Sharded Jedis j = getShard(key); return j.zcount(key, min, max); } - + public Long zcount(byte[] key, byte[] min, byte[] max) { Jedis j = getShard(key); return j.zcount(key, min, max); @@ -394,8 +393,8 @@ public class BinaryShardedJedis extends Sharded } public Set zrangeByScore(byte[] key, byte[] min, byte[] max) { - Jedis j = getShard(key); - return j.zrangeByScore(key, min, max); + Jedis j = getShard(key); + return j.zrangeByScore(key, min, max); } public Set zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max) { @@ -404,14 +403,15 @@ public class BinaryShardedJedis extends Sharded } public Set zrangeByScoreWithScores(byte[] key, byte[] min, - byte[] max, int offset, int count) { + byte[] max, int offset, int count) { Jedis j = getShard(key); return j.zrangeByScoreWithScores(key, min, max, offset, count); } - public Set zrangeByScore(byte[] key, byte[] min, byte[] max, int offset, int count) { - Jedis j = getShard(key); - return j.zrangeByScore(key, min, max, offset, count); + public Set zrangeByScore(byte[] key, byte[] min, byte[] max, + int offset, int count) { + Jedis j = getShard(key); + return j.zrangeByScore(key, min, max, offset, count); } public Set zrevrangeByScore(byte[] key, double max, double min) { @@ -436,7 +436,7 @@ public class BinaryShardedJedis extends Sharded Jedis j = getShard(key); return j.zrevrangeByScoreWithScores(key, max, min, offset, count); } - + public Set zrevrangeByScore(byte[] key, byte[] max, byte[] min) { Jedis j = getShard(key); return j.zrevrangeByScore(key, max, min); @@ -449,13 +449,13 @@ public class BinaryShardedJedis extends Sharded } public Set zrevrangeByScoreWithScores(byte[] key, byte[] max, - byte[] min) { + byte[] min) { Jedis j = getShard(key); return j.zrevrangeByScoreWithScores(key, max, min); } public Set zrevrangeByScoreWithScores(byte[] key, byte[] max, - byte[] min, int offset, int count) { + byte[] min, int offset, int count) { Jedis j = getShard(key); return j.zrevrangeByScoreWithScores(key, max, min, offset, count); } @@ -510,57 +510,57 @@ public class BinaryShardedJedis extends Sharded } public Boolean setbit(byte[] key, long offset, boolean value) { - Jedis j = getShard(key); - return j.setbit(key, offset, value); + Jedis j = getShard(key); + return j.setbit(key, offset, value); } public Boolean setbit(byte[] key, long offset, byte[] value) { - Jedis j = getShard(key); - return j.setbit(key, offset, value); + Jedis j = getShard(key); + return j.setbit(key, offset, value); } public Boolean getbit(byte[] key, long offset) { - Jedis j = getShard(key); - return j.getbit(key, offset); + Jedis j = getShard(key); + return j.getbit(key, offset); } public Long setrange(byte[] key, long offset, byte[] value) { - Jedis j = getShard(key); - return j.setrange(key, offset, value); + Jedis j = getShard(key); + return j.setrange(key, offset, value); } public byte[] getrange(byte[] key, long startOffset, long endOffset) { - Jedis j = getShard(key); - return j.getrange(key, startOffset, endOffset); + Jedis j = getShard(key); + return j.getrange(key, startOffset, endOffset); } public Long move(byte[] key, int dbIndex) { - Jedis j = getShard(key); - return j.move(key, dbIndex); + Jedis j = getShard(key); + return j.move(key, dbIndex); } public byte[] echo(byte[] arg) { - Jedis j = getShard(arg); - return j.echo(arg); + Jedis j = getShard(arg); + return j.echo(arg); } public List brpop(byte[] arg) { - Jedis j = getShard(arg); - return j.brpop(arg); + Jedis j = getShard(arg); + return j.brpop(arg); } public List blpop(byte[] arg) { - Jedis j = getShard(arg); - return j.blpop(arg); + Jedis j = getShard(arg); + return j.blpop(arg); } public Long bitcount(byte[] key) { - Jedis j = getShard(key); - return j.bitcount(key); + Jedis j = getShard(key); + return j.bitcount(key); } public Long bitcount(byte[] key, long start, long end) { - Jedis j = getShard(key); - return j.bitcount(key, start, end); + Jedis j = getShard(key); + return j.bitcount(key, start, end); } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/JedisFactory.java b/src/main/java/redis/clients/jedis/JedisFactory.java index 66592fe..3597d69 100644 --- a/src/main/java/redis/clients/jedis/JedisFactory.java +++ b/src/main/java/redis/clients/jedis/JedisFactory.java @@ -1,11 +1,13 @@ package redis.clients.jedis; -import org.apache.commons.pool.BasePoolableObjectFactory; +import org.apache.commons.pool2.PooledObject; +import org.apache.commons.pool2.PooledObjectFactory; +import org.apache.commons.pool2.impl.DefaultPooledObject; /** * PoolableObjectFactory custom impl. */ -class JedisFactory extends BasePoolableObjectFactory { +class JedisFactory implements PooledObjectFactory { private final String host; private final int port; private final int timeout; @@ -13,75 +15,80 @@ class JedisFactory extends BasePoolableObjectFactory { private final int database; private final String clientName; - public JedisFactory(final String host, final int port, - final int timeout, final String password, final int database) { - this(host, port, timeout, password, database, null); - } - public JedisFactory(final String host, final int port, - final int timeout, final String password, final int database, final String clientName) { - super(); - this.host = host; - this.port = port; - this.timeout = timeout; - this.password = password; - this.database = database; - this.clientName = clientName; + public JedisFactory(final String host, final int port, final int timeout, + final String password, final int database) { + this(host, port, timeout, password, database, null); } - public Object makeObject() throws Exception { - final Jedis jedis = new Jedis(this.host, this.port, this.timeout); - - jedis.connect(); - if (null != this.password) { - jedis.auth(this.password); - } - if( database != 0 ) { - jedis.select(database); - } - if ( clientName != null ) { - jedis.clientSetname(clientName); - } - - return jedis; + public JedisFactory(final String host, final int port, final int timeout, + final String password, final int database, final String clientName) { + super(); + this.host = host; + this.port = port; + this.timeout = timeout; + this.password = password; + this.database = database; + this.clientName = clientName; } @Override - public void activateObject(Object obj) throws Exception { - if (obj instanceof Jedis) { - final Jedis jedis = (Jedis)obj; - if (jedis.getDB() != database) { - jedis.select(database); - } + public void activateObject(PooledObject pooledJedis) + throws Exception { + final BinaryJedis jedis = pooledJedis.getObject(); + if (jedis.getDB() != database) { + jedis.select(database); + } + + } + + @Override + public void destroyObject(PooledObject pooledJedis) throws Exception { + final BinaryJedis jedis = pooledJedis.getObject(); + if (jedis.isConnected()) { + try { + try { + jedis.quit(); + } catch (Exception e) { } + jedis.disconnect(); + } catch (Exception e) { + + } + } + } - public void destroyObject(final Object obj) throws Exception { - if (obj instanceof Jedis) { - final Jedis jedis = (Jedis) obj; - if (jedis.isConnected()) { - try { - try { - jedis.quit(); - } catch (Exception e) { - } - jedis.disconnect(); - } catch (Exception e) { + @Override + public PooledObject makeObject() throws Exception { + final Jedis jedis = new Jedis(this.host, this.port, this.timeout); - } - } - } + jedis.connect(); + if (null != this.password) { + jedis.auth(this.password); + } + if (database != 0) { + jedis.select(database); + } + if (clientName != null) { + jedis.clientSetname(clientName); + } + + return new DefaultPooledObject(jedis); } - public boolean validateObject(final Object obj) { - if (obj instanceof Jedis) { - final Jedis jedis = (Jedis) obj; - try { - return jedis.isConnected() && jedis.ping().equals("PONG"); - } catch (final Exception e) { - return false; - } - } else { - return false; - } + @Override + public void passivateObject(PooledObject pooledJedis) + throws Exception { + // TODO maybe should select db 0? Not sure right now. + } + + @Override + public boolean validateObject(PooledObject pooledJedis) { + final BinaryJedis jedis = pooledJedis.getObject(); + try { + return jedis.isConnected() && jedis.ping().equals("PONG"); + } catch (final Exception e) { + return false; + } } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index 6e8b74d..58212ba 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -2,19 +2,21 @@ package redis.clients.jedis; import java.net.URI; -import org.apache.commons.pool.impl.GenericObjectPool; -import org.apache.commons.pool.impl.GenericObjectPool.Config; +import org.apache.commons.pool2.impl.GenericObjectPool; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.util.Pool; public class JedisPool extends Pool { - public JedisPool(final Config poolConfig, final String host) { - this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null); + public JedisPool(final GenericObjectPoolConfig poolConfig, final String host) { + this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, + null, Protocol.DEFAULT_DATABASE, null); } public JedisPool(String host, int port) { - this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null); + this(new GenericObjectPoolConfig(), host, port, + Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null); } public JedisPool(final String host) { @@ -24,12 +26,15 @@ public class JedisPool extends Pool { int port = uri.getPort(); String password = uri.getUserInfo().split(":", 2)[1]; int database = Integer.parseInt(uri.getPath().split("/", 2)[1]); - this.internalPool = new GenericObjectPool(new JedisFactory(h, port, - Protocol.DEFAULT_TIMEOUT, password, database, null), new Config()); + this.internalPool = new GenericObjectPool( + new JedisFactory(h, port, Protocol.DEFAULT_TIMEOUT, + password, database, null), + new GenericObjectPoolConfig()); } else { - this.internalPool = new GenericObjectPool(new JedisFactory(host, - Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, - Protocol.DEFAULT_DATABASE, null), new Config()); + this.internalPool = new GenericObjectPool(new JedisFactory( + host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, + null, Protocol.DEFAULT_DATABASE, null), + new GenericObjectPoolConfig()); } } @@ -38,39 +43,47 @@ public class JedisPool extends Pool { int port = uri.getPort(); String password = uri.getUserInfo().split(":", 2)[1]; int database = Integer.parseInt(uri.getPath().split("/", 2)[1]); - this.internalPool = new GenericObjectPool(new JedisFactory(h, port, - Protocol.DEFAULT_TIMEOUT, password, database, null), new Config()); + this.internalPool = new GenericObjectPool(new JedisFactory(h, + port, Protocol.DEFAULT_TIMEOUT, password, database, null), + new GenericObjectPoolConfig()); } - public JedisPool(final Config poolConfig, final String host, int port, - int timeout, final String password) { - this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE, null); + public JedisPool(final GenericObjectPoolConfig poolConfig, + final String host, int port, int timeout, final String password) { + this(poolConfig, host, port, timeout, password, + Protocol.DEFAULT_DATABASE, null); } - public JedisPool(final Config poolConfig, final String host, final int port) { - this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null); + public JedisPool(final GenericObjectPoolConfig poolConfig, + final String host, final int port) { + this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, + Protocol.DEFAULT_DATABASE, null); } - public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) { - this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE, null); + public JedisPool(final GenericObjectPoolConfig poolConfig, + final String host, final int port, final int timeout) { + this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE, + null); } - public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password, - final int database) { - this(poolConfig, host, port, timeout, password, database, null); + public JedisPool(final GenericObjectPoolConfig poolConfig, + final String host, int port, int timeout, final String password, + final int database) { + this(poolConfig, host, port, timeout, password, database, null); } - public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password, - final int database, final String clientName) { - super(poolConfig, new JedisFactory(host, port, timeout, password, database, clientName)); + public JedisPool(final GenericObjectPoolConfig poolConfig, + final String host, int port, int timeout, final String password, + final int database, final String clientName) { + super(poolConfig, new JedisFactory(host, port, timeout, password, + database, clientName)); } - - public void returnBrokenResource(final BinaryJedis resource) { - returnBrokenResourceObject(resource); + public void returnBrokenResource(final Jedis resource) { + returnBrokenResourceObject(resource); } - public void returnResource(final BinaryJedis resource) { - returnResourceObject(resource); + public void returnResource(final Jedis resource) { + returnResourceObject(resource); } } diff --git a/src/main/java/redis/clients/jedis/JedisPoolConfig.java b/src/main/java/redis/clients/jedis/JedisPoolConfig.java index b079c07..fff38d4 100644 --- a/src/main/java/redis/clients/jedis/JedisPoolConfig.java +++ b/src/main/java/redis/clients/jedis/JedisPoolConfig.java @@ -1,28 +1,8 @@ package redis.clients.jedis; -import org.apache.commons.pool.impl.GenericObjectPool.Config; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -/** - * Subclass of org.apache.commons.pool.impl.GenericObjectPool.Config that - * includes getters/setters so it can be more easily configured by Spring and - * other IoC frameworks. - * - * Spring example: - * - * - * - * - * - * - * For information on parameters refer to: - * - * http://commons.apache.org/pool/apidocs/org/apache/commons/pool/impl/ - * GenericObjectPool.html - */ -public class JedisPoolConfig extends Config { +public class JedisPoolConfig extends GenericObjectPoolConfig { public JedisPoolConfig() { // defaults to make your life with connection pool easier :) setTestWhileIdle(true); @@ -30,103 +10,4 @@ public class JedisPoolConfig extends Config { setTimeBetweenEvictionRunsMillis(30000); setNumTestsPerEvictionRun(-1); } - - public int getMaxIdle() { - return maxIdle; - } - - public void setMaxIdle(int maxIdle) { - this.maxIdle = maxIdle; - } - - public int getMinIdle() { - return minIdle; - } - - public void setMinIdle(int minIdle) { - this.minIdle = minIdle; - } - - public int getMaxActive() { - return maxActive; - } - - public void setMaxActive(int maxActive) { - this.maxActive = maxActive; - } - - public long getMaxWait() { - return maxWait; - } - - public void setMaxWait(long maxWait) { - this.maxWait = maxWait; - } - - public byte getWhenExhaustedAction() { - return whenExhaustedAction; - } - - public void setWhenExhaustedAction(byte whenExhaustedAction) { - this.whenExhaustedAction = whenExhaustedAction; - } - - public boolean isTestOnBorrow() { - return testOnBorrow; - } - - public void setTestOnBorrow(boolean testOnBorrow) { - this.testOnBorrow = testOnBorrow; - } - - public boolean isTestOnReturn() { - return testOnReturn; - } - - public void setTestOnReturn(boolean testOnReturn) { - this.testOnReturn = testOnReturn; - } - - public boolean isTestWhileIdle() { - return testWhileIdle; - } - - public void setTestWhileIdle(boolean testWhileIdle) { - this.testWhileIdle = testWhileIdle; - } - - public long getTimeBetweenEvictionRunsMillis() { - return timeBetweenEvictionRunsMillis; - } - - public void setTimeBetweenEvictionRunsMillis( - long timeBetweenEvictionRunsMillis) { - this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; - } - - public int getNumTestsPerEvictionRun() { - return numTestsPerEvictionRun; - } - - public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) { - this.numTestsPerEvictionRun = numTestsPerEvictionRun; - } - - public long getMinEvictableIdleTimeMillis() { - return minEvictableIdleTimeMillis; - } - - public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { - this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; - } - - public long getSoftMinEvictableIdleTimeMillis() { - return softMinEvictableIdleTimeMillis; - } - - public void setSoftMinEvictableIdleTimeMillis( - long softMinEvictableIdleTimeMillis) { - this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis; - } - } diff --git a/src/main/java/redis/clients/jedis/JedisSentinelPool.java b/src/main/java/redis/clients/jedis/JedisSentinelPool.java index 0d87bdd..2761c90 100644 --- a/src/main/java/redis/clients/jedis/JedisSentinelPool.java +++ b/src/main/java/redis/clients/jedis/JedisSentinelPool.java @@ -7,14 +7,14 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Logger; -import org.apache.commons.pool.impl.GenericObjectPool.Config; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.util.Pool; public class JedisSentinelPool extends Pool { - protected Config poolConfig; + protected GenericObjectPoolConfig poolConfig; protected int timeout = Protocol.DEFAULT_TIMEOUT; @@ -27,57 +27,58 @@ public class JedisSentinelPool extends Pool { protected Logger log = Logger.getLogger(getClass().getName()); public JedisSentinelPool(String masterName, Set sentinels, - final Config poolConfig) { + final GenericObjectPoolConfig poolConfig) { this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE); } public JedisSentinelPool(String masterName, Set sentinels) { - this(masterName, sentinels, new Config(), Protocol.DEFAULT_TIMEOUT, - null, Protocol.DEFAULT_DATABASE); + this(masterName, sentinels, new GenericObjectPoolConfig(), + Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE); } public JedisSentinelPool(String masterName, Set sentinels, String password) { - this(masterName, sentinels, new Config(), Protocol.DEFAULT_TIMEOUT, - password); + this(masterName, sentinels, new GenericObjectPoolConfig(), + Protocol.DEFAULT_TIMEOUT, password); } public JedisSentinelPool(String masterName, Set sentinels, - final Config poolConfig, int timeout, final String password) { + final GenericObjectPoolConfig poolConfig, int timeout, + final String password) { this(masterName, sentinels, poolConfig, timeout, password, Protocol.DEFAULT_DATABASE); } public JedisSentinelPool(String masterName, Set sentinels, - final Config poolConfig, final int timeout) { + final GenericObjectPoolConfig poolConfig, final int timeout) { this(masterName, sentinels, poolConfig, timeout, null, Protocol.DEFAULT_DATABASE); } public JedisSentinelPool(String masterName, Set sentinels, - final Config poolConfig, final String password) { + final GenericObjectPoolConfig poolConfig, final String password) { this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, password); } public JedisSentinelPool(String masterName, Set sentinels, - final Config poolConfig, int timeout, final String password, - final int database) { + final GenericObjectPoolConfig poolConfig, int timeout, + final String password, final int database) { this.poolConfig = poolConfig; this.timeout = timeout; this.password = password; this.database = database; - + HostAndPort master = initSentinels(sentinels, masterName); initPool(master); } - public void returnBrokenResource(final BinaryJedis resource) { + public void returnBrokenResource(final Jedis resource) { returnBrokenResourceObject(resource); } - public void returnResource(final BinaryJedis resource) { + public void returnResource(final Jedis resource) { returnResourceObject(resource); } diff --git a/src/main/java/redis/clients/jedis/ShardedJedisPool.java b/src/main/java/redis/clients/jedis/ShardedJedisPool.java index ce11884..dd56ac1 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedisPool.java +++ b/src/main/java/redis/clients/jedis/ShardedJedisPool.java @@ -3,83 +3,101 @@ package redis.clients.jedis; import java.util.List; import java.util.regex.Pattern; -import org.apache.commons.pool.BasePoolableObjectFactory; -import org.apache.commons.pool.impl.GenericObjectPool; +import org.apache.commons.pool2.PooledObject; +import org.apache.commons.pool2.PooledObjectFactory; +import org.apache.commons.pool2.impl.DefaultPooledObject; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.util.Hashing; import redis.clients.util.Pool; public class ShardedJedisPool extends Pool { - public ShardedJedisPool(final GenericObjectPool.Config poolConfig, - List shards) { - this(poolConfig, shards, Hashing.MURMUR_HASH); + public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, + List shards) { + this(poolConfig, shards, Hashing.MURMUR_HASH); } - public ShardedJedisPool(final GenericObjectPool.Config poolConfig, - List shards, Hashing algo) { - this(poolConfig, shards, algo, null); + public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, + List shards, Hashing algo) { + this(poolConfig, shards, algo, null); } - public ShardedJedisPool(final GenericObjectPool.Config poolConfig, - List shards, Pattern keyTagPattern) { - this(poolConfig, shards, Hashing.MURMUR_HASH, keyTagPattern); + public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, + List shards, Pattern keyTagPattern) { + this(poolConfig, shards, Hashing.MURMUR_HASH, keyTagPattern); } - public ShardedJedisPool(final GenericObjectPool.Config poolConfig, - List shards, Hashing algo, Pattern keyTagPattern) { - super(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern)); + public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, + List shards, Hashing algo, Pattern keyTagPattern) { + super(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern)); } /** * PoolableObjectFactory custom impl. */ - private static class ShardedJedisFactory extends BasePoolableObjectFactory { - private List shards; - private Hashing algo; - private Pattern keyTagPattern; + private static class ShardedJedisFactory implements + PooledObjectFactory { + private List shards; + private Hashing algo; + private Pattern keyTagPattern; - public ShardedJedisFactory(List shards, Hashing algo, - Pattern keyTagPattern) { - this.shards = shards; - this.algo = algo; - this.keyTagPattern = keyTagPattern; - } + public ShardedJedisFactory(List shards, Hashing algo, + Pattern keyTagPattern) { + this.shards = shards; + this.algo = algo; + this.keyTagPattern = keyTagPattern; + } - public Object makeObject() throws Exception { - ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern); - return jedis; - } + @Override + public PooledObject makeObject() throws Exception { + ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern); + return new DefaultPooledObject(jedis); + } - public void destroyObject(final Object obj) throws Exception { - if ((obj != null) && (obj instanceof ShardedJedis)) { - ShardedJedis shardedJedis = (ShardedJedis) obj; - for (Jedis jedis : shardedJedis.getAllShards()) { - try { - try { - jedis.quit(); - } catch (Exception e) { + @Override + public void destroyObject(PooledObject pooledShardedJedis) + throws Exception { + final ShardedJedis shardedJedis = pooledShardedJedis.getObject(); + for (Jedis jedis : shardedJedis.getAllShards()) { + try { + try { + jedis.quit(); + } catch (Exception e) { - } - jedis.disconnect(); - } catch (Exception e) { + } + jedis.disconnect(); + } catch (Exception e) { - } - } - } - } + } + } + } - public boolean validateObject(final Object obj) { - try { - ShardedJedis jedis = (ShardedJedis) obj; - for (Jedis shard : jedis.getAllShards()) { - if (!shard.ping().equals("PONG")) { - return false; - } - } - return true; - } catch (Exception ex) { - return false; - } - } + @Override + public boolean validateObject( + PooledObject pooledShardedJedis) { + try { + ShardedJedis jedis = pooledShardedJedis.getObject(); + for (Jedis shard : jedis.getAllShards()) { + if (!shard.ping().equals("PONG")) { + return false; + } + } + return true; + } catch (Exception ex) { + return false; + } + } + + @Override + public void activateObject(PooledObject p) + throws Exception { + + } + + @Override + public void passivateObject(PooledObject p) + throws Exception { + + } } } \ No newline at end of file diff --git a/src/main/java/redis/clients/util/Pool.java b/src/main/java/redis/clients/util/Pool.java index fb4fede..09d8ebb 100644 --- a/src/main/java/redis/clients/util/Pool.java +++ b/src/main/java/redis/clients/util/Pool.java @@ -1,82 +1,84 @@ package redis.clients.util; -import org.apache.commons.pool.PoolableObjectFactory; -import org.apache.commons.pool.impl.GenericObjectPool; +import org.apache.commons.pool2.PooledObjectFactory; +import org.apache.commons.pool2.impl.GenericObjectPool; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; public abstract class Pool { - protected GenericObjectPool internalPool; + protected GenericObjectPool internalPool; /** - * Using this constructor means you have to set - * and initialize the internalPool yourself. + * Using this constructor means you have to set and initialize the + * internalPool yourself. */ - public Pool() {} - - public Pool(final GenericObjectPool.Config poolConfig, - PoolableObjectFactory factory) { - initPool(poolConfig, factory); + public Pool() { } - - public void initPool(final GenericObjectPool.Config poolConfig, PoolableObjectFactory factory) { - - if (this.internalPool != null) { - try { - closeInternalPool(); - } catch (Exception e) { - } - } - - this.internalPool = new GenericObjectPool(factory, poolConfig); + + public Pool(final GenericObjectPoolConfig poolConfig, + PooledObjectFactory factory) { + initPool(poolConfig, factory); } - - @SuppressWarnings("unchecked") + + public void initPool(final GenericObjectPoolConfig poolConfig, + PooledObjectFactory factory) { + + if (this.internalPool != null) { + try { + closeInternalPool(); + } catch (Exception e) { + } + } + + this.internalPool = new GenericObjectPool(factory, poolConfig); + } + public T getResource() { - try { - return (T) internalPool.borrowObject(); - } catch (Exception e) { - throw new JedisConnectionException( - "Could not get a resource from the pool", e); - } + try { + return internalPool.borrowObject(); + } catch (Exception e) { + throw new JedisConnectionException( + "Could not get a resource from the pool", e); + } } - - public void returnResourceObject(final Object resource) { - try { - internalPool.returnObject(resource); - } catch (Exception e) { - throw new JedisException( - "Could not return the resource to the pool", e); - } + + public void returnResourceObject(final T resource) { + try { + internalPool.returnObject(resource); + } catch (Exception e) { + throw new JedisException( + "Could not return the resource to the pool", e); + } } - + public void returnBrokenResource(final T resource) { - returnBrokenResourceObject(resource); + returnBrokenResourceObject(resource); } - + public void returnResource(final T resource) { - returnResourceObject(resource); + returnResourceObject(resource); } - + public void destroy() { - closeInternalPool(); + closeInternalPool(); } - - protected void returnBrokenResourceObject(final Object resource) { - try { - internalPool.invalidateObject(resource); - } catch (Exception e) { - throw new JedisException( - "Could not return the resource to the pool", e); - } + + protected void returnBrokenResourceObject(final T resource) { + try { + internalPool.invalidateObject(resource); + } catch (Exception e) { + throw new JedisException( + "Could not return the resource to the pool", e); + } } protected void closeInternalPool() { - try { - internalPool.close(); - } catch (Exception e) { - throw new JedisException("Could not destroy the pool", e); - } + try { + internalPool.close(); + } catch (Exception e) { + throw new JedisException("Could not destroy the pool", e); + } } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java index 58964ff..286d681 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java @@ -3,12 +3,10 @@ package redis.clients.jedis.tests; import java.net.URI; import java.net.URISyntaxException; -import org.apache.commons.pool.impl.GenericObjectPool; -import org.apache.commons.pool.impl.GenericObjectPool.Config; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Assert; import org.junit.Test; -import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @@ -20,111 +18,103 @@ public class JedisPoolTest extends Assert { @Test public void checkConnections() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, - hnp.port, 2000); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - pool.returnResource(jedis); - pool.destroy(); + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port, 2000); + Jedis jedis = pool.getResource(); + jedis.auth("foobared"); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + pool.returnResource(jedis); + pool.destroy(); } @Test public void checkConnectionWithDefaultPort() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, - hnp.port); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - pool.returnResource(jedis); - pool.destroy(); + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port); + Jedis jedis = pool.getResource(); + jedis.auth("foobared"); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + pool.returnResource(jedis); + pool.destroy(); } @Test public void checkJedisIsReusedWhenReturned() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, - hnp.port); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "0"); - pool.returnResource(jedis); + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port); + Jedis jedis = pool.getResource(); + jedis.auth("foobared"); + jedis.set("foo", "0"); + pool.returnResource(jedis); - jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.incr("foo"); - pool.returnResource(jedis); - pool.destroy(); + jedis = pool.getResource(); + jedis.auth("foobared"); + jedis.incr("foo"); + pool.returnResource(jedis); + pool.destroy(); } @Test public void checkPoolRepairedWhenJedisIsBroken() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, - hnp.port); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.quit(); - pool.returnBrokenResource(jedis); + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port); + Jedis jedis = pool.getResource(); + jedis.auth("foobared"); + jedis.quit(); + pool.returnBrokenResource(jedis); - jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.incr("foo"); - pool.returnResource(jedis); - pool.destroy(); + jedis = pool.getResource(); + jedis.auth("foobared"); + jedis.incr("foo"); + pool.returnResource(jedis); + pool.destroy(); } @Test(expected = JedisConnectionException.class) public void checkPoolOverflow() { - Config config = new Config(); - config.maxActive = 1; - config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; - JedisPool pool = new JedisPool(config, hnp.host, hnp.port); - Jedis jedis = pool.getResource(); - jedis.auth("foobared"); - jedis.set("foo", "0"); + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); + config.setMaxTotal(1); + config.setBlockWhenExhausted(false); + JedisPool pool = new JedisPool(config, hnp.host, hnp.port); + Jedis jedis = pool.getResource(); + jedis.auth("foobared"); + jedis.set("foo", "0"); - Jedis newJedis = pool.getResource(); - newJedis.auth("foobared"); - newJedis.incr("foo"); + Jedis newJedis = pool.getResource(); + newJedis.auth("foobared"); + newJedis.incr("foo"); } @Test public void securePool() { - JedisPoolConfig config = new JedisPoolConfig(); - config.setTestOnBorrow(true); - JedisPool pool = new JedisPool(config, hnp.host, hnp.port, 2000, "foobared"); - Jedis jedis = pool.getResource(); - jedis.set("foo", "bar"); - pool.returnResource(jedis); - pool.destroy(); + JedisPoolConfig config = new JedisPoolConfig(); + config.setTestOnBorrow(true); + JedisPool pool = new JedisPool(config, hnp.host, hnp.port, 2000, + "foobared"); + Jedis jedis = pool.getResource(); + jedis.set("foo", "bar"); + pool.returnResource(jedis); + pool.destroy(); } @Test public void nonDefaultDatabase() { - JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host, - hnp.port, 2000, "foobared"); - Jedis jedis0 = pool0.getResource(); - jedis0.set("foo", "bar"); - assertEquals( "bar", jedis0.get("foo") ); - pool0.returnResource(jedis0); - pool0.destroy(); + JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port, 2000, "foobared"); + Jedis jedis0 = pool0.getResource(); + jedis0.set("foo", "bar"); + assertEquals("bar", jedis0.get("foo")); + pool0.returnResource(jedis0); + pool0.destroy(); - JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.host, - hnp.port, 2000, "foobared", 1); - Jedis jedis1 = pool1.getResource(); - assertNull( jedis1.get("foo") ); - pool1.returnResource(jedis0); - pool1.destroy(); - } - - @Test - public void returnBinary() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, - hnp.port, 2000); - BinaryJedis jedis = pool.getResource(); - pool.returnResource(jedis); - pool.destroy(); + JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port, 2000, "foobared", 1); + Jedis jedis1 = pool1.getResource(); + assertNull(jedis1.get("foo")); + pool1.returnResource(jedis1); + pool1.destroy(); } @Test @@ -145,43 +135,44 @@ public class JedisPoolTest extends Assert { j.auth("foobared"); j.select(2); j.set("foo", "bar"); - JedisPool pool = new JedisPool(new URI("redis://:foobared@localhost:6380/2")); + JedisPool pool = new JedisPool(new URI( + "redis://:foobared@localhost:6380/2")); Jedis jedis = pool.getResource(); assertEquals("PONG", jedis.ping()); assertEquals("bar", jedis.get("foo")); } - @Test - public void selectDatabaseOnActivation() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, hnp.port, 2000, "foobared"); + @Test + public void selectDatabaseOnActivation() { + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port, 2000, "foobared"); - Jedis jedis0 = pool.getResource(); - assertEquals(0L, jedis0.getDB().longValue()); - - jedis0.select(1); - assertEquals(1L, jedis0.getDB().longValue()); + Jedis jedis0 = pool.getResource(); + assertEquals(0L, jedis0.getDB().longValue()); - pool.returnResource(jedis0); + jedis0.select(1); + assertEquals(1L, jedis0.getDB().longValue()); - Jedis jedis1 = pool.getResource(); - assertTrue("Jedis instance was not reused", jedis1 == jedis0); - assertEquals(0L, jedis1.getDB().longValue()); + pool.returnResource(jedis0); - pool.returnResource(jedis1); - pool.destroy(); - } + Jedis jedis1 = pool.getResource(); + assertTrue("Jedis instance was not reused", jedis1 == jedis0); + assertEquals(0L, jedis1.getDB().longValue()); + + pool.returnResource(jedis1); + pool.destroy(); + } @Test public void customClientName() { - JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host, - hnp.port, 2000, "foobared", 0, "my_shiny_client_name"); + JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host, + hnp.port, 2000, "foobared", 0, "my_shiny_client_name"); - Jedis jedis = pool0.getResource(); + Jedis jedis = pool0.getResource(); - assertEquals("my_shiny_client_name", jedis.clientGetname()); + assertEquals("my_shiny_client_name", jedis.clientGetname()); - pool0.returnResource(jedis); - pool0.destroy(); + pool0.returnResource(jedis); + pool0.destroy(); } } - diff --git a/src/test/java/redis/clients/jedis/tests/JedisSentinelPoolTest.java b/src/test/java/redis/clients/jedis/tests/JedisSentinelPoolTest.java index 5f8e494..2487c8d 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisSentinelPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisSentinelPoolTest.java @@ -3,7 +3,7 @@ package redis.clients.jedis.tests; import java.util.HashSet; import java.util.Set; -import org.apache.commons.pool.impl.GenericObjectPool.Config; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Before; import org.junit.Test; @@ -19,7 +19,7 @@ public class JedisSentinelPoolTest extends JedisTestBase { protected static HostAndPort slave1 = HostAndPortUtil.getRedisServers() .get(3); protected static HostAndPort slave2 = HostAndPortUtil.getRedisServers() - .get(4); + .get(4); protected static HostAndPort sentinel1 = HostAndPortUtil .getSentinelServers().get(1); protected static HostAndPort sentinel2 = HostAndPortUtil @@ -28,7 +28,7 @@ public class JedisSentinelPoolTest extends JedisTestBase { protected static Jedis masterJedis; protected static Jedis slaveJedis1; protected static Jedis slaveJedis2; - + protected static int slaveCount = 0; protected Set sentinels = new HashSet(); @@ -45,7 +45,7 @@ public class JedisSentinelPoolTest extends JedisTestBase { slaveJedis1.auth("foobared"); slaveJedis1.slaveof(master.host, master.port); slaveCount++; - + slaveJedis2 = new Jedis(slave2.host, slave2.port); slaveJedis2.auth("foobared"); slaveJedis2.slaveof(master.host, master.port); @@ -55,44 +55,45 @@ public class JedisSentinelPoolTest extends JedisTestBase { sentinels.add(sentinel2.toString()); // FIXME: The following allows the master/slave relationship to - // be established, and let sentinels know about this relationship. + // be established, and let sentinels know about this relationship. // We can do this more elegantly. Thread.sleep(10000); } @Test public void ensureSafeTwiceFailover() throws InterruptedException { - JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels, - new Config(), 1000, "foobared", 2); - - // perform failover - doSegFaultMaster(pool); - - // perform failover once again - doSegFaultMaster(pool); - - // you can test failover as much as possible - // but you need to prepare additional slave per failover + JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels, + new GenericObjectPoolConfig(), 1000, "foobared", 2); + + // perform failover + doSegFaultMaster(pool); + + // perform failover once again + doSegFaultMaster(pool); + + // you can test failover as much as possible + // but you need to prepare additional slave per failover } - - private void doSegFaultMaster(JedisSentinelPool pool) throws InterruptedException { - // jedis connection should be master - Jedis jedis = pool.getResource(); - assertEquals("PONG", jedis.ping()); - try { - jedis.debug(DebugParams.SEGFAULT()); - } catch (Exception e) { - } + private void doSegFaultMaster(JedisSentinelPool pool) + throws InterruptedException { + // jedis connection should be master + Jedis jedis = pool.getResource(); + assertEquals("PONG", jedis.ping()); - // wait for the sentinel to promote a master - // FIXME: we can query the sentinel and sleep - // right until the master is promoted - Thread.sleep(35000); + try { + jedis.debug(DebugParams.SEGFAULT()); + } catch (Exception e) { + } - jedis = pool.getResource(); - assertEquals("PONG", jedis.ping()); - assertEquals("foobared", jedis.configGet("requirepass").get(1)); - assertEquals(2, jedis.getDB().intValue()); + // wait for the sentinel to promote a master + // FIXME: we can query the sentinel and sleep + // right until the master is promoted + Thread.sleep(35000); + + jedis = pool.getResource(); + assertEquals("PONG", jedis.ping()); + assertEquals("foobared", jedis.configGet("requirepass").get(1)); + assertEquals(2, jedis.getDB().intValue()); } } diff --git a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java index 868527f..c2369c7 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java @@ -1,8 +1,8 @@ package redis.clients.jedis.tests; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import java.util.List; import java.util.Map; diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisPipelineTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisPipelineTest.java index 7574f08..0f2e183 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisPipelineTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisPipelineTest.java @@ -1,9 +1,9 @@ package redis.clients.jedis.tests; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import java.io.UnsupportedEncodingException; import java.util.ArrayList; @@ -25,104 +25,104 @@ import redis.clients.jedis.exceptions.JedisDataException; public class ShardedJedisPipelineTest { private static HostAndPortUtil.HostAndPort redis1 = HostAndPortUtil - .getRedisServers().get(0); + .getRedisServers().get(0); private static HostAndPortUtil.HostAndPort redis2 = HostAndPortUtil - .getRedisServers().get(1); + .getRedisServers().get(1); private ShardedJedis jedis; @Before public void setUp() throws Exception { - Jedis jedis = new Jedis(redis1.host, redis1.port); - jedis.auth("foobared"); - jedis.flushAll(); - jedis.disconnect(); - jedis = new Jedis(redis2.host, redis2.port); - jedis.auth("foobared"); - jedis.flushAll(); - jedis.disconnect(); + Jedis jedis = new Jedis(redis1.host, redis1.port); + jedis.auth("foobared"); + jedis.flushAll(); + jedis.disconnect(); + jedis = new Jedis(redis2.host, redis2.port); + jedis.auth("foobared"); + jedis.flushAll(); + jedis.disconnect(); - JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.host, redis1.port); - JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.host, redis2.port); - shardInfo1.setPassword("foobared"); - shardInfo2.setPassword("foobared"); - List shards = new ArrayList(); - shards.add(shardInfo1); - shards.add(shardInfo2); - this.jedis = new ShardedJedis(shards); + JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.host, redis1.port); + JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.host, redis2.port); + shardInfo1.setPassword("foobared"); + shardInfo2.setPassword("foobared"); + List shards = new ArrayList(); + shards.add(shardInfo1); + shards.add(shardInfo2); + this.jedis = new ShardedJedis(shards); } @Test public void pipeline() throws UnsupportedEncodingException { - ShardedJedisPipeline p = jedis.pipelined(); - p.set("foo", "bar"); - p.get("foo"); - List results = p.syncAndReturnAll(); + ShardedJedisPipeline p = jedis.pipelined(); + p.set("foo", "bar"); + p.get("foo"); + List results = p.syncAndReturnAll(); - assertEquals(2, results.size()); - assertEquals("OK", results.get(0)); - assertEquals("bar", results.get(1)); + assertEquals(2, results.size()); + assertEquals("OK", results.get(0)); + assertEquals("bar", results.get(1)); } @Test public void pipelineResponse() { - jedis.set("string", "foo"); - jedis.lpush("list", "foo"); - jedis.hset("hash", "foo", "bar"); - jedis.zadd("zset", 1, "foo"); - jedis.sadd("set", "foo"); + jedis.set("string", "foo"); + jedis.lpush("list", "foo"); + jedis.hset("hash", "foo", "bar"); + jedis.zadd("zset", 1, "foo"); + jedis.sadd("set", "foo"); - ShardedJedisPipeline p = jedis.pipelined(); - Response string = p.get("string"); - Response del = p.del("string"); - Response emptyString = p.get("string"); - Response list = p.lpop("list"); - Response hash = p.hget("hash", "foo"); - Response> zset = p.zrange("zset", 0, -1); - Response set = p.spop("set"); - Response blist = p.exists("list"); - Response zincrby = p.zincrby("zset", 1, "foo"); - Response zcard = p.zcard("zset"); - p.lpush("list", "bar"); - Response> lrange = p.lrange("list", 0, -1); - Response> hgetAll = p.hgetAll("hash"); - p.sadd("set", "foo"); - Response> smembers = p.smembers("set"); - Response> zrangeWithScores = p.zrangeWithScores("zset", 0, - -1); - p.sync(); + ShardedJedisPipeline p = jedis.pipelined(); + Response string = p.get("string"); + Response del = p.del("string"); + Response emptyString = p.get("string"); + Response list = p.lpop("list"); + Response hash = p.hget("hash", "foo"); + Response> zset = p.zrange("zset", 0, -1); + Response set = p.spop("set"); + Response blist = p.exists("list"); + Response zincrby = p.zincrby("zset", 1, "foo"); + Response zcard = p.zcard("zset"); + p.lpush("list", "bar"); + Response> lrange = p.lrange("list", 0, -1); + Response> hgetAll = p.hgetAll("hash"); + p.sadd("set", "foo"); + Response> smembers = p.smembers("set"); + Response> zrangeWithScores = p.zrangeWithScores("zset", 0, + -1); + p.sync(); - assertEquals("foo", string.get()); - assertEquals(Long.valueOf(1), del.get()); - assertNull(emptyString.get()); - assertEquals("foo", list.get()); - assertEquals("bar", hash.get()); - assertEquals("foo", zset.get().iterator().next()); - assertEquals("foo", set.get()); - assertFalse(blist.get()); - assertEquals(Double.valueOf(2), zincrby.get()); - assertEquals(Long.valueOf(1), zcard.get()); - assertEquals(1, lrange.get().size()); - assertNotNull(hgetAll.get().get("foo")); - assertEquals(1, smembers.get().size()); - assertEquals(1, zrangeWithScores.get().size()); + assertEquals("foo", string.get()); + assertEquals(Long.valueOf(1), del.get()); + assertNull(emptyString.get()); + assertEquals("foo", list.get()); + assertEquals("bar", hash.get()); + assertEquals("foo", zset.get().iterator().next()); + assertEquals("foo", set.get()); + assertFalse(blist.get()); + assertEquals(Double.valueOf(2), zincrby.get()); + assertEquals(Long.valueOf(1), zcard.get()); + assertEquals(1, lrange.get().size()); + assertNotNull(hgetAll.get().get("foo")); + assertEquals(1, smembers.get().size()); + assertEquals(1, zrangeWithScores.get().size()); } @Test(expected = JedisDataException.class) public void pipelineResponseWithinPipeline() { - jedis.set("string", "foo"); + jedis.set("string", "foo"); - ShardedJedisPipeline p = jedis.pipelined(); - Response string = p.get("string"); - string.get(); - p.sync(); + ShardedJedisPipeline p = jedis.pipelined(); + Response string = p.get("string"); + string.get(); + p.sync(); } @Test public void canRetrieveUnsetKey() { - ShardedJedisPipeline p = jedis.pipelined(); - Response shouldNotExist = p.get(UUID.randomUUID().toString()); - p.sync(); - assertNull(shouldNotExist.get()); + ShardedJedisPipeline p = jedis.pipelined(); + Response shouldNotExist = p.get(UUID.randomUUID().toString()); + p.sync(); + assertNull(shouldNotExist.get()); } } diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java index 664e767..fbb078e 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java @@ -5,8 +5,7 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; -import org.apache.commons.pool.impl.GenericObjectPool; -import org.apache.commons.pool.impl.GenericObjectPool.Config; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -20,207 +19,215 @@ import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; public class ShardedJedisPoolTest extends Assert { private static HostAndPort redis1 = HostAndPortUtil.getRedisServers() - .get(0); + .get(0); private static HostAndPort redis2 = HostAndPortUtil.getRedisServers() - .get(1); + .get(1); private List shards; @Before public void startUp() { - shards = new ArrayList(); - shards.add(new JedisShardInfo(redis1.host, redis1.port)); - shards.add(new JedisShardInfo(redis2.host, redis2.port)); - shards.get(0).setPassword("foobared"); - shards.get(1).setPassword("foobared"); - Jedis j = new Jedis(shards.get(0)); - j.connect(); - j.flushAll(); - j.disconnect(); - j = new Jedis(shards.get(1)); - j.connect(); - j.flushAll(); - j.disconnect(); + shards = new ArrayList(); + shards.add(new JedisShardInfo(redis1.host, redis1.port)); + shards.add(new JedisShardInfo(redis2.host, redis2.port)); + shards.get(0).setPassword("foobared"); + shards.get(1).setPassword("foobared"); + Jedis j = new Jedis(shards.get(0)); + j.connect(); + j.flushAll(); + j.disconnect(); + j = new Jedis(shards.get(1)); + j.connect(); + j.flushAll(); + j.disconnect(); } @Test public void checkConnections() { - ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards); - ShardedJedis jedis = pool.getResource(); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - pool.returnResource(jedis); - pool.destroy(); + ShardedJedisPool pool = new ShardedJedisPool( + new GenericObjectPoolConfig(), shards); + ShardedJedis jedis = pool.getResource(); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + pool.returnResource(jedis); + pool.destroy(); } @Test public void checkConnectionWithDefaultPort() { - ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards); - ShardedJedis jedis = pool.getResource(); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - pool.returnResource(jedis); - pool.destroy(); + ShardedJedisPool pool = new ShardedJedisPool( + new GenericObjectPoolConfig(), shards); + ShardedJedis jedis = pool.getResource(); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + pool.returnResource(jedis); + pool.destroy(); } @Test public void checkJedisIsReusedWhenReturned() { - ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards); - ShardedJedis jedis = pool.getResource(); - jedis.set("foo", "0"); - pool.returnResource(jedis); + ShardedJedisPool pool = new ShardedJedisPool( + new GenericObjectPoolConfig(), shards); + ShardedJedis jedis = pool.getResource(); + jedis.set("foo", "0"); + pool.returnResource(jedis); - jedis = pool.getResource(); - jedis.incr("foo"); - pool.returnResource(jedis); - pool.destroy(); + jedis = pool.getResource(); + jedis.incr("foo"); + pool.returnResource(jedis); + pool.destroy(); } @Test public void checkPoolRepairedWhenJedisIsBroken() { - ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards); - ShardedJedis jedis = pool.getResource(); - jedis.disconnect(); - pool.returnBrokenResource(jedis); + ShardedJedisPool pool = new ShardedJedisPool( + new GenericObjectPoolConfig(), shards); + ShardedJedis jedis = pool.getResource(); + jedis.disconnect(); + pool.returnBrokenResource(jedis); - jedis = pool.getResource(); - jedis.incr("foo"); - pool.returnResource(jedis); - pool.destroy(); + jedis = pool.getResource(); + jedis.incr("foo"); + pool.returnResource(jedis); + pool.destroy(); } @Test(expected = JedisConnectionException.class) public void checkPoolOverflow() { - Config config = new Config(); - config.maxActive = 1; - config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); + config.setMaxTotal(1); + config.setBlockWhenExhausted(false); + + ShardedJedisPool pool = new ShardedJedisPool(config, shards); - ShardedJedisPool pool = new ShardedJedisPool(config, shards); + ShardedJedis jedis = pool.getResource(); + jedis.set("foo", "0"); - ShardedJedis jedis = pool.getResource(); - jedis.set("foo", "0"); - - ShardedJedis newJedis = pool.getResource(); - newJedis.incr("foo"); + ShardedJedis newJedis = pool.getResource(); + newJedis.incr("foo"); } @Test public void shouldNotShareInstances() { - Config config = new Config(); - config.maxActive = 2; - config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); + config.setMaxTotal(2); - ShardedJedisPool pool = new ShardedJedisPool(config, shards); + ShardedJedisPool pool = new ShardedJedisPool(config, shards); - ShardedJedis j1 = pool.getResource(); - ShardedJedis j2 = pool.getResource(); + ShardedJedis j1 = pool.getResource(); + ShardedJedis j2 = pool.getResource(); - assertNotSame(j1.getShard("foo"), j2.getShard("foo")); + assertNotSame(j1.getShard("foo"), j2.getShard("foo")); } @Test public void checkFailedJedisServer() { - ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards); - ShardedJedis jedis = pool.getResource(); - jedis.incr("foo"); - pool.returnResource(jedis); - pool.destroy(); + ShardedJedisPool pool = new ShardedJedisPool( + new GenericObjectPoolConfig(), shards); + ShardedJedis jedis = pool.getResource(); + jedis.incr("foo"); + pool.returnResource(jedis); + pool.destroy(); } @Test public void shouldReturnActiveShardsWhenOneGoesOffline() { - Config redisConfig = new Config(); - redisConfig.testOnBorrow = 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"); - } - pool.returnResource(jedis); - // 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("nohost", 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++; - } - } - pool.returnResource(jedis); - pool.destroy(); - assertEquals(actual, c1); - assertEquals(fails, c2); + 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"); + } + pool.returnResource(jedis); + // 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("nohost", 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++; + } + } + pool.returnResource(jedis); + pool.destroy(); + assertEquals(actual, c1); + assertEquals(fails, c2); } - + @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 shards = new ArrayList(); shards.add(new JedisShardInfo("redis://:foobared@localhost:6380")); shards.add(new JedisShardInfo("redis://:foobared@localhost:6379")); - - Config redisConfig = new Config(); + + GenericObjectPoolConfig redisConfig = new GenericObjectPoolConfig(); ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards); - - Jedis[] jedises = pool.getResource().getAllShards().toArray(new Jedis[2]); - + + 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")); } - + @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 shards = new ArrayList(); - shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380"))); - shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379"))); - - Config redisConfig = new Config(); + 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[] 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")); diff --git a/src/test/java/redis/clients/jedis/tests/benchmark/PoolBenchmark.java b/src/test/java/redis/clients/jedis/tests/benchmark/PoolBenchmark.java index ae1ffc3..224f004 100644 --- a/src/test/java/redis/clients/jedis/tests/benchmark/PoolBenchmark.java +++ b/src/test/java/redis/clients/jedis/tests/benchmark/PoolBenchmark.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -import org.apache.commons.pool.impl.GenericObjectPool.Config; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @@ -16,48 +16,48 @@ public class PoolBenchmark { private static final int TOTAL_OPERATIONS = 100000; public static void main(String[] args) throws Exception { - Jedis j = new Jedis(hnp.host, hnp.port); - j.connect(); - j.auth("foobared"); - j.flushAll(); - j.quit(); - j.disconnect(); - long t = System.currentTimeMillis(); - // withoutPool(); - withPool(); - long elapsed = System.currentTimeMillis() - t; - System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops"); + Jedis j = new Jedis(hnp.host, hnp.port); + j.connect(); + j.auth("foobared"); + j.flushAll(); + j.quit(); + j.disconnect(); + long t = System.currentTimeMillis(); + // withoutPool(); + withPool(); + long elapsed = System.currentTimeMillis() - t; + System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops"); } private static void withPool() throws Exception { - final JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port, - 2000, "foobared"); - List tds = new ArrayList(); + final JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), + hnp.host, hnp.port, 2000, "foobared"); + List tds = new ArrayList(); - final AtomicInteger ind = new AtomicInteger(); - for (int i = 0; i < 50; i++) { - Thread hj = new Thread(new Runnable() { - public void run() { - for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS;) { - try { - Jedis j = pool.getResource(); - final String key = "foo" + i; - j.set(key, key); - j.get(key); - pool.returnResource(j); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - }); - tds.add(hj); - hj.start(); - } + final AtomicInteger ind = new AtomicInteger(); + for (int i = 0; i < 50; i++) { + Thread hj = new Thread(new Runnable() { + public void run() { + for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS;) { + try { + Jedis j = pool.getResource(); + final String key = "foo" + i; + j.set(key, key); + j.get(key); + pool.returnResource(j); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + }); + tds.add(hj); + hj.start(); + } - for (Thread t : tds) - t.join(); + for (Thread t : tds) + t.join(); - pool.destroy(); + pool.destroy(); } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java index f0ee6bf..b21d739 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java @@ -299,11 +299,8 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { @Test public void ttl() { - // This is supposed to return -2 according to - // http://redis.io/commands/ttl - // and needs to be fixed in Redis. long ttl = jedis.ttl("foo"); - assertEquals(-1, ttl); + assertEquals(-2, ttl); jedis.set("foo", "bar"); ttl = jedis.ttl("foo"); @@ -313,13 +310,9 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { ttl = jedis.ttl("foo"); assertTrue(ttl >= 0 && ttl <= 20); - // This is supposed to return -2 according to - // http://redis.io/commands/ttl - // and needs to be fixed in Redis. - // Binary long bttl = jedis.ttl(bfoo); - assertEquals(-1, bttl); + assertEquals(-2, bttl); jedis.set(bfoo, bbar); bttl = jedis.ttl(bfoo); @@ -499,7 +492,7 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { @Test public void pttl() { long pttl = jedis.pttl("foo"); - assertEquals(-1, pttl); + assertEquals(-2, pttl); jedis.set("foo", "bar"); pttl = jedis.pttl("foo"); diff --git a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java index 5e03270..b3f6a7e 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java @@ -11,11 +11,10 @@ import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Jedis; -import redis.clients.jedis.Pipeline; +import redis.clients.jedis.Protocol.Keyword; import redis.clients.jedis.Response; import redis.clients.jedis.Transaction; import redis.clients.jedis.TransactionBlock; -import redis.clients.jedis.Protocol.Keyword; import redis.clients.jedis.exceptions.JedisDataException; public class TransactionCommandsTest extends JedisCommandTestBase { @@ -30,269 +29,269 @@ public class TransactionCommandsTest extends JedisCommandTestBase { @Before public void setUp() throws Exception { - super.setUp(); + super.setUp(); - nj = new Jedis(hnp.host, hnp.port, 500); - nj.connect(); - nj.auth("foobared"); - nj.flushAll(); + nj = new Jedis(hnp.host, hnp.port, 500); + nj.connect(); + nj.auth("foobared"); + nj.flushAll(); } @Test public void multi() { - Transaction trans = jedis.multi(); + Transaction trans = jedis.multi(); - trans.sadd("foo", "a"); - trans.sadd("foo", "b"); - trans.scard("foo"); + trans.sadd("foo", "a"); + trans.sadd("foo", "b"); + trans.scard("foo"); - List response = trans.exec(); + List response = trans.exec(); - List expected = new ArrayList(); - expected.add(1L); - expected.add(1L); - expected.add(2L); - assertEquals(expected, response); + List expected = new ArrayList(); + expected.add(1L); + expected.add(1L); + expected.add(2L); + assertEquals(expected, response); - // Binary - trans = jedis.multi(); + // Binary + trans = jedis.multi(); - trans.sadd(bfoo, ba); - trans.sadd(bfoo, bb); - trans.scard(bfoo); + trans.sadd(bfoo, ba); + trans.sadd(bfoo, bb); + trans.scard(bfoo); - response = trans.exec(); + response = trans.exec(); - expected = new ArrayList(); - expected.add(1L); - expected.add(1L); - expected.add(2L); - assertEquals(expected, response); + expected = new ArrayList(); + expected.add(1L); + expected.add(1L); + expected.add(2L); + assertEquals(expected, response); } @Test public void multiBlock() { - List response = jedis.multi(new TransactionBlock() { - @Override - public void execute() { - sadd("foo", "a"); - sadd("foo", "b"); - scard("foo"); - } - }); + List response = jedis.multi(new TransactionBlock() { + @Override + public void execute() { + sadd("foo", "a"); + sadd("foo", "b"); + scard("foo"); + } + }); - List expected = new ArrayList(); - expected.add(1L); - expected.add(1L); - expected.add(2L); - assertEquals(expected, response); + List expected = new ArrayList(); + expected.add(1L); + expected.add(1L); + expected.add(2L); + assertEquals(expected, response); - // Binary - response = jedis.multi(new TransactionBlock() { - @Override - public void execute() { - sadd(bfoo, ba); - sadd(bfoo, bb); - scard(bfoo); - } - }); + // Binary + response = jedis.multi(new TransactionBlock() { + @Override + public void execute() { + sadd(bfoo, ba); + sadd(bfoo, bb); + scard(bfoo); + } + }); - expected = new ArrayList(); - expected.add(1L); - expected.add(1L); - expected.add(2L); - assertEquals(expected, response); + expected = new ArrayList(); + expected.add(1L); + expected.add(1L); + expected.add(2L); + assertEquals(expected, response); } @Test public void watch() throws UnknownHostException, IOException { - jedis.watch("mykey", "somekey"); - Transaction t = jedis.multi(); + jedis.watch("mykey", "somekey"); + Transaction t = jedis.multi(); - nj.connect(); - nj.auth("foobared"); - nj.set("mykey", "bar"); - nj.disconnect(); + nj.connect(); + nj.auth("foobared"); + nj.set("mykey", "bar"); + nj.disconnect(); - t.set("mykey", "foo"); - List resp = t.exec(); - assertEquals(null, resp); - assertEquals("bar", jedis.get("mykey")); + t.set("mykey", "foo"); + List resp = t.exec(); + assertEquals(null, resp); + assertEquals("bar", jedis.get("mykey")); - // Binary - jedis.watch(bmykey, "foobar".getBytes()); - t = jedis.multi(); + // Binary + jedis.watch(bmykey, "foobar".getBytes()); + t = jedis.multi(); - nj.connect(); - nj.auth("foobared"); - nj.set(bmykey, bbar); - nj.disconnect(); + nj.connect(); + nj.auth("foobared"); + nj.set(bmykey, bbar); + nj.disconnect(); - t.set(bmykey, bfoo); - resp = t.exec(); - assertEquals(null, resp); - assertTrue(Arrays.equals(bbar, jedis.get(bmykey))); + t.set(bmykey, bfoo); + resp = t.exec(); + assertEquals(null, resp); + assertTrue(Arrays.equals(bbar, jedis.get(bmykey))); } @Test public void unwatch() throws UnknownHostException, IOException { - jedis.watch("mykey"); - String val = jedis.get("mykey"); - val = "foo"; - String status = jedis.unwatch(); - assertEquals("OK", status); - Transaction t = jedis.multi(); + jedis.watch("mykey"); + String val = jedis.get("mykey"); + val = "foo"; + String status = jedis.unwatch(); + assertEquals("OK", status); + Transaction t = jedis.multi(); - nj.connect(); - nj.auth("foobared"); - nj.set("mykey", "bar"); - nj.disconnect(); + nj.connect(); + nj.auth("foobared"); + nj.set("mykey", "bar"); + nj.disconnect(); - t.set("mykey", val); - List resp = t.exec(); - assertEquals(1, resp.size()); - assertEquals("OK", resp.get(0)); + t.set("mykey", val); + List resp = t.exec(); + assertEquals(1, resp.size()); + assertEquals("OK", resp.get(0)); - // Binary - jedis.watch(bmykey); - byte[] bval = jedis.get(bmykey); - bval = bfoo; - status = jedis.unwatch(); - assertEquals(Keyword.OK.name(), status); - t = jedis.multi(); + // Binary + jedis.watch(bmykey); + byte[] bval = jedis.get(bmykey); + bval = bfoo; + status = jedis.unwatch(); + assertEquals(Keyword.OK.name(), status); + t = jedis.multi(); - nj.connect(); - nj.auth("foobared"); - nj.set(bmykey, bbar); - nj.disconnect(); + nj.connect(); + nj.auth("foobared"); + nj.set(bmykey, bbar); + nj.disconnect(); - t.set(bmykey, bval); - resp = t.exec(); - assertEquals(1, resp.size()); - assertEquals("OK", resp.get(0)); + t.set(bmykey, bval); + resp = t.exec(); + assertEquals(1, resp.size()); + assertEquals("OK", resp.get(0)); } @Test(expected = JedisDataException.class) public void validateWhenInMulti() { - jedis.multi(); - jedis.ping(); + jedis.multi(); + jedis.ping(); } @Test public void discard() { - Transaction t = jedis.multi(); - String status = t.discard(); - assertEquals("OK", status); + Transaction t = jedis.multi(); + String status = t.discard(); + assertEquals("OK", status); } @Test public void transactionResponse() { - jedis.set("string", "foo"); - jedis.lpush("list", "foo"); - jedis.hset("hash", "foo", "bar"); - jedis.zadd("zset", 1, "foo"); - jedis.sadd("set", "foo"); + jedis.set("string", "foo"); + jedis.lpush("list", "foo"); + jedis.hset("hash", "foo", "bar"); + jedis.zadd("zset", 1, "foo"); + jedis.sadd("set", "foo"); - Transaction t = jedis.multi(); - Response string = t.get("string"); - Response list = t.lpop("list"); - Response hash = t.hget("hash", "foo"); - Response> zset = t.zrange("zset", 0, -1); - Response set = t.spop("set"); - t.exec(); + Transaction t = jedis.multi(); + Response string = t.get("string"); + Response list = t.lpop("list"); + Response hash = t.hget("hash", "foo"); + Response> zset = t.zrange("zset", 0, -1); + Response set = t.spop("set"); + t.exec(); - assertEquals("foo", string.get()); - assertEquals("foo", list.get()); - assertEquals("bar", hash.get()); - assertEquals("foo", zset.get().iterator().next()); - assertEquals("foo", set.get()); + assertEquals("foo", string.get()); + assertEquals("foo", list.get()); + assertEquals("bar", hash.get()); + assertEquals("foo", zset.get().iterator().next()); + assertEquals("foo", set.get()); } @Test public void transactionResponseBinary() { - jedis.set("string", "foo"); - jedis.lpush("list", "foo"); - jedis.hset("hash", "foo", "bar"); - jedis.zadd("zset", 1, "foo"); - jedis.sadd("set", "foo"); + jedis.set("string", "foo"); + jedis.lpush("list", "foo"); + jedis.hset("hash", "foo", "bar"); + jedis.zadd("zset", 1, "foo"); + jedis.sadd("set", "foo"); - Transaction t = jedis.multi(); - Response string = t.get("string".getBytes()); - Response list = t.lpop("list".getBytes()); - Response hash = t.hget("hash".getBytes(), "foo".getBytes()); - Response> zset = t.zrange("zset".getBytes(), 0, -1); - Response set = t.spop("set".getBytes()); - t.exec(); + Transaction t = jedis.multi(); + Response string = t.get("string".getBytes()); + Response list = t.lpop("list".getBytes()); + Response hash = t.hget("hash".getBytes(), "foo".getBytes()); + Response> zset = t.zrange("zset".getBytes(), 0, -1); + Response set = t.spop("set".getBytes()); + t.exec(); - assertArrayEquals("foo".getBytes(), string.get()); - assertArrayEquals("foo".getBytes(), list.get()); - assertArrayEquals("bar".getBytes(), hash.get()); - assertArrayEquals("foo".getBytes(), zset.get().iterator().next()); - assertArrayEquals("foo".getBytes(), set.get()); + assertArrayEquals("foo".getBytes(), string.get()); + assertArrayEquals("foo".getBytes(), list.get()); + assertArrayEquals("bar".getBytes(), hash.get()); + assertArrayEquals("foo".getBytes(), zset.get().iterator().next()); + assertArrayEquals("foo".getBytes(), set.get()); } @Test(expected = JedisDataException.class) public void transactionResponseWithinPipeline() { - jedis.set("string", "foo"); + jedis.set("string", "foo"); - Transaction t = jedis.multi(); - Response string = t.get("string"); - string.get(); - t.exec(); + Transaction t = jedis.multi(); + Response string = t.get("string"); + string.get(); + t.exec(); } - + @Test public void transactionResponseWithError() { - Transaction t = jedis.multi(); - t.set("foo", "bar"); - Response> error = t.smembers("foo"); - Response r = t.get("foo"); - List l = t.exec(); - assertEquals(JedisDataException.class, l.get(1).getClass()); - try{ - error.get(); - fail("We expect exception here!"); - }catch(JedisDataException e){ - //that is fine we should be here - } - assertEquals(r.get(), "bar"); + Transaction t = jedis.multi(); + t.set("foo", "bar"); + Response> error = t.smembers("foo"); + Response r = t.get("foo"); + List l = t.exec(); + assertEquals(JedisDataException.class, l.get(1).getClass()); + try { + error.get(); + fail("We expect exception here!"); + } catch (JedisDataException e) { + // that is fine we should be here + } + assertEquals(r.get(), "bar"); } - + @Test public void execGetResponse() { - Transaction t = jedis.multi(); + Transaction t = jedis.multi(); - t.set("foo", "bar"); - t.smembers("foo"); - t.get("foo"); + t.set("foo", "bar"); + t.smembers("foo"); + t.get("foo"); - List> lr = t.execGetResponse(); - try{ - lr.get(1).get(); - fail("We expect exception here!"); - }catch(JedisDataException e){ - //that is fine we should be here - } - assertEquals("bar", lr.get(2).get()); + List> lr = t.execGetResponse(); + try { + lr.get(1).get(); + fail("We expect exception here!"); + } catch (JedisDataException e) { + // that is fine we should be here + } + assertEquals("bar", lr.get(2).get()); } - + @Test public void select() { - jedis.select(1); - jedis.set("foo", "bar"); - jedis.watch("foo"); - Transaction t = jedis.multi(); - t.select(0); - t.set("bar", "foo"); - - Jedis jedis2 = createJedis(); - jedis2.select(1); - jedis2.set("foo", "bar2"); - - List results = t.exec(); - - assertNull(results); + jedis.select(1); + jedis.set("foo", "bar"); + jedis.watch("foo"); + Transaction t = jedis.multi(); + t.select(0); + t.set("bar", "foo"); + + Jedis jedis2 = createJedis(); + jedis2.select(1); + jedis2.set("foo", "bar2"); + + List results = t.exec(); + + assertNull(results); } } \ No newline at end of file