I have the following JedisCluster impl which I want to make thread safe -
public class Cluster {
private List<String> hostPorts;
private Map<String, JedisPool> hostPool =
new ConcurrentHashMap<String, JedisPool>();
public add(String node) {
hostPorts.add(node);
hostPool.add(node, create_pool);
}
public remove(String node) {
hostPorts.remove(node);
JedisPool pool = hostPool.remove(node)
pool.destroy();
}
public String getMaster(String key) {
return hostPorts.get(some_hash() % hostPool.size());
}
public JedisPool getPool(String node) {
return redisHostPool.get(node);
}
The following are the threads -
- 1 write thread which will update the state when a node is added/removed from a cluster - This happens very rarely
- 1 read thread which will frequently read making call to getMaster() and getPool().
I want to know the best strategy to handle the above scenario using concurrency. I would like to avoid synchronize at method level as the read are very frequent.