blob: 60d54e3766812a3b64b4b28ccd6d916fe214f447 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.link.impl;
Madan Jampani2ff05592014-10-10 15:42:47 -070017
Jonathan Hart7d656f42015-01-27 14:07:23 -080018import com.google.common.base.Function;
19import com.google.common.collect.FluentIterable;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Multimaps;
22import com.google.common.collect.SetMultimap;
23import com.google.common.collect.Sets;
Madan Jampania97e8202014-10-10 17:01:33 -070024import org.apache.commons.lang3.RandomUtils;
Madan Jampani2ff05592014-10-10 15:42:47 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080031import org.onlab.util.KryoNamespace;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.cluster.ClusterService;
33import org.onosproject.cluster.ControllerNode;
34import org.onosproject.cluster.NodeId;
Marc De Leenheerb473b9d2015-02-06 15:21:03 -080035import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.AnnotationKeys;
37import org.onosproject.net.AnnotationsUtil;
38import org.onosproject.net.ConnectPoint;
39import org.onosproject.net.DefaultAnnotations;
40import org.onosproject.net.DefaultLink;
41import org.onosproject.net.DeviceId;
42import org.onosproject.net.Link;
43import org.onosproject.net.Link.Type;
44import org.onosproject.net.LinkKey;
45import org.onosproject.net.SparseAnnotations;
46import org.onosproject.net.device.DeviceClockService;
47import org.onosproject.net.link.DefaultLinkDescription;
48import org.onosproject.net.link.LinkDescription;
49import org.onosproject.net.link.LinkEvent;
50import org.onosproject.net.link.LinkStore;
51import org.onosproject.net.link.LinkStoreDelegate;
52import org.onosproject.net.provider.ProviderId;
53import org.onosproject.store.AbstractStore;
54import org.onosproject.store.Timestamp;
55import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
56import org.onosproject.store.cluster.messaging.ClusterMessage;
57import org.onosproject.store.cluster.messaging.ClusterMessageHandler;
58import org.onosproject.store.cluster.messaging.MessageSubject;
59import org.onosproject.store.impl.Timestamped;
60import org.onosproject.store.serializers.KryoSerializer;
61import org.onosproject.store.serializers.impl.DistributedStoreSerializers;
Madan Jampani2ff05592014-10-10 15:42:47 -070062import org.slf4j.Logger;
63
Jonathan Hart7d656f42015-01-27 14:07:23 -080064import java.io.IOException;
65import java.util.Collection;
66import java.util.Collections;
67import java.util.HashMap;
68import java.util.HashSet;
69import java.util.Map;
70import java.util.Map.Entry;
71import java.util.Objects;
72import java.util.Set;
73import java.util.concurrent.ConcurrentHashMap;
74import java.util.concurrent.ConcurrentMap;
75import java.util.concurrent.ExecutorService;
76import java.util.concurrent.Executors;
77import java.util.concurrent.ScheduledExecutorService;
78import java.util.concurrent.TimeUnit;
Madan Jampani2ff05592014-10-10 15:42:47 -070079
Thomas Vachuska57126fe2014-11-11 17:13:24 -080080import static com.google.common.base.Preconditions.checkNotNull;
81import static com.google.common.base.Predicates.notNull;
82import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Madan Jampania97e8202014-10-10 17:01:33 -070083import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080084import static org.onlab.util.Tools.groupedThreads;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080085import static org.onlab.util.Tools.minPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080086import static org.onosproject.cluster.ControllerNodeToNodeId.toNodeId;
87import static org.onosproject.net.DefaultAnnotations.merge;
88import static org.onosproject.net.DefaultAnnotations.union;
89import static org.onosproject.net.Link.State.ACTIVE;
90import static org.onosproject.net.Link.State.INACTIVE;
91import static org.onosproject.net.Link.Type.DIRECT;
92import static org.onosproject.net.Link.Type.INDIRECT;
93import static org.onosproject.net.LinkKey.linkKey;
Marc De Leenheerb473b9d2015-02-06 15:21:03 -080094import static org.onosproject.net.link.LinkEvent.Type.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080095import static org.onosproject.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Madan Jampani2ff05592014-10-10 15:42:47 -070096import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani2ff05592014-10-10 15:42:47 -070097
98/**
99 * Manages inventory of infrastructure links in distributed data store
100 * that uses optimistic replication and gossip based techniques.
101 */
102@Component(immediate = true)
103@Service
104public class GossipLinkStore
105 extends AbstractStore<LinkEvent, LinkStoreDelegate>
106 implements LinkStore {
107
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800108 // Timeout in milliseconds to process links on remote master node
109 private static final int REMOTE_MASTER_TIMEOUT = 1000;
110
Madan Jampani2ff05592014-10-10 15:42:47 -0700111 private final Logger log = getLogger(getClass());
112
113 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700114 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700115 new ConcurrentHashMap<>();
116
117 // Link instance cache
118 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
119
120 // Egress and ingress link sets
121 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
122 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
123
124 // Remove links
Yuta HIGUCHIb9125562014-12-01 23:28:22 -0800125 private final Map<LinkKey, Timestamp> removedLinks = new ConcurrentHashMap<>();
Madan Jampani2ff05592014-10-10 15:42:47 -0700126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700128 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 protected ClusterCommunicationService clusterCommunicator;
132
133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
134 protected ClusterService clusterService;
135
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
137 protected MastershipService mastershipService;
138
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800139 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700140 @Override
141 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700142 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800143 .register(DistributedStoreSerializers.STORE_COMMON)
144 .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
Madan Jampani2ff05592014-10-10 15:42:47 -0700145 .register(InternalLinkEvent.class)
146 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700147 .register(LinkAntiEntropyAdvertisement.class)
148 .register(LinkFragmentId.class)
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800149 .register(LinkInjectedEvent.class)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800150 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700151 }
152 };
153
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800154 private ExecutorService executor;
155
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800156 private ScheduledExecutorService backgroundExecutors;
Madan Jampania97e8202014-10-10 17:01:33 -0700157
Madan Jampani2ff05592014-10-10 15:42:47 -0700158 @Activate
159 public void activate() {
160
161 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700162 GossipLinkStoreMessageSubjects.LINK_UPDATE,
163 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700164 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700165 GossipLinkStoreMessageSubjects.LINK_REMOVED,
166 new InternalLinkRemovedEventListener());
167 clusterCommunicator.addSubscriber(
168 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
169 new InternalLinkAntiEntropyAdvertisementListener());
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800170 clusterCommunicator.addSubscriber(
171 GossipLinkStoreMessageSubjects.LINK_INJECTED,
172 new LinkInjectedEventListener());
Madan Jampania97e8202014-10-10 17:01:33 -0700173
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800174 executor = Executors.newCachedThreadPool(groupedThreads("onos/link", "fg-%d"));
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800175
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800176 backgroundExecutors =
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800177 newSingleThreadScheduledExecutor(minPriority(groupedThreads("onos/link", "bg-%d")));
Madan Jampania97e8202014-10-10 17:01:33 -0700178
Madan Jampania97e8202014-10-10 17:01:33 -0700179 long initialDelaySec = 5;
180 long periodSec = 5;
181 // start anti-entropy thread
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800182 backgroundExecutors.scheduleAtFixedRate(new SendAdvertisementTask(),
Madan Jampania97e8202014-10-10 17:01:33 -0700183 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700184
185 log.info("Started");
186 }
187
188 @Deactivate
189 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700190
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800191 executor.shutdownNow();
192
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800193 backgroundExecutors.shutdownNow();
Madan Jampani3ffbb272014-10-13 11:19:37 -0700194 try {
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800195 if (!backgroundExecutors.awaitTermination(5, TimeUnit.SECONDS)) {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700196 log.error("Timeout during executor shutdown");
197 }
198 } catch (InterruptedException e) {
199 log.error("Error during executor shutdown", e);
200 }
201
Madan Jampani2ff05592014-10-10 15:42:47 -0700202 linkDescs.clear();
203 links.clear();
204 srcLinks.clear();
205 dstLinks.clear();
206 log.info("Stopped");
207 }
208
209 @Override
210 public int getLinkCount() {
211 return links.size();
212 }
213
214 @Override
215 public Iterable<Link> getLinks() {
216 return Collections.unmodifiableCollection(links.values());
217 }
218
219 @Override
220 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
221 // lock for iteration
222 synchronized (srcLinks) {
223 return FluentIterable.from(srcLinks.get(deviceId))
224 .transform(lookupLink())
225 .filter(notNull())
226 .toSet();
227 }
228 }
229
230 @Override
231 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
232 // lock for iteration
233 synchronized (dstLinks) {
234 return FluentIterable.from(dstLinks.get(deviceId))
235 .transform(lookupLink())
236 .filter(notNull())
237 .toSet();
238 }
239 }
240
241 @Override
242 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700243 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700244 }
245
246 @Override
247 public Set<Link> getEgressLinks(ConnectPoint src) {
248 Set<Link> egress = new HashSet<>();
249 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
250 if (linkKey.src().equals(src)) {
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800251 Link link = links.get(linkKey);
252 if (link != null) {
253 egress.add(link);
254 } else {
255 log.debug("Egress link for {} was null, skipped", linkKey);
256 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700257 }
258 }
259 return egress;
260 }
261
262 @Override
263 public Set<Link> getIngressLinks(ConnectPoint dst) {
264 Set<Link> ingress = new HashSet<>();
265 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
266 if (linkKey.dst().equals(dst)) {
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800267 Link link = links.get(linkKey);
268 if (link != null) {
269 ingress.add(link);
270 } else {
271 log.debug("Ingress link for {} was null, skipped", linkKey);
272 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700273 }
274 }
275 return ingress;
276 }
277
278 @Override
279 public LinkEvent createOrUpdateLink(ProviderId providerId,
280 LinkDescription linkDescription) {
281
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800282 final DeviceId dstDeviceId = linkDescription.dst().deviceId();
283 final NodeId localNode = clusterService.getLocalNode().id();
284 final NodeId dstNode = mastershipService.getMasterFor(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700285
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800286 // Process link update only if we're the master of the destination node,
287 // otherwise signal the actual master.
288 LinkEvent linkEvent = null;
289 if (localNode.equals(dstNode)) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700290
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800291 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
292
293 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
294
295 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
296 final Timestamped<LinkDescription> mergedDesc;
297 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
298
299 synchronized (map) {
300 linkEvent = createOrUpdateLinkInternal(providerId, deltaDesc);
301 mergedDesc = map.get(providerId);
302 }
303
304 if (linkEvent != null) {
305 log.info("Notifying peers of a link update topology event from providerId: "
306 + "{} between src: {} and dst: {}",
307 providerId, linkDescription.src(), linkDescription.dst());
308 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
309 }
310
311 } else {
312
313 LinkInjectedEvent linkInjectedEvent = new LinkInjectedEvent(providerId, linkDescription);
314 ClusterMessage linkInjectedMessage = new ClusterMessage(localNode,
315 GossipLinkStoreMessageSubjects.LINK_INJECTED, SERIALIZER.encode(linkInjectedEvent));
316
317 try {
318 clusterCommunicator.unicast(linkInjectedMessage, dstNode);
319 } catch (IOException e) {
320 log.warn("Failed to process link update between src: {} and dst: {} " +
321 "(cluster messaging failed: {})",
322 linkDescription.src(), linkDescription.dst(), e);
323 }
324
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700325 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700326
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800327 return linkEvent;
Madan Jampani2ff05592014-10-10 15:42:47 -0700328 }
329
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800330 @Override
331 public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) {
332 Link link = getLink(src, dst);
333 if (link == null) {
334 return null;
335 }
336
337 if (link.isDurable()) {
338 // FIXME: this is not the right thing to call for the gossip store; will not sync link state!!!
339 return link.state() == INACTIVE ? null :
340 updateLink(linkKey(link.src(), link.dst()), link,
341 new DefaultLink(link.providerId(),
342 link.src(), link.dst(),
343 link.type(), INACTIVE,
344 link.isDurable(),
345 link.annotations()));
346 }
347 return removeLink(src, dst);
348 }
349
Madan Jampani2ff05592014-10-10 15:42:47 -0700350 private LinkEvent createOrUpdateLinkInternal(
351 ProviderId providerId,
352 Timestamped<LinkDescription> linkDescription) {
353
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700354 final LinkKey key = linkKey(linkDescription.value().src(),
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800355 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700356 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700357
358 synchronized (descs) {
359 // if the link was previously removed, we should proceed if and
360 // only if this request is more recent.
361 Timestamp linkRemovedTimestamp = removedLinks.get(key);
362 if (linkRemovedTimestamp != null) {
363 if (linkDescription.isNewer(linkRemovedTimestamp)) {
364 removedLinks.remove(key);
365 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700366 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700367 return null;
368 }
369 }
370
371 final Link oldLink = links.get(key);
372 // update description
373 createOrUpdateLinkDescription(descs, providerId, linkDescription);
374 final Link newLink = composeLink(descs);
375 if (oldLink == null) {
376 return createLink(key, newLink);
377 }
378 return updateLink(key, oldLink, newLink);
379 }
380 }
381
382 // Guarded by linkDescs value (=locking each Link)
383 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700384 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700385 ProviderId providerId,
386 Timestamped<LinkDescription> linkDescription) {
387
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700388 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700389 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700390 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700391 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700392 return null;
393 }
394 Timestamped<LinkDescription> newLinkDescription = linkDescription;
395 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700396 // we only allow transition from INDIRECT -> DIRECT
397 final Type newType;
398 if (existingLinkDescription.value().type() == DIRECT) {
399 newType = DIRECT;
400 } else {
401 newType = linkDescription.value().type();
402 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700403 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
404 linkDescription.value().annotations());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800405 newLinkDescription = new Timestamped<>(
Madan Jampani2ff05592014-10-10 15:42:47 -0700406 new DefaultLinkDescription(
407 linkDescription.value().src(),
408 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700409 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700410 linkDescription.timestamp());
411 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700412 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700413 }
414
415 // Creates and stores the link and returns the appropriate event.
416 // Guarded by linkDescs value (=locking each Link)
417 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700418 links.put(key, newLink);
419 srcLinks.put(newLink.src().deviceId(), key);
420 dstLinks.put(newLink.dst().deviceId(), key);
421 return new LinkEvent(LINK_ADDED, newLink);
422 }
423
424 // Updates, if necessary the specified link and returns the appropriate event.
425 // Guarded by linkDescs value (=locking each Link)
426 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700427 // Note: INDIRECT -> DIRECT transition only
428 // so that BDDP discovered Link will not overwrite LDDP Link
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800429 if (oldLink.state() != newLink.state() ||
430 (oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
Madan Jampani2ff05592014-10-10 15:42:47 -0700431 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
432
433 links.put(key, newLink);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800434 // strictly speaking following can be omitted
Madan Jampani2ff05592014-10-10 15:42:47 -0700435 srcLinks.put(oldLink.src().deviceId(), key);
436 dstLinks.put(oldLink.dst().deviceId(), key);
437 return new LinkEvent(LINK_UPDATED, newLink);
438 }
439 return null;
440 }
441
442 @Override
443 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700444 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700445
446 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700447 Timestamp timestamp = null;
448 try {
449 timestamp = deviceClockService.getTimestamp(dstDeviceId);
450 } catch (IllegalStateException e) {
Yuta HIGUCHId6a0ac32014-11-04 09:26:31 -0800451 log.warn("Failed to remove link {}, was not the master", key);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700452 //there are times when this is called before mastership
453 // handoff correctly completes.
454 return null;
455 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700456
457 LinkEvent event = removeLinkInternal(key, timestamp);
458
459 if (event != null) {
460 log.info("Notifying peers of a link removed topology event for a link "
461 + "between src: {} and dst: {}", src, dst);
Jonathan Hart7d656f42015-01-27 14:07:23 -0800462 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
Madan Jampani2ff05592014-10-10 15:42:47 -0700463 }
464 return event;
465 }
466
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700467 private static Timestamped<LinkDescription> getPrimaryDescription(
468 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
469
Madan Jampani2ff05592014-10-10 15:42:47 -0700470 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700471 for (Entry<ProviderId, Timestamped<LinkDescription>>
472 e : linkDescriptions.entrySet()) {
473
474 if (!e.getKey().isAncillary()) {
475 return e.getValue();
476 }
477 }
478 }
479 return null;
480 }
481
482
483 // TODO: consider slicing out as Timestamp utils
484 /**
485 * Checks is timestamp is more recent than timestamped object.
486 *
487 * @param timestamp to check if this is more recent then other
488 * @param timestamped object to be tested against
489 * @return true if {@code timestamp} is more recent than {@code timestamped}
490 * or {@code timestamped is null}
491 */
492 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
493 checkNotNull(timestamp);
494 if (timestamped == null) {
495 return true;
496 }
497 return timestamp.compareTo(timestamped.timestamp()) > 0;
498 }
499
500 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
501 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
502 = getOrCreateLinkDescriptions(key);
503
504 synchronized (linkDescriptions) {
505 if (linkDescriptions.isEmpty()) {
506 // never seen such link before. keeping timestamp for record
507 removedLinks.put(key, timestamp);
508 return null;
509 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700510 // accept removal request if given timestamp is newer than
511 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700512 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
513 if (!isMoreRecent(timestamp, prim)) {
514 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700515 return null;
516 }
517 removedLinks.put(key, timestamp);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800518 Link link = links.remove(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700519 linkDescriptions.clear();
520 if (link != null) {
521 srcLinks.remove(link.src().deviceId(), key);
522 dstLinks.remove(link.dst().deviceId(), key);
523 return new LinkEvent(LINK_REMOVED, link);
524 }
525 return null;
526 }
527 }
528
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800529 /**
530 * Creates concurrent readable, synchronized HashMultimap.
531 *
532 * @return SetMultimap
533 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700534 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800535 return synchronizedSetMultimap(
536 Multimaps.newSetMultimap(new ConcurrentHashMap<K, Collection<V>>(),
537 () -> Sets.newConcurrentHashSet()));
Madan Jampani2ff05592014-10-10 15:42:47 -0700538 }
539
540 /**
541 * @return primary ProviderID, or randomly chosen one if none exists
542 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700543 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700544 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700545
546 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700547 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700548 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700549 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700550 return e.getKey();
551 } else if (fallBackPrimary == null) {
552 // pick randomly as a fallback in case there is no primary
553 fallBackPrimary = e.getKey();
554 }
555 }
556 return fallBackPrimary;
557 }
558
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700559 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700560 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700561 ProviderId baseProviderId = pickBaseProviderId(descs);
562 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700563
564 ConnectPoint src = base.value().src();
565 ConnectPoint dst = base.value().dst();
566 Type type = base.value().type();
567 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
568 annotations = merge(annotations, base.value().annotations());
569
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700570 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700571 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700572 continue;
573 }
574
Yuta HIGUCHI65934892014-12-04 17:47:44 -0800575 // Note: In the long run we should keep track of Description timestamp
Madan Jampani2ff05592014-10-10 15:42:47 -0700576 // and only merge conflicting keys when timestamp is newer
577 // Currently assuming there will never be a key conflict between
578 // providers
579
580 // annotation merging. not so efficient, should revisit later
581 annotations = merge(annotations, e.getValue().value().annotations());
582 }
583
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800584 boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800585 return new DefaultLink(baseProviderId, src, dst, type, ACTIVE, isDurable, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700586 }
587
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700588 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700589 Map<ProviderId, Timestamped<LinkDescription>> r;
590 r = linkDescs.get(key);
591 if (r != null) {
592 return r;
593 }
594 r = new HashMap<>();
595 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
596 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
597 if (concurrentlyAdded != null) {
598 return concurrentlyAdded;
599 } else {
600 return r;
601 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700602 }
603
604 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800605
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700606 /**
607 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800608 *
609 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700610 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700611 private Function<LinkKey, Link> lookupLink() {
612 return lookupLink;
613 }
614
615 private final class LookupLink implements Function<LinkKey, Link> {
616 @Override
617 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700618 if (input == null) {
619 return null;
620 } else {
621 return links.get(input);
622 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700623 }
624 }
625
Madan Jampani2ff05592014-10-10 15:42:47 -0700626 private void notifyDelegateIfNotNull(LinkEvent event) {
627 if (event != null) {
628 notifyDelegate(event);
629 }
630 }
631
Jonathan Hart7d656f42015-01-27 14:07:23 -0800632 private void broadcastMessage(MessageSubject subject, Object event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700633 ClusterMessage message = new ClusterMessage(
634 clusterService.getLocalNode().id(),
635 subject,
636 SERIALIZER.encode(event));
637 clusterCommunicator.broadcast(message);
638 }
639
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700640 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
641 ClusterMessage message = new ClusterMessage(
642 clusterService.getLocalNode().id(),
643 subject,
644 SERIALIZER.encode(event));
645 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700646 }
647
Jonathan Hart7d656f42015-01-27 14:07:23 -0800648 private void notifyPeers(InternalLinkEvent event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700649 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
650 }
651
Jonathan Hart7d656f42015-01-27 14:07:23 -0800652 private void notifyPeers(InternalLinkRemovedEvent event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700653 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
654 }
655
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700656 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700657 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700658 try {
659 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
660 } catch (IOException e) {
661 log.debug("Failed to notify peer {} with message {}", peer, event);
662 }
Madan Jampania97e8202014-10-10 17:01:33 -0700663 }
664
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700665 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700666 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700667 try {
668 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
669 } catch (IOException e) {
670 log.debug("Failed to notify peer {} with message {}", peer, event);
671 }
Madan Jampania97e8202014-10-10 17:01:33 -0700672 }
673
674 private final class SendAdvertisementTask implements Runnable {
675
676 @Override
677 public void run() {
678 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800679 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700680 return;
681 }
682
683 try {
684 final NodeId self = clusterService.getLocalNode().id();
685 Set<ControllerNode> nodes = clusterService.getNodes();
686
687 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
688 .transform(toNodeId())
689 .toList();
690
691 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800692 log.trace("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700693 return;
694 }
695
696 NodeId peer;
697 do {
698 int idx = RandomUtils.nextInt(0, nodeIds.size());
699 peer = nodeIds.get(idx);
700 } while (peer.equals(self));
701
702 LinkAntiEntropyAdvertisement ad = createAdvertisement();
703
704 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800705 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700706 return;
707 }
708
709 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700710 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
711 } catch (IOException e) {
712 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700713 return;
714 }
715 } catch (Exception e) {
716 // catch all Exception to avoid Scheduled task being suppressed.
717 log.error("Exception thrown while sending advertisement", e);
718 }
719 }
720 }
721
722 private LinkAntiEntropyAdvertisement createAdvertisement() {
723 final NodeId self = clusterService.getLocalNode().id();
724
725 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
726 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
727
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800728 linkDescs.forEach((linkKey, linkDesc) -> {
Madan Jampania97e8202014-10-10 17:01:33 -0700729 synchronized (linkDesc) {
730 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
731 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
732 }
733 }
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800734 });
Madan Jampania97e8202014-10-10 17:01:33 -0700735
736 linkTombstones.putAll(removedLinks);
737
738 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
739 }
740
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700741 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700742
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700743 final NodeId sender = ad.sender();
744 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700745
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700746 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
747 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700748
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700749 final LinkKey key = l.getKey();
750 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
751 synchronized (link) {
752 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700753
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700754 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
755 final ProviderId providerId = p.getKey();
756 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700757
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700758 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
759 // remote
760 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
761 if (remoteTimestamp == null) {
762 remoteTimestamp = ad.linkTombstones().get(key);
763 }
764 if (remoteTimestamp == null ||
765 pDesc.isNewer(remoteTimestamp)) {
766 // I have more recent link description. update peer.
767 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
768 } else {
769 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
770 if (remoteLive != null &&
771 remoteLive.compareTo(pDesc.timestamp()) > 0) {
772 // I have something outdated
773 localOutdated = true;
774 }
775 }
776
777 // search local latest along the way
778 if (localLatest == null ||
779 pDesc.isNewer(localLatest)) {
780 localLatest = pDesc.timestamp();
781 }
782 }
783 // Tests if remote remove is more recent then local latest.
784 final Timestamp remoteRemove = ad.linkTombstones().get(key);
785 if (remoteRemove != null) {
786 if (localLatest != null &&
787 localLatest.compareTo(remoteRemove) < 0) {
788 // remote remove is more recent
789 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
790 }
791 }
Madan Jampania97e8202014-10-10 17:01:33 -0700792 }
793 }
794
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700795 // populate remove info if not known locally
796 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
797 final LinkKey key = remoteRm.getKey();
798 final Timestamp remoteRemove = remoteRm.getValue();
799 // relying on removeLinkInternal to ignore stale info
800 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
801 }
Madan Jampania97e8202014-10-10 17:01:33 -0700802
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700803 if (localOutdated) {
804 // send back advertisement to speed up convergence
805 try {
806 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
807 createAdvertisement());
808 } catch (IOException e) {
809 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700810 }
811 }
812 }
813
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800814 private final class InternalLinkEventListener
815 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700816 @Override
817 public void handle(ClusterMessage message) {
818
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700819 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700820 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
821
822 ProviderId providerId = event.providerId();
823 Timestamped<LinkDescription> linkDescription = event.linkDescription();
824
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800825 executor.submit(new Runnable() {
826
827 @Override
828 public void run() {
829 try {
830 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
831 } catch (Exception e) {
832 log.warn("Exception thrown handling link event", e);
833 }
834 }
835 });
Madan Jampani2ff05592014-10-10 15:42:47 -0700836 }
837 }
838
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800839 private final class InternalLinkRemovedEventListener
840 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700841 @Override
842 public void handle(ClusterMessage message) {
843
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700844 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700845 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
846
847 LinkKey linkKey = event.linkKey();
848 Timestamp timestamp = event.timestamp();
849
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800850 executor.submit(new Runnable() {
851
852 @Override
853 public void run() {
854 try {
855 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
856 } catch (Exception e) {
857 log.warn("Exception thrown handling link removed", e);
858 }
859 }
860 });
Madan Jampani2ff05592014-10-10 15:42:47 -0700861 }
862 }
Madan Jampania97e8202014-10-10 17:01:33 -0700863
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800864 private final class InternalLinkAntiEntropyAdvertisementListener
865 implements ClusterMessageHandler {
Madan Jampania97e8202014-10-10 17:01:33 -0700866
867 @Override
868 public void handle(ClusterMessage message) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800869 log.trace("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700870 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800871 backgroundExecutors.submit(new Runnable() {
872
873 @Override
874 public void run() {
875 try {
876 handleAntiEntropyAdvertisement(advertisement);
877 } catch (Exception e) {
878 log.warn("Exception thrown while handling Link advertisements", e);
879 throw e;
880 }
881 }
882 });
Madan Jampania97e8202014-10-10 17:01:33 -0700883 }
884 }
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800885
886 private final class LinkInjectedEventListener
887 implements ClusterMessageHandler {
888 @Override
889 public void handle(ClusterMessage message) {
890
891 log.trace("Received injected link event from peer: {}", message.sender());
892 LinkInjectedEvent linkInjectedEvent = SERIALIZER.decode(message.payload());
893
894 ProviderId providerId = linkInjectedEvent.providerId();
895 LinkDescription linkDescription = linkInjectedEvent.linkDescription();
896
897 executor.submit(new Runnable() {
898
899 @Override
900 public void run() {
901 createOrUpdateLink(providerId, linkDescription);
902 }
903 });
904 }
905 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700906}