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