Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 1 | package net.onrc.onos.registry.controller; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 2 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 3 | import java.io.IOException; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 6 | import java.util.Collections; |
Jonathan Hart | 599c6b3 | 2013-03-24 22:42:02 -0700 | [diff] [blame] | 7 | import java.util.Comparator; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 8 | import java.util.HashMap; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 9 | import java.util.List; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 10 | import java.util.Map; |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 11 | import java.util.concurrent.ConcurrentHashMap; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 12 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 13 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 14 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 17 | import net.floodlightcontroller.restserver.IRestApiService; |
Naoki Shiota | b32edf5 | 2013-12-12 14:09:36 -0800 | [diff] [blame] | 18 | import net.onrc.onos.registry.controller.web.RegistryWebRoutable; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 19 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 20 | import org.openflow.util.HexString; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 23 | |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 24 | import com.google.common.base.Charsets; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 25 | import com.netflix.curator.RetryPolicy; |
| 26 | import com.netflix.curator.framework.CuratorFramework; |
| 27 | import com.netflix.curator.framework.CuratorFrameworkFactory; |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 28 | import com.netflix.curator.framework.recipes.atomic.AtomicValue; |
| 29 | import com.netflix.curator.framework.recipes.atomic.DistributedAtomicLong; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 30 | import com.netflix.curator.framework.recipes.cache.ChildData; |
| 31 | import com.netflix.curator.framework.recipes.cache.PathChildrenCache; |
| 32 | import com.netflix.curator.framework.recipes.cache.PathChildrenCache.StartMode; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 33 | import com.netflix.curator.framework.recipes.cache.PathChildrenCacheEvent; |
| 34 | import com.netflix.curator.framework.recipes.cache.PathChildrenCacheListener; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 35 | import com.netflix.curator.framework.recipes.leader.LeaderLatch; |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 36 | import com.netflix.curator.framework.recipes.leader.LeaderLatchEvent; |
| 37 | import com.netflix.curator.framework.recipes.leader.LeaderLatchListener; |
Pavlin Radoslavov | f1377ce | 2014-02-05 17:37:24 -0800 | [diff] [blame] | 38 | import com.netflix.curator.framework.recipes.leader.Participant; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 39 | import com.netflix.curator.retry.ExponentialBackoffRetry; |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 40 | import com.netflix.curator.retry.RetryOneTime; |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 41 | import com.netflix.curator.x.discovery.ServiceCache; |
| 42 | import com.netflix.curator.x.discovery.ServiceDiscovery; |
| 43 | import com.netflix.curator.x.discovery.ServiceDiscoveryBuilder; |
| 44 | import com.netflix.curator.x.discovery.ServiceInstance; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 45 | import java.util.concurrent.ArrayBlockingQueue; |
| 46 | import java.util.concurrent.BlockingQueue; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 47 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 48 | /** |
| 49 | * A registry service that uses Zookeeper. All data is stored in Zookeeper, |
| 50 | * so this can be used as a global registry in a multi-node ONOS cluster. |
| 51 | * @author jono |
| 52 | * |
| 53 | */ |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 54 | public class ZookeeperRegistry implements IFloodlightModule, IControllerRegistryService { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 55 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 56 | protected final static Logger log = LoggerFactory.getLogger(ZookeeperRegistry.class); |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 57 | protected String controllerId = null; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 58 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 59 | protected IRestApiService restApi; |
| 60 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 61 | //This is the default, it's overwritten by the connectionString configuration parameter |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 62 | protected String connectionString = "localhost:2181"; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 63 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 64 | private final String namespace = "onos"; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 65 | private final String switchLatchesPath = "/switches"; |
Pavlin Radoslavov | f1377ce | 2014-02-05 17:37:24 -0800 | [diff] [blame] | 66 | private final String CLUSTER_LEADER_PATH = "/cluster/leader"; |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 67 | |
| 68 | private final String SERVICES_PATH = "/"; //i.e. the root of our namespace |
| 69 | private final String CONTROLLER_SERVICE_NAME = "controllers"; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 70 | |
| 71 | protected CuratorFramework client; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 72 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 73 | protected PathChildrenCache switchCache; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 74 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 75 | protected ConcurrentHashMap<String, SwitchLeadershipData> switches; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 76 | protected Map<String, PathChildrenCache> switchPathCaches; |
Pavlin Radoslavov | f1377ce | 2014-02-05 17:37:24 -0800 | [diff] [blame] | 77 | |
| 78 | protected LeaderLatch clusterLeaderLatch; |
| 79 | protected ClusterLeaderListener clusterLeaderListener; |
| 80 | private static final long CLUSTER_LEADER_ELECTION_RETRY_MS = 100; |
| 81 | |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 82 | private final String ID_COUNTER_PATH = "/flowidcounter"; |
| 83 | private final Long ID_BLOCK_SIZE = 0x100000000L; |
| 84 | protected DistributedAtomicLong distributedIdCounter; |
| 85 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 86 | //Zookeeper performance-related configuration |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame] | 87 | protected static final int sessionTimeout = 5000; |
| 88 | protected static final int connectionTimeout = 7000; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 89 | private volatile IdBlock idBlock = null; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 90 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 91 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 92 | protected class SwitchLeaderListener implements LeaderLatchListener{ |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 93 | String dpid; |
| 94 | LeaderLatch latch; |
| 95 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 96 | public SwitchLeaderListener(String dpid, LeaderLatch latch){ |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 97 | this.dpid = dpid; |
| 98 | this.latch = latch; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void leaderLatchEvent(CuratorFramework arg0, |
| 103 | LeaderLatchEvent arg1) { |
Pavlin Radoslavov | f1377ce | 2014-02-05 17:37:24 -0800 | [diff] [blame] | 104 | log.debug("Switch leadership changed for {}, now {}", |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 105 | dpid, latch.hasLeadership()); |
| 106 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 107 | //Check that the leadership request is still active - the client |
| 108 | //may have since released the request or even begun another request |
| 109 | //(this is why we use == to check the object instance is the same) |
| 110 | SwitchLeadershipData swData = switches.get(dpid); |
Naoki Shiota | 2999e3d | 2014-01-03 17:22:59 -0800 | [diff] [blame] | 111 | if (swData == null) { |
Naoki Shiota | 1a5ca91 | 2014-01-03 17:02:31 -0800 | [diff] [blame] | 112 | log.debug("Leadership data {} not found", dpid); |
Naoki Shiota | 2999e3d | 2014-01-03 17:22:59 -0800 | [diff] [blame] | 113 | return; |
Naoki Shiota | 1a5ca91 | 2014-01-03 17:02:31 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (swData.getLatch() == latch){ |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 117 | swData.getCallback().controlChanged( |
| 118 | HexString.toLong(dpid), latch.hasLeadership()); |
| 119 | } |
| 120 | else { |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 121 | log.debug("Latch for {} has changed: old latch {} - new latch {}", |
| 122 | new Object[]{dpid, latch, swData.getLatch()}); |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 123 | } |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 127 | protected class SwitchPathCacheListener implements PathChildrenCacheListener { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 128 | @Override |
| 129 | public void childEvent(CuratorFramework client, |
| 130 | PathChildrenCacheEvent event) throws Exception { |
Jonathan Hart | cbb4b95 | 2013-03-18 16:15:18 -0700 | [diff] [blame] | 131 | //log.debug("Root switch path cache got {} event", event.getType()); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 132 | |
| 133 | String strSwitch = null; |
| 134 | if (event.getData() != null){ |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 135 | String[] splitted = event.getData().getPath().split("/"); |
| 136 | strSwitch = splitted[splitted.length - 1]; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | switch (event.getType()){ |
| 140 | case CHILD_ADDED: |
| 141 | case CHILD_UPDATED: |
| 142 | //Check we have a PathChildrenCache for this child, add one if not |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 143 | synchronized (switchPathCaches){ |
| 144 | if (switchPathCaches.get(strSwitch) == null){ |
| 145 | PathChildrenCache pc = new PathChildrenCache(client, |
| 146 | event.getData().getPath(), true); |
| 147 | pc.start(StartMode.NORMAL); |
| 148 | switchPathCaches.put(strSwitch, pc); |
| 149 | } |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 150 | } |
| 151 | break; |
| 152 | case CHILD_REMOVED: |
| 153 | //Remove our PathChildrenCache for this child |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 154 | PathChildrenCache pc = null; |
| 155 | synchronized(switchPathCaches){ |
| 156 | pc = switchPathCaches.remove(strSwitch); |
| 157 | } |
| 158 | if (pc != null){ |
| 159 | pc.close(); |
| 160 | } |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 161 | break; |
| 162 | default: |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 163 | //All other events are connection status events. We don't need to |
| 164 | //do anything as the path cache handles these on its own. |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | }; |
Pavlin Radoslavov | f1377ce | 2014-02-05 17:37:24 -0800 | [diff] [blame] | 170 | |
| 171 | protected class ClusterLeaderListener implements LeaderLatchListener { |
| 172 | LeaderLatch latch; |
| 173 | |
| 174 | public ClusterLeaderListener(LeaderLatch latch) { |
| 175 | this.latch = latch; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void leaderLatchEvent(CuratorFramework arg0, |
| 180 | LeaderLatchEvent arg1) { |
| 181 | log.debug("Cluster leadership changed, now {}", |
| 182 | latch.hasLeadership()); |
| 183 | // |
| 184 | // NOTE: If we need to support callbacks when the |
| 185 | // leadership changes, those should be called here. |
| 186 | // |
| 187 | } |
| 188 | } |
| 189 | |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Listens for changes to the switch znodes in Zookeeper. This maintains |
| 192 | * the second level of PathChildrenCaches that hold the controllers |
| 193 | * contending for each switch - there's one for each switch. |
| 194 | */ |
| 195 | PathChildrenCacheListener switchPathCacheListener = new SwitchPathCacheListener(); |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 196 | protected ServiceDiscovery<ControllerService> serviceDiscovery; |
| 197 | protected ServiceCache<ControllerService> serviceCache; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 198 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 199 | |
| 200 | @Override |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 201 | public void requestControl(long dpid, ControlChangeCallback cb) throws RegistryException { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 202 | log.info("Requesting control for {}", HexString.toHexString(dpid)); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 203 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 204 | if (controllerId == null){ |
| 205 | throw new RuntimeException("Must register a controller before calling requestControl"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | String dpidStr = HexString.toHexString(dpid); |
| 209 | String latchPath = switchLatchesPath + "/" + dpidStr; |
| 210 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 211 | if (switches.get(dpidStr) != null){ |
Jonathan Hart | 3c0eccd | 2013-03-12 22:32:50 -0700 | [diff] [blame] | 212 | log.debug("Already contesting {}, returning", HexString.toHexString(dpid)); |
Pankaj Berde | da7187b | 2013-03-18 15:24:59 -0700 | [diff] [blame] | 213 | throw new RegistryException("Already contesting control for " + dpidStr); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 216 | LeaderLatch latch = new LeaderLatch(client, latchPath, controllerId); |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 217 | latch.addListener(new SwitchLeaderListener(dpidStr, latch)); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 218 | |
Jonathan Hart | 44e56fc | 2013-03-14 16:53:59 -0700 | [diff] [blame] | 219 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 220 | SwitchLeadershipData swData = new SwitchLeadershipData(latch, cb); |
| 221 | SwitchLeadershipData oldData = switches.putIfAbsent(dpidStr, swData); |
| 222 | |
| 223 | if (oldData != null){ |
| 224 | //There was already data for that key in the map |
| 225 | //i.e. someone else got here first so we can't succeed |
| 226 | log.debug("Already requested control for {}", dpidStr); |
| 227 | throw new RegistryException("Already requested control for " + dpidStr); |
| 228 | } |
| 229 | |
| 230 | //Now that we know we were able to add our latch to the collection, |
Jonathan Hart | 44e56fc | 2013-03-14 16:53:59 -0700 | [diff] [blame] | 231 | //we can start the leader election in Zookeeper. However I don't know |
| 232 | //how to handle if the start fails - the latch is already in our |
| 233 | //switches list. |
| 234 | //TODO seems like there's a Curator bug when latch.start is called when |
| 235 | //there's no Zookeeper connection which causes two znodes to be put in |
| 236 | //Zookeeper at the latch path when we reconnect to Zookeeper. |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 237 | try { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 238 | latch.start(); |
| 239 | } catch (Exception e) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 240 | log.warn("Error starting leader latch: {}", e.getMessage()); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 241 | throw new RegistryException("Error starting leader latch for " + dpidStr, e); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | } |
| 245 | |
| 246 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 247 | public void releaseControl(long dpid) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 248 | log.info("Releasing control for {}", HexString.toHexString(dpid)); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 249 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 250 | String dpidStr = HexString.toHexString(dpid); |
| 251 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 252 | SwitchLeadershipData swData = switches.remove(dpidStr); |
| 253 | |
| 254 | if (swData == null) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 255 | log.debug("Trying to release control of a switch we are not contesting"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 256 | return; |
| 257 | } |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 258 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 259 | LeaderLatch latch = swData.getLatch(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 260 | |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 261 | latch.removeAllListeners(); |
| 262 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 263 | try { |
| 264 | latch.close(); |
| 265 | } catch (IOException e) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 266 | //I think it's OK not to do anything here. Either the node got |
| 267 | //deleted correctly, or the connection went down and the node got deleted. |
Umesh Krishnaswamy | 0ef75ee | 2013-03-25 17:50:27 -0700 | [diff] [blame] | 268 | log.debug("releaseControl: caught IOException {}", dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 273 | public boolean hasControl(long dpid) { |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 274 | String dpidStr = HexString.toHexString(dpid); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 275 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 276 | SwitchLeadershipData swData = switches.get(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 277 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 278 | if (swData == null) { |
| 279 | log.warn("No leader latch for dpid {}", dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 283 | return swData.getLatch().hasLeadership(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | @Override |
Pavlin Radoslavov | f1377ce | 2014-02-05 17:37:24 -0800 | [diff] [blame] | 287 | public boolean isClusterLeader() { |
| 288 | return clusterLeaderLatch.hasLeadership(); |
| 289 | } |
| 290 | |
| 291 | @Override |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 292 | public String getControllerId() { |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 293 | return controllerId; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 296 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 297 | public Collection<String> getAllControllers() throws RegistryException { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 298 | log.debug("Getting all controllers"); |
Jonathan Hart | 1be4626 | 2013-02-20 16:43:51 -0800 | [diff] [blame] | 299 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 300 | List<String> controllers = new ArrayList<String>(); |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 301 | for (ServiceInstance<ControllerService> instance : serviceCache.getInstances()){ |
| 302 | String id = instance.getPayload().getControllerId(); |
| 303 | if (!controllers.contains(id)){ |
| 304 | controllers.add(id); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 305 | } |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 306 | } |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 307 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 308 | return controllers; |
| 309 | } |
| 310 | |
| 311 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 312 | public void registerController(String id) throws RegistryException { |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 313 | if (controllerId != null) { |
| 314 | throw new RegistryException( |
| 315 | "Controller already registered with id " + controllerId); |
| 316 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 317 | |
| 318 | controllerId = id; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 319 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 320 | try { |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 321 | ServiceInstance<ControllerService> thisInstance = ServiceInstance.<ControllerService>builder() |
| 322 | .name(CONTROLLER_SERVICE_NAME) |
| 323 | .payload(new ControllerService(controllerId)) |
| 324 | //.port((int)(65535 * Math.random())) // in a real application, you'd use a common port |
| 325 | //.uriSpec(uriSpec) |
| 326 | .build(); |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame] | 327 | |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 328 | serviceDiscovery.registerService(thisInstance); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 329 | } catch (Exception e) { |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 330 | // TODO Auto-generated catch block |
| 331 | e.printStackTrace(); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 332 | } |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 333 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 337 | public String getControllerForSwitch(long dpid) throws RegistryException { |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 338 | String dpidStr = HexString.toHexString(dpid); |
Pankaj Berde | 017960a | 2013-03-14 20:32:26 -0700 | [diff] [blame] | 339 | |
Jonathan Hart | 599c6b3 | 2013-03-24 22:42:02 -0700 | [diff] [blame] | 340 | PathChildrenCache switchCache = switchPathCaches.get(dpidStr); |
| 341 | |
| 342 | if (switchCache == null){ |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 343 | log.warn("Tried to get controller for non-existent switch"); |
| 344 | return null; |
| 345 | } |
| 346 | |
Jonathan Hart | f4e8084 | 2013-03-26 23:55:02 -0700 | [diff] [blame] | 347 | try { |
| 348 | //We've seen issues with these caches get stuck out of date, so we'll have to |
| 349 | //force them to refresh before each read. This slows down the method as it |
| 350 | //blocks on a Zookeeper query, however at the moment only the cleanup thread |
| 351 | //uses this and that isn't particularly time-sensitive. |
| 352 | switchCache.rebuild(); |
| 353 | } catch (Exception e) { |
| 354 | // TODO Auto-generated catch block |
| 355 | e.printStackTrace(); |
| 356 | } |
| 357 | |
Jonathan Hart | 599c6b3 | 2013-03-24 22:42:02 -0700 | [diff] [blame] | 358 | List<ChildData> sortedData = new ArrayList<ChildData>(switchCache.getCurrentData()); |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame] | 359 | |
Jonathan Hart | 599c6b3 | 2013-03-24 22:42:02 -0700 | [diff] [blame] | 360 | Collections.sort( |
| 361 | sortedData, |
| 362 | new Comparator<ChildData>(){ |
| 363 | private String getSequenceNumber(String path){ |
| 364 | return path.substring(path.lastIndexOf('-') + 1); |
| 365 | } |
| 366 | @Override |
| 367 | public int compare(ChildData lhs, ChildData rhs) { |
| 368 | return getSequenceNumber(lhs.getPath()). |
| 369 | compareTo(getSequenceNumber(rhs.getPath())); |
| 370 | } |
| 371 | } |
| 372 | ); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 373 | |
Jonathan Hart | 56b296e | 2013-03-25 13:30:10 -0700 | [diff] [blame] | 374 | if (sortedData.size() == 0){ |
| 375 | return null; |
| 376 | } |
| 377 | |
Jonathan Hart | 599c6b3 | 2013-03-24 22:42:02 -0700 | [diff] [blame] | 378 | return new String(sortedData.get(0).getData(), Charsets.UTF_8); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public Collection<Long> getSwitchesControlledByController(String controllerId) { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 383 | //TODO remove this if not needed |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 384 | throw new RuntimeException("Not yet implemented"); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 385 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 386 | |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 387 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 388 | //TODO what should happen when there's no ZK connection? Currently we just return |
| 389 | //the cache but this may lead to false impressions - i.e. we don't actually know |
| 390 | //what's in ZK so we shouldn't say we do |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 391 | @Override |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 392 | public Map<String, List<ControllerRegistryEntry>> getAllSwitches() { |
| 393 | Map<String, List<ControllerRegistryEntry>> data = |
| 394 | new HashMap<String, List<ControllerRegistryEntry>>(); |
| 395 | |
| 396 | for (Map.Entry<String, PathChildrenCache> entry : switchPathCaches.entrySet()){ |
| 397 | List<ControllerRegistryEntry> contendingControllers = |
| 398 | new ArrayList<ControllerRegistryEntry>(); |
| 399 | |
| 400 | if (entry.getValue().getCurrentData().size() < 1){ |
Jonathan Hart | cbb4b95 | 2013-03-18 16:15:18 -0700 | [diff] [blame] | 401 | //TODO prevent even having the PathChildrenCache in this case |
| 402 | //log.info("Switch entry with no leader elections: {}", entry.getKey()); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 403 | continue; |
| 404 | } |
| 405 | |
| 406 | for (ChildData d : entry.getValue().getCurrentData()) { |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 407 | |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 408 | String controllerId = new String(d.getData(), Charsets.UTF_8); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 409 | |
| 410 | String[] splitted = d.getPath().split("-"); |
| 411 | int sequenceNumber = Integer.parseInt(splitted[splitted.length - 1]); |
| 412 | |
| 413 | contendingControllers.add(new ControllerRegistryEntry(controllerId, sequenceNumber)); |
| 414 | } |
| 415 | |
| 416 | Collections.sort(contendingControllers); |
| 417 | data.put(entry.getKey(), contendingControllers); |
| 418 | } |
| 419 | return data; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 422 | public IdBlock allocateUniqueIdBlock(long range) { |
| 423 | try { |
| 424 | AtomicValue<Long> result = null; |
| 425 | do { |
| 426 | result = distributedIdCounter.add(range); |
| 427 | } while (result == null || !result.succeeded()); |
| 428 | |
| 429 | return new IdBlock(result.preValue(), result.postValue() - 1, range); |
| 430 | } catch (Exception e) { |
| 431 | log.error("Error allocating ID block"); |
| 432 | } |
| 433 | return null; |
| 434 | } |
| 435 | |
Naoki Shiota | a3b2dfa | 2013-06-27 13:52:24 -0700 | [diff] [blame] | 436 | /** |
| 437 | * Returns a block of IDs which are unique and unused. |
| 438 | * Range of IDs is fixed size and is assigned incrementally as this method called. |
| 439 | * Since the range of IDs is managed by Zookeeper in distributed way, this method may block when |
| 440 | * requests come up simultaneously. |
| 441 | */ |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 442 | @Override |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 443 | public IdBlock allocateUniqueIdBlock(){ |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 444 | return allocateUniqueIdBlock(ID_BLOCK_SIZE); |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 445 | } |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 446 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 447 | /* |
| 448 | * IFloodlightModule |
| 449 | */ |
| 450 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 451 | @Override |
| 452 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 453 | Collection<Class<? extends IFloodlightService>> l = |
| 454 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 455 | l.add(IControllerRegistryService.class); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 456 | return l; |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 461 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 462 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 463 | m.put(IControllerRegistryService.class, this); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 464 | return m; |
| 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 469 | Collection<Class<? extends IFloodlightService>> l = |
| 470 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 471 | l.add(IRestApiService.class); |
| 472 | return l; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 475 | //TODO currently blocks startup when it can't get a Zookeeper connection. |
| 476 | //Do we support starting up with no Zookeeper connection? |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 477 | @Override |
| 478 | public void init (FloodlightModuleContext context) throws FloodlightModuleException { |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 479 | log.info("Initialising the Zookeeper Registry - Zookeeper connection required"); |
| 480 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 481 | //Read the Zookeeper connection string from the config |
| 482 | Map<String, String> configParams = context.getConfigParams(this); |
| 483 | String connectionString = configParams.get("connectionString"); |
| 484 | if (connectionString != null){ |
| 485 | this.connectionString = connectionString; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 486 | } |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 487 | log.info("Setting Zookeeper connection string to {}", this.connectionString); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 488 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 489 | restApi = context.getServiceImpl(IRestApiService.class); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 490 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 491 | switches = new ConcurrentHashMap<String, SwitchLeadershipData>(); |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 492 | //switchPathCaches = new HashMap<String, PathChildrenCache>(); |
| 493 | switchPathCaches = new ConcurrentHashMap<String, PathChildrenCache>(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 494 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 495 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 496 | client = CuratorFrameworkFactory.newClient(this.connectionString, |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 497 | sessionTimeout, connectionTimeout, retryPolicy); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 498 | |
| 499 | client.start(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 500 | client = client.usingNamespace(namespace); |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 501 | |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 502 | distributedIdCounter = new DistributedAtomicLong( |
| 503 | client, |
| 504 | ID_COUNTER_PATH, |
| 505 | new RetryOneTime(100)); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 506 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 507 | switchCache = new PathChildrenCache(client, switchLatchesPath, true); |
| 508 | switchCache.getListenable().addListener(switchPathCacheListener); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 509 | |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 510 | //Build the service discovery object |
| 511 | serviceDiscovery = ServiceDiscoveryBuilder.builder(ControllerService.class) |
| 512 | .client(client).basePath(SERVICES_PATH).build(); |
| 513 | |
| 514 | //We read the list of services very frequently (GUI periodically queries them) |
| 515 | //so we'll cache them to cut down on Zookeeper queries. |
| 516 | serviceCache = serviceDiscovery.serviceCacheBuilder() |
| 517 | .name(CONTROLLER_SERVICE_NAME).build(); |
| 518 | |
| 519 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 520 | try { |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 521 | serviceDiscovery.start(); |
| 522 | serviceCache.start(); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 523 | |
| 524 | //Don't prime the cache, we want a notification for each child node in the path |
| 525 | switchCache.start(StartMode.NORMAL); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 526 | } catch (Exception e) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 527 | throw new FloodlightModuleException("Error initialising ZookeeperRegistry: " |
| 528 | + e.getMessage()); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 529 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | @Override |
| 533 | public void startUp (FloodlightModuleContext context) { |
Pavlin Radoslavov | f1377ce | 2014-02-05 17:37:24 -0800 | [diff] [blame] | 534 | // |
| 535 | // Cluster Leader election setup. |
| 536 | // NOTE: We have to do it here, because during the init stage |
| 537 | // we don't know the Controller ID. |
| 538 | // |
| 539 | if (controllerId == null) { |
| 540 | log.error("Error on startup: unknown ControllerId"); |
| 541 | } |
| 542 | clusterLeaderLatch = new LeaderLatch(client, |
| 543 | CLUSTER_LEADER_PATH, |
| 544 | controllerId); |
| 545 | clusterLeaderListener = new ClusterLeaderListener(clusterLeaderLatch); |
| 546 | clusterLeaderLatch.addListener(clusterLeaderListener); |
| 547 | try { |
| 548 | clusterLeaderLatch.start(); |
| 549 | } catch (Exception e) { |
| 550 | log.error("Error on startup starting the cluster leader election: {}", e.getMessage()); |
| 551 | } |
| 552 | |
| 553 | // Keep trying until there is a cluster leader |
| 554 | do { |
| 555 | try { |
| 556 | Participant leader = clusterLeaderLatch.getLeader(); |
| 557 | if (! leader.getId().isEmpty()) |
| 558 | break; |
| 559 | Thread.sleep(CLUSTER_LEADER_ELECTION_RETRY_MS); |
| 560 | } catch (Exception e) { |
| 561 | log.error("Error on startup waiting for cluster leader election: {}", e.getMessage()); |
| 562 | } |
| 563 | } while (true); |
| 564 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 565 | restApi.addRestletRoutable(new RegistryWebRoutable()); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 566 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 567 | } |