'JedisCluster configurations and how it maintains the pool of connections

I have recently started using JedisCluster for my application. There is little to no documentation and examples for the same. I tested a use case and the results are not what I expected

public class test {
private static JedisCluster setConnection(HashSet<HostAndPort> IP) {
    JedisCluster jediscluster = new JedisCluster(IP, 30000, 3,
            new GenericObjectPoolConfig() {{
                setMaxTotal(500);
                setMinIdle(1);
                setMaxIdle(500);
                setBlockWhenExhausted(true);
                setMaxWaitMillis(30000);
            }});
    return jediscluster;
}

public static int getIdleconn(Map<String, JedisPool> nodes){
    int i = 0;
    for (String k : nodes.keySet()) {
        i+=nodes.get(k).getNumIdle();
    }
    return i;
}

public static void main(String[] args) {

    HashSet IP = new HashSet<HostAndPort>() {
        {
            add(new HostAndPort("host1", port1));
            add(new HostAndPort("host2", port2));
        }};

    JedisCluster cluster = setConnection(IP);

    System.out.println(getIdleconn(cluster.getClusterNodes()));
            cluster.set("Dummy", "0");
            cluster.set("Dummy1", "0");
            cluster.set("Dummy3", "0");
    System.out.println(getIdleconn(cluster.getClusterNodes()));

    try {
        Thread.sleep(60000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println(getIdleconn(cluster.getClusterNodes()));
}

}

The output for this snippet is:

0
3
3

Questions=>

  1. I have set the timeout to 30000 JedisCluster(IP, 30000, 3,new GenericObjectPoolConfig() . I believe this is the connection timeout which means Idle connections are closed after 30 seconds. Although this doesn't seem to be happening. After sleeping for 60 seconds, the number of idle connections is still 3. What I am doing/understanding wrong here? I want the pool to close the connection if not used for more than 30 seconds.

  2. setMinIdle(1). Does this mean that regardless the connection timeout, the pool will always maintain one connection?

  3. I prefer availability more than throughput for my app. What should be the value for setMaxWaitMillis if conn timeout is 30 secs?

  4. Though rare, the app fails with redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster. This i think is connected to 1. How to prevent this?



Solution 1:[1]

  1. 30000 or 30 seconds here refers to (socket) timeout; the timeout for single socket (read) operation. It is not related with closing idle connections.

    Closing idle connections are controlled by GenericObjectPoolConfig. So check the parameters there.

  2. Yes (mostly).

  3. setMaxWaitMillis is the timeout for getting a connection object from a connection object pool. It is not related to 30 secs and not really solve you anything in terms of availability.

  4. Keep your cluster nodes available.

    There has been changes in Jedis related to this. You can try a recent version (4.x, even better 4.2.x).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sazzad