redis cluster发布后我们项目中使用了cluster,使用驱动是jedis,但是在压力测试过程中发现有一定数量的redis访问非常缓慢高达几十秒数分钟,经过分析jedis驱动JedisClusterInfoCache中加锁造成
private Map<String, JedisPool> nodes = new HashMap<String, JedisPool>();
private Map<Integer, JedisPool> slots = new HashMap<Integer, JedisPool>();
public JedisPool getNode(String nodeKey) {
r.lock();
try {
return nodes.get(nodeKey);
} finally {
r.unlock();
}
}
public JedisPool getSlotPool(int slot) {
r.lock();
try {
return slots.get(slot);
} finally {
r.unlock();
}
}
将map替换成ConcurrentHashMap
private Map<String, JedisPool> nodes = new ConcurrentHashMap<String, JedisPool>();
private Map<Integer, JedisPool> slots = new ConcurrentHashMap<Integer, JedisPool>();
去除锁
public JedisPool getNode(String nodeKey) {
return nodes.get(nodeKey);
}
public JedisPool getSlotPool(int slot) {
return slots.get(slot);
}
测试后比较处理比较平稳,未出现某一个访问耗时非常长得的情况。