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 | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.link.impl; |
| 17 | |
| 18 | import com.google.common.base.Function; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 19 | import com.google.common.collect.FluentIterable; |
| 20 | import com.google.common.collect.HashMultimap; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.SetMultimap; |
| 24 | |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 25 | import org.apache.commons.lang3.RandomUtils; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
| 32 | import org.onlab.onos.cluster.ClusterService; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 33 | import org.onlab.onos.cluster.ControllerNode; |
| 34 | import org.onlab.onos.cluster.NodeId; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.AnnotationsUtil; |
| 36 | import org.onlab.onos.net.ConnectPoint; |
| 37 | import org.onlab.onos.net.DefaultAnnotations; |
| 38 | import org.onlab.onos.net.DefaultLink; |
| 39 | import org.onlab.onos.net.DeviceId; |
| 40 | import org.onlab.onos.net.Link; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.Link.Type; |
| 42 | import org.onlab.onos.net.LinkKey; |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.SparseAnnotations; |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 44 | import org.onlab.onos.net.device.DeviceClockService; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.link.DefaultLinkDescription; |
| 46 | import org.onlab.onos.net.link.LinkDescription; |
| 47 | import org.onlab.onos.net.link.LinkEvent; |
| 48 | import org.onlab.onos.net.link.LinkStore; |
| 49 | import org.onlab.onos.net.link.LinkStoreDelegate; |
| 50 | import org.onlab.onos.net.provider.ProviderId; |
| 51 | import org.onlab.onos.store.AbstractStore; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 52 | import org.onlab.onos.store.Timestamp; |
| 53 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 54 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
| 55 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
| 56 | import org.onlab.onos.store.cluster.messaging.MessageSubject; |
Yuta HIGUCHI | eecee55 | 2014-10-16 14:09:01 -0700 | [diff] [blame] | 57 | import org.onlab.onos.store.impl.Timestamped; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 58 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 60a190b | 2014-11-07 16:24:47 -0800 | [diff] [blame] | 59 | import org.onlab.onos.store.serializers.impl.DistributedStoreSerializers; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 60 | import org.onlab.util.KryoNamespace; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | |
| 63 | import java.io.IOException; |
| 64 | import java.util.Collections; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 65 | import java.util.HashMap; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 66 | import java.util.HashSet; |
| 67 | import java.util.Map; |
Thomas Vachuska | 29a6a78 | 2014-11-10 21:31:41 -0800 | [diff] [blame] | 68 | import java.util.Objects; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 69 | import java.util.Set; |
| 70 | import java.util.Map.Entry; |
| 71 | import java.util.concurrent.ConcurrentHashMap; |
| 72 | import java.util.concurrent.ConcurrentMap; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 73 | import java.util.concurrent.ScheduledExecutorService; |
| 74 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 75 | |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 76 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 77 | import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 78 | import static org.onlab.onos.net.DefaultAnnotations.union; |
| 79 | import static org.onlab.onos.net.DefaultAnnotations.merge; |
| 80 | import static org.onlab.onos.net.Link.Type.DIRECT; |
| 81 | import static org.onlab.onos.net.Link.Type.INDIRECT; |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 82 | import static org.onlab.onos.net.LinkKey.linkKey; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 83 | import static org.onlab.onos.net.link.LinkEvent.Type.*; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 84 | import static org.onlab.util.Tools.namedThreads; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 85 | import static org.slf4j.LoggerFactory.getLogger; |
| 86 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 87 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 88 | import static com.google.common.base.Predicates.notNull; |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 89 | import static org.onlab.onos.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Manages inventory of infrastructure links in distributed data store |
| 93 | * that uses optimistic replication and gossip based techniques. |
| 94 | */ |
| 95 | @Component(immediate = true) |
| 96 | @Service |
| 97 | public class GossipLinkStore |
| 98 | extends AbstractStore<LinkEvent, LinkStoreDelegate> |
| 99 | implements LinkStore { |
| 100 | |
| 101 | private final Logger log = getLogger(getClass()); |
| 102 | |
| 103 | // Link inventory |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 104 | private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs = |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 105 | new ConcurrentHashMap<>(); |
| 106 | |
| 107 | // Link instance cache |
| 108 | private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>(); |
| 109 | |
| 110 | // Egress and ingress link sets |
| 111 | private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap(); |
| 112 | private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap(); |
| 113 | |
| 114 | // Remove links |
| 115 | private final Map<LinkKey, Timestamp> removedLinks = Maps.newHashMap(); |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 118 | protected DeviceClockService deviceClockService; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 121 | protected ClusterCommunicationService clusterCommunicator; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected ClusterService clusterService; |
| 125 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 126 | protected static final KryoSerializer SERIALIZER = new KryoSerializer() { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 127 | @Override |
| 128 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 129 | serializerPool = KryoNamespace.newBuilder() |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 130 | .register(DistributedStoreSerializers.COMMON) |
| 131 | .register(InternalLinkEvent.class) |
| 132 | .register(InternalLinkRemovedEvent.class) |
Madan Jampani | d2054d4 | 2014-10-10 17:27:06 -0700 | [diff] [blame] | 133 | .register(LinkAntiEntropyAdvertisement.class) |
| 134 | .register(LinkFragmentId.class) |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 135 | .build() |
| 136 | .populate(1); |
| 137 | } |
| 138 | }; |
| 139 | |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 140 | private ScheduledExecutorService executor; |
| 141 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 142 | @Activate |
| 143 | public void activate() { |
| 144 | |
| 145 | clusterCommunicator.addSubscriber( |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 146 | GossipLinkStoreMessageSubjects.LINK_UPDATE, |
| 147 | new InternalLinkEventListener()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 148 | clusterCommunicator.addSubscriber( |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 149 | GossipLinkStoreMessageSubjects.LINK_REMOVED, |
| 150 | new InternalLinkRemovedEventListener()); |
| 151 | clusterCommunicator.addSubscriber( |
| 152 | GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT, |
| 153 | new InternalLinkAntiEntropyAdvertisementListener()); |
| 154 | |
| 155 | executor = |
| 156 | newSingleThreadScheduledExecutor(namedThreads("link-anti-entropy-%d")); |
| 157 | |
| 158 | // TODO: Make these configurable |
| 159 | long initialDelaySec = 5; |
| 160 | long periodSec = 5; |
| 161 | // start anti-entropy thread |
| 162 | executor.scheduleAtFixedRate(new SendAdvertisementTask(), |
| 163 | initialDelaySec, periodSec, TimeUnit.SECONDS); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 164 | |
| 165 | log.info("Started"); |
| 166 | } |
| 167 | |
| 168 | @Deactivate |
| 169 | public void deactivate() { |
Madan Jampani | 3ffbb27 | 2014-10-13 11:19:37 -0700 | [diff] [blame] | 170 | |
| 171 | executor.shutdownNow(); |
| 172 | try { |
| 173 | if (!executor.awaitTermination(5, TimeUnit.SECONDS)) { |
| 174 | log.error("Timeout during executor shutdown"); |
| 175 | } |
| 176 | } catch (InterruptedException e) { |
| 177 | log.error("Error during executor shutdown", e); |
| 178 | } |
| 179 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 180 | linkDescs.clear(); |
| 181 | links.clear(); |
| 182 | srcLinks.clear(); |
| 183 | dstLinks.clear(); |
| 184 | log.info("Stopped"); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public int getLinkCount() { |
| 189 | return links.size(); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Iterable<Link> getLinks() { |
| 194 | return Collections.unmodifiableCollection(links.values()); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public Set<Link> getDeviceEgressLinks(DeviceId deviceId) { |
| 199 | // lock for iteration |
| 200 | synchronized (srcLinks) { |
| 201 | return FluentIterable.from(srcLinks.get(deviceId)) |
| 202 | .transform(lookupLink()) |
| 203 | .filter(notNull()) |
| 204 | .toSet(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { |
| 210 | // lock for iteration |
| 211 | synchronized (dstLinks) { |
| 212 | return FluentIterable.from(dstLinks.get(deviceId)) |
| 213 | .transform(lookupLink()) |
| 214 | .filter(notNull()) |
| 215 | .toSet(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public Link getLink(ConnectPoint src, ConnectPoint dst) { |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 221 | return links.get(linkKey(src, dst)); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public Set<Link> getEgressLinks(ConnectPoint src) { |
| 226 | Set<Link> egress = new HashSet<>(); |
| 227 | for (LinkKey linkKey : srcLinks.get(src.deviceId())) { |
| 228 | if (linkKey.src().equals(src)) { |
| 229 | egress.add(links.get(linkKey)); |
| 230 | } |
| 231 | } |
| 232 | return egress; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public Set<Link> getIngressLinks(ConnectPoint dst) { |
| 237 | Set<Link> ingress = new HashSet<>(); |
| 238 | for (LinkKey linkKey : dstLinks.get(dst.deviceId())) { |
| 239 | if (linkKey.dst().equals(dst)) { |
| 240 | ingress.add(links.get(linkKey)); |
| 241 | } |
| 242 | } |
| 243 | return ingress; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public LinkEvent createOrUpdateLink(ProviderId providerId, |
| 248 | LinkDescription linkDescription) { |
| 249 | |
| 250 | DeviceId dstDeviceId = linkDescription.dst().deviceId(); |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 251 | Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 252 | |
| 253 | final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp); |
| 254 | |
Yuta HIGUCHI | 990aecc | 2014-10-13 22:21:42 -0700 | [diff] [blame] | 255 | LinkKey key = linkKey(linkDescription.src(), linkDescription.dst()); |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 256 | final LinkEvent event; |
| 257 | final Timestamped<LinkDescription> mergedDesc; |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 258 | Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key); |
| 259 | synchronized (map) { |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 260 | event = createOrUpdateLinkInternal(providerId, deltaDesc); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 261 | mergedDesc = map.get(providerId); |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 262 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 263 | |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 264 | |
| 265 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 266 | if (event != null) { |
| 267 | log.info("Notifying peers of a link update topology event from providerId: " |
| 268 | + "{} between src: {} and dst: {}", |
| 269 | providerId, linkDescription.src(), linkDescription.dst()); |
| 270 | try { |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 271 | notifyPeers(new InternalLinkEvent(providerId, mergedDesc)); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 272 | } catch (IOException e) { |
| 273 | log.info("Failed to notify peers of a link update topology event from providerId: " |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 274 | + "{} between src: {} and dst: {}", |
| 275 | providerId, linkDescription.src(), linkDescription.dst()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | return event; |
| 279 | } |
| 280 | |
| 281 | private LinkEvent createOrUpdateLinkInternal( |
| 282 | ProviderId providerId, |
| 283 | Timestamped<LinkDescription> linkDescription) { |
| 284 | |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 285 | final LinkKey key = linkKey(linkDescription.value().src(), |
| 286 | linkDescription.value().dst()); |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 287 | Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 288 | |
| 289 | synchronized (descs) { |
| 290 | // if the link was previously removed, we should proceed if and |
| 291 | // only if this request is more recent. |
| 292 | Timestamp linkRemovedTimestamp = removedLinks.get(key); |
| 293 | if (linkRemovedTimestamp != null) { |
| 294 | if (linkDescription.isNewer(linkRemovedTimestamp)) { |
| 295 | removedLinks.remove(key); |
| 296 | } else { |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 297 | log.trace("Link {} was already removed ignoring.", key); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 298 | return null; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | final Link oldLink = links.get(key); |
| 303 | // update description |
| 304 | createOrUpdateLinkDescription(descs, providerId, linkDescription); |
| 305 | final Link newLink = composeLink(descs); |
| 306 | if (oldLink == null) { |
| 307 | return createLink(key, newLink); |
| 308 | } |
| 309 | return updateLink(key, oldLink, newLink); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // Guarded by linkDescs value (=locking each Link) |
| 314 | private Timestamped<LinkDescription> createOrUpdateLinkDescription( |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 315 | Map<ProviderId, Timestamped<LinkDescription>> descs, |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 316 | ProviderId providerId, |
| 317 | Timestamped<LinkDescription> linkDescription) { |
| 318 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 319 | // merge existing annotations |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 320 | Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 321 | if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) { |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 322 | log.trace("local info is more up-to-date, ignoring {}.", linkDescription); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 323 | return null; |
| 324 | } |
| 325 | Timestamped<LinkDescription> newLinkDescription = linkDescription; |
| 326 | if (existingLinkDescription != null) { |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 327 | // we only allow transition from INDIRECT -> DIRECT |
| 328 | final Type newType; |
| 329 | if (existingLinkDescription.value().type() == DIRECT) { |
| 330 | newType = DIRECT; |
| 331 | } else { |
| 332 | newType = linkDescription.value().type(); |
| 333 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 334 | SparseAnnotations merged = union(existingLinkDescription.value().annotations(), |
| 335 | linkDescription.value().annotations()); |
| 336 | newLinkDescription = new Timestamped<LinkDescription>( |
| 337 | new DefaultLinkDescription( |
| 338 | linkDescription.value().src(), |
| 339 | linkDescription.value().dst(), |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 340 | newType, merged), |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 341 | linkDescription.timestamp()); |
| 342 | } |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 343 | return descs.put(providerId, newLinkDescription); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // Creates and stores the link and returns the appropriate event. |
| 347 | // Guarded by linkDescs value (=locking each Link) |
| 348 | private LinkEvent createLink(LinkKey key, Link newLink) { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 349 | links.put(key, newLink); |
| 350 | srcLinks.put(newLink.src().deviceId(), key); |
| 351 | dstLinks.put(newLink.dst().deviceId(), key); |
| 352 | return new LinkEvent(LINK_ADDED, newLink); |
| 353 | } |
| 354 | |
| 355 | // Updates, if necessary the specified link and returns the appropriate event. |
| 356 | // Guarded by linkDescs value (=locking each Link) |
| 357 | private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) { |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 358 | // Note: INDIRECT -> DIRECT transition only |
| 359 | // so that BDDP discovered Link will not overwrite LDDP Link |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 360 | if ((oldLink.type() == INDIRECT && newLink.type() == DIRECT) || |
| 361 | !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) { |
| 362 | |
| 363 | links.put(key, newLink); |
| 364 | // strictly speaking following can be ommitted |
| 365 | srcLinks.put(oldLink.src().deviceId(), key); |
| 366 | dstLinks.put(oldLink.dst().deviceId(), key); |
| 367 | return new LinkEvent(LINK_UPDATED, newLink); |
| 368 | } |
| 369 | return null; |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) { |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 374 | final LinkKey key = linkKey(src, dst); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 375 | |
| 376 | DeviceId dstDeviceId = dst.deviceId(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 377 | Timestamp timestamp = null; |
| 378 | try { |
| 379 | timestamp = deviceClockService.getTimestamp(dstDeviceId); |
| 380 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | d6a0ac3 | 2014-11-04 09:26:31 -0800 | [diff] [blame] | 381 | log.warn("Failed to remove link {}, was not the master", key); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 382 | //there are times when this is called before mastership |
| 383 | // handoff correctly completes. |
| 384 | return null; |
| 385 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 386 | |
| 387 | LinkEvent event = removeLinkInternal(key, timestamp); |
| 388 | |
| 389 | if (event != null) { |
| 390 | log.info("Notifying peers of a link removed topology event for a link " |
| 391 | + "between src: {} and dst: {}", src, dst); |
| 392 | try { |
| 393 | notifyPeers(new InternalLinkRemovedEvent(key, timestamp)); |
| 394 | } catch (IOException e) { |
| 395 | log.error("Failed to notify peers of a link removed topology event for a link " |
| 396 | + "between src: {} and dst: {}", src, dst); |
| 397 | } |
| 398 | } |
| 399 | return event; |
| 400 | } |
| 401 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 402 | private static Timestamped<LinkDescription> getPrimaryDescription( |
| 403 | Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) { |
| 404 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 405 | synchronized (linkDescriptions) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 406 | for (Entry<ProviderId, Timestamped<LinkDescription>> |
| 407 | e : linkDescriptions.entrySet()) { |
| 408 | |
| 409 | if (!e.getKey().isAncillary()) { |
| 410 | return e.getValue(); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | return null; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | // TODO: consider slicing out as Timestamp utils |
| 419 | /** |
| 420 | * Checks is timestamp is more recent than timestamped object. |
| 421 | * |
| 422 | * @param timestamp to check if this is more recent then other |
| 423 | * @param timestamped object to be tested against |
| 424 | * @return true if {@code timestamp} is more recent than {@code timestamped} |
| 425 | * or {@code timestamped is null} |
| 426 | */ |
| 427 | private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) { |
| 428 | checkNotNull(timestamp); |
| 429 | if (timestamped == null) { |
| 430 | return true; |
| 431 | } |
| 432 | return timestamp.compareTo(timestamped.timestamp()) > 0; |
| 433 | } |
| 434 | |
| 435 | private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) { |
| 436 | Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions |
| 437 | = getOrCreateLinkDescriptions(key); |
| 438 | |
| 439 | synchronized (linkDescriptions) { |
| 440 | if (linkDescriptions.isEmpty()) { |
| 441 | // never seen such link before. keeping timestamp for record |
| 442 | removedLinks.put(key, timestamp); |
| 443 | return null; |
| 444 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 445 | // accept removal request if given timestamp is newer than |
| 446 | // the latest Timestamp from Primary provider |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 447 | Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions); |
| 448 | if (!isMoreRecent(timestamp, prim)) { |
| 449 | // outdated remove request, ignore |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 450 | return null; |
| 451 | } |
Thomas Vachuska | 29a6a78 | 2014-11-10 21:31:41 -0800 | [diff] [blame] | 452 | Link link = links.get(key); |
| 453 | if (isDurable(link)) { |
| 454 | return null; |
| 455 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 456 | removedLinks.put(key, timestamp); |
Thomas Vachuska | 29a6a78 | 2014-11-10 21:31:41 -0800 | [diff] [blame] | 457 | links.remove(key); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 458 | linkDescriptions.clear(); |
| 459 | if (link != null) { |
| 460 | srcLinks.remove(link.src().deviceId(), key); |
| 461 | dstLinks.remove(link.dst().deviceId(), key); |
| 462 | return new LinkEvent(LINK_REMOVED, link); |
| 463 | } |
| 464 | return null; |
| 465 | } |
| 466 | } |
| 467 | |
Thomas Vachuska | 29a6a78 | 2014-11-10 21:31:41 -0800 | [diff] [blame] | 468 | // Indicates if the link has been marked as durable via annotations. |
| 469 | private boolean isDurable(Link link) { |
| 470 | return link != null && Objects.equals(link.annotations().value("durable"), "true"); |
| 471 | } |
| 472 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 473 | private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() { |
| 474 | return synchronizedSetMultimap(HashMultimap.<K, V>create()); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * @return primary ProviderID, or randomly chosen one if none exists |
| 479 | */ |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 480 | private static ProviderId pickBaseProviderId( |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 481 | Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 482 | |
| 483 | ProviderId fallBackPrimary = null; |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 484 | for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 485 | if (!e.getKey().isAncillary()) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 486 | // found primary |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 487 | return e.getKey(); |
| 488 | } else if (fallBackPrimary == null) { |
| 489 | // pick randomly as a fallback in case there is no primary |
| 490 | fallBackPrimary = e.getKey(); |
| 491 | } |
| 492 | } |
| 493 | return fallBackPrimary; |
| 494 | } |
| 495 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 496 | // Guarded by linkDescs value (=locking each Link) |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 497 | private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 498 | ProviderId baseProviderId = pickBaseProviderId(descs); |
| 499 | Timestamped<LinkDescription> base = descs.get(baseProviderId); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 500 | |
| 501 | ConnectPoint src = base.value().src(); |
| 502 | ConnectPoint dst = base.value().dst(); |
| 503 | Type type = base.value().type(); |
| 504 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 505 | annotations = merge(annotations, base.value().annotations()); |
| 506 | |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 507 | for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 508 | if (baseProviderId.equals(e.getKey())) { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 509 | continue; |
| 510 | } |
| 511 | |
| 512 | // TODO: should keep track of Description timestamp |
| 513 | // and only merge conflicting keys when timestamp is newer |
| 514 | // Currently assuming there will never be a key conflict between |
| 515 | // providers |
| 516 | |
| 517 | // annotation merging. not so efficient, should revisit later |
| 518 | annotations = merge(annotations, e.getValue().value().annotations()); |
| 519 | } |
| 520 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 521 | return new DefaultLink(baseProviderId, src, dst, type, annotations); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 524 | private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) { |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 525 | Map<ProviderId, Timestamped<LinkDescription>> r; |
| 526 | r = linkDescs.get(key); |
| 527 | if (r != null) { |
| 528 | return r; |
| 529 | } |
| 530 | r = new HashMap<>(); |
| 531 | final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded; |
| 532 | concurrentlyAdded = linkDescs.putIfAbsent(key, r); |
| 533 | if (concurrentlyAdded != null) { |
| 534 | return concurrentlyAdded; |
| 535 | } else { |
| 536 | return r; |
| 537 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | private final Function<LinkKey, Link> lookupLink = new LookupLink(); |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 541 | /** |
| 542 | * Returns a Function to lookup Link instance using LinkKey from cache. |
| 543 | * @return |
| 544 | */ |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 545 | private Function<LinkKey, Link> lookupLink() { |
| 546 | return lookupLink; |
| 547 | } |
| 548 | |
| 549 | private final class LookupLink implements Function<LinkKey, Link> { |
| 550 | @Override |
| 551 | public Link apply(LinkKey input) { |
Yuta HIGUCHI | 023295a | 2014-10-15 23:29:46 -0700 | [diff] [blame] | 552 | if (input == null) { |
| 553 | return null; |
| 554 | } else { |
| 555 | return links.get(input); |
| 556 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 560 | private void notifyDelegateIfNotNull(LinkEvent event) { |
| 561 | if (event != null) { |
| 562 | notifyDelegate(event); |
| 563 | } |
| 564 | } |
| 565 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 566 | private void broadcastMessage(MessageSubject subject, Object event) throws IOException { |
| 567 | ClusterMessage message = new ClusterMessage( |
| 568 | clusterService.getLocalNode().id(), |
| 569 | subject, |
| 570 | SERIALIZER.encode(event)); |
| 571 | clusterCommunicator.broadcast(message); |
| 572 | } |
| 573 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 574 | private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException { |
| 575 | ClusterMessage message = new ClusterMessage( |
| 576 | clusterService.getLocalNode().id(), |
| 577 | subject, |
| 578 | SERIALIZER.encode(event)); |
| 579 | clusterCommunicator.unicast(message, recipient); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 582 | private void notifyPeers(InternalLinkEvent event) throws IOException { |
| 583 | broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event); |
| 584 | } |
| 585 | |
| 586 | private void notifyPeers(InternalLinkRemovedEvent event) throws IOException { |
| 587 | broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event); |
| 588 | } |
| 589 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 590 | // notify peer, silently ignoring error |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 591 | private void notifyPeer(NodeId peer, InternalLinkEvent event) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 592 | try { |
| 593 | unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event); |
| 594 | } catch (IOException e) { |
| 595 | log.debug("Failed to notify peer {} with message {}", peer, event); |
| 596 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 599 | // notify peer, silently ignoring error |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 600 | private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 601 | try { |
| 602 | unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event); |
| 603 | } catch (IOException e) { |
| 604 | log.debug("Failed to notify peer {} with message {}", peer, event); |
| 605 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | private final class SendAdvertisementTask implements Runnable { |
| 609 | |
| 610 | @Override |
| 611 | public void run() { |
| 612 | if (Thread.currentThread().isInterrupted()) { |
| 613 | log.info("Interrupted, quitting"); |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | try { |
| 618 | final NodeId self = clusterService.getLocalNode().id(); |
| 619 | Set<ControllerNode> nodes = clusterService.getNodes(); |
| 620 | |
| 621 | ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes) |
| 622 | .transform(toNodeId()) |
| 623 | .toList(); |
| 624 | |
| 625 | if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) { |
Yuta HIGUCHI | 3708308 | 2014-10-13 10:38:38 -0700 | [diff] [blame] | 626 | log.debug("No other peers in the cluster."); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | |
| 630 | NodeId peer; |
| 631 | do { |
| 632 | int idx = RandomUtils.nextInt(0, nodeIds.size()); |
| 633 | peer = nodeIds.get(idx); |
| 634 | } while (peer.equals(self)); |
| 635 | |
| 636 | LinkAntiEntropyAdvertisement ad = createAdvertisement(); |
| 637 | |
| 638 | if (Thread.currentThread().isInterrupted()) { |
| 639 | log.info("Interrupted, quitting"); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | try { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 644 | unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad); |
| 645 | } catch (IOException e) { |
| 646 | log.debug("Failed to send anti-entropy advertisement to {}", peer); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | } catch (Exception e) { |
| 650 | // catch all Exception to avoid Scheduled task being suppressed. |
| 651 | log.error("Exception thrown while sending advertisement", e); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | private LinkAntiEntropyAdvertisement createAdvertisement() { |
| 657 | final NodeId self = clusterService.getLocalNode().id(); |
| 658 | |
| 659 | Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size()); |
| 660 | Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size()); |
| 661 | |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 662 | for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 663 | provs : linkDescs.entrySet()) { |
| 664 | |
| 665 | final LinkKey linkKey = provs.getKey(); |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 666 | final Map<ProviderId, Timestamped<LinkDescription>> linkDesc = provs.getValue(); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 667 | synchronized (linkDesc) { |
| 668 | for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) { |
| 669 | linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp()); |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | linkTombstones.putAll(removedLinks); |
| 675 | |
| 676 | return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones); |
| 677 | } |
| 678 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 679 | private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) { |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 680 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 681 | final NodeId sender = ad.sender(); |
| 682 | boolean localOutdated = false; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 683 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 684 | for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> |
| 685 | l : linkDescs.entrySet()) { |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 686 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 687 | final LinkKey key = l.getKey(); |
| 688 | final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue(); |
| 689 | synchronized (link) { |
| 690 | Timestamp localLatest = removedLinks.get(key); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 691 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 692 | for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) { |
| 693 | final ProviderId providerId = p.getKey(); |
| 694 | final Timestamped<LinkDescription> pDesc = p.getValue(); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 695 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 696 | final LinkFragmentId fragId = new LinkFragmentId(key, providerId); |
| 697 | // remote |
| 698 | Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId); |
| 699 | if (remoteTimestamp == null) { |
| 700 | remoteTimestamp = ad.linkTombstones().get(key); |
| 701 | } |
| 702 | if (remoteTimestamp == null || |
| 703 | pDesc.isNewer(remoteTimestamp)) { |
| 704 | // I have more recent link description. update peer. |
| 705 | notifyPeer(sender, new InternalLinkEvent(providerId, pDesc)); |
| 706 | } else { |
| 707 | final Timestamp remoteLive = ad.linkTimestamps().get(fragId); |
| 708 | if (remoteLive != null && |
| 709 | remoteLive.compareTo(pDesc.timestamp()) > 0) { |
| 710 | // I have something outdated |
| 711 | localOutdated = true; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | // search local latest along the way |
| 716 | if (localLatest == null || |
| 717 | pDesc.isNewer(localLatest)) { |
| 718 | localLatest = pDesc.timestamp(); |
| 719 | } |
| 720 | } |
| 721 | // Tests if remote remove is more recent then local latest. |
| 722 | final Timestamp remoteRemove = ad.linkTombstones().get(key); |
| 723 | if (remoteRemove != null) { |
| 724 | if (localLatest != null && |
| 725 | localLatest.compareTo(remoteRemove) < 0) { |
| 726 | // remote remove is more recent |
| 727 | notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove)); |
| 728 | } |
| 729 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 733 | // populate remove info if not known locally |
| 734 | for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) { |
| 735 | final LinkKey key = remoteRm.getKey(); |
| 736 | final Timestamp remoteRemove = remoteRm.getValue(); |
| 737 | // relying on removeLinkInternal to ignore stale info |
| 738 | notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove)); |
| 739 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 740 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 741 | if (localOutdated) { |
| 742 | // send back advertisement to speed up convergence |
| 743 | try { |
| 744 | unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT, |
| 745 | createAdvertisement()); |
| 746 | } catch (IOException e) { |
| 747 | log.debug("Failed to send back active advertisement"); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 752 | private class InternalLinkEventListener implements ClusterMessageHandler { |
| 753 | @Override |
| 754 | public void handle(ClusterMessage message) { |
| 755 | |
Yuta HIGUCHI | c01d2aa | 2014-10-19 01:19:34 -0700 | [diff] [blame] | 756 | log.trace("Received link event from peer: {}", message.sender()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 757 | InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload()); |
| 758 | |
| 759 | ProviderId providerId = event.providerId(); |
| 760 | Timestamped<LinkDescription> linkDescription = event.linkDescription(); |
| 761 | |
| 762 | notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription)); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | private class InternalLinkRemovedEventListener implements ClusterMessageHandler { |
| 767 | @Override |
| 768 | public void handle(ClusterMessage message) { |
| 769 | |
Yuta HIGUCHI | c01d2aa | 2014-10-19 01:19:34 -0700 | [diff] [blame] | 770 | log.trace("Received link removed event from peer: {}", message.sender()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 771 | InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload()); |
| 772 | |
| 773 | LinkKey linkKey = event.linkKey(); |
| 774 | Timestamp timestamp = event.timestamp(); |
| 775 | |
| 776 | notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp)); |
| 777 | } |
| 778 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 779 | |
| 780 | private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler { |
| 781 | |
| 782 | @Override |
| 783 | public void handle(ClusterMessage message) { |
Yuta HIGUCHI | 9a0a1d1 | 2014-10-13 22:38:02 -0700 | [diff] [blame] | 784 | log.debug("Received Link Anti-Entropy advertisement from peer: {}", message.sender()); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 785 | LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload()); |
| 786 | handleAntiEntropyAdvertisement(advertisement); |
| 787 | } |
| 788 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 789 | } |