blob: a6c1660d93843b7fcd590e2521ed27713ae32f38 [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.felix.scr.annotations.Activate;
13import org.apache.felix.scr.annotations.Component;
14import org.apache.felix.scr.annotations.Deactivate;
15import org.apache.felix.scr.annotations.Reference;
16import org.apache.felix.scr.annotations.ReferenceCardinality;
17import org.apache.felix.scr.annotations.Service;
18import org.onlab.onos.cluster.ClusterService;
Madan Jampania97e8202014-10-10 17:01:33 -070019import org.onlab.onos.cluster.ControllerNode;
20import org.onlab.onos.cluster.NodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070021import org.onlab.onos.net.AnnotationsUtil;
22import org.onlab.onos.net.ConnectPoint;
23import org.onlab.onos.net.DefaultAnnotations;
24import org.onlab.onos.net.DefaultLink;
25import org.onlab.onos.net.DeviceId;
26import org.onlab.onos.net.Link;
27import org.onlab.onos.net.SparseAnnotations;
28import org.onlab.onos.net.Link.Type;
29import org.onlab.onos.net.LinkKey;
30import org.onlab.onos.net.Provided;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070031import org.onlab.onos.net.device.DeviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -070032import org.onlab.onos.net.link.DefaultLinkDescription;
33import org.onlab.onos.net.link.LinkDescription;
34import org.onlab.onos.net.link.LinkEvent;
35import org.onlab.onos.net.link.LinkStore;
36import org.onlab.onos.net.link.LinkStoreDelegate;
37import org.onlab.onos.net.provider.ProviderId;
38import org.onlab.onos.store.AbstractStore;
Madan Jampani2ff05592014-10-10 15:42:47 -070039import org.onlab.onos.store.Timestamp;
40import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
41import org.onlab.onos.store.cluster.messaging.ClusterMessage;
42import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler;
43import org.onlab.onos.store.cluster.messaging.MessageSubject;
44import org.onlab.onos.store.common.impl.Timestamped;
45import org.onlab.onos.store.serializers.DistributedStoreSerializers;
46import org.onlab.onos.store.serializers.KryoSerializer;
47import org.onlab.util.KryoPool;
Madan Jampani2ff05592014-10-10 15:42:47 -070048import org.slf4j.Logger;
49
50import java.io.IOException;
51import java.util.Collections;
Madan Jampania97e8202014-10-10 17:01:33 -070052import java.util.HashMap;
Madan Jampani2ff05592014-10-10 15:42:47 -070053import java.util.HashSet;
54import java.util.Map;
55import java.util.Set;
56import java.util.Map.Entry;
57import java.util.concurrent.ConcurrentHashMap;
58import java.util.concurrent.ConcurrentMap;
Madan Jampania97e8202014-10-10 17:01:33 -070059import java.util.concurrent.ScheduledExecutorService;
60import java.util.concurrent.TimeUnit;
Madan Jampani2ff05592014-10-10 15:42:47 -070061
Madan Jampania97e8202014-10-10 17:01:33 -070062import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
63import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070064import static org.onlab.onos.net.DefaultAnnotations.union;
65import static org.onlab.onos.net.DefaultAnnotations.merge;
66import static org.onlab.onos.net.Link.Type.DIRECT;
67import static org.onlab.onos.net.Link.Type.INDIRECT;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070068import static org.onlab.onos.net.LinkKey.linkKey;
Madan Jampani2ff05592014-10-10 15:42:47 -070069import static org.onlab.onos.net.link.LinkEvent.Type.*;
Madan Jampania97e8202014-10-10 17:01:33 -070070import static org.onlab.util.Tools.namedThreads;
Madan Jampani2ff05592014-10-10 15:42:47 -070071import static org.slf4j.LoggerFactory.getLogger;
72import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
73import static com.google.common.base.Predicates.notNull;
74
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() {
113 serializerPool = KryoPool.newBuilder()
114 .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;
242 synchronized (getLinkDescriptions(key)) {
243 event = createOrUpdateLinkInternal(providerId, deltaDesc);
244 mergedDesc = getLinkDescriptions(key).get(providerId);
245 }
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 HIGUCHI990aecc2014-10-13 22:21:42 -0700266 LinkKey key = linkKey(linkDescription.value().src(),
267 linkDescription.value().dst());
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700268 Map<ProviderId, Timestamped<LinkDescription>> descs = getLinkDescriptions(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 {
278 return null;
279 }
280 }
281
282 final Link oldLink = links.get(key);
283 // update description
284 createOrUpdateLinkDescription(descs, providerId, linkDescription);
285 final Link newLink = composeLink(descs);
286 if (oldLink == null) {
287 return createLink(key, newLink);
288 }
289 return updateLink(key, oldLink, newLink);
290 }
291 }
292
293 // Guarded by linkDescs value (=locking each Link)
294 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700295 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700296 ProviderId providerId,
297 Timestamped<LinkDescription> linkDescription) {
298
299 // merge existing attributes and merge
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700300 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700301 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
302 return null;
303 }
304 Timestamped<LinkDescription> newLinkDescription = linkDescription;
305 if (existingLinkDescription != null) {
306 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
307 linkDescription.value().annotations());
308 newLinkDescription = new Timestamped<LinkDescription>(
309 new DefaultLinkDescription(
310 linkDescription.value().src(),
311 linkDescription.value().dst(),
312 linkDescription.value().type(), merged),
313 linkDescription.timestamp());
314 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700315 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700316 }
317
318 // Creates and stores the link and returns the appropriate event.
319 // Guarded by linkDescs value (=locking each Link)
320 private LinkEvent createLink(LinkKey key, Link newLink) {
321
322 if (newLink.providerId().isAncillary()) {
323 // TODO: revisit ancillary only Link handling
324
325 // currently treating ancillary only as down (not visible outside)
326 return null;
327 }
328
329 links.put(key, newLink);
330 srcLinks.put(newLink.src().deviceId(), key);
331 dstLinks.put(newLink.dst().deviceId(), key);
332 return new LinkEvent(LINK_ADDED, newLink);
333 }
334
335 // Updates, if necessary the specified link and returns the appropriate event.
336 // Guarded by linkDescs value (=locking each Link)
337 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
338
339 if (newLink.providerId().isAncillary()) {
340 // TODO: revisit ancillary only Link handling
341
342 // currently treating ancillary only as down (not visible outside)
343 return null;
344 }
345
346 if ((oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
347 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
348
349 links.put(key, newLink);
350 // strictly speaking following can be ommitted
351 srcLinks.put(oldLink.src().deviceId(), key);
352 dstLinks.put(oldLink.dst().deviceId(), key);
353 return new LinkEvent(LINK_UPDATED, newLink);
354 }
355 return null;
356 }
357
358 @Override
359 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700360 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700361
362 DeviceId dstDeviceId = dst.deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700363 Timestamp timestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700364
365 LinkEvent event = removeLinkInternal(key, timestamp);
366
367 if (event != null) {
368 log.info("Notifying peers of a link removed topology event for a link "
369 + "between src: {} and dst: {}", src, dst);
370 try {
371 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
372 } catch (IOException e) {
373 log.error("Failed to notify peers of a link removed topology event for a link "
374 + "between src: {} and dst: {}", src, dst);
375 }
376 }
377 return event;
378 }
379
380 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700381 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions =
Madan Jampani2ff05592014-10-10 15:42:47 -0700382 getLinkDescriptions(key);
383 synchronized (linkDescriptions) {
384 // accept removal request if given timestamp is newer than
385 // the latest Timestamp from Primary provider
386 ProviderId primaryProviderId = pickPrimaryProviderId(linkDescriptions);
387 if (linkDescriptions.get(primaryProviderId).isNewer(timestamp)) {
388 return null;
389 }
390 removedLinks.put(key, timestamp);
391 Link link = links.remove(key);
392 linkDescriptions.clear();
393 if (link != null) {
394 srcLinks.remove(link.src().deviceId(), key);
395 dstLinks.remove(link.dst().deviceId(), key);
396 return new LinkEvent(LINK_REMOVED, link);
397 }
398 return null;
399 }
400 }
401
402 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
403 return synchronizedSetMultimap(HashMultimap.<K, V>create());
404 }
405
406 /**
407 * @return primary ProviderID, or randomly chosen one if none exists
408 */
409 private ProviderId pickPrimaryProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700410 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700411
412 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700413 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700414 if (!e.getKey().isAncillary()) {
415 return e.getKey();
416 } else if (fallBackPrimary == null) {
417 // pick randomly as a fallback in case there is no primary
418 fallBackPrimary = e.getKey();
419 }
420 }
421 return fallBackPrimary;
422 }
423
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700424 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
425 ProviderId primaryProviderId = pickPrimaryProviderId(descs);
426 Timestamped<LinkDescription> base = descs.get(primaryProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700427
428 ConnectPoint src = base.value().src();
429 ConnectPoint dst = base.value().dst();
430 Type type = base.value().type();
431 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
432 annotations = merge(annotations, base.value().annotations());
433
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700434 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700435 if (primaryProviderId.equals(e.getKey())) {
436 continue;
437 }
438
439 // TODO: should keep track of Description timestamp
440 // and only merge conflicting keys when timestamp is newer
441 // Currently assuming there will never be a key conflict between
442 // providers
443
444 // annotation merging. not so efficient, should revisit later
445 annotations = merge(annotations, e.getValue().value().annotations());
446 }
447
448 return new DefaultLink(primaryProviderId , src, dst, type, annotations);
449 }
450
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700451 private Map<ProviderId, Timestamped<LinkDescription>> getLinkDescriptions(LinkKey key) {
452 Map<ProviderId, Timestamped<LinkDescription>> r;
453 r = linkDescs.get(key);
454 if (r != null) {
455 return r;
456 }
457 r = new HashMap<>();
458 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
459 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
460 if (concurrentlyAdded != null) {
461 return concurrentlyAdded;
462 } else {
463 return r;
464 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700465 }
466
Madan Jampania97e8202014-10-10 17:01:33 -0700467 private Timestamped<LinkDescription> getLinkDescription(LinkKey key, ProviderId providerId) {
468 return getLinkDescriptions(key).get(providerId);
469 }
470
Madan Jampani2ff05592014-10-10 15:42:47 -0700471 private final Function<LinkKey, Link> lookupLink = new LookupLink();
472 private Function<LinkKey, Link> lookupLink() {
473 return lookupLink;
474 }
475
476 private final class LookupLink implements Function<LinkKey, Link> {
477 @Override
478 public Link apply(LinkKey input) {
479 return links.get(input);
480 }
481 }
482
Madan Jampani2ff05592014-10-10 15:42:47 -0700483 private static final class IsPrimary implements Predicate<Provided> {
484
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700485 private static final Predicate<Provided> IS_PRIMARY = new IsPrimary();
486 public static final Predicate<Provided> isPrimary() {
487 return IS_PRIMARY;
488 }
489
Madan Jampani2ff05592014-10-10 15:42:47 -0700490 @Override
491 public boolean apply(Provided input) {
492 return !input.providerId().isAncillary();
493 }
494 }
495
496 private void notifyDelegateIfNotNull(LinkEvent event) {
497 if (event != null) {
498 notifyDelegate(event);
499 }
500 }
501
502 // TODO: should we be throwing exception?
503 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
504 ClusterMessage message = new ClusterMessage(
505 clusterService.getLocalNode().id(),
506 subject,
507 SERIALIZER.encode(event));
508 clusterCommunicator.broadcast(message);
509 }
510
Madan Jampania97e8202014-10-10 17:01:33 -0700511 // TODO: should we be throwing exception?
512 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) {
513 try {
514 ClusterMessage message = new ClusterMessage(
515 clusterService.getLocalNode().id(),
516 subject,
517 SERIALIZER.encode(event));
518 clusterCommunicator.unicast(message, recipient);
519 } catch (IOException e) {
520 log.error("Failed to send a {} message to {}", subject.value(), recipient);
521 }
522 }
523
Madan Jampani2ff05592014-10-10 15:42:47 -0700524 private void notifyPeers(InternalLinkEvent event) throws IOException {
525 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
526 }
527
528 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
529 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
530 }
531
Madan Jampania97e8202014-10-10 17:01:33 -0700532 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
533 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
534 }
535
536 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
537 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
538 }
539
540 private final class SendAdvertisementTask implements Runnable {
541
542 @Override
543 public void run() {
544 if (Thread.currentThread().isInterrupted()) {
545 log.info("Interrupted, quitting");
546 return;
547 }
548
549 try {
550 final NodeId self = clusterService.getLocalNode().id();
551 Set<ControllerNode> nodes = clusterService.getNodes();
552
553 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
554 .transform(toNodeId())
555 .toList();
556
557 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHI37083082014-10-13 10:38:38 -0700558 log.debug("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700559 return;
560 }
561
562 NodeId peer;
563 do {
564 int idx = RandomUtils.nextInt(0, nodeIds.size());
565 peer = nodeIds.get(idx);
566 } while (peer.equals(self));
567
568 LinkAntiEntropyAdvertisement ad = createAdvertisement();
569
570 if (Thread.currentThread().isInterrupted()) {
571 log.info("Interrupted, quitting");
572 return;
573 }
574
575 try {
576 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
577 } catch (Exception e) {
578 log.error("Failed to send anti-entropy advertisement", e);
579 return;
580 }
581 } catch (Exception e) {
582 // catch all Exception to avoid Scheduled task being suppressed.
583 log.error("Exception thrown while sending advertisement", e);
584 }
585 }
586 }
587
588 private LinkAntiEntropyAdvertisement createAdvertisement() {
589 final NodeId self = clusterService.getLocalNode().id();
590
591 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
592 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
593
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700594 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
Madan Jampania97e8202014-10-10 17:01:33 -0700595 provs : linkDescs.entrySet()) {
596
597 final LinkKey linkKey = provs.getKey();
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700598 final Map<ProviderId, Timestamped<LinkDescription>> linkDesc = provs.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700599 synchronized (linkDesc) {
600 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
601 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
602 }
603 }
604 }
605
606 linkTombstones.putAll(removedLinks);
607
608 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
609 }
610
611 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement advertisement) {
612
613 NodeId peer = advertisement.sender();
614
615 Map<LinkFragmentId, Timestamp> linkTimestamps = advertisement.linkTimestamps();
616 Map<LinkKey, Timestamp> linkTombstones = advertisement.linkTombstones();
617 for (Map.Entry<LinkFragmentId, Timestamp> entry : linkTimestamps.entrySet()) {
618 LinkFragmentId linkFragmentId = entry.getKey();
619 Timestamp peerTimestamp = entry.getValue();
620
621 LinkKey key = linkFragmentId.linkKey();
622 ProviderId providerId = linkFragmentId.providerId();
623
624 Timestamped<LinkDescription> linkDescription = getLinkDescription(key, providerId);
625 if (linkDescription.isNewer(peerTimestamp)) {
626 // I have more recent link description. update peer.
627 notifyPeer(peer, new InternalLinkEvent(providerId, linkDescription));
628 }
629 // else TODO: Peer has more recent link description. request it.
630
631 Timestamp linkRemovedTimestamp = removedLinks.get(key);
632 if (linkRemovedTimestamp != null && linkRemovedTimestamp.compareTo(peerTimestamp) > 0) {
633 // peer has a zombie link. update peer.
634 notifyPeer(peer, new InternalLinkRemovedEvent(key, linkRemovedTimestamp));
635 }
636 }
637
638 for (Map.Entry<LinkKey, Timestamp> entry : linkTombstones.entrySet()) {
639 LinkKey key = entry.getKey();
640 Timestamp peerTimestamp = entry.getValue();
641
642 ProviderId primaryProviderId = pickPrimaryProviderId(getLinkDescriptions(key));
643 if (primaryProviderId != null) {
644 if (!getLinkDescription(key, primaryProviderId).isNewer(peerTimestamp)) {
645 notifyDelegateIfNotNull(removeLinkInternal(key, peerTimestamp));
646 }
647 }
648 }
649 }
650
Madan Jampani2ff05592014-10-10 15:42:47 -0700651 private class InternalLinkEventListener implements ClusterMessageHandler {
652 @Override
653 public void handle(ClusterMessage message) {
654
655 log.info("Received link event from peer: {}", message.sender());
656 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
657
658 ProviderId providerId = event.providerId();
659 Timestamped<LinkDescription> linkDescription = event.linkDescription();
660
661 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
662 }
663 }
664
665 private class InternalLinkRemovedEventListener implements ClusterMessageHandler {
666 @Override
667 public void handle(ClusterMessage message) {
668
669 log.info("Received link removed event from peer: {}", message.sender());
670 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
671
672 LinkKey linkKey = event.linkKey();
673 Timestamp timestamp = event.timestamp();
674
675 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
676 }
677 }
Madan Jampania97e8202014-10-10 17:01:33 -0700678
679 private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler {
680
681 @Override
682 public void handle(ClusterMessage message) {
Yuta HIGUCHI9a0a1d12014-10-13 22:38:02 -0700683 log.debug("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700684 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
685 handleAntiEntropyAdvertisement(advertisement);
686 }
687 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700688}