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