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