HDFS NameNode and YARN ResourceManager both use ZooKeeper to perform active/standby failover. The central class is ZKFailoverController.java; the election itself is driven by ActiveStandbyElector.java.
Active/standby election
ActiveStandbyElector is the core of the active/standby election. During initialization, it must open a ZooKeeper connection and attempt to create a node in ZooKeeper. Callback events are involved both when the connection is created and when the node is created.
Callback handling has three main entry points.
Node-creation callback
The entry method is:
public synchronized void processResult(int rc, String path, Object ctx,
String name) {
// .....
}
The processing flow is shown below.

Watch callback
The entry method is:
public synchronized void processResult(int rc, String path, Object ctx,
Stat stat) {
// ...
}
The processing flow is shown below.

Event callback
The entry method is:
synchronized void processWatchEvent(ZooKeeper zk, WatchedEvent event) {
// ..
}
The processing flow is shown below.
