Source:
public boolean tryBecomeLeader() { String currentPath = zkUtils.getEphemeralPath(); if (currentPath == null || currentPath.isEmpty()) { currentPath = zkUtils.registerProcessorAndGetId(hostName); } List<String> children = zkUtils.getActiveProcessors(); LOGGER.debug(zLog("Current active processors - " + children)); int index = children.indexOf(ZkKeyBuilder.parseIdFromPath(currentPath)); if (children.size() == 0 || index == -1) { throw new SamzaException("Looks like we are no longer connected to Zk. Need to reconnect!"); } leaderId = ZkKeyBuilder.parseIdFromPath(children.get(0)); if (index == 0) { LOGGER.info(zLog("Eligible to become the leader!")); return true; } LOGGER.info("Index = " + index + " Not eligible to be a leader yet!"); String predecessor = children.get(index - 1); if (!predecessor.equals(currentSubscription)) { if (currentSubscription != null) { LOGGER.debug(zLog("Unsubscribing data change for " + currentSubscription)); zkUtils.unsubscribeDataChanges(keyBuilder.getProcessorsPath() + "/" + currentSubscription, zkLeaderElectionListener); } currentSubscription = predecessor; LOGGER.info(zLog("Subscribing data change for " + predecessor)); zkUtils.subscribeDataChanges(keyBuilder.getProcessorsPath() + "/" + currentSubscription, zkLeaderElectionListener); } boolean predecessorExists = zkUtils.exists(keyBuilder.getProcessorsPath() + "/" + currentSubscription); if (predecessorExists) { LOGGER.info(zLog("Predecessor still exists. Current subscription is valid. Continuing as non-leader.")); } else { try { Thread.sleep(random.nextInt(1000)); } catch (InterruptedException e) { Thread.interrupted(); } LOGGER.info(zLog("Predecessor doesn't exist anymore. Trying to become leader again...")); return tryBecomeLeader(); } return false; }

Target:
public boolean tryBecomeLeader() { String currentPath = zkUtils.registerProcessorAndGetId(hostName); List<String> children = zkUtils.getSortedActiveProcessors(); LOGGER.debug(zLog("Current active processors - " + children)); int index = children.indexOf(ZkKeyBuilder.parseIdFromPath(currentPath)); if (children.size() == 0 || index == -1) { throw new SamzaException("Looks like we are no longer connected to Zk. Need to reconnect!"); } if (index == 0) { isLeader.getAndSet(true); LOGGER.info(zLog("Eligible to become the leader!")); return true; } isLeader.getAndSet(false); LOGGER.info("Index = " + index + " Not eligible to be a leader yet!"); String predecessor = children.get(index - 1); if (!predecessor.equals(currentSubscription)) { if (currentSubscription != null) { LOGGER.debug(zLog("Unsubscribing data change for " + currentSubscription)); zkUtils.unsubscribeDataChanges(keyBuilder.getProcessorsPath() + "/" + currentSubscription, zkLeaderElectionListener); } currentSubscription = predecessor; LOGGER.info(zLog("Subscribing data change for " + predecessor)); zkUtils.subscribeDataChanges(keyBuilder.getProcessorsPath() + "/" + currentSubscription, zkLeaderElectionListener); } boolean predecessorExists = zkUtils.exists(keyBuilder.getProcessorsPath() + "/" + currentSubscription); if (predecessorExists) { LOGGER.info(zLog("Predecessor still exists. Current subscription is valid. Continuing as non-leader.")); } else { try { Thread.sleep(random.nextInt(1000)); } catch (InterruptedException e) { Thread.interrupted(); } LOGGER.info(zLog("Predecessor doesn't exist anymore. Trying to become leader again...")); return tryBecomeLeader(); } return false; }