Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow leadership election with apache zookeeper+curator

I'me playing with leader election using LeaderLatch. With ZooKeeper installed locally I have ~30secs to elect leader when there is just one instance and nearly same time to elect new leader when leaders goes down (when I terminate the process). Does this supposed to work like this and can I speed this up?

I use following code:

    CuratorFramework curator = CuratorFrameworkFactory.newClient("127.0.0.1", new ExponentialBackoffRetry(100, 3));
    curator.start();
    LeaderLatch leaderLatch = new LeaderLatch(curator, "/test/t");
    leaderLatch.addListener(new LeaderLatchListener() {
        @Override
        public void isLeader() {
            System.out.println("Leader");
        }

        @Override
        public void notLeader() {
        }
    });
    leaderLatch.start();
like image 962
Daniil Avatar asked Aug 11 '14 17:08

Daniil


1 Answers

I figured this out: ZooKeeper has 30sec timeout for sessions during which it does not delete ephemeral nodes. This is why new leader was not elected (because leader node was not elected). Also this prevents from electing leader when all nodes are shutted down and started again before last leader timeout finishes.

In order to avoid this you need to manually close curator with close method, in which case session is terminated instantaneously.

like image 131
Daniil Avatar answered Oct 03 '22 06:10

Daniil