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