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