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