Add jedis pool caching.

This commit seeks to reduce the number of idle connections to redis that
our code uses by making clients share thread-safe connection pools
(which is the point of a connection pool). This only changes utility
methods to generate and access jedis pools, and does not seek to address
any issues relating to the use of the connections that may or may not be
causing problems.

The changes are as follows:
1. Add a static cache of all connection pools - Each connection pool is
distinguished by its ip and port. Two requested connections to the same
ip:port combinations will use the same connection pool.
2. Increase the max size of each pool to 20 - Overall, by having fewer
idle connections, this commit should still end up with fewer connections
going at any given time.
3. Make explicit setting to block while waiting for a connection - This
should already be the default, but it is made explicit just in case.
This commit is contained in:
Conrad 2015-07-27 08:47:40 -04:00
parent 727427165f
commit 421eb12228

View File

@ -1,5 +1,7 @@
package mineplex.serverdata; package mineplex.serverdata;
import java.util.concurrent.ConcurrentHashMap;
import mineplex.serverdata.servers.ConnectionData; import mineplex.serverdata.servers.ConnectionData;
import mineplex.serverdata.servers.ServerManager; import mineplex.serverdata.servers.ServerManager;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
@ -22,6 +24,9 @@ public class Utility
private static Gson _gson = new GsonBuilder().create(); private static Gson _gson = new GsonBuilder().create();
public static Gson getGson() { return _gson; } public static Gson getGson() { return _gson; }
// map of all instantiated connection pools, distinguished by their ip:port combination
private static final ConcurrentHashMap<String, JedisPool> _pools = new ConcurrentHashMap<String, JedisPool>();
// Public static jedis pool for interacting with central default jedis repo. // Public static jedis pool for interacting with central default jedis repo.
private static JedisPool _masterPool; private static JedisPool _masterPool;
private static JedisPool _slavePool; private static JedisPool _slavePool;
@ -130,12 +135,24 @@ public class Utility
* @return a newly instantiated {@link JedisPool} connected to the provided {@link ConnectionData} repository. * @return a newly instantiated {@link JedisPool} connected to the provided {@link ConnectionData} repository.
*/ */
public static JedisPool generatePool(ConnectionData connData) public static JedisPool generatePool(ConnectionData connData)
{
String key = getConnKey(connData);
JedisPool pool = _pools.get(key);
if (pool == null)
{ {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxWaitMillis(1000); jedisPoolConfig.setMaxWaitMillis(1000);
jedisPoolConfig.setMinIdle(5); jedisPoolConfig.setMinIdle(5);
jedisPoolConfig.setTestOnBorrow(true); jedisPoolConfig.setTestOnBorrow(true);
return new JedisPool(jedisPoolConfig, connData.getHost(), connData.getPort());
jedisPoolConfig.setMaxTotal(20);
jedisPoolConfig.setBlockWhenExhausted(true);
pool = new JedisPool(jedisPoolConfig, connData.getHost(), connData.getPort());
_pools.put(key, pool);
}
return pool;
} }
/** /**
@ -165,4 +182,10 @@ public class Utility
return _slavePool; return _slavePool;
} }
} }
private static String getConnKey(ConnectionData connData)
{
return connData.getHost() + ":" + connData.getPort();
}
} }