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