Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.host.impl; |
| 2 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 3 | import com.google.common.collect.FluentIterable; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 4 | import com.google.common.collect.HashMultimap; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 5 | import com.google.common.collect.ImmutableList; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 6 | import com.google.common.collect.ImmutableSet; |
| 7 | import com.google.common.collect.Multimap; |
| 8 | import com.google.common.collect.Sets; |
| 9 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 10 | import org.apache.commons.lang3.RandomUtils; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 11 | import org.apache.felix.scr.annotations.Activate; |
| 12 | import org.apache.felix.scr.annotations.Component; |
| 13 | import org.apache.felix.scr.annotations.Deactivate; |
| 14 | import org.apache.felix.scr.annotations.Reference; |
| 15 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 16 | import org.apache.felix.scr.annotations.Service; |
| 17 | import org.onlab.onos.cluster.ClusterService; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 18 | import org.onlab.onos.cluster.ControllerNode; |
| 19 | import org.onlab.onos.cluster.NodeId; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.Annotations; |
| 21 | import org.onlab.onos.net.ConnectPoint; |
| 22 | import org.onlab.onos.net.DefaultHost; |
| 23 | import org.onlab.onos.net.DeviceId; |
| 24 | import org.onlab.onos.net.Host; |
| 25 | import org.onlab.onos.net.HostId; |
| 26 | import org.onlab.onos.net.HostLocation; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.host.DefaultHostDescription; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.host.HostClockService; |
| 29 | import org.onlab.onos.net.host.HostDescription; |
| 30 | import org.onlab.onos.net.host.HostEvent; |
| 31 | import org.onlab.onos.net.host.HostStore; |
| 32 | import org.onlab.onos.net.host.HostStoreDelegate; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.host.InterfaceIpAddress; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.host.PortAddresses; |
| 35 | import org.onlab.onos.net.provider.ProviderId; |
| 36 | import org.onlab.onos.store.AbstractStore; |
| 37 | import org.onlab.onos.store.Timestamp; |
| 38 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 39 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 40 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 41 | import org.onlab.onos.store.cluster.messaging.MessageSubject; |
Yuta HIGUCHI | eecee55 | 2014-10-16 14:09:01 -0700 | [diff] [blame] | 42 | import org.onlab.onos.store.impl.Timestamped; |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 43 | import org.onlab.onos.store.serializers.DistributedStoreSerializers; |
| 44 | import org.onlab.onos.store.serializers.KryoSerializer; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 45 | import org.onlab.packet.IpAddress; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 46 | import org.onlab.packet.MacAddress; |
| 47 | import org.onlab.packet.VlanId; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 48 | import org.onlab.util.KryoNamespace; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 51 | import java.io.IOException; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 52 | import java.util.HashMap; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 53 | import java.util.HashSet; |
| 54 | import java.util.Map; |
| 55 | import java.util.Set; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 56 | import java.util.Map.Entry; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 57 | import java.util.concurrent.ConcurrentHashMap; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 58 | import java.util.concurrent.ScheduledExecutorService; |
| 59 | import java.util.concurrent.TimeUnit; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 60 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 61 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 62 | import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 63 | import static org.onlab.onos.net.host.HostEvent.Type.*; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 64 | import static org.onlab.util.Tools.namedThreads; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
Yuta HIGUCHI | a263915 | 2014-10-14 15:08:10 -0700 | [diff] [blame] | 67 | //TODO: multi-provider, annotation not supported. |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Manages inventory of end-station hosts in distributed data store |
| 70 | * that uses optimistic replication and gossip based techniques. |
| 71 | */ |
| 72 | @Component(immediate = true) |
| 73 | @Service |
| 74 | public class GossipHostStore |
| 75 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 76 | implements HostStore { |
| 77 | |
| 78 | private final Logger log = getLogger(getClass()); |
| 79 | |
| 80 | // Host inventory |
| 81 | private final Map<HostId, StoredHost> hosts = new ConcurrentHashMap<>(2000000, 0.75f, 16); |
| 82 | |
| 83 | private final Map<HostId, Timestamped<Host>> removedHosts = new ConcurrentHashMap<>(2000000, 0.75f, 16); |
| 84 | |
| 85 | // Hosts tracked by their location |
| 86 | private final Multimap<ConnectPoint, Host> locations = HashMultimap.create(); |
| 87 | |
| 88 | private final Map<ConnectPoint, PortAddresses> portAddresses = |
| 89 | new ConcurrentHashMap<>(); |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected HostClockService hostClockService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected ClusterCommunicationService clusterCommunicator; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected ClusterService clusterService; |
| 99 | |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 100 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 101 | @Override |
| 102 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 103 | serializerPool = KryoNamespace.newBuilder() |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 104 | .register(DistributedStoreSerializers.COMMON) |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 105 | .register(InternalHostEvent.class) |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 106 | .register(InternalHostRemovedEvent.class) |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 107 | .register(HostFragmentId.class) |
| 108 | .register(HostAntiEntropyAdvertisement.class) |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 109 | .build() |
| 110 | .populate(1); |
| 111 | } |
| 112 | }; |
| 113 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 114 | private ScheduledExecutorService executor; |
| 115 | |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 116 | @Activate |
| 117 | public void activate() { |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 118 | clusterCommunicator.addSubscriber( |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 119 | GossipHostStoreMessageSubjects.HOST_UPDATED, |
| 120 | new InternalHostEventListener()); |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 121 | clusterCommunicator.addSubscriber( |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 122 | GossipHostStoreMessageSubjects.HOST_REMOVED, |
| 123 | new InternalHostRemovedEventListener()); |
| 124 | clusterCommunicator.addSubscriber( |
| 125 | GossipHostStoreMessageSubjects.HOST_ANTI_ENTROPY_ADVERTISEMENT, |
| 126 | new InternalHostAntiEntropyAdvertisementListener()); |
| 127 | |
| 128 | executor = |
| 129 | newSingleThreadScheduledExecutor(namedThreads("link-anti-entropy-%d")); |
| 130 | |
| 131 | // TODO: Make these configurable |
| 132 | long initialDelaySec = 5; |
| 133 | long periodSec = 5; |
| 134 | // start anti-entropy thread |
| 135 | executor.scheduleAtFixedRate(new SendAdvertisementTask(), |
| 136 | initialDelaySec, periodSec, TimeUnit.SECONDS); |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 137 | |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 138 | log.info("Started"); |
| 139 | } |
| 140 | |
| 141 | @Deactivate |
| 142 | public void deactivate() { |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 143 | executor.shutdownNow(); |
| 144 | try { |
| 145 | if (!executor.awaitTermination(5, TimeUnit.SECONDS)) { |
| 146 | log.error("Timeout during executor shutdown"); |
| 147 | } |
| 148 | } catch (InterruptedException e) { |
| 149 | log.error("Error during executor shutdown", e); |
| 150 | } |
| 151 | |
| 152 | hosts.clear(); |
| 153 | removedHosts.clear(); |
| 154 | locations.clear(); |
| 155 | portAddresses.clear(); |
| 156 | |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 157 | log.info("Stopped"); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId, |
| 162 | HostDescription hostDescription) { |
| 163 | Timestamp timestamp = hostClockService.getTimestamp(hostId); |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 164 | HostEvent event = createOrUpdateHostInternal(providerId, hostId, hostDescription, timestamp); |
| 165 | if (event != null) { |
| 166 | log.info("Notifying peers of a host topology event for providerId: " |
| 167 | + "{}; hostId: {}; hostDescription: {}", providerId, hostId, hostDescription); |
| 168 | try { |
| 169 | notifyPeers(new InternalHostEvent(providerId, hostId, hostDescription, timestamp)); |
| 170 | } catch (IOException e) { |
| 171 | log.error("Failed to notify peers of a host topology event for providerId: " |
| 172 | + "{}; hostId: {}; hostDescription: {}", providerId, hostId, hostDescription); |
| 173 | } |
| 174 | } |
| 175 | return event; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | private HostEvent createOrUpdateHostInternal(ProviderId providerId, HostId hostId, |
| 179 | HostDescription hostDescription, Timestamp timestamp) { |
| 180 | StoredHost host = hosts.get(hostId); |
| 181 | if (host == null) { |
| 182 | return createHost(providerId, hostId, hostDescription, timestamp); |
| 183 | } |
| 184 | return updateHost(providerId, host, hostDescription, timestamp); |
| 185 | } |
| 186 | |
| 187 | // creates a new host and sends HOST_ADDED |
| 188 | private HostEvent createHost(ProviderId providerId, HostId hostId, |
| 189 | HostDescription descr, Timestamp timestamp) { |
| 190 | synchronized (this) { |
| 191 | // If this host was previously removed, first ensure |
| 192 | // this new request is "newer" |
| 193 | if (removedHosts.containsKey(hostId)) { |
| 194 | if (removedHosts.get(hostId).isNewer(timestamp)) { |
| 195 | return null; |
| 196 | } else { |
| 197 | removedHosts.remove(hostId); |
| 198 | } |
| 199 | } |
| 200 | StoredHost newhost = new StoredHost(providerId, hostId, |
| 201 | descr.hwAddress(), |
| 202 | descr.vlan(), |
| 203 | new Timestamped<>(descr.location(), timestamp), |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 204 | ImmutableSet.copyOf(descr.ipAddress())); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 205 | hosts.put(hostId, newhost); |
| 206 | locations.put(descr.location(), newhost); |
| 207 | return new HostEvent(HOST_ADDED, newhost); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // checks for type of update to host, sends appropriate event |
| 212 | private HostEvent updateHost(ProviderId providerId, StoredHost host, |
| 213 | HostDescription descr, Timestamp timestamp) { |
| 214 | HostEvent event; |
| 215 | if (!host.location.isNewer(timestamp) && !host.location().equals(descr.location())) { |
| 216 | host.setLocation(new Timestamped<>(descr.location(), timestamp)); |
| 217 | return new HostEvent(HOST_MOVED, host); |
| 218 | } |
| 219 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 220 | if (host.ipAddresses().containsAll(descr.ipAddress())) { |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 221 | return null; |
| 222 | } |
| 223 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 224 | Set<IpAddress> addresses = new HashSet<>(host.ipAddresses()); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 225 | addresses.addAll(descr.ipAddress()); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 226 | StoredHost updated = new StoredHost(providerId, host.id(), |
| 227 | host.mac(), host.vlan(), |
| 228 | host.location, addresses); |
| 229 | event = new HostEvent(HOST_UPDATED, updated); |
| 230 | synchronized (this) { |
| 231 | hosts.put(host.id(), updated); |
| 232 | locations.remove(host.location(), host); |
| 233 | locations.put(updated.location(), updated); |
| 234 | } |
| 235 | return event; |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public HostEvent removeHost(HostId hostId) { |
| 240 | Timestamp timestamp = hostClockService.getTimestamp(hostId); |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 241 | HostEvent event = removeHostInternal(hostId, timestamp); |
| 242 | if (event != null) { |
| 243 | log.info("Notifying peers of a host removed topology event for hostId: {}", hostId); |
| 244 | try { |
| 245 | notifyPeers(new InternalHostRemovedEvent(hostId, timestamp)); |
| 246 | } catch (IOException e) { |
| 247 | log.info("Failed to notify peers of a host removed topology event for hostId: {}", hostId); |
| 248 | } |
| 249 | } |
| 250 | return event; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private HostEvent removeHostInternal(HostId hostId, Timestamp timestamp) { |
| 254 | synchronized (this) { |
| 255 | Host host = hosts.remove(hostId); |
| 256 | if (host != null) { |
| 257 | locations.remove((host.location()), host); |
| 258 | removedHosts.put(hostId, new Timestamped<>(host, timestamp)); |
| 259 | return new HostEvent(HOST_REMOVED, host); |
| 260 | } |
| 261 | return null; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public int getHostCount() { |
| 267 | return hosts.size(); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public Iterable<Host> getHosts() { |
| 272 | return ImmutableSet.<Host>copyOf(hosts.values()); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public Host getHost(HostId hostId) { |
| 277 | return hosts.get(hostId); |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public Set<Host> getHosts(VlanId vlanId) { |
| 282 | Set<Host> vlanset = new HashSet<>(); |
| 283 | for (Host h : hosts.values()) { |
| 284 | if (h.vlan().equals(vlanId)) { |
| 285 | vlanset.add(h); |
| 286 | } |
| 287 | } |
| 288 | return vlanset; |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public Set<Host> getHosts(MacAddress mac) { |
| 293 | Set<Host> macset = new HashSet<>(); |
| 294 | for (Host h : hosts.values()) { |
| 295 | if (h.mac().equals(mac)) { |
| 296 | macset.add(h); |
| 297 | } |
| 298 | } |
| 299 | return macset; |
| 300 | } |
| 301 | |
| 302 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 303 | public Set<Host> getHosts(IpAddress ip) { |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 304 | Set<Host> ipset = new HashSet<>(); |
| 305 | for (Host h : hosts.values()) { |
| 306 | if (h.ipAddresses().contains(ip)) { |
| 307 | ipset.add(h); |
| 308 | } |
| 309 | } |
| 310 | return ipset; |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
| 315 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
| 320 | Set<Host> hostset = new HashSet<>(); |
| 321 | for (ConnectPoint p : locations.keySet()) { |
| 322 | if (p.deviceId().equals(deviceId)) { |
| 323 | hostset.addAll(locations.get(p)); |
| 324 | } |
| 325 | } |
| 326 | return hostset; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void updateAddressBindings(PortAddresses addresses) { |
| 331 | synchronized (portAddresses) { |
| 332 | PortAddresses existing = portAddresses.get(addresses.connectPoint()); |
| 333 | if (existing == null) { |
| 334 | portAddresses.put(addresses.connectPoint(), addresses); |
| 335 | } else { |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 336 | Set<InterfaceIpAddress> union = |
| 337 | Sets.union(existing.ipAddresses(), |
| 338 | addresses.ipAddresses()).immutableCopy(); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 339 | |
| 340 | MacAddress newMac = (addresses.mac() == null) ? existing.mac() |
| 341 | : addresses.mac(); |
| 342 | |
| 343 | PortAddresses newAddresses = |
| 344 | new PortAddresses(addresses.connectPoint(), union, newMac); |
| 345 | |
| 346 | portAddresses.put(newAddresses.connectPoint(), newAddresses); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void removeAddressBindings(PortAddresses addresses) { |
| 353 | synchronized (portAddresses) { |
| 354 | PortAddresses existing = portAddresses.get(addresses.connectPoint()); |
| 355 | if (existing != null) { |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 356 | Set<InterfaceIpAddress> difference = |
| 357 | Sets.difference(existing.ipAddresses(), |
| 358 | addresses.ipAddresses()).immutableCopy(); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 359 | |
| 360 | // If they removed the existing mac, set the new mac to null. |
| 361 | // Otherwise, keep the existing mac. |
| 362 | MacAddress newMac = existing.mac(); |
| 363 | if (addresses.mac() != null && addresses.mac().equals(existing.mac())) { |
| 364 | newMac = null; |
| 365 | } |
| 366 | |
| 367 | PortAddresses newAddresses = |
| 368 | new PortAddresses(addresses.connectPoint(), difference, newMac); |
| 369 | |
| 370 | portAddresses.put(newAddresses.connectPoint(), newAddresses); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void clearAddressBindings(ConnectPoint connectPoint) { |
| 377 | synchronized (portAddresses) { |
| 378 | portAddresses.remove(connectPoint); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public Set<PortAddresses> getAddressBindings() { |
| 384 | synchronized (portAddresses) { |
| 385 | return new HashSet<>(portAddresses.values()); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public PortAddresses getAddressBindingsForPort(ConnectPoint connectPoint) { |
| 391 | PortAddresses addresses; |
| 392 | |
| 393 | synchronized (portAddresses) { |
| 394 | addresses = portAddresses.get(connectPoint); |
| 395 | } |
| 396 | |
| 397 | if (addresses == null) { |
| 398 | addresses = new PortAddresses(connectPoint, null, null); |
| 399 | } |
| 400 | |
| 401 | return addresses; |
| 402 | } |
| 403 | |
| 404 | // Auxiliary extension to allow location to mutate. |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 405 | private static final class StoredHost extends DefaultHost { |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 406 | private Timestamped<HostLocation> location; |
| 407 | |
| 408 | /** |
| 409 | * Creates an end-station host using the supplied information. |
| 410 | * |
| 411 | * @param providerId provider identity |
| 412 | * @param id host identifier |
| 413 | * @param mac host MAC address |
| 414 | * @param vlan host VLAN identifier |
| 415 | * @param location host location |
| 416 | * @param ips host IP addresses |
| 417 | * @param annotations optional key/value annotations |
| 418 | */ |
| 419 | public StoredHost(ProviderId providerId, HostId id, |
| 420 | MacAddress mac, VlanId vlan, Timestamped<HostLocation> location, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame^] | 421 | Set<IpAddress> ips, Annotations... annotations) { |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 422 | super(providerId, id, mac, vlan, location.value(), ips, annotations); |
| 423 | this.location = location; |
| 424 | } |
| 425 | |
| 426 | void setLocation(Timestamped<HostLocation> location) { |
| 427 | this.location = location; |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public HostLocation location() { |
| 432 | return location.value(); |
| 433 | } |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 434 | |
| 435 | public Timestamp timestamp() { |
| 436 | return location.timestamp(); |
| 437 | } |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 438 | } |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 439 | |
| 440 | private void notifyPeers(InternalHostRemovedEvent event) throws IOException { |
| 441 | broadcastMessage(GossipHostStoreMessageSubjects.HOST_REMOVED, event); |
| 442 | } |
| 443 | |
| 444 | private void notifyPeers(InternalHostEvent event) throws IOException { |
| 445 | broadcastMessage(GossipHostStoreMessageSubjects.HOST_UPDATED, event); |
| 446 | } |
| 447 | |
| 448 | private void broadcastMessage(MessageSubject subject, Object event) throws IOException { |
| 449 | ClusterMessage message = new ClusterMessage( |
| 450 | clusterService.getLocalNode().id(), |
| 451 | subject, |
| 452 | SERIALIZER.encode(event)); |
| 453 | clusterCommunicator.broadcast(message); |
| 454 | } |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 455 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 456 | private void unicastMessage(NodeId peer, |
| 457 | MessageSubject subject, |
| 458 | Object event) throws IOException { |
| 459 | ClusterMessage message = new ClusterMessage( |
| 460 | clusterService.getLocalNode().id(), |
| 461 | subject, |
| 462 | SERIALIZER.encode(event)); |
| 463 | clusterCommunicator.unicast(message, peer); |
| 464 | } |
| 465 | |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 466 | private void notifyDelegateIfNotNull(HostEvent event) { |
| 467 | if (event != null) { |
| 468 | notifyDelegate(event); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | private class InternalHostEventListener implements ClusterMessageHandler { |
| 473 | @Override |
| 474 | public void handle(ClusterMessage message) { |
| 475 | |
| 476 | log.info("Received host update event from peer: {}", message.sender()); |
| 477 | InternalHostEvent event = (InternalHostEvent) SERIALIZER.decode(message.payload()); |
| 478 | |
| 479 | ProviderId providerId = event.providerId(); |
| 480 | HostId hostId = event.hostId(); |
| 481 | HostDescription hostDescription = event.hostDescription(); |
| 482 | Timestamp timestamp = event.timestamp(); |
| 483 | |
| 484 | notifyDelegateIfNotNull(createOrUpdateHostInternal(providerId, hostId, hostDescription, timestamp)); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | private class InternalHostRemovedEventListener implements ClusterMessageHandler { |
| 489 | @Override |
| 490 | public void handle(ClusterMessage message) { |
| 491 | |
| 492 | log.info("Received host removed event from peer: {}", message.sender()); |
| 493 | InternalHostRemovedEvent event = (InternalHostRemovedEvent) SERIALIZER.decode(message.payload()); |
| 494 | |
| 495 | HostId hostId = event.hostId(); |
| 496 | Timestamp timestamp = event.timestamp(); |
| 497 | |
| 498 | notifyDelegateIfNotNull(removeHostInternal(hostId, timestamp)); |
| 499 | } |
| 500 | } |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 501 | |
| 502 | private final class SendAdvertisementTask implements Runnable { |
| 503 | |
| 504 | @Override |
| 505 | public void run() { |
| 506 | if (Thread.currentThread().isInterrupted()) { |
| 507 | log.info("Interrupted, quitting"); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | try { |
| 512 | final NodeId self = clusterService.getLocalNode().id(); |
| 513 | Set<ControllerNode> nodes = clusterService.getNodes(); |
| 514 | |
| 515 | ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes) |
| 516 | .transform(toNodeId()) |
| 517 | .toList(); |
| 518 | |
| 519 | if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) { |
| 520 | log.debug("No other peers in the cluster."); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | NodeId peer; |
| 525 | do { |
| 526 | int idx = RandomUtils.nextInt(0, nodeIds.size()); |
| 527 | peer = nodeIds.get(idx); |
| 528 | } while (peer.equals(self)); |
| 529 | |
| 530 | HostAntiEntropyAdvertisement ad = createAdvertisement(); |
| 531 | |
| 532 | if (Thread.currentThread().isInterrupted()) { |
| 533 | log.info("Interrupted, quitting"); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | try { |
| 538 | unicastMessage(peer, GossipHostStoreMessageSubjects.HOST_ANTI_ENTROPY_ADVERTISEMENT, ad); |
| 539 | } catch (IOException e) { |
Yuta HIGUCHI | 78f3a0a | 2014-10-16 17:24:20 -0700 | [diff] [blame] | 540 | log.debug("Failed to send anti-entropy advertisement to {}", peer); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 541 | return; |
| 542 | } |
| 543 | } catch (Exception e) { |
| 544 | // catch all Exception to avoid Scheduled task being suppressed. |
| 545 | log.error("Exception thrown while sending advertisement", e); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | private HostAntiEntropyAdvertisement createAdvertisement() { |
| 551 | final NodeId self = clusterService.getLocalNode().id(); |
| 552 | |
| 553 | Map<HostFragmentId, Timestamp> timestamps = new HashMap<>(hosts.size()); |
| 554 | Map<HostId, Timestamp> tombstones = new HashMap<>(removedHosts.size()); |
| 555 | |
| 556 | for (Entry<HostId, StoredHost> e : hosts.entrySet()) { |
| 557 | |
| 558 | final HostId hostId = e.getKey(); |
| 559 | final StoredHost hostInfo = e.getValue(); |
| 560 | final ProviderId providerId = hostInfo.providerId(); |
| 561 | timestamps.put(new HostFragmentId(hostId, providerId), hostInfo.timestamp()); |
| 562 | } |
| 563 | |
| 564 | for (Entry<HostId, Timestamped<Host>> e : removedHosts.entrySet()) { |
| 565 | tombstones.put(e.getKey(), e.getValue().timestamp()); |
| 566 | } |
| 567 | |
| 568 | return new HostAntiEntropyAdvertisement(self, timestamps, tombstones); |
| 569 | } |
| 570 | |
| 571 | private synchronized void handleAntiEntropyAdvertisement(HostAntiEntropyAdvertisement ad) { |
| 572 | |
| 573 | final NodeId sender = ad.sender(); |
| 574 | |
| 575 | for (Entry<HostId, StoredHost> host : hosts.entrySet()) { |
| 576 | // for each locally live Hosts... |
| 577 | final HostId hostId = host.getKey(); |
| 578 | final StoredHost localHost = host.getValue(); |
| 579 | final ProviderId providerId = localHost.providerId(); |
| 580 | final HostFragmentId hostFragId = new HostFragmentId(hostId, providerId); |
| 581 | final Timestamp localLiveTimestamp = localHost.timestamp(); |
| 582 | |
| 583 | Timestamp remoteTimestamp = ad.timestamps().get(hostFragId); |
| 584 | if (remoteTimestamp == null) { |
| 585 | remoteTimestamp = ad.tombstones().get(hostId); |
| 586 | } |
| 587 | if (remoteTimestamp == null || |
| 588 | localLiveTimestamp.compareTo(remoteTimestamp) > 0) { |
| 589 | |
| 590 | // local is more recent, push |
| 591 | // TODO: annotation is lost |
| 592 | final HostDescription desc = new DefaultHostDescription( |
| 593 | localHost.mac(), |
| 594 | localHost.vlan(), |
| 595 | localHost.location(), |
| 596 | localHost.ipAddresses()); |
| 597 | try { |
| 598 | unicastMessage(sender, GossipHostStoreMessageSubjects.HOST_UPDATED, |
| 599 | new InternalHostEvent(providerId, hostId, desc, localHost.timestamp())); |
| 600 | } catch (IOException e1) { |
| 601 | log.debug("Failed to send advertisement response", e1); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | final Timestamp remoteDeadTimestamp = ad.tombstones().get(hostId); |
| 606 | if (remoteDeadTimestamp != null && |
| 607 | remoteDeadTimestamp.compareTo(localLiveTimestamp) > 0) { |
| 608 | // sender has recent remove |
| 609 | notifyDelegateIfNotNull(removeHostInternal(hostId, remoteDeadTimestamp)); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | for (Entry<HostId, Timestamped<Host>> dead : removedHosts.entrySet()) { |
| 614 | // for each locally dead Hosts |
| 615 | final HostId hostId = dead.getKey(); |
| 616 | final Timestamp localDeadTimestamp = dead.getValue().timestamp(); |
| 617 | |
| 618 | // TODO: pick proper ProviderId, when supporting multi-provider |
| 619 | final ProviderId providerId = dead.getValue().value().providerId(); |
| 620 | final HostFragmentId hostFragId = new HostFragmentId(hostId, providerId); |
| 621 | |
| 622 | final Timestamp remoteLiveTimestamp = ad.timestamps().get(hostFragId); |
| 623 | if (remoteLiveTimestamp != null && |
| 624 | localDeadTimestamp.compareTo(remoteLiveTimestamp) > 0) { |
| 625 | // sender has zombie, push |
| 626 | try { |
| 627 | unicastMessage(sender, GossipHostStoreMessageSubjects.HOST_REMOVED, |
| 628 | new InternalHostRemovedEvent(hostId, localDeadTimestamp)); |
| 629 | } catch (IOException e1) { |
| 630 | log.debug("Failed to send advertisement response", e1); |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | |
| 636 | for (Entry<HostId, Timestamp> e : ad.tombstones().entrySet()) { |
| 637 | // for each remote tombstone advertisement... |
| 638 | final HostId hostId = e.getKey(); |
| 639 | final Timestamp adRemoveTimestamp = e.getValue(); |
| 640 | |
| 641 | final StoredHost storedHost = hosts.get(hostId); |
| 642 | if (storedHost == null) { |
| 643 | continue; |
| 644 | } |
| 645 | if (adRemoveTimestamp.compareTo(storedHost.timestamp()) > 0) { |
| 646 | // sender has recent remove info, locally remove |
| 647 | notifyDelegateIfNotNull(removeHostInternal(hostId, adRemoveTimestamp)); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | private final class InternalHostAntiEntropyAdvertisementListener implements |
| 653 | ClusterMessageHandler { |
| 654 | |
| 655 | @Override |
| 656 | public void handle(ClusterMessage message) { |
| 657 | log.debug("Received Host Anti-Entropy advertisement from peer: {}", message.sender()); |
| 658 | HostAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload()); |
| 659 | handleAntiEntropyAdvertisement(advertisement); |
| 660 | } |
| 661 | } |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 662 | } |