Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.mastership; |
| 2 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 3 | import java.io.IOException; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 4 | import java.io.UnsupportedEncodingException; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 5 | import java.net.UnknownHostException; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 6 | import java.util.ArrayList; |
| 7 | import java.util.Collection; |
| 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; |
| 11 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 13 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 14 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightService; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 16 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 17 | import org.apache.zookeeper.CreateMode; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 18 | import org.apache.zookeeper.WatchedEvent; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 19 | import org.apache.zookeeper.Watcher.Event.KeeperState; |
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 | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 24 | import com.netflix.curator.RetryPolicy; |
| 25 | import com.netflix.curator.framework.CuratorFramework; |
| 26 | import com.netflix.curator.framework.CuratorFrameworkFactory; |
| 27 | import com.netflix.curator.framework.api.CuratorWatcher; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 28 | import com.netflix.curator.framework.recipes.cache.ChildData; |
| 29 | import com.netflix.curator.framework.recipes.cache.PathChildrenCache; |
| 30 | import com.netflix.curator.framework.recipes.cache.PathChildrenCache.StartMode; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 31 | import com.netflix.curator.framework.recipes.leader.LeaderLatch; |
| 32 | import com.netflix.curator.framework.recipes.leader.Participant; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 33 | import com.netflix.curator.retry.ExponentialBackoffRetry; |
| 34 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 35 | public class MastershipManager implements IFloodlightModule, IMastershipService { |
| 36 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 37 | protected static Logger log = LoggerFactory.getLogger(MastershipManager.class); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 38 | protected String mastershipId = null; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 39 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 40 | //TODO read this from configuration |
| 41 | protected String connectionString = "localhost:2181"; |
| 42 | private final String namespace = "onos"; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 43 | private final String switchLatchesPath = "/switches"; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 44 | |
| 45 | protected CuratorFramework client; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 46 | |
| 47 | private final String controllerPath = "/controllers"; |
| 48 | protected PathChildrenCache controllerCache; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 49 | |
| 50 | protected Map<String, LeaderLatch> switchLatches; |
| 51 | protected Map<String, MastershipCallback> switchCallbacks; |
| 52 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 53 | protected class ParamaterizedCuratorWatcher implements CuratorWatcher { |
| 54 | private String dpid; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 55 | private boolean isLeader = false; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 56 | private String latchPath; |
| 57 | |
| 58 | public ParamaterizedCuratorWatcher(String dpid, String latchPath){ |
| 59 | this.dpid = dpid; |
| 60 | this.latchPath = latchPath; |
| 61 | } |
| 62 | |
| 63 | @Override |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 64 | public synchronized void process(WatchedEvent event) throws Exception { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 65 | log.debug("Watch Event: {}", event); |
| 66 | |
| 67 | LeaderLatch latch = switchLatches.get(dpid); |
| 68 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 69 | if (event.getState() == KeeperState.Disconnected){ |
| 70 | if (isLeader) { |
| 71 | log.debug("Disconnected while leader - lost leadership for {}", dpid); |
| 72 | |
| 73 | isLeader = false; |
| 74 | switchCallbacks.get(dpid).changeCallback(HexString.toLong(dpid), false); |
| 75 | } |
| 76 | return; |
| 77 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 78 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 79 | try { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 80 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 81 | Participant leader = latch.getLeader(); |
| 82 | |
| 83 | if (leader.getId().equals(mastershipId) && !isLeader){ |
| 84 | log.debug("Became leader for {}", dpid); |
| 85 | |
| 86 | isLeader = true; |
| 87 | switchCallbacks.get(dpid).changeCallback(HexString.toLong(dpid), true); |
| 88 | } |
| 89 | else if (!leader.getId().equals(mastershipId) && isLeader){ |
| 90 | log.debug("Lost leadership for {}", dpid); |
| 91 | |
| 92 | isLeader = false; |
| 93 | switchCallbacks.get(dpid).changeCallback(HexString.toLong(dpid), false); |
| 94 | } |
| 95 | } catch (Exception e){ |
| 96 | if (isLeader){ |
| 97 | log.debug("Exception checking leadership status. Assume leadship lost for {}", |
| 98 | dpid); |
| 99 | |
| 100 | isLeader = false; |
| 101 | switchCallbacks.get(dpid).changeCallback(HexString.toLong(dpid), false); |
| 102 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | client.getChildren().usingWatcher(this).inBackground().forPath(latchPath); |
| 106 | //client.getChildren().usingWatcher(this).forPath(latchPath); |
| 107 | } |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 108 | } |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 109 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 110 | |
| 111 | @Override |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 112 | public void acquireMastership(long dpid, MastershipCallback cb) throws Exception { |
| 113 | |
| 114 | if (mastershipId == null){ |
| 115 | throw new RuntimeException("Must set mastershipId before calling aquireMastership"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | String dpidStr = HexString.toHexString(dpid); |
| 119 | String latchPath = switchLatchesPath + "/" + dpidStr; |
| 120 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 121 | if (switchLatches.get(dpidStr) != null){ |
| 122 | throw new RuntimeException("Leader election for switch " + dpidStr + |
| 123 | "is already running"); |
| 124 | } |
| 125 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 126 | LeaderLatch latch = new LeaderLatch(client, latchPath, mastershipId); |
| 127 | switchLatches.put(dpidStr, latch); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 128 | switchCallbacks.put(dpidStr, cb); |
| 129 | |
| 130 | try { |
| 131 | //client.getChildren().usingWatcher(watcher).inBackground().forPath(singleLatchPath); |
| 132 | client.getChildren().usingWatcher( |
| 133 | new ParamaterizedCuratorWatcher(dpidStr, latchPath)) |
| 134 | .inBackground().forPath(latchPath); |
| 135 | latch.start(); |
| 136 | } catch (Exception e) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 137 | log.warn("Error starting leader latch: {}", e.getMessage()); |
| 138 | throw e; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void releaseMastership(long dpid) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 145 | String dpidStr = HexString.toHexString(dpid); |
| 146 | |
| 147 | LeaderLatch latch = switchLatches.get(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 148 | if (latch == null) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 149 | log.debug("Trying to release mastership for switch we are not contesting"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
| 153 | try { |
| 154 | latch.close(); |
| 155 | } catch (IOException e) { |
| 156 | |
| 157 | } |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 158 | |
| 159 | switchLatches.remove(dpidStr); |
| 160 | switchCallbacks.remove(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public boolean amMaster(long dpid) { |
| 165 | LeaderLatch latch = switchLatches.get(HexString.toHexString(dpid)); |
| 166 | |
| 167 | if (latch == null) { |
| 168 | log.warn("No leader latch for dpid {}", HexString.toHexString(dpid)); |
| 169 | return false; |
| 170 | } |
| 171 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 172 | try { |
| 173 | return latch.getLeader().getId().equals(mastershipId); |
| 174 | } catch (Exception e) { |
| 175 | //TODO swallow exception? |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void setMastershipId(String id) { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 182 | mastershipId = id; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public String getMastershipId() { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 187 | return mastershipId; |
| 188 | } |
| 189 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 190 | @Override |
| 191 | public Collection<String> getAllControllers() { |
| 192 | // TODO Auto-generated method stub |
| 193 | log.debug("Getting all controllers"); |
| 194 | List<String> controllers = new ArrayList<String>(); |
| 195 | for (ChildData data : controllerCache.getCurrentData()){ |
| 196 | |
| 197 | String d = null; |
| 198 | try { |
| 199 | d = new String(data.getData(), "UTF-8"); |
| 200 | } catch (UnsupportedEncodingException e) { |
| 201 | // TODO unlikely(throw exception) |
| 202 | e.printStackTrace(); |
| 203 | } |
| 204 | |
| 205 | controllers.add(d); |
| 206 | } |
| 207 | return controllers; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void registerController(String id) { |
| 212 | // TODO Auto-generated method stub |
| 213 | byte bytes[] = null; |
| 214 | try { |
| 215 | bytes = id.getBytes("UTF-8"); |
| 216 | } catch (UnsupportedEncodingException e1) { |
| 217 | // TODO Throw some exception (very unlikely this will happen) |
| 218 | e1.printStackTrace(); |
| 219 | } |
| 220 | |
| 221 | String path = controllerPath + "/" + id; |
| 222 | |
| 223 | log.info("Registering controller with id {}", id); |
| 224 | |
| 225 | //Create ephemeral node with my id |
| 226 | try { |
| 227 | client.create().withProtection().withMode(CreateMode.EPHEMERAL) |
| 228 | .forPath(path, bytes); |
| 229 | } catch (Exception e) { |
| 230 | // TODO Auto-generated catch block |
| 231 | // TODO Make an exception that will be thrown here |
| 232 | e.printStackTrace(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public String getControllerForSwitch(long dpid) { |
| 238 | // TODO Work out how we should store this controller/switch data. |
| 239 | // The leader latch might be a index to the /controllers collections |
| 240 | // which holds more info on the controller (how to talk to it for example). |
| 241 | |
| 242 | |
| 243 | String strDpid = HexString.toHexString(dpid); |
| 244 | LeaderLatch latch = switchLatches.get(strDpid); |
| 245 | |
| 246 | if (latch == null){ |
| 247 | log.warn("Tried to get controller for non-existent switch"); |
| 248 | return null; |
| 249 | } |
| 250 | |
| 251 | Participant leader = null; |
| 252 | try { |
| 253 | leader = latch.getLeader(); |
| 254 | } catch (Exception e) { |
| 255 | // TODO Zookeeper issue, throw exception |
| 256 | e.printStackTrace(); |
| 257 | } |
| 258 | |
| 259 | return leader.getId(); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public Collection<Long> getSwitchesControlledByController(String controllerId) { |
| 264 | // TODO Auto-generated method stub |
| 265 | return null; |
| 266 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * IFloodlightModule |
| 270 | */ |
| 271 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 272 | @Override |
| 273 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 274 | Collection<Class<? extends IFloodlightService>> l = |
| 275 | new ArrayList<Class<? extends IFloodlightService>>(); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 276 | l.add(IMastershipService.class); |
| 277 | return l; |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 282 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 283 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 284 | m.put(IMastershipService.class, this); |
| 285 | return m; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 290 | // no module dependencies |
| 291 | return null; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void init (FloodlightModuleContext context) throws FloodlightModuleException { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 296 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 297 | try { |
| 298 | String localHostname = java.net.InetAddress.getLocalHost().getHostName(); |
| 299 | mastershipId = localHostname; |
| 300 | log.debug("Setting mastership id to {}", mastershipId); |
| 301 | } catch (UnknownHostException e) { |
| 302 | // TODO Handle this exception |
| 303 | e.printStackTrace(); |
| 304 | } |
| 305 | |
| 306 | switchLatches = new HashMap<String, LeaderLatch>(); |
| 307 | switchCallbacks = new HashMap<String, MastershipCallback>(); |
| 308 | |
| 309 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); |
| 310 | client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy); |
| 311 | |
| 312 | client.start(); |
| 313 | |
| 314 | client = client.usingNamespace(namespace); |
| 315 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame^] | 316 | controllerCache = new PathChildrenCache(client, controllerPath, true); |
| 317 | |
| 318 | try { |
| 319 | controllerCache.start(StartMode.BUILD_INITIAL_CACHE); |
| 320 | |
| 321 | |
| 322 | } catch (Exception e) { |
| 323 | // TODO Auto-generated catch block |
| 324 | e.printStackTrace(); |
| 325 | } |
| 326 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void startUp (FloodlightModuleContext context) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 331 | // Nothing to be done on startup |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 332 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 333 | } |