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