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