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