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