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