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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With