blob: 4cb936ea0bde437add102cf9a355566aeef70f2d [file] [log] [blame]
Madan Jampani2ff05592014-10-10 15:42:47 -07001package org.onlab.onos.store.link.impl;
2
3import com.google.common.base.Function;
4import com.google.common.base.Predicate;
5import com.google.common.collect.FluentIterable;
6import com.google.common.collect.HashMultimap;
Madan Jampania97e8202014-10-10 17:01:33 -07007import com.google.common.collect.ImmutableList;
Madan Jampani2ff05592014-10-10 15:42:47 -07008import com.google.common.collect.Maps;
9import com.google.common.collect.SetMultimap;
10
Madan Jampania97e8202014-10-10 17:01:33 -070011import org.apache.commons.lang3.RandomUtils;
Madan Jampani2ff05592014-10-10 15:42:47 -070012import org.apache.commons.lang3.concurrent.ConcurrentUtils;
13import org.apache.felix.scr.annotations.Activate;
14import org.apache.felix.scr.annotations.Component;
15import org.apache.felix.scr.annotations.Deactivate;
16import org.apache.felix.scr.annotations.Reference;
17import org.apache.felix.scr.annotations.ReferenceCardinality;
18import org.apache.felix.scr.annotations.Service;
19import org.onlab.onos.cluster.ClusterService;
Madan Jampania97e8202014-10-10 17:01:33 -070020import org.onlab.onos.cluster.ControllerNode;
21import org.onlab.onos.cluster.NodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070022import org.onlab.onos.net.AnnotationsUtil;
23import org.onlab.onos.net.ConnectPoint;
24import org.onlab.onos.net.DefaultAnnotations;
25import org.onlab.onos.net.DefaultLink;
26import org.onlab.onos.net.DeviceId;
27import org.onlab.onos.net.Link;
28import org.onlab.onos.net.SparseAnnotations;
29import org.onlab.onos.net.Link.Type;
30import org.onlab.onos.net.LinkKey;
31import org.onlab.onos.net.Provided;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070032import org.onlab.onos.net.device.DeviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -070033import org.onlab.onos.net.link.DefaultLinkDescription;
34import org.onlab.onos.net.link.LinkDescription;
35import org.onlab.onos.net.link.LinkEvent;
36import org.onlab.onos.net.link.LinkStore;
37import org.onlab.onos.net.link.LinkStoreDelegate;
38import org.onlab.onos.net.provider.ProviderId;
39import org.onlab.onos.store.AbstractStore;
Madan Jampani2ff05592014-10-10 15:42:47 -070040import org.onlab.onos.store.Timestamp;
41import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
42import org.onlab.onos.store.cluster.messaging.ClusterMessage;
43import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler;
44import org.onlab.onos.store.cluster.messaging.MessageSubject;
45import org.onlab.onos.store.common.impl.Timestamped;
46import org.onlab.onos.store.serializers.DistributedStoreSerializers;
47import org.onlab.onos.store.serializers.KryoSerializer;
48import org.onlab.util.KryoPool;
49import org.onlab.util.NewConcurrentHashMap;
50import org.slf4j.Logger;
51
52import java.io.IOException;
53import java.util.Collections;
Madan Jampania97e8202014-10-10 17:01:33 -070054import java.util.HashMap;
Madan Jampani2ff05592014-10-10 15:42:47 -070055import java.util.HashSet;
56import java.util.Map;
57import java.util.Set;
58import java.util.Map.Entry;
59import java.util.concurrent.ConcurrentHashMap;
60import java.util.concurrent.ConcurrentMap;
Madan Jampania97e8202014-10-10 17:01:33 -070061import java.util.concurrent.ScheduledExecutorService;
62import java.util.concurrent.TimeUnit;
Madan Jampani2ff05592014-10-10 15:42:47 -070063
Madan Jampania97e8202014-10-10 17:01:33 -070064import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
65import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070066import static org.onlab.onos.net.DefaultAnnotations.union;
67import static org.onlab.onos.net.DefaultAnnotations.merge;
68import static org.onlab.onos.net.Link.Type.DIRECT;
69import static org.onlab.onos.net.Link.Type.INDIRECT;
70import static org.onlab.onos.net.link.LinkEvent.Type.*;
Madan Jampania97e8202014-10-10 17:01:33 -070071import static org.onlab.util.Tools.namedThreads;
Madan Jampani2ff05592014-10-10 15:42:47 -070072import static org.slf4j.LoggerFactory.getLogger;
73import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
74import static com.google.common.base.Predicates.notNull;
75
76/**
77 * Manages inventory of infrastructure links in distributed data store
78 * that uses optimistic replication and gossip based techniques.
79 */
80@Component(immediate = true)
81@Service
82public class GossipLinkStore
83 extends AbstractStore<LinkEvent, LinkStoreDelegate>
84 implements LinkStore {
85
86 private final Logger log = getLogger(getClass());
87
88 // Link inventory
89 private final ConcurrentMap<LinkKey, ConcurrentMap<ProviderId, Timestamped<LinkDescription>>> linkDescs =
90 new ConcurrentHashMap<>();
91
92 // Link instance cache
93 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
94
95 // Egress and ingress link sets
96 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
97 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
98
99 // Remove links
100 private final Map<LinkKey, Timestamp> removedLinks = Maps.newHashMap();
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700103 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected ClusterCommunicationService clusterCommunicator;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected ClusterService clusterService;
110
111 private static final KryoSerializer SERIALIZER = new KryoSerializer() {
112 @Override
113 protected void setupKryoPool() {
114 serializerPool = KryoPool.newBuilder()
115 .register(DistributedStoreSerializers.COMMON)
116 .register(InternalLinkEvent.class)
117 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700118 .register(LinkAntiEntropyAdvertisement.class)
119 .register(LinkFragmentId.class)
Madan Jampani2ff05592014-10-10 15:42:47 -0700120 .build()
121 .populate(1);
122 }
123 };
124
Madan Jampania97e8202014-10-10 17:01:33 -0700125 private ScheduledExecutorService executor;
126
Madan Jampani2ff05592014-10-10 15:42:47 -0700127 @Activate
128 public void activate() {
129
130 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700131 GossipLinkStoreMessageSubjects.LINK_UPDATE,
132 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700133 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700134 GossipLinkStoreMessageSubjects.LINK_REMOVED,
135 new InternalLinkRemovedEventListener());
136 clusterCommunicator.addSubscriber(
137 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
138 new InternalLinkAntiEntropyAdvertisementListener());
139
140 executor =
141 newSingleThreadScheduledExecutor(namedThreads("link-anti-entropy-%d"));
142
143 // TODO: Make these configurable
144 long initialDelaySec = 5;
145 long periodSec = 5;
146 // start anti-entropy thread
147 executor.scheduleAtFixedRate(new SendAdvertisementTask(),
148 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700149
150 log.info("Started");
151 }
152
153 @Deactivate
154 public void deactivate() {
155 linkDescs.clear();
156 links.clear();
157 srcLinks.clear();
158 dstLinks.clear();
159 log.info("Stopped");
160 }
161
162 @Override
163 public int getLinkCount() {
164 return links.size();
165 }
166
167 @Override
168 public Iterable<Link> getLinks() {
169 return Collections.unmodifiableCollection(links.values());
170 }
171
172 @Override
173 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
174 // lock for iteration
175 synchronized (srcLinks) {
176 return FluentIterable.from(srcLinks.get(deviceId))
177 .transform(lookupLink())
178 .filter(notNull())
179 .toSet();
180 }
181 }
182
183 @Override
184 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
185 // lock for iteration
186 synchronized (dstLinks) {
187 return FluentIterable.from(dstLinks.get(deviceId))
188 .transform(lookupLink())
189 .filter(notNull())
190 .toSet();
191 }
192 }
193
194 @Override
195 public Link getLink(ConnectPoint src, ConnectPoint dst) {
196 return links.get(new LinkKey(src, dst));
197 }
198
199 @Override
200 public Set<Link> getEgressLinks(ConnectPoint src) {
201 Set<Link> egress = new HashSet<>();
202 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
203 if (linkKey.src().equals(src)) {
204 egress.add(links.get(linkKey));
205 }
206 }
207 return egress;
208 }
209
210 @Override
211 public Set<Link> getIngressLinks(ConnectPoint dst) {
212 Set<Link> ingress = new HashSet<>();
213 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
214 if (linkKey.dst().equals(dst)) {
215 ingress.add(links.get(linkKey));
216 }
217 }
218 return ingress;
219 }
220
221 @Override
222 public LinkEvent createOrUpdateLink(ProviderId providerId,
223 LinkDescription linkDescription) {
224
225 DeviceId dstDeviceId = linkDescription.dst().deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700226 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700227
228 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
229
230 LinkEvent event = createOrUpdateLinkInternal(providerId, deltaDesc);
231
232 if (event != null) {
233 log.info("Notifying peers of a link update topology event from providerId: "
234 + "{} between src: {} and dst: {}",
235 providerId, linkDescription.src(), linkDescription.dst());
236 try {
237 notifyPeers(new InternalLinkEvent(providerId, deltaDesc));
238 } catch (IOException e) {
239 log.info("Failed to notify peers of a link update topology event from providerId: "
240 + "{} between src: {} and dst: {}",
241 providerId, linkDescription.src(), linkDescription.dst());
242 }
243 }
244 return event;
245 }
246
247 private LinkEvent createOrUpdateLinkInternal(
248 ProviderId providerId,
249 Timestamped<LinkDescription> linkDescription) {
250
251 LinkKey key = new LinkKey(linkDescription.value().src(), linkDescription.value().dst());
252 ConcurrentMap<ProviderId, Timestamped<LinkDescription>> descs = getLinkDescriptions(key);
253
254 synchronized (descs) {
255 // if the link was previously removed, we should proceed if and
256 // only if this request is more recent.
257 Timestamp linkRemovedTimestamp = removedLinks.get(key);
258 if (linkRemovedTimestamp != null) {
259 if (linkDescription.isNewer(linkRemovedTimestamp)) {
260 removedLinks.remove(key);
261 } else {
262 return null;
263 }
264 }
265
266 final Link oldLink = links.get(key);
267 // update description
268 createOrUpdateLinkDescription(descs, providerId, linkDescription);
269 final Link newLink = composeLink(descs);
270 if (oldLink == null) {
271 return createLink(key, newLink);
272 }
273 return updateLink(key, oldLink, newLink);
274 }
275 }
276
277 // Guarded by linkDescs value (=locking each Link)
278 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
279 ConcurrentMap<ProviderId, Timestamped<LinkDescription>> existingLinkDescriptions,
280 ProviderId providerId,
281 Timestamped<LinkDescription> linkDescription) {
282
283 // merge existing attributes and merge
284 Timestamped<LinkDescription> existingLinkDescription = existingLinkDescriptions.get(providerId);
285 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
286 return null;
287 }
288 Timestamped<LinkDescription> newLinkDescription = linkDescription;
289 if (existingLinkDescription != null) {
290 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
291 linkDescription.value().annotations());
292 newLinkDescription = new Timestamped<LinkDescription>(
293 new DefaultLinkDescription(
294 linkDescription.value().src(),
295 linkDescription.value().dst(),
296 linkDescription.value().type(), merged),
297 linkDescription.timestamp());
298 }
299 return existingLinkDescriptions.put(providerId, newLinkDescription);
300 }
301
302 // Creates and stores the link and returns the appropriate event.
303 // Guarded by linkDescs value (=locking each Link)
304 private LinkEvent createLink(LinkKey key, Link newLink) {
305
306 if (newLink.providerId().isAncillary()) {
307 // TODO: revisit ancillary only Link handling
308
309 // currently treating ancillary only as down (not visible outside)
310 return null;
311 }
312
313 links.put(key, newLink);
314 srcLinks.put(newLink.src().deviceId(), key);
315 dstLinks.put(newLink.dst().deviceId(), key);
316 return new LinkEvent(LINK_ADDED, newLink);
317 }
318
319 // Updates, if necessary the specified link and returns the appropriate event.
320 // Guarded by linkDescs value (=locking each Link)
321 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
322
323 if (newLink.providerId().isAncillary()) {
324 // TODO: revisit ancillary only Link handling
325
326 // currently treating ancillary only as down (not visible outside)
327 return null;
328 }
329
330 if ((oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
331 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
332
333 links.put(key, newLink);
334 // strictly speaking following can be ommitted
335 srcLinks.put(oldLink.src().deviceId(), key);
336 dstLinks.put(oldLink.dst().deviceId(), key);
337 return new LinkEvent(LINK_UPDATED, newLink);
338 }
339 return null;
340 }
341
342 @Override
343 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
344 final LinkKey key = new LinkKey(src, dst);
345
346 DeviceId dstDeviceId = dst.deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700347 Timestamp timestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700348
349 LinkEvent event = removeLinkInternal(key, timestamp);
350
351 if (event != null) {
352 log.info("Notifying peers of a link removed topology event for a link "
353 + "between src: {} and dst: {}", src, dst);
354 try {
355 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
356 } catch (IOException e) {
357 log.error("Failed to notify peers of a link removed topology event for a link "
358 + "between src: {} and dst: {}", src, dst);
359 }
360 }
361 return event;
362 }
363
364 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
365 ConcurrentMap<ProviderId, Timestamped<LinkDescription>> linkDescriptions =
366 getLinkDescriptions(key);
367 synchronized (linkDescriptions) {
368 // accept removal request if given timestamp is newer than
369 // the latest Timestamp from Primary provider
370 ProviderId primaryProviderId = pickPrimaryProviderId(linkDescriptions);
371 if (linkDescriptions.get(primaryProviderId).isNewer(timestamp)) {
372 return null;
373 }
374 removedLinks.put(key, timestamp);
375 Link link = links.remove(key);
376 linkDescriptions.clear();
377 if (link != null) {
378 srcLinks.remove(link.src().deviceId(), key);
379 dstLinks.remove(link.dst().deviceId(), key);
380 return new LinkEvent(LINK_REMOVED, link);
381 }
382 return null;
383 }
384 }
385
386 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
387 return synchronizedSetMultimap(HashMultimap.<K, V>create());
388 }
389
390 /**
391 * @return primary ProviderID, or randomly chosen one if none exists
392 */
393 private ProviderId pickPrimaryProviderId(
394 ConcurrentMap<ProviderId, Timestamped<LinkDescription>> providerDescs) {
395
396 ProviderId fallBackPrimary = null;
397 for (Entry<ProviderId, Timestamped<LinkDescription>> e : providerDescs.entrySet()) {
398 if (!e.getKey().isAncillary()) {
399 return e.getKey();
400 } else if (fallBackPrimary == null) {
401 // pick randomly as a fallback in case there is no primary
402 fallBackPrimary = e.getKey();
403 }
404 }
405 return fallBackPrimary;
406 }
407
408 private Link composeLink(ConcurrentMap<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
409 ProviderId primaryProviderId = pickPrimaryProviderId(linkDescriptions);
410 Timestamped<LinkDescription> base = linkDescriptions.get(primaryProviderId);
411
412 ConnectPoint src = base.value().src();
413 ConnectPoint dst = base.value().dst();
414 Type type = base.value().type();
415 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
416 annotations = merge(annotations, base.value().annotations());
417
418 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
419 if (primaryProviderId.equals(e.getKey())) {
420 continue;
421 }
422
423 // TODO: should keep track of Description timestamp
424 // and only merge conflicting keys when timestamp is newer
425 // Currently assuming there will never be a key conflict between
426 // providers
427
428 // annotation merging. not so efficient, should revisit later
429 annotations = merge(annotations, e.getValue().value().annotations());
430 }
431
432 return new DefaultLink(primaryProviderId , src, dst, type, annotations);
433 }
434
435 private ConcurrentMap<ProviderId, Timestamped<LinkDescription>> getLinkDescriptions(LinkKey key) {
436 return ConcurrentUtils.createIfAbsentUnchecked(linkDescs, key,
437 NewConcurrentHashMap.<ProviderId, Timestamped<LinkDescription>>ifNeeded());
438 }
439
Madan Jampania97e8202014-10-10 17:01:33 -0700440 private Timestamped<LinkDescription> getLinkDescription(LinkKey key, ProviderId providerId) {
441 return getLinkDescriptions(key).get(providerId);
442 }
443
Madan Jampani2ff05592014-10-10 15:42:47 -0700444 private final Function<LinkKey, Link> lookupLink = new LookupLink();
445 private Function<LinkKey, Link> lookupLink() {
446 return lookupLink;
447 }
448
449 private final class LookupLink implements Function<LinkKey, Link> {
450 @Override
451 public Link apply(LinkKey input) {
452 return links.get(input);
453 }
454 }
455
456 private static final Predicate<Provided> IS_PRIMARY = new IsPrimary();
457 private static final Predicate<Provided> isPrimary() {
458 return IS_PRIMARY;
459 }
460
461 private static final class IsPrimary implements Predicate<Provided> {
462
463 @Override
464 public boolean apply(Provided input) {
465 return !input.providerId().isAncillary();
466 }
467 }
468
469 private void notifyDelegateIfNotNull(LinkEvent event) {
470 if (event != null) {
471 notifyDelegate(event);
472 }
473 }
474
475 // TODO: should we be throwing exception?
476 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
477 ClusterMessage message = new ClusterMessage(
478 clusterService.getLocalNode().id(),
479 subject,
480 SERIALIZER.encode(event));
481 clusterCommunicator.broadcast(message);
482 }
483
Madan Jampania97e8202014-10-10 17:01:33 -0700484 // TODO: should we be throwing exception?
485 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) {
486 try {
487 ClusterMessage message = new ClusterMessage(
488 clusterService.getLocalNode().id(),
489 subject,
490 SERIALIZER.encode(event));
491 clusterCommunicator.unicast(message, recipient);
492 } catch (IOException e) {
493 log.error("Failed to send a {} message to {}", subject.value(), recipient);
494 }
495 }
496
Madan Jampani2ff05592014-10-10 15:42:47 -0700497 private void notifyPeers(InternalLinkEvent event) throws IOException {
498 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
499 }
500
501 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
502 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
503 }
504
Madan Jampania97e8202014-10-10 17:01:33 -0700505 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
506 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
507 }
508
509 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
510 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
511 }
512
513 private final class SendAdvertisementTask implements Runnable {
514
515 @Override
516 public void run() {
517 if (Thread.currentThread().isInterrupted()) {
518 log.info("Interrupted, quitting");
519 return;
520 }
521
522 try {
523 final NodeId self = clusterService.getLocalNode().id();
524 Set<ControllerNode> nodes = clusterService.getNodes();
525
526 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
527 .transform(toNodeId())
528 .toList();
529
530 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
531 log.info("No other peers in the cluster.");
532 return;
533 }
534
535 NodeId peer;
536 do {
537 int idx = RandomUtils.nextInt(0, nodeIds.size());
538 peer = nodeIds.get(idx);
539 } while (peer.equals(self));
540
541 LinkAntiEntropyAdvertisement ad = createAdvertisement();
542
543 if (Thread.currentThread().isInterrupted()) {
544 log.info("Interrupted, quitting");
545 return;
546 }
547
548 try {
549 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
550 } catch (Exception e) {
551 log.error("Failed to send anti-entropy advertisement", e);
552 return;
553 }
554 } catch (Exception e) {
555 // catch all Exception to avoid Scheduled task being suppressed.
556 log.error("Exception thrown while sending advertisement", e);
557 }
558 }
559 }
560
561 private LinkAntiEntropyAdvertisement createAdvertisement() {
562 final NodeId self = clusterService.getLocalNode().id();
563
564 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
565 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
566
567 for (Entry<LinkKey, ConcurrentMap<ProviderId, Timestamped<LinkDescription>>>
568 provs : linkDescs.entrySet()) {
569
570 final LinkKey linkKey = provs.getKey();
571 final ConcurrentMap<ProviderId, Timestamped<LinkDescription>> linkDesc = provs.getValue();
572 synchronized (linkDesc) {
573 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
574 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
575 }
576 }
577 }
578
579 linkTombstones.putAll(removedLinks);
580
581 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
582 }
583
584 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement advertisement) {
585
586 NodeId peer = advertisement.sender();
587
588 Map<LinkFragmentId, Timestamp> linkTimestamps = advertisement.linkTimestamps();
589 Map<LinkKey, Timestamp> linkTombstones = advertisement.linkTombstones();
590 for (Map.Entry<LinkFragmentId, Timestamp> entry : linkTimestamps.entrySet()) {
591 LinkFragmentId linkFragmentId = entry.getKey();
592 Timestamp peerTimestamp = entry.getValue();
593
594 LinkKey key = linkFragmentId.linkKey();
595 ProviderId providerId = linkFragmentId.providerId();
596
597 Timestamped<LinkDescription> linkDescription = getLinkDescription(key, providerId);
598 if (linkDescription.isNewer(peerTimestamp)) {
599 // I have more recent link description. update peer.
600 notifyPeer(peer, new InternalLinkEvent(providerId, linkDescription));
601 }
602 // else TODO: Peer has more recent link description. request it.
603
604 Timestamp linkRemovedTimestamp = removedLinks.get(key);
605 if (linkRemovedTimestamp != null && linkRemovedTimestamp.compareTo(peerTimestamp) > 0) {
606 // peer has a zombie link. update peer.
607 notifyPeer(peer, new InternalLinkRemovedEvent(key, linkRemovedTimestamp));
608 }
609 }
610
611 for (Map.Entry<LinkKey, Timestamp> entry : linkTombstones.entrySet()) {
612 LinkKey key = entry.getKey();
613 Timestamp peerTimestamp = entry.getValue();
614
615 ProviderId primaryProviderId = pickPrimaryProviderId(getLinkDescriptions(key));
616 if (primaryProviderId != null) {
617 if (!getLinkDescription(key, primaryProviderId).isNewer(peerTimestamp)) {
618 notifyDelegateIfNotNull(removeLinkInternal(key, peerTimestamp));
619 }
620 }
621 }
622 }
623
Madan Jampani2ff05592014-10-10 15:42:47 -0700624 private class InternalLinkEventListener implements ClusterMessageHandler {
625 @Override
626 public void handle(ClusterMessage message) {
627
628 log.info("Received link event from peer: {}", message.sender());
629 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
630
631 ProviderId providerId = event.providerId();
632 Timestamped<LinkDescription> linkDescription = event.linkDescription();
633
634 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
635 }
636 }
637
638 private class InternalLinkRemovedEventListener implements ClusterMessageHandler {
639 @Override
640 public void handle(ClusterMessage message) {
641
642 log.info("Received link removed event from peer: {}", message.sender());
643 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
644
645 LinkKey linkKey = event.linkKey();
646 Timestamp timestamp = event.timestamp();
647
648 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
649 }
650 }
Madan Jampania97e8202014-10-10 17:01:33 -0700651
652 private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler {
653
654 @Override
655 public void handle(ClusterMessage message) {
656 log.info("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
657 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
658 handleAntiEntropyAdvertisement(advertisement);
659 }
660 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700661}