Daniil
Daniil

Reputation: 5790

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();

Upvotes: 7

Views: 2290

Answers (2)

Sandeep Das
Sandeep Das

Reputation: 1050

You can decrease "ticktime" parameter in zookeeper configuration file .and restart all servers.

Upvotes: 1

Daniil
Daniil

Reputation: 5790

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.

Upvotes: 7

Related Questions