zookeeper check if a particular server is leader
apache-zookeeper
Solution
You can find this information from different locations. The easiest way is to run from each Zookeeper machine the script `./bin/zkServer.sh status`. You will get an answer of the following form:
ZooKeeper JMX enabled by default Using config: /path/to/conf/zoo.cfg Mode: follower
For the leader node the last line will show that accordingly.
If you want to achieve this from Java code where you have imported the ZK client library you can use the class `FourLetterWordMain` like this:
String zkAnswer = FourLetterWordMain.send4LetterWord ("<zk-address>", <zk-port>, "srvr");
The answer String will look as follows:
Zookeeper version: 3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT Latency min/avg/max: 0/0/6 Received: 18893 Sent: 18957 Connections: 2 Outstanding: 0 Zxid: 0x280000016d Mode: leader Node count: 337
This code is basically also used by the `zkServer.sh` script.
Problem
Using the Zookeeper API, is it possible to know if the current server is the leader of the Zookeeper cluster? The docs mention The leader has seen the highest zxid of all the followers.. Is it possible to check this somehow?