Gioachino Bartolotta
Gioachino Bartolotta

Reputation: 123

Redisson + redis sentinel. how to handle failover and write into redis?

I have just edited my previous question, and I am providing more details, (hopefully someone would be able to help).

I have a Redis cluster with 1 master and 2 slaves. All 3 nodes are managed by Sentinel. The failover works fine and when the new master is elected, I can write on the new master (from the command line). Now, I am trying to write a small Java program using Redisson, which ideally should write records into redis, and be able to handle the failover (which it should do as far as I have understood). This is my code until now.

import org.redisson.Redisson;
import org.redisson.RedissonNode;
import org.redisson.api.*;
import org.redisson.api.annotation.RInject;
import org.redisson.config.Config;
import org.redisson.config.RedissonNodeConfig;
import org.redisson.config.SubscriptionMode;
import java.util.Collections;
import java.util.UUID;


public class RedissonTest {

    public static class RunnableTask implements Runnable {
        @RInject
        RedissonClient client;

        @Override
        public void run(){
            System.out.println("I am in ..");
            RMap<String, String> map = client.getMap("completeNewMap");
            System.out.println("is thread interrupted?? " + Thread.currentThread().isInterrupted());

            NodesGroup ngroup = client.getNodesGroup();
            Collection<Node> nodes = ngroup.getNodes();
            for(Node node : nodes){
                System.out.println("Node ip "+ node.getAddr().toString()+" type: "+node.getType().toString());
            }

            for(int i=0; i < 10000; i++) {
                String key = "bg_key_"+String.valueOf(i);
                String value = String.valueOf(UUID.randomUUID());
                String oldVal = map.get(key);
                map.put(key, value);

                RBucket<String> bck = client.getBucket(key);

                bck.set(value);

                System.out.println("I am going to replace the old value " + oldVal + " with new value " + value + " at key "+key);

            }
            System.out.println("I am outta here!!");
        }
    }

    public static void main(String[] args) {
        Config config = new Config();
        config.useSentinelServers()
                .setMasterName("redis-cluster")
                .addSentinelAddress("192.168.56.101:26379")
                .addSentinelAddress("192.168.56.102:26379")
                .addSentinelAddress("192.168.56.103:26379")
                .setPingTimeout(100)
                .setTimeout(60000)
                .setRetryAttempts(25)
                .setReconnectionTimeout(45000)
                .setRetryInterval(1500)
                .setReadMode(ReadMode.SLAVE)
                .setConnectTimeout(20000)
                .setSubscriptionMode(SubscriptionMode.MASTER);

        RedissonClient client = Redisson.create(config);

        RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
        nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("myExecutor6", 1));
        RedissonNode node = RedissonNode.create(nodeConfig);
        node.start();

        System.out.println("Node address "+node.getRemoteAddress().toString());
        RExecutorService e = client.getExecutorService("myExecutor6");
        e.execute(new RunnableTask());
        e.shutdown();
        if(e.isShutdown()) {
            e.delete();
        }
        client.shutdown();
        node.shutdown();
        System.out.println("Hello World!" );
    }

}

Running the code, a couple of things that I don't understand happen. The first one is:

The idea is that after I have been able to write into redis, I would start to test the failover killing the master and expecting that the program will manage it and continues to write to the new master, without losing a message(it would be nice to be able to cache the messages while the failover occurs).

What happen with this simple program is that I can write into redis, but when I kill the master, the execution just hangs for a time that seems to be close to the setTimeout and exits without completing the task.

Any suggestion?

Upvotes: 1

Views: 3614

Answers (1)

Nikita Koksharov
Nikita Koksharov

Reputation: 10803

You should set retryAttempts parameter big enough to make Redisson survive failover period.

Upvotes: 0

Related Questions