blob: ab5cc8ad9b90e824b87abbd509190b7665a2eaf7 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Madan Jampani2ff05592014-10-10 15:42:47 -070016package org.onlab.onos.store.link.impl;
17
18import com.google.common.base.Function;
Madan Jampani2ff05592014-10-10 15:42:47 -070019import com.google.common.collect.FluentIterable;
20import com.google.common.collect.HashMultimap;
Madan Jampania97e8202014-10-10 17:01:33 -070021import com.google.common.collect.ImmutableList;
Madan Jampani2ff05592014-10-10 15:42:47 -070022import com.google.common.collect.Maps;
23import com.google.common.collect.SetMultimap;
24
Madan Jampania97e8202014-10-10 17:01:33 -070025import org.apache.commons.lang3.RandomUtils;
Madan Jampani2ff05592014-10-10 15:42:47 -070026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.apache.felix.scr.annotations.Service;
32import org.onlab.onos.cluster.ClusterService;
Madan Jampania97e8202014-10-10 17:01:33 -070033import org.onlab.onos.cluster.ControllerNode;
34import org.onlab.onos.cluster.NodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070035import org.onlab.onos.net.AnnotationsUtil;
36import org.onlab.onos.net.ConnectPoint;
37import org.onlab.onos.net.DefaultAnnotations;
38import org.onlab.onos.net.DefaultLink;
39import org.onlab.onos.net.DeviceId;
40import org.onlab.onos.net.Link;
Madan Jampani2ff05592014-10-10 15:42:47 -070041import org.onlab.onos.net.Link.Type;
42import org.onlab.onos.net.LinkKey;
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -070043import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070044import org.onlab.onos.net.device.DeviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -070045import org.onlab.onos.net.link.DefaultLinkDescription;
46import org.onlab.onos.net.link.LinkDescription;
47import org.onlab.onos.net.link.LinkEvent;
48import org.onlab.onos.net.link.LinkStore;
49import org.onlab.onos.net.link.LinkStoreDelegate;
50import org.onlab.onos.net.provider.ProviderId;
51import org.onlab.onos.store.AbstractStore;
Madan Jampani2ff05592014-10-10 15:42:47 -070052import org.onlab.onos.store.Timestamp;
53import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
54import org.onlab.onos.store.cluster.messaging.ClusterMessage;
55import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler;
56import org.onlab.onos.store.cluster.messaging.MessageSubject;
Yuta HIGUCHIeecee552014-10-16 14:09:01 -070057import org.onlab.onos.store.impl.Timestamped;
Madan Jampani2ff05592014-10-10 15:42:47 -070058import org.onlab.onos.store.serializers.DistributedStoreSerializers;
59import org.onlab.onos.store.serializers.KryoSerializer;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070060import org.onlab.util.KryoNamespace;
Madan Jampani2ff05592014-10-10 15:42:47 -070061import org.slf4j.Logger;
62
63import java.io.IOException;
64import java.util.Collections;
Madan Jampania97e8202014-10-10 17:01:33 -070065import java.util.HashMap;
Madan Jampani2ff05592014-10-10 15:42:47 -070066import java.util.HashSet;
67import java.util.Map;
68import java.util.Set;
69import java.util.Map.Entry;
70import java.util.concurrent.ConcurrentHashMap;
71import java.util.concurrent.ConcurrentMap;
Madan Jampania97e8202014-10-10 17:01:33 -070072import java.util.concurrent.ScheduledExecutorService;
73import java.util.concurrent.TimeUnit;
Madan Jampani2ff05592014-10-10 15:42:47 -070074
Madan Jampania97e8202014-10-10 17:01:33 -070075import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
76import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070077import static org.onlab.onos.net.DefaultAnnotations.union;
78import static org.onlab.onos.net.DefaultAnnotations.merge;
79import static org.onlab.onos.net.Link.Type.DIRECT;
80import static org.onlab.onos.net.Link.Type.INDIRECT;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070081import static org.onlab.onos.net.LinkKey.linkKey;
Madan Jampani2ff05592014-10-10 15:42:47 -070082import static org.onlab.onos.net.link.LinkEvent.Type.*;
Madan Jampania97e8202014-10-10 17:01:33 -070083import static org.onlab.util.Tools.namedThreads;
Madan Jampani2ff05592014-10-10 15:42:47 -070084import static org.slf4j.LoggerFactory.getLogger;
85import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -070086import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampani2ff05592014-10-10 15:42:47 -070087import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -070088import static org.onlab.onos.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Madan Jampani2ff05592014-10-10 15:42:47 -070089
90/**
91 * Manages inventory of infrastructure links in distributed data store
92 * that uses optimistic replication and gossip based techniques.
93 */
94@Component(immediate = true)
95@Service
96public class GossipLinkStore
97 extends AbstractStore<LinkEvent, LinkStoreDelegate>
98 implements LinkStore {
99
100 private final Logger log = getLogger(getClass());
101
102 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700103 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700104 new ConcurrentHashMap<>();
105
106 // Link instance cache
107 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
108
109 // Egress and ingress link sets
110 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
111 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
112
113 // Remove links
114 private final Map<LinkKey, Timestamp> removedLinks = Maps.newHashMap();
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700117 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
120 protected ClusterCommunicationService clusterCommunicator;
121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected ClusterService clusterService;
124
125 private static final KryoSerializer SERIALIZER = new KryoSerializer() {
126 @Override
127 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700128 serializerPool = KryoNamespace.newBuilder()
Madan Jampani2ff05592014-10-10 15:42:47 -0700129 .register(DistributedStoreSerializers.COMMON)
130 .register(InternalLinkEvent.class)
131 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700132 .register(LinkAntiEntropyAdvertisement.class)
133 .register(LinkFragmentId.class)
Madan Jampani2ff05592014-10-10 15:42:47 -0700134 .build()
135 .populate(1);
136 }
137 };
138
Madan Jampania97e8202014-10-10 17:01:33 -0700139 private ScheduledExecutorService executor;
140
Madan Jampani2ff05592014-10-10 15:42:47 -0700141 @Activate
142 public void activate() {
143
144 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700145 GossipLinkStoreMessageSubjects.LINK_UPDATE,
146 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700147 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700148 GossipLinkStoreMessageSubjects.LINK_REMOVED,
149 new InternalLinkRemovedEventListener());
150 clusterCommunicator.addSubscriber(
151 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
152 new InternalLinkAntiEntropyAdvertisementListener());
153
154 executor =
155 newSingleThreadScheduledExecutor(namedThreads("link-anti-entropy-%d"));
156
157 // TODO: Make these configurable
158 long initialDelaySec = 5;
159 long periodSec = 5;
160 // start anti-entropy thread
161 executor.scheduleAtFixedRate(new SendAdvertisementTask(),
162 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700163
164 log.info("Started");
165 }
166
167 @Deactivate
168 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700169
170 executor.shutdownNow();
171 try {
172 if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
173 log.error("Timeout during executor shutdown");
174 }
175 } catch (InterruptedException e) {
176 log.error("Error during executor shutdown", e);
177 }
178
Madan Jampani2ff05592014-10-10 15:42:47 -0700179 linkDescs.clear();
180 links.clear();
181 srcLinks.clear();
182 dstLinks.clear();
183 log.info("Stopped");
184 }
185
186 @Override
187 public int getLinkCount() {
188 return links.size();
189 }
190
191 @Override
192 public Iterable<Link> getLinks() {
193 return Collections.unmodifiableCollection(links.values());
194 }
195
196 @Override
197 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
198 // lock for iteration
199 synchronized (srcLinks) {
200 return FluentIterable.from(srcLinks.get(deviceId))
201 .transform(lookupLink())
202 .filter(notNull())
203 .toSet();
204 }
205 }
206
207 @Override
208 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
209 // lock for iteration
210 synchronized (dstLinks) {
211 return FluentIterable.from(dstLinks.get(deviceId))
212 .transform(lookupLink())
213 .filter(notNull())
214 .toSet();
215 }
216 }
217
218 @Override
219 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700220 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700221 }
222
223 @Override
224 public Set<Link> getEgressLinks(ConnectPoint src) {
225 Set<Link> egress = new HashSet<>();
226 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
227 if (linkKey.src().equals(src)) {
228 egress.add(links.get(linkKey));
229 }
230 }
231 return egress;
232 }
233
234 @Override
235 public Set<Link> getIngressLinks(ConnectPoint dst) {
236 Set<Link> ingress = new HashSet<>();
237 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
238 if (linkKey.dst().equals(dst)) {
239 ingress.add(links.get(linkKey));
240 }
241 }
242 return ingress;
243 }
244
245 @Override
246 public LinkEvent createOrUpdateLink(ProviderId providerId,
247 LinkDescription linkDescription) {
248
249 DeviceId dstDeviceId = linkDescription.dst().deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700250 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700251
252 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
253
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700254 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700255 final LinkEvent event;
256 final Timestamped<LinkDescription> mergedDesc;
alshabibdfc7afb2014-10-21 20:13:27 -0700257 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
258 synchronized (map) {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700259 event = createOrUpdateLinkInternal(providerId, deltaDesc);
alshabibdfc7afb2014-10-21 20:13:27 -0700260 mergedDesc = map.get(providerId);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700261 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700262
alshabibdfc7afb2014-10-21 20:13:27 -0700263
264
Madan Jampani2ff05592014-10-10 15:42:47 -0700265 if (event != null) {
266 log.info("Notifying peers of a link update topology event from providerId: "
267 + "{} between src: {} and dst: {}",
268 providerId, linkDescription.src(), linkDescription.dst());
269 try {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700270 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
Madan Jampani2ff05592014-10-10 15:42:47 -0700271 } catch (IOException e) {
272 log.info("Failed to notify peers of a link update topology event from providerId: "
alshabibdfc7afb2014-10-21 20:13:27 -0700273 + "{} between src: {} and dst: {}",
274 providerId, linkDescription.src(), linkDescription.dst());
Madan Jampani2ff05592014-10-10 15:42:47 -0700275 }
276 }
277 return event;
278 }
279
280 private LinkEvent createOrUpdateLinkInternal(
281 ProviderId providerId,
282 Timestamped<LinkDescription> linkDescription) {
283
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700284 final LinkKey key = linkKey(linkDescription.value().src(),
285 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700286 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700287
288 synchronized (descs) {
289 // if the link was previously removed, we should proceed if and
290 // only if this request is more recent.
291 Timestamp linkRemovedTimestamp = removedLinks.get(key);
292 if (linkRemovedTimestamp != null) {
293 if (linkDescription.isNewer(linkRemovedTimestamp)) {
294 removedLinks.remove(key);
295 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700296 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700297 return null;
298 }
299 }
300
301 final Link oldLink = links.get(key);
302 // update description
303 createOrUpdateLinkDescription(descs, providerId, linkDescription);
304 final Link newLink = composeLink(descs);
305 if (oldLink == null) {
306 return createLink(key, newLink);
307 }
308 return updateLink(key, oldLink, newLink);
309 }
310 }
311
312 // Guarded by linkDescs value (=locking each Link)
313 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700314 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700315 ProviderId providerId,
316 Timestamped<LinkDescription> linkDescription) {
317
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700318 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700319 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700320 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700321 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700322 return null;
323 }
324 Timestamped<LinkDescription> newLinkDescription = linkDescription;
325 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700326 // we only allow transition from INDIRECT -> DIRECT
327 final Type newType;
328 if (existingLinkDescription.value().type() == DIRECT) {
329 newType = DIRECT;
330 } else {
331 newType = linkDescription.value().type();
332 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700333 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
334 linkDescription.value().annotations());
335 newLinkDescription = new Timestamped<LinkDescription>(
336 new DefaultLinkDescription(
337 linkDescription.value().src(),
338 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700339 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700340 linkDescription.timestamp());
341 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700342 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700343 }
344
345 // Creates and stores the link and returns the appropriate event.
346 // Guarded by linkDescs value (=locking each Link)
347 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700348 links.put(key, newLink);
349 srcLinks.put(newLink.src().deviceId(), key);
350 dstLinks.put(newLink.dst().deviceId(), key);
351 return new LinkEvent(LINK_ADDED, newLink);
352 }
353
354 // Updates, if necessary the specified link and returns the appropriate event.
355 // Guarded by linkDescs value (=locking each Link)
356 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700357 // Note: INDIRECT -> DIRECT transition only
358 // so that BDDP discovered Link will not overwrite LDDP Link
Madan Jampani2ff05592014-10-10 15:42:47 -0700359 if ((oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
360 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
361
362 links.put(key, newLink);
363 // strictly speaking following can be ommitted
364 srcLinks.put(oldLink.src().deviceId(), key);
365 dstLinks.put(oldLink.dst().deviceId(), key);
366 return new LinkEvent(LINK_UPDATED, newLink);
367 }
368 return null;
369 }
370
371 @Override
372 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700373 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700374
375 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700376 Timestamp timestamp = null;
377 try {
378 timestamp = deviceClockService.getTimestamp(dstDeviceId);
379 } catch (IllegalStateException e) {
380 //there are times when this is called before mastership
381 // handoff correctly completes.
382 return null;
383 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700384
385 LinkEvent event = removeLinkInternal(key, timestamp);
386
387 if (event != null) {
388 log.info("Notifying peers of a link removed topology event for a link "
389 + "between src: {} and dst: {}", src, dst);
390 try {
391 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
392 } catch (IOException e) {
393 log.error("Failed to notify peers of a link removed topology event for a link "
394 + "between src: {} and dst: {}", src, dst);
395 }
396 }
397 return event;
398 }
399
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700400 private static Timestamped<LinkDescription> getPrimaryDescription(
401 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
402
Madan Jampani2ff05592014-10-10 15:42:47 -0700403 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700404 for (Entry<ProviderId, Timestamped<LinkDescription>>
405 e : linkDescriptions.entrySet()) {
406
407 if (!e.getKey().isAncillary()) {
408 return e.getValue();
409 }
410 }
411 }
412 return null;
413 }
414
415
416 // TODO: consider slicing out as Timestamp utils
417 /**
418 * Checks is timestamp is more recent than timestamped object.
419 *
420 * @param timestamp to check if this is more recent then other
421 * @param timestamped object to be tested against
422 * @return true if {@code timestamp} is more recent than {@code timestamped}
423 * or {@code timestamped is null}
424 */
425 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
426 checkNotNull(timestamp);
427 if (timestamped == null) {
428 return true;
429 }
430 return timestamp.compareTo(timestamped.timestamp()) > 0;
431 }
432
433 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
434 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
435 = getOrCreateLinkDescriptions(key);
436
437 synchronized (linkDescriptions) {
438 if (linkDescriptions.isEmpty()) {
439 // never seen such link before. keeping timestamp for record
440 removedLinks.put(key, timestamp);
441 return null;
442 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700443 // accept removal request if given timestamp is newer than
444 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700445 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
446 if (!isMoreRecent(timestamp, prim)) {
447 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700448 return null;
449 }
450 removedLinks.put(key, timestamp);
451 Link link = links.remove(key);
452 linkDescriptions.clear();
453 if (link != null) {
454 srcLinks.remove(link.src().deviceId(), key);
455 dstLinks.remove(link.dst().deviceId(), key);
456 return new LinkEvent(LINK_REMOVED, link);
457 }
458 return null;
459 }
460 }
461
462 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
463 return synchronizedSetMultimap(HashMultimap.<K, V>create());
464 }
465
466 /**
467 * @return primary ProviderID, or randomly chosen one if none exists
468 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700469 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700470 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700471
472 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700473 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700474 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700475 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700476 return e.getKey();
477 } else if (fallBackPrimary == null) {
478 // pick randomly as a fallback in case there is no primary
479 fallBackPrimary = e.getKey();
480 }
481 }
482 return fallBackPrimary;
483 }
484
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700485 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700486 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700487 ProviderId baseProviderId = pickBaseProviderId(descs);
488 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700489
490 ConnectPoint src = base.value().src();
491 ConnectPoint dst = base.value().dst();
492 Type type = base.value().type();
493 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
494 annotations = merge(annotations, base.value().annotations());
495
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700496 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700497 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700498 continue;
499 }
500
501 // TODO: should keep track of Description timestamp
502 // and only merge conflicting keys when timestamp is newer
503 // Currently assuming there will never be a key conflict between
504 // providers
505
506 // annotation merging. not so efficient, should revisit later
507 annotations = merge(annotations, e.getValue().value().annotations());
508 }
509
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700510 return new DefaultLink(baseProviderId, src, dst, type, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700511 }
512
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700513 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700514 Map<ProviderId, Timestamped<LinkDescription>> r;
515 r = linkDescs.get(key);
516 if (r != null) {
517 return r;
518 }
519 r = new HashMap<>();
520 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
521 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
522 if (concurrentlyAdded != null) {
523 return concurrentlyAdded;
524 } else {
525 return r;
526 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700527 }
528
529 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700530 /**
531 * Returns a Function to lookup Link instance using LinkKey from cache.
532 * @return
533 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700534 private Function<LinkKey, Link> lookupLink() {
535 return lookupLink;
536 }
537
538 private final class LookupLink implements Function<LinkKey, Link> {
539 @Override
540 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700541 if (input == null) {
542 return null;
543 } else {
544 return links.get(input);
545 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700546 }
547 }
548
Madan Jampani2ff05592014-10-10 15:42:47 -0700549 private void notifyDelegateIfNotNull(LinkEvent event) {
550 if (event != null) {
551 notifyDelegate(event);
552 }
553 }
554
Madan Jampani2ff05592014-10-10 15:42:47 -0700555 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
556 ClusterMessage message = new ClusterMessage(
557 clusterService.getLocalNode().id(),
558 subject,
559 SERIALIZER.encode(event));
560 clusterCommunicator.broadcast(message);
561 }
562
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700563 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
564 ClusterMessage message = new ClusterMessage(
565 clusterService.getLocalNode().id(),
566 subject,
567 SERIALIZER.encode(event));
568 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700569 }
570
Madan Jampani2ff05592014-10-10 15:42:47 -0700571 private void notifyPeers(InternalLinkEvent event) throws IOException {
572 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
573 }
574
575 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
576 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
577 }
578
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700579 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700580 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700581 try {
582 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
583 } catch (IOException e) {
584 log.debug("Failed to notify peer {} with message {}", peer, event);
585 }
Madan Jampania97e8202014-10-10 17:01:33 -0700586 }
587
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700588 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700589 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700590 try {
591 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
592 } catch (IOException e) {
593 log.debug("Failed to notify peer {} with message {}", peer, event);
594 }
Madan Jampania97e8202014-10-10 17:01:33 -0700595 }
596
597 private final class SendAdvertisementTask implements Runnable {
598
599 @Override
600 public void run() {
601 if (Thread.currentThread().isInterrupted()) {
602 log.info("Interrupted, quitting");
603 return;
604 }
605
606 try {
607 final NodeId self = clusterService.getLocalNode().id();
608 Set<ControllerNode> nodes = clusterService.getNodes();
609
610 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
611 .transform(toNodeId())
612 .toList();
613
614 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHI37083082014-10-13 10:38:38 -0700615 log.debug("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700616 return;
617 }
618
619 NodeId peer;
620 do {
621 int idx = RandomUtils.nextInt(0, nodeIds.size());
622 peer = nodeIds.get(idx);
623 } while (peer.equals(self));
624
625 LinkAntiEntropyAdvertisement ad = createAdvertisement();
626
627 if (Thread.currentThread().isInterrupted()) {
628 log.info("Interrupted, quitting");
629 return;
630 }
631
632 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700633 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
634 } catch (IOException e) {
635 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700636 return;
637 }
638 } catch (Exception e) {
639 // catch all Exception to avoid Scheduled task being suppressed.
640 log.error("Exception thrown while sending advertisement", e);
641 }
642 }
643 }
644
645 private LinkAntiEntropyAdvertisement createAdvertisement() {
646 final NodeId self = clusterService.getLocalNode().id();
647
648 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
649 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
650
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700651 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
Madan Jampania97e8202014-10-10 17:01:33 -0700652 provs : linkDescs.entrySet()) {
653
654 final LinkKey linkKey = provs.getKey();
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700655 final Map<ProviderId, Timestamped<LinkDescription>> linkDesc = provs.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700656 synchronized (linkDesc) {
657 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
658 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
659 }
660 }
661 }
662
663 linkTombstones.putAll(removedLinks);
664
665 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
666 }
667
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700668 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700669
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700670 final NodeId sender = ad.sender();
671 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700672
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700673 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
674 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700675
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700676 final LinkKey key = l.getKey();
677 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
678 synchronized (link) {
679 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700680
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700681 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
682 final ProviderId providerId = p.getKey();
683 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700684
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700685 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
686 // remote
687 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
688 if (remoteTimestamp == null) {
689 remoteTimestamp = ad.linkTombstones().get(key);
690 }
691 if (remoteTimestamp == null ||
692 pDesc.isNewer(remoteTimestamp)) {
693 // I have more recent link description. update peer.
694 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
695 } else {
696 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
697 if (remoteLive != null &&
698 remoteLive.compareTo(pDesc.timestamp()) > 0) {
699 // I have something outdated
700 localOutdated = true;
701 }
702 }
703
704 // search local latest along the way
705 if (localLatest == null ||
706 pDesc.isNewer(localLatest)) {
707 localLatest = pDesc.timestamp();
708 }
709 }
710 // Tests if remote remove is more recent then local latest.
711 final Timestamp remoteRemove = ad.linkTombstones().get(key);
712 if (remoteRemove != null) {
713 if (localLatest != null &&
714 localLatest.compareTo(remoteRemove) < 0) {
715 // remote remove is more recent
716 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
717 }
718 }
Madan Jampania97e8202014-10-10 17:01:33 -0700719 }
720 }
721
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700722 // populate remove info if not known locally
723 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
724 final LinkKey key = remoteRm.getKey();
725 final Timestamp remoteRemove = remoteRm.getValue();
726 // relying on removeLinkInternal to ignore stale info
727 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
728 }
Madan Jampania97e8202014-10-10 17:01:33 -0700729
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700730 if (localOutdated) {
731 // send back advertisement to speed up convergence
732 try {
733 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
734 createAdvertisement());
735 } catch (IOException e) {
736 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700737 }
738 }
739 }
740
Madan Jampani2ff05592014-10-10 15:42:47 -0700741 private class InternalLinkEventListener implements ClusterMessageHandler {
742 @Override
743 public void handle(ClusterMessage message) {
744
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700745 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700746 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
747
748 ProviderId providerId = event.providerId();
749 Timestamped<LinkDescription> linkDescription = event.linkDescription();
750
751 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
752 }
753 }
754
755 private class InternalLinkRemovedEventListener implements ClusterMessageHandler {
756 @Override
757 public void handle(ClusterMessage message) {
758
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700759 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700760 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
761
762 LinkKey linkKey = event.linkKey();
763 Timestamp timestamp = event.timestamp();
764
765 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
766 }
767 }
Madan Jampania97e8202014-10-10 17:01:33 -0700768
769 private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler {
770
771 @Override
772 public void handle(ClusterMessage message) {
Yuta HIGUCHI9a0a1d12014-10-13 22:38:02 -0700773 log.debug("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700774 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
775 handleAntiEntropyAdvertisement(advertisement);
776 }
777 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700778}