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