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