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