|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package com.sts.allprogtutorials.zk.leaderelection.nodes; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.apache.log4j.Logger; |
| 11 | +import org.apache.zookeeper.WatchedEvent; |
| 12 | +import org.apache.zookeeper.Watcher; |
| 13 | +import org.apache.zookeeper.Watcher.Event.EventType; |
| 14 | + |
| 15 | +import com.sts.allprogtutorials.zk.utils.ZooKeeperService; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Sain Technology Solutions |
| 19 | + * |
| 20 | + */ |
| 21 | +public class ProcessNode implements Runnable{ |
| 22 | + |
| 23 | + private static final Logger LOG = Logger.getLogger(ProcessNode.class); |
| 24 | + |
| 25 | + private static final String LEADER_ELECTION_ROOT_NODE = "/election"; |
| 26 | + private static final String PROCESS_NODE_PREFIX = "/p_"; |
| 27 | + |
| 28 | + private final int id; |
| 29 | + private final ZooKeeperService zooKeeperService; |
| 30 | + |
| 31 | + private String processNodePath; |
| 32 | + private String watchedNodePath; |
| 33 | + |
| 34 | + |
| 35 | + public ProcessNode(final int id, final String zkURL) throws IOException { |
| 36 | + this.id = id; |
| 37 | + zooKeeperService = new ZooKeeperService(zkURL, new ProcessNodeWatcher()); |
| 38 | + } |
| 39 | + |
| 40 | + private void attemptForLeaderPosition() { |
| 41 | + |
| 42 | + final List<String> childNodePaths = zooKeeperService.getChildren(LEADER_ELECTION_ROOT_NODE, false); |
| 43 | + |
| 44 | + Collections.sort(childNodePaths); |
| 45 | + |
| 46 | + int index = childNodePaths.indexOf(processNodePath.substring(processNodePath.lastIndexOf('/') + 1)); |
| 47 | + if(index == 0) { |
| 48 | + if(LOG.isInfoEnabled()) { |
| 49 | + LOG.info("[Process: " + id + "] I am the new leader!"); |
| 50 | + } |
| 51 | + } else { |
| 52 | + final String watchedNodeShortPath = childNodePaths.get(index - 1); |
| 53 | + |
| 54 | + watchedNodePath = LEADER_ELECTION_ROOT_NODE + "/" + watchedNodeShortPath; |
| 55 | + |
| 56 | + if(LOG.isInfoEnabled()) { |
| 57 | + LOG.info("[Process: " + id + "] - Setting watch on node with path: " + watchedNodePath); |
| 58 | + } |
| 59 | + zooKeeperService.watchNode(watchedNodePath, true); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void run() { |
| 65 | + |
| 66 | + System.out.println("Process with id: " + id + " has started!"); |
| 67 | + |
| 68 | + final String rootNodePath = zooKeeperService.createNode(LEADER_ELECTION_ROOT_NODE, false, false); |
| 69 | + if(rootNodePath == null) { |
| 70 | + throw new IllegalStateException("Unable to create/access leader election root node with path: " + LEADER_ELECTION_ROOT_NODE); |
| 71 | + } |
| 72 | + |
| 73 | + processNodePath = zooKeeperService.createNode(rootNodePath + PROCESS_NODE_PREFIX, false, true); |
| 74 | + if(processNodePath == null) { |
| 75 | + throw new IllegalStateException("Unable to create/access process node with path: " + LEADER_ELECTION_ROOT_NODE); |
| 76 | + } |
| 77 | + |
| 78 | + if(LOG.isDebugEnabled()) { |
| 79 | + LOG.debug("[Process: " + id + "] Process node created with path: " + processNodePath); |
| 80 | + } |
| 81 | + |
| 82 | + attemptForLeaderPosition(); |
| 83 | + } |
| 84 | + |
| 85 | + public class ProcessNodeWatcher implements Watcher{ |
| 86 | + |
| 87 | + @Override |
| 88 | + public void process(WatchedEvent event) { |
| 89 | + if(LOG.isDebugEnabled()) { |
| 90 | + LOG.debug("[Process: " + id + "] Event received: " + event); |
| 91 | + } |
| 92 | + |
| 93 | + final EventType eventType = event.getType(); |
| 94 | + if(EventType.NodeDeleted.equals(eventType)) { |
| 95 | + if(event.getPath().equalsIgnoreCase(watchedNodePath)) { |
| 96 | + attemptForLeaderPosition(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments