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