blob: 3693d5953401fcb707f132823da41fdfc6d9691c [file] [log] [blame]
Madan Jampani2ff05592014-10-10 15:42:47 -07001package org.onlab.onos.store.link.impl;
2
3import com.google.common.base.Function;
Madan Jampani2ff05592014-10-10 15:42:47 -07004import com.google.common.collect.FluentIterable;
5import com.google.common.collect.HashMultimap;
Madan Jampania97e8202014-10-10 17:01:33 -07006import com.google.common.collect.ImmutableList;
Madan Jampani2ff05592014-10-10 15:42:47 -07007import com.google.common.collect.Maps;
8import com.google.common.collect.SetMultimap;
9
Madan Jampania97e8202014-10-10 17:01:33 -070010import org.apache.commons.lang3.RandomUtils;
Madan Jampani2ff05592014-10-10 15:42:47 -070011import org.apache.felix.scr.annotations.Activate;
12import org.apache.felix.scr.annotations.Component;
13import org.apache.felix.scr.annotations.Deactivate;
14import org.apache.felix.scr.annotations.Reference;
15import org.apache.felix.scr.annotations.ReferenceCardinality;
16import org.apache.felix.scr.annotations.Service;
17import org.onlab.onos.cluster.ClusterService;
Madan Jampania97e8202014-10-10 17:01:33 -070018import org.onlab.onos.cluster.ControllerNode;
19import org.onlab.onos.cluster.NodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070020import org.onlab.onos.net.AnnotationsUtil;
21import org.onlab.onos.net.ConnectPoint;
22import org.onlab.onos.net.DefaultAnnotations;
23import org.onlab.onos.net.DefaultLink;
24import org.onlab.onos.net.DeviceId;
25import org.onlab.onos.net.Link;
Madan Jampani2ff05592014-10-10 15:42:47 -070026import org.onlab.onos.net.Link.Type;
27import org.onlab.onos.net.LinkKey;
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -070028import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070029import org.onlab.onos.net.device.DeviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -070030import org.onlab.onos.net.link.DefaultLinkDescription;
31import org.onlab.onos.net.link.LinkDescription;
32import org.onlab.onos.net.link.LinkEvent;
33import org.onlab.onos.net.link.LinkStore;
34import org.onlab.onos.net.link.LinkStoreDelegate;
35import org.onlab.onos.net.provider.ProviderId;
36import org.onlab.onos.store.AbstractStore;
Madan Jampani2ff05592014-10-10 15:42:47 -070037import org.onlab.onos.store.Timestamp;
38import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
39import org.onlab.onos.store.cluster.messaging.ClusterMessage;
40import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler;
41import org.onlab.onos.store.cluster.messaging.MessageSubject;
Yuta HIGUCHIeecee552014-10-16 14:09:01 -070042import org.onlab.onos.store.impl.Timestamped;
Madan Jampani2ff05592014-10-10 15:42:47 -070043import org.onlab.onos.store.serializers.DistributedStoreSerializers;
44import org.onlab.onos.store.serializers.KryoSerializer;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070045import org.onlab.util.KryoNamespace;
Madan Jampani2ff05592014-10-10 15:42:47 -070046import org.slf4j.Logger;
47
48import java.io.IOException;
49import java.util.Collections;
Madan Jampania97e8202014-10-10 17:01:33 -070050import java.util.HashMap;
Madan Jampani2ff05592014-10-10 15:42:47 -070051import java.util.HashSet;
52import java.util.Map;
53import java.util.Set;
54import java.util.Map.Entry;
55import java.util.concurrent.ConcurrentHashMap;
56import java.util.concurrent.ConcurrentMap;
Madan Jampania97e8202014-10-10 17:01:33 -070057import java.util.concurrent.ScheduledExecutorService;
58import java.util.concurrent.TimeUnit;
Madan Jampani2ff05592014-10-10 15:42:47 -070059
Madan Jampania97e8202014-10-10 17:01:33 -070060import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
61import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070062import static org.onlab.onos.net.DefaultAnnotations.union;
63import static org.onlab.onos.net.DefaultAnnotations.merge;
64import static org.onlab.onos.net.Link.Type.DIRECT;
65import static org.onlab.onos.net.Link.Type.INDIRECT;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070066import static org.onlab.onos.net.LinkKey.linkKey;
Madan Jampani2ff05592014-10-10 15:42:47 -070067import static org.onlab.onos.net.link.LinkEvent.Type.*;
Madan Jampania97e8202014-10-10 17:01:33 -070068import static org.onlab.util.Tools.namedThreads;
Madan Jampani2ff05592014-10-10 15:42:47 -070069import static org.slf4j.LoggerFactory.getLogger;
70import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -070071import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampani2ff05592014-10-10 15:42:47 -070072import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -070073import static org.onlab.onos.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Madan Jampani2ff05592014-10-10 15:42:47 -070074
75/**
76 * Manages inventory of infrastructure links in distributed data store
77 * that uses optimistic replication and gossip based techniques.
78 */
79@Component(immediate = true)
80@Service
81public class GossipLinkStore
82 extends AbstractStore<LinkEvent, LinkStoreDelegate>
83 implements LinkStore {
84
85 private final Logger log = getLogger(getClass());
86
87 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -070088 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -070089 new ConcurrentHashMap<>();
90
91 // Link instance cache
92 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
93
94 // Egress and ingress link sets
95 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
96 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
97
98 // Remove links
99 private final Map<LinkKey, Timestamp> removedLinks = Maps.newHashMap();
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700102 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700103
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected ClusterCommunicationService clusterCommunicator;
106
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected ClusterService clusterService;
109
110 private static final KryoSerializer SERIALIZER = new KryoSerializer() {
111 @Override
112 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700113 serializerPool = KryoNamespace.newBuilder()
Madan Jampani2ff05592014-10-10 15:42:47 -0700114 .register(DistributedStoreSerializers.COMMON)
115 .register(InternalLinkEvent.class)
116 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700117 .register(LinkAntiEntropyAdvertisement.class)
118 .register(LinkFragmentId.class)
Madan Jampani2ff05592014-10-10 15:42:47 -0700119 .build()
120 .populate(1);
121 }
122 };
123
Madan Jampania97e8202014-10-10 17:01:33 -0700124 private ScheduledExecutorService executor;
125
Madan Jampani2ff05592014-10-10 15:42:47 -0700126 @Activate
127 public void activate() {
128
129 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700130 GossipLinkStoreMessageSubjects.LINK_UPDATE,
131 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700132 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700133 GossipLinkStoreMessageSubjects.LINK_REMOVED,
134 new InternalLinkRemovedEventListener());
135 clusterCommunicator.addSubscriber(
136 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
137 new InternalLinkAntiEntropyAdvertisementListener());
138
139 executor =
140 newSingleThreadScheduledExecutor(namedThreads("link-anti-entropy-%d"));
141
142 // TODO: Make these configurable
143 long initialDelaySec = 5;
144 long periodSec = 5;
145 // start anti-entropy thread
146 executor.scheduleAtFixedRate(new SendAdvertisementTask(),
147 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700148
149 log.info("Started");
150 }
151
152 @Deactivate
153 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700154
155 executor.shutdownNow();
156 try {
157 if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
158 log.error("Timeout during executor shutdown");
159 }
160 } catch (InterruptedException e) {
161 log.error("Error during executor shutdown", e);
162 }
163
Madan Jampani2ff05592014-10-10 15:42:47 -0700164 linkDescs.clear();
165 links.clear();
166 srcLinks.clear();
167 dstLinks.clear();
168 log.info("Stopped");
169 }
170
171 @Override
172 public int getLinkCount() {
173 return links.size();
174 }
175
176 @Override
177 public Iterable<Link> getLinks() {
178 return Collections.unmodifiableCollection(links.values());
179 }
180
181 @Override
182 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
183 // lock for iteration
184 synchronized (srcLinks) {
185 return FluentIterable.from(srcLinks.get(deviceId))
186 .transform(lookupLink())
187 .filter(notNull())
188 .toSet();
189 }
190 }
191
192 @Override
193 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
194 // lock for iteration
195 synchronized (dstLinks) {
196 return FluentIterable.from(dstLinks.get(deviceId))
197 .transform(lookupLink())
198 .filter(notNull())
199 .toSet();
200 }
201 }
202
203 @Override
204 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700205 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700206 }
207
208 @Override
209 public Set<Link> getEgressLinks(ConnectPoint src) {
210 Set<Link> egress = new HashSet<>();
211 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
212 if (linkKey.src().equals(src)) {
213 egress.add(links.get(linkKey));
214 }
215 }
216 return egress;
217 }
218
219 @Override
220 public Set<Link> getIngressLinks(ConnectPoint dst) {
221 Set<Link> ingress = new HashSet<>();
222 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
223 if (linkKey.dst().equals(dst)) {
224 ingress.add(links.get(linkKey));
225 }
226 }
227 return ingress;
228 }
229
230 @Override
231 public LinkEvent createOrUpdateLink(ProviderId providerId,
232 LinkDescription linkDescription) {
233
234 DeviceId dstDeviceId = linkDescription.dst().deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700235 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700236
237 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
238
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700239 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700240 final LinkEvent event;
241 final Timestamped<LinkDescription> mergedDesc;
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700242 synchronized (getOrCreateLinkDescriptions(key)) {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700243 event = createOrUpdateLinkInternal(providerId, deltaDesc);
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700244 mergedDesc = getOrCreateLinkDescriptions(key).get(providerId);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700245 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700246
247 if (event != null) {
248 log.info("Notifying peers of a link update topology event from providerId: "
249 + "{} between src: {} and dst: {}",
250 providerId, linkDescription.src(), linkDescription.dst());
251 try {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700252 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
Madan Jampani2ff05592014-10-10 15:42:47 -0700253 } catch (IOException e) {
254 log.info("Failed to notify peers of a link update topology event from providerId: "
255 + "{} between src: {} and dst: {}",
256 providerId, linkDescription.src(), linkDescription.dst());
257 }
258 }
259 return event;
260 }
261
262 private LinkEvent createOrUpdateLinkInternal(
263 ProviderId providerId,
264 Timestamped<LinkDescription> linkDescription) {
265
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700266 final LinkKey key = linkKey(linkDescription.value().src(),
267 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700268 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700269
270 synchronized (descs) {
271 // if the link was previously removed, we should proceed if and
272 // only if this request is more recent.
273 Timestamp linkRemovedTimestamp = removedLinks.get(key);
274 if (linkRemovedTimestamp != null) {
275 if (linkDescription.isNewer(linkRemovedTimestamp)) {
276 removedLinks.remove(key);
277 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700278 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700279 return null;
280 }
281 }
282
283 final Link oldLink = links.get(key);
284 // update description
285 createOrUpdateLinkDescription(descs, providerId, linkDescription);
286 final Link newLink = composeLink(descs);
287 if (oldLink == null) {
288 return createLink(key, newLink);
289 }
290 return updateLink(key, oldLink, newLink);
291 }
292 }
293
294 // Guarded by linkDescs value (=locking each Link)
295 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700296 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700297 ProviderId providerId,
298 Timestamped<LinkDescription> linkDescription) {
299
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700300 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700301 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700302 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700303 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700304 return null;
305 }
306 Timestamped<LinkDescription> newLinkDescription = linkDescription;
307 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700308 // we only allow transition from INDIRECT -> DIRECT
309 final Type newType;
310 if (existingLinkDescription.value().type() == DIRECT) {
311 newType = DIRECT;
312 } else {
313 newType = linkDescription.value().type();
314 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700315 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
316 linkDescription.value().annotations());
317 newLinkDescription = new Timestamped<LinkDescription>(
318 new DefaultLinkDescription(
319 linkDescription.value().src(),
320 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700321 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700322 linkDescription.timestamp());
323 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700324 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700325 }
326
327 // Creates and stores the link and returns the appropriate event.
328 // Guarded by linkDescs value (=locking each Link)
329 private LinkEvent createLink(LinkKey key, Link newLink) {
330
331 if (newLink.providerId().isAncillary()) {
332 // TODO: revisit ancillary only Link handling
333
334 // currently treating ancillary only as down (not visible outside)
335 return null;
336 }
337
338 links.put(key, newLink);
339 srcLinks.put(newLink.src().deviceId(), key);
340 dstLinks.put(newLink.dst().deviceId(), key);
341 return new LinkEvent(LINK_ADDED, newLink);
342 }
343
344 // Updates, if necessary the specified link and returns the appropriate event.
345 // Guarded by linkDescs value (=locking each Link)
346 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
347
348 if (newLink.providerId().isAncillary()) {
349 // TODO: revisit ancillary only Link handling
350
351 // currently treating ancillary only as down (not visible outside)
352 return null;
353 }
354
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700355 // Note: INDIRECT -> DIRECT transition only
356 // so that BDDP discovered Link will not overwrite LDDP Link
Madan Jampani2ff05592014-10-10 15:42:47 -0700357 if ((oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
358 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
359
360 links.put(key, newLink);
361 // strictly speaking following can be ommitted
362 srcLinks.put(oldLink.src().deviceId(), key);
363 dstLinks.put(oldLink.dst().deviceId(), key);
364 return new LinkEvent(LINK_UPDATED, newLink);
365 }
366 return null;
367 }
368
369 @Override
370 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700371 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700372
373 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700374 Timestamp timestamp = null;
375 try {
376 timestamp = deviceClockService.getTimestamp(dstDeviceId);
377 } catch (IllegalStateException e) {
378 //there are times when this is called before mastership
379 // handoff correctly completes.
380 return null;
381 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700382
383 LinkEvent event = removeLinkInternal(key, timestamp);
384
385 if (event != null) {
386 log.info("Notifying peers of a link removed topology event for a link "
387 + "between src: {} and dst: {}", src, dst);
388 try {
389 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
390 } catch (IOException e) {
391 log.error("Failed to notify peers of a link removed topology event for a link "
392 + "between src: {} and dst: {}", src, dst);
393 }
394 }
395 return event;
396 }
397
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700398 private static Timestamped<LinkDescription> getPrimaryDescription(
399 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
400
Madan Jampani2ff05592014-10-10 15:42:47 -0700401 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700402 for (Entry<ProviderId, Timestamped<LinkDescription>>
403 e : linkDescriptions.entrySet()) {
404
405 if (!e.getKey().isAncillary()) {
406 return e.getValue();
407 }
408 }
409 }
410 return null;
411 }
412
413
414 // TODO: consider slicing out as Timestamp utils
415 /**
416 * Checks is timestamp is more recent than timestamped object.
417 *
418 * @param timestamp to check if this is more recent then other
419 * @param timestamped object to be tested against
420 * @return true if {@code timestamp} is more recent than {@code timestamped}
421 * or {@code timestamped is null}
422 */
423 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
424 checkNotNull(timestamp);
425 if (timestamped == null) {
426 return true;
427 }
428 return timestamp.compareTo(timestamped.timestamp()) > 0;
429 }
430
431 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
432 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
433 = getOrCreateLinkDescriptions(key);
434
435 synchronized (linkDescriptions) {
436 if (linkDescriptions.isEmpty()) {
437 // never seen such link before. keeping timestamp for record
438 removedLinks.put(key, timestamp);
439 return null;
440 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700441 // accept removal request if given timestamp is newer than
442 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700443 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
444 if (!isMoreRecent(timestamp, prim)) {
445 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700446 return null;
447 }
448 removedLinks.put(key, timestamp);
449 Link link = links.remove(key);
450 linkDescriptions.clear();
451 if (link != null) {
452 srcLinks.remove(link.src().deviceId(), key);
453 dstLinks.remove(link.dst().deviceId(), key);
454 return new LinkEvent(LINK_REMOVED, link);
455 }
456 return null;
457 }
458 }
459
460 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
461 return synchronizedSetMultimap(HashMultimap.<K, V>create());
462 }
463
464 /**
465 * @return primary ProviderID, or randomly chosen one if none exists
466 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700467 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700468 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700469
470 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700471 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700472 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700473 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700474 return e.getKey();
475 } else if (fallBackPrimary == null) {
476 // pick randomly as a fallback in case there is no primary
477 fallBackPrimary = e.getKey();
478 }
479 }
480 return fallBackPrimary;
481 }
482
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700483 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700484 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700485 ProviderId baseProviderId = pickBaseProviderId(descs);
486 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700487
488 ConnectPoint src = base.value().src();
489 ConnectPoint dst = base.value().dst();
490 Type type = base.value().type();
491 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
492 annotations = merge(annotations, base.value().annotations());
493
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700494 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700495 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700496 continue;
497 }
498
499 // TODO: should keep track of Description timestamp
500 // and only merge conflicting keys when timestamp is newer
501 // Currently assuming there will never be a key conflict between
502 // providers
503
504 // annotation merging. not so efficient, should revisit later
505 annotations = merge(annotations, e.getValue().value().annotations());
506 }
507
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700508 return new DefaultLink(baseProviderId, src, dst, type, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700509 }
510
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700511 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700512 Map<ProviderId, Timestamped<LinkDescription>> r;
513 r = linkDescs.get(key);
514 if (r != null) {
515 return r;
516 }
517 r = new HashMap<>();
518 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
519 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
520 if (concurrentlyAdded != null) {
521 return concurrentlyAdded;
522 } else {
523 return r;
524 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700525 }
526
527 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700528 /**
529 * Returns a Function to lookup Link instance using LinkKey from cache.
530 * @return
531 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700532 private Function<LinkKey, Link> lookupLink() {
533 return lookupLink;
534 }
535
536 private final class LookupLink implements Function<LinkKey, Link> {
537 @Override
538 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700539 if (input == null) {
540 return null;
541 } else {
542 return links.get(input);
543 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700544 }
545 }
546
Madan Jampani2ff05592014-10-10 15:42:47 -0700547 private void notifyDelegateIfNotNull(LinkEvent event) {
548 if (event != null) {
549 notifyDelegate(event);
550 }
551 }
552
Madan Jampani2ff05592014-10-10 15:42:47 -0700553 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
554 ClusterMessage message = new ClusterMessage(
555 clusterService.getLocalNode().id(),
556 subject,
557 SERIALIZER.encode(event));
558 clusterCommunicator.broadcast(message);
559 }
560
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700561 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
562 ClusterMessage message = new ClusterMessage(
563 clusterService.getLocalNode().id(),
564 subject,
565 SERIALIZER.encode(event));
566 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700567 }
568
Madan Jampani2ff05592014-10-10 15:42:47 -0700569 private void notifyPeers(InternalLinkEvent event) throws IOException {
570 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
571 }
572
573 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
574 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
575 }
576
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700577 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700578 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700579 try {
580 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
581 } catch (IOException e) {
582 log.debug("Failed to notify peer {} with message {}", peer, event);
583 }
Madan Jampania97e8202014-10-10 17:01:33 -0700584 }
585
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700586 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700587 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700588 try {
589 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
590 } catch (IOException e) {
591 log.debug("Failed to notify peer {} with message {}", peer, event);
592 }
Madan Jampania97e8202014-10-10 17:01:33 -0700593 }
594
595 private final class SendAdvertisementTask implements Runnable {
596
597 @Override
598 public void run() {
599 if (Thread.currentThread().isInterrupted()) {
600 log.info("Interrupted, quitting");
601 return;
602 }
603
604 try {
605 final NodeId self = clusterService.getLocalNode().id();
606 Set<ControllerNode> nodes = clusterService.getNodes();
607
608 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
609 .transform(toNodeId())
610 .toList();
611
612 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHI37083082014-10-13 10:38:38 -0700613 log.debug("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700614 return;
615 }
616
617 NodeId peer;
618 do {
619 int idx = RandomUtils.nextInt(0, nodeIds.size());
620 peer = nodeIds.get(idx);
621 } while (peer.equals(self));
622
623 LinkAntiEntropyAdvertisement ad = createAdvertisement();
624
625 if (Thread.currentThread().isInterrupted()) {
626 log.info("Interrupted, quitting");
627 return;
628 }
629
630 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700631 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
632 } catch (IOException e) {
633 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700634 return;
635 }
636 } catch (Exception e) {
637 // catch all Exception to avoid Scheduled task being suppressed.
638 log.error("Exception thrown while sending advertisement", e);
639 }
640 }
641 }
642
643 private LinkAntiEntropyAdvertisement createAdvertisement() {
644 final NodeId self = clusterService.getLocalNode().id();
645
646 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
647 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
648
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700649 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
Madan Jampania97e8202014-10-10 17:01:33 -0700650 provs : linkDescs.entrySet()) {
651
652 final LinkKey linkKey = provs.getKey();
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700653 final Map<ProviderId, Timestamped<LinkDescription>> linkDesc = provs.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700654 synchronized (linkDesc) {
655 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
656 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
657 }
658 }
659 }
660
661 linkTombstones.putAll(removedLinks);
662
663 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
664 }
665
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700666 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700667
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700668 final NodeId sender = ad.sender();
669 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700670
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700671 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
672 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700673
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700674 final LinkKey key = l.getKey();
675 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
676 synchronized (link) {
677 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700678
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700679 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
680 final ProviderId providerId = p.getKey();
681 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700682
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700683 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
684 // remote
685 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
686 if (remoteTimestamp == null) {
687 remoteTimestamp = ad.linkTombstones().get(key);
688 }
689 if (remoteTimestamp == null ||
690 pDesc.isNewer(remoteTimestamp)) {
691 // I have more recent link description. update peer.
692 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
693 } else {
694 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
695 if (remoteLive != null &&
696 remoteLive.compareTo(pDesc.timestamp()) > 0) {
697 // I have something outdated
698 localOutdated = true;
699 }
700 }
701
702 // search local latest along the way
703 if (localLatest == null ||
704 pDesc.isNewer(localLatest)) {
705 localLatest = pDesc.timestamp();
706 }
707 }
708 // Tests if remote remove is more recent then local latest.
709 final Timestamp remoteRemove = ad.linkTombstones().get(key);
710 if (remoteRemove != null) {
711 if (localLatest != null &&
712 localLatest.compareTo(remoteRemove) < 0) {
713 // remote remove is more recent
714 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
715 }
716 }
Madan Jampania97e8202014-10-10 17:01:33 -0700717 }
718 }
719
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700720 // populate remove info if not known locally
721 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
722 final LinkKey key = remoteRm.getKey();
723 final Timestamp remoteRemove = remoteRm.getValue();
724 // relying on removeLinkInternal to ignore stale info
725 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
726 }
Madan Jampania97e8202014-10-10 17:01:33 -0700727
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700728 if (localOutdated) {
729 // send back advertisement to speed up convergence
730 try {
731 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
732 createAdvertisement());
733 } catch (IOException e) {
734 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700735 }
736 }
737 }
738
Madan Jampani2ff05592014-10-10 15:42:47 -0700739 private class InternalLinkEventListener implements ClusterMessageHandler {
740 @Override
741 public void handle(ClusterMessage message) {
742
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700743 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700744 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
745
746 ProviderId providerId = event.providerId();
747 Timestamped<LinkDescription> linkDescription = event.linkDescription();
748
749 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
750 }
751 }
752
753 private class InternalLinkRemovedEventListener implements ClusterMessageHandler {
754 @Override
755 public void handle(ClusterMessage message) {
756
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700757 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700758 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
759
760 LinkKey linkKey = event.linkKey();
761 Timestamp timestamp = event.timestamp();
762
763 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
764 }
765 }
Madan Jampania97e8202014-10-10 17:01:33 -0700766
767 private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler {
768
769 @Override
770 public void handle(ClusterMessage message) {
Yuta HIGUCHI9a0a1d12014-10-13 22:38:02 -0700771 log.debug("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700772 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
773 handleAntiEntropyAdvertisement(advertisement);
774 }
775 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700776}