blob: e82742a13c14a55e589e025514dff09dcb807cf3 [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.KryoSerializer;
Yuta HIGUCHI60a190b2014-11-07 16:24:47 -080059import org.onlab.onos.store.serializers.impl.DistributedStoreSerializers;
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
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800125 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700126 @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) {
Yuta HIGUCHId6a0ac32014-11-04 09:26:31 -0800380 log.warn("Failed to remove link {}, was not the master", key);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700381 //there are times when this is called before mastership
382 // handoff correctly completes.
383 return null;
384 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700385
386 LinkEvent event = removeLinkInternal(key, timestamp);
387
388 if (event != null) {
389 log.info("Notifying peers of a link removed topology event for a link "
390 + "between src: {} and dst: {}", src, dst);
391 try {
392 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
393 } catch (IOException e) {
394 log.error("Failed to notify peers of a link removed topology event for a link "
395 + "between src: {} and dst: {}", src, dst);
396 }
397 }
398 return event;
399 }
400
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700401 private static Timestamped<LinkDescription> getPrimaryDescription(
402 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
403
Madan Jampani2ff05592014-10-10 15:42:47 -0700404 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700405 for (Entry<ProviderId, Timestamped<LinkDescription>>
406 e : linkDescriptions.entrySet()) {
407
408 if (!e.getKey().isAncillary()) {
409 return e.getValue();
410 }
411 }
412 }
413 return null;
414 }
415
416
417 // TODO: consider slicing out as Timestamp utils
418 /**
419 * Checks is timestamp is more recent than timestamped object.
420 *
421 * @param timestamp to check if this is more recent then other
422 * @param timestamped object to be tested against
423 * @return true if {@code timestamp} is more recent than {@code timestamped}
424 * or {@code timestamped is null}
425 */
426 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
427 checkNotNull(timestamp);
428 if (timestamped == null) {
429 return true;
430 }
431 return timestamp.compareTo(timestamped.timestamp()) > 0;
432 }
433
434 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
435 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
436 = getOrCreateLinkDescriptions(key);
437
438 synchronized (linkDescriptions) {
439 if (linkDescriptions.isEmpty()) {
440 // never seen such link before. keeping timestamp for record
441 removedLinks.put(key, timestamp);
442 return null;
443 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700444 // accept removal request if given timestamp is newer than
445 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700446 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
447 if (!isMoreRecent(timestamp, prim)) {
448 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700449 return null;
450 }
451 removedLinks.put(key, timestamp);
452 Link link = links.remove(key);
453 linkDescriptions.clear();
454 if (link != null) {
455 srcLinks.remove(link.src().deviceId(), key);
456 dstLinks.remove(link.dst().deviceId(), key);
457 return new LinkEvent(LINK_REMOVED, link);
458 }
459 return null;
460 }
461 }
462
463 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
464 return synchronizedSetMultimap(HashMultimap.<K, V>create());
465 }
466
467 /**
468 * @return primary ProviderID, or randomly chosen one if none exists
469 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700470 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700471 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700472
473 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700474 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700475 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700476 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700477 return e.getKey();
478 } else if (fallBackPrimary == null) {
479 // pick randomly as a fallback in case there is no primary
480 fallBackPrimary = e.getKey();
481 }
482 }
483 return fallBackPrimary;
484 }
485
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700486 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700487 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700488 ProviderId baseProviderId = pickBaseProviderId(descs);
489 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700490
491 ConnectPoint src = base.value().src();
492 ConnectPoint dst = base.value().dst();
493 Type type = base.value().type();
494 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
495 annotations = merge(annotations, base.value().annotations());
496
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700497 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700498 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700499 continue;
500 }
501
502 // TODO: should keep track of Description timestamp
503 // and only merge conflicting keys when timestamp is newer
504 // Currently assuming there will never be a key conflict between
505 // providers
506
507 // annotation merging. not so efficient, should revisit later
508 annotations = merge(annotations, e.getValue().value().annotations());
509 }
510
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700511 return new DefaultLink(baseProviderId, src, dst, type, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700512 }
513
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700514 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700515 Map<ProviderId, Timestamped<LinkDescription>> r;
516 r = linkDescs.get(key);
517 if (r != null) {
518 return r;
519 }
520 r = new HashMap<>();
521 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
522 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
523 if (concurrentlyAdded != null) {
524 return concurrentlyAdded;
525 } else {
526 return r;
527 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700528 }
529
530 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700531 /**
532 * Returns a Function to lookup Link instance using LinkKey from cache.
533 * @return
534 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700535 private Function<LinkKey, Link> lookupLink() {
536 return lookupLink;
537 }
538
539 private final class LookupLink implements Function<LinkKey, Link> {
540 @Override
541 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700542 if (input == null) {
543 return null;
544 } else {
545 return links.get(input);
546 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700547 }
548 }
549
Madan Jampani2ff05592014-10-10 15:42:47 -0700550 private void notifyDelegateIfNotNull(LinkEvent event) {
551 if (event != null) {
552 notifyDelegate(event);
553 }
554 }
555
Madan Jampani2ff05592014-10-10 15:42:47 -0700556 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
557 ClusterMessage message = new ClusterMessage(
558 clusterService.getLocalNode().id(),
559 subject,
560 SERIALIZER.encode(event));
561 clusterCommunicator.broadcast(message);
562 }
563
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700564 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
565 ClusterMessage message = new ClusterMessage(
566 clusterService.getLocalNode().id(),
567 subject,
568 SERIALIZER.encode(event));
569 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700570 }
571
Madan Jampani2ff05592014-10-10 15:42:47 -0700572 private void notifyPeers(InternalLinkEvent event) throws IOException {
573 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
574 }
575
576 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
577 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
578 }
579
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700580 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700581 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700582 try {
583 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
584 } catch (IOException e) {
585 log.debug("Failed to notify peer {} with message {}", peer, event);
586 }
Madan Jampania97e8202014-10-10 17:01:33 -0700587 }
588
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700589 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700590 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700591 try {
592 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
593 } catch (IOException e) {
594 log.debug("Failed to notify peer {} with message {}", peer, event);
595 }
Madan Jampania97e8202014-10-10 17:01:33 -0700596 }
597
598 private final class SendAdvertisementTask implements Runnable {
599
600 @Override
601 public void run() {
602 if (Thread.currentThread().isInterrupted()) {
603 log.info("Interrupted, quitting");
604 return;
605 }
606
607 try {
608 final NodeId self = clusterService.getLocalNode().id();
609 Set<ControllerNode> nodes = clusterService.getNodes();
610
611 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
612 .transform(toNodeId())
613 .toList();
614
615 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHI37083082014-10-13 10:38:38 -0700616 log.debug("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700617 return;
618 }
619
620 NodeId peer;
621 do {
622 int idx = RandomUtils.nextInt(0, nodeIds.size());
623 peer = nodeIds.get(idx);
624 } while (peer.equals(self));
625
626 LinkAntiEntropyAdvertisement ad = createAdvertisement();
627
628 if (Thread.currentThread().isInterrupted()) {
629 log.info("Interrupted, quitting");
630 return;
631 }
632
633 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700634 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
635 } catch (IOException e) {
636 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700637 return;
638 }
639 } catch (Exception e) {
640 // catch all Exception to avoid Scheduled task being suppressed.
641 log.error("Exception thrown while sending advertisement", e);
642 }
643 }
644 }
645
646 private LinkAntiEntropyAdvertisement createAdvertisement() {
647 final NodeId self = clusterService.getLocalNode().id();
648
649 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
650 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
651
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700652 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
Madan Jampania97e8202014-10-10 17:01:33 -0700653 provs : linkDescs.entrySet()) {
654
655 final LinkKey linkKey = provs.getKey();
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700656 final Map<ProviderId, Timestamped<LinkDescription>> linkDesc = provs.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700657 synchronized (linkDesc) {
658 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
659 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
660 }
661 }
662 }
663
664 linkTombstones.putAll(removedLinks);
665
666 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
667 }
668
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700669 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700670
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700671 final NodeId sender = ad.sender();
672 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700673
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700674 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
675 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700676
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700677 final LinkKey key = l.getKey();
678 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
679 synchronized (link) {
680 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700681
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700682 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
683 final ProviderId providerId = p.getKey();
684 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700685
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700686 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
687 // remote
688 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
689 if (remoteTimestamp == null) {
690 remoteTimestamp = ad.linkTombstones().get(key);
691 }
692 if (remoteTimestamp == null ||
693 pDesc.isNewer(remoteTimestamp)) {
694 // I have more recent link description. update peer.
695 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
696 } else {
697 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
698 if (remoteLive != null &&
699 remoteLive.compareTo(pDesc.timestamp()) > 0) {
700 // I have something outdated
701 localOutdated = true;
702 }
703 }
704
705 // search local latest along the way
706 if (localLatest == null ||
707 pDesc.isNewer(localLatest)) {
708 localLatest = pDesc.timestamp();
709 }
710 }
711 // Tests if remote remove is more recent then local latest.
712 final Timestamp remoteRemove = ad.linkTombstones().get(key);
713 if (remoteRemove != null) {
714 if (localLatest != null &&
715 localLatest.compareTo(remoteRemove) < 0) {
716 // remote remove is more recent
717 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
718 }
719 }
Madan Jampania97e8202014-10-10 17:01:33 -0700720 }
721 }
722
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700723 // populate remove info if not known locally
724 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
725 final LinkKey key = remoteRm.getKey();
726 final Timestamp remoteRemove = remoteRm.getValue();
727 // relying on removeLinkInternal to ignore stale info
728 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
729 }
Madan Jampania97e8202014-10-10 17:01:33 -0700730
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700731 if (localOutdated) {
732 // send back advertisement to speed up convergence
733 try {
734 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
735 createAdvertisement());
736 } catch (IOException e) {
737 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700738 }
739 }
740 }
741
Madan Jampani2ff05592014-10-10 15:42:47 -0700742 private class InternalLinkEventListener implements ClusterMessageHandler {
743 @Override
744 public void handle(ClusterMessage message) {
745
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700746 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700747 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
748
749 ProviderId providerId = event.providerId();
750 Timestamped<LinkDescription> linkDescription = event.linkDescription();
751
752 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
753 }
754 }
755
756 private class InternalLinkRemovedEventListener implements ClusterMessageHandler {
757 @Override
758 public void handle(ClusterMessage message) {
759
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700760 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700761 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
762
763 LinkKey linkKey = event.linkKey();
764 Timestamp timestamp = event.timestamp();
765
766 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
767 }
768 }
Madan Jampania97e8202014-10-10 17:01:33 -0700769
770 private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler {
771
772 @Override
773 public void handle(ClusterMessage message) {
Yuta HIGUCHI9a0a1d12014-10-13 22:38:02 -0700774 log.debug("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700775 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
776 handleAntiEntropyAdvertisement(advertisement);
777 }
778 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700779}