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