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