code: private void handleMasterNodeChange() {
      try {
        synchronized (hasActiveServer) {
          if (ZooKeeperUtil.watchAndCheckExists(this, masterZnode)) {
            // A master node exists, there is an active master
            if (LOG.isDebugEnabled()) {
              LOG.debug("A master is now available");
            }
            hasActiveServer.set(true);
          } else {
            // Node is no longer there, cluster does not have an active master
            if (LOG.isDebugEnabled()) {
              LOG.debug("No master available. Notifying waiting threads");
            }
            hasActiveServer.set(false);
            // Notify any thread waiting to become the active master
            hasActiveServer.notifyAll();
          }
        }
      } catch (KeeperException ke) {
        LOG.error("Received an unexpected KeeperException, aborting", ke);
      }
    }
docs: Implement the logic of leader election.