blob: d496ebe17b4af2f8b982f6e125632963778aa6c0 [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;
Madan Jampania97e8202014-10-10 17:01:33 -070024import org.apache.commons.lang3.RandomUtils;
Madan Jampani2ff05592014-10-10 15:42:47 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
31import org.onlab.onos.cluster.ClusterService;
Madan Jampania97e8202014-10-10 17:01:33 -070032import org.onlab.onos.cluster.ControllerNode;
33import org.onlab.onos.cluster.NodeId;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080034import org.onlab.onos.net.AnnotationKeys;
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;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080068import java.util.Map.Entry;
Thomas Vachuska29a6a782014-11-10 21:31:41 -080069import java.util.Objects;
Madan Jampani2ff05592014-10-10 15:42:47 -070070import java.util.Set;
Madan Jampani2ff05592014-10-10 15:42:47 -070071import java.util.concurrent.ConcurrentHashMap;
72import java.util.concurrent.ConcurrentMap;
Madan Jampania97e8202014-10-10 17:01:33 -070073import java.util.concurrent.ScheduledExecutorService;
74import java.util.concurrent.TimeUnit;
Madan Jampani2ff05592014-10-10 15:42:47 -070075
Thomas Vachuska57126fe2014-11-11 17:13:24 -080076import static com.google.common.base.Preconditions.checkNotNull;
77import static com.google.common.base.Predicates.notNull;
78import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Madan Jampania97e8202014-10-10 17:01:33 -070079import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
80import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070081import static org.onlab.onos.net.DefaultAnnotations.merge;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080082import static org.onlab.onos.net.DefaultAnnotations.union;
83import static org.onlab.onos.net.Link.State.ACTIVE;
84import static org.onlab.onos.net.Link.State.INACTIVE;
Madan Jampani2ff05592014-10-10 15:42:47 -070085import static org.onlab.onos.net.Link.Type.DIRECT;
86import static org.onlab.onos.net.Link.Type.INDIRECT;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070087import static org.onlab.onos.net.LinkKey.linkKey;
Madan Jampani2ff05592014-10-10 15:42:47 -070088import static org.onlab.onos.net.link.LinkEvent.Type.*;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080089import static org.onlab.onos.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Madan Jampania97e8202014-10-10 17:01:33 -070090import static org.onlab.util.Tools.namedThreads;
Madan Jampani2ff05592014-10-10 15:42:47 -070091import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani2ff05592014-10-10 15:42:47 -070092
93/**
94 * Manages inventory of infrastructure links in distributed data store
95 * that uses optimistic replication and gossip based techniques.
96 */
97@Component(immediate = true)
98@Service
99public class GossipLinkStore
100 extends AbstractStore<LinkEvent, LinkStoreDelegate>
101 implements LinkStore {
102
103 private final Logger log = getLogger(getClass());
104
105 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700106 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700107 new ConcurrentHashMap<>();
108
109 // Link instance cache
110 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
111
112 // Egress and ingress link sets
113 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
114 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
115
116 // Remove links
117 private final Map<LinkKey, Timestamp> removedLinks = Maps.newHashMap();
118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700120 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected ClusterCommunicationService clusterCommunicator;
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected ClusterService clusterService;
127
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800128 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700129 @Override
130 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700131 serializerPool = KryoNamespace.newBuilder()
Madan Jampani2ff05592014-10-10 15:42:47 -0700132 .register(DistributedStoreSerializers.COMMON)
133 .register(InternalLinkEvent.class)
134 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700135 .register(LinkAntiEntropyAdvertisement.class)
136 .register(LinkFragmentId.class)
Madan Jampani2ff05592014-10-10 15:42:47 -0700137 .build()
138 .populate(1);
139 }
140 };
141
Madan Jampania97e8202014-10-10 17:01:33 -0700142 private ScheduledExecutorService executor;
143
Madan Jampani2ff05592014-10-10 15:42:47 -0700144 @Activate
145 public void activate() {
146
147 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700148 GossipLinkStoreMessageSubjects.LINK_UPDATE,
149 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700150 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700151 GossipLinkStoreMessageSubjects.LINK_REMOVED,
152 new InternalLinkRemovedEventListener());
153 clusterCommunicator.addSubscriber(
154 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
155 new InternalLinkAntiEntropyAdvertisementListener());
156
157 executor =
158 newSingleThreadScheduledExecutor(namedThreads("link-anti-entropy-%d"));
159
160 // TODO: Make these configurable
161 long initialDelaySec = 5;
162 long periodSec = 5;
163 // start anti-entropy thread
164 executor.scheduleAtFixedRate(new SendAdvertisementTask(),
165 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700166
167 log.info("Started");
168 }
169
170 @Deactivate
171 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700172
173 executor.shutdownNow();
174 try {
175 if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
176 log.error("Timeout during executor shutdown");
177 }
178 } catch (InterruptedException e) {
179 log.error("Error during executor shutdown", e);
180 }
181
Madan Jampani2ff05592014-10-10 15:42:47 -0700182 linkDescs.clear();
183 links.clear();
184 srcLinks.clear();
185 dstLinks.clear();
186 log.info("Stopped");
187 }
188
189 @Override
190 public int getLinkCount() {
191 return links.size();
192 }
193
194 @Override
195 public Iterable<Link> getLinks() {
196 return Collections.unmodifiableCollection(links.values());
197 }
198
199 @Override
200 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
201 // lock for iteration
202 synchronized (srcLinks) {
203 return FluentIterable.from(srcLinks.get(deviceId))
204 .transform(lookupLink())
205 .filter(notNull())
206 .toSet();
207 }
208 }
209
210 @Override
211 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
212 // lock for iteration
213 synchronized (dstLinks) {
214 return FluentIterable.from(dstLinks.get(deviceId))
215 .transform(lookupLink())
216 .filter(notNull())
217 .toSet();
218 }
219 }
220
221 @Override
222 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700223 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700224 }
225
226 @Override
227 public Set<Link> getEgressLinks(ConnectPoint src) {
228 Set<Link> egress = new HashSet<>();
229 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
230 if (linkKey.src().equals(src)) {
231 egress.add(links.get(linkKey));
232 }
233 }
234 return egress;
235 }
236
237 @Override
238 public Set<Link> getIngressLinks(ConnectPoint dst) {
239 Set<Link> ingress = new HashSet<>();
240 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
241 if (linkKey.dst().equals(dst)) {
242 ingress.add(links.get(linkKey));
243 }
244 }
245 return ingress;
246 }
247
248 @Override
249 public LinkEvent createOrUpdateLink(ProviderId providerId,
250 LinkDescription linkDescription) {
251
252 DeviceId dstDeviceId = linkDescription.dst().deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700253 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700254
255 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
256
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700257 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700258 final LinkEvent event;
259 final Timestamped<LinkDescription> mergedDesc;
alshabibdfc7afb2014-10-21 20:13:27 -0700260 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
261 synchronized (map) {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700262 event = createOrUpdateLinkInternal(providerId, deltaDesc);
alshabibdfc7afb2014-10-21 20:13:27 -0700263 mergedDesc = map.get(providerId);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700264 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700265
266 if (event != null) {
267 log.info("Notifying peers of a link update topology event from providerId: "
268 + "{} between src: {} and dst: {}",
269 providerId, linkDescription.src(), linkDescription.dst());
270 try {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700271 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
Madan Jampani2ff05592014-10-10 15:42:47 -0700272 } catch (IOException e) {
273 log.info("Failed to notify peers of a link update topology event from providerId: "
alshabibdfc7afb2014-10-21 20:13:27 -0700274 + "{} between src: {} and dst: {}",
275 providerId, linkDescription.src(), linkDescription.dst());
Madan Jampani2ff05592014-10-10 15:42:47 -0700276 }
277 }
278 return event;
279 }
280
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800281 @Override
282 public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) {
283 Link link = getLink(src, dst);
284 if (link == null) {
285 return null;
286 }
287
288 if (link.isDurable()) {
289 // FIXME: this is not the right thing to call for the gossip store; will not sync link state!!!
290 return link.state() == INACTIVE ? null :
291 updateLink(linkKey(link.src(), link.dst()), link,
292 new DefaultLink(link.providerId(),
293 link.src(), link.dst(),
294 link.type(), INACTIVE,
295 link.isDurable(),
296 link.annotations()));
297 }
298 return removeLink(src, dst);
299 }
300
Madan Jampani2ff05592014-10-10 15:42:47 -0700301 private LinkEvent createOrUpdateLinkInternal(
302 ProviderId providerId,
303 Timestamped<LinkDescription> linkDescription) {
304
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700305 final LinkKey key = linkKey(linkDescription.value().src(),
306 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700307 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700308
309 synchronized (descs) {
310 // if the link was previously removed, we should proceed if and
311 // only if this request is more recent.
312 Timestamp linkRemovedTimestamp = removedLinks.get(key);
313 if (linkRemovedTimestamp != null) {
314 if (linkDescription.isNewer(linkRemovedTimestamp)) {
315 removedLinks.remove(key);
316 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700317 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700318 return null;
319 }
320 }
321
322 final Link oldLink = links.get(key);
323 // update description
324 createOrUpdateLinkDescription(descs, providerId, linkDescription);
325 final Link newLink = composeLink(descs);
326 if (oldLink == null) {
327 return createLink(key, newLink);
328 }
329 return updateLink(key, oldLink, newLink);
330 }
331 }
332
333 // Guarded by linkDescs value (=locking each Link)
334 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700335 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700336 ProviderId providerId,
337 Timestamped<LinkDescription> linkDescription) {
338
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700339 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700340 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700341 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700342 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700343 return null;
344 }
345 Timestamped<LinkDescription> newLinkDescription = linkDescription;
346 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700347 // we only allow transition from INDIRECT -> DIRECT
348 final Type newType;
349 if (existingLinkDescription.value().type() == DIRECT) {
350 newType = DIRECT;
351 } else {
352 newType = linkDescription.value().type();
353 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700354 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
355 linkDescription.value().annotations());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800356 newLinkDescription = new Timestamped<>(
Madan Jampani2ff05592014-10-10 15:42:47 -0700357 new DefaultLinkDescription(
358 linkDescription.value().src(),
359 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700360 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700361 linkDescription.timestamp());
362 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700363 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700364 }
365
366 // Creates and stores the link and returns the appropriate event.
367 // Guarded by linkDescs value (=locking each Link)
368 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700369 links.put(key, newLink);
370 srcLinks.put(newLink.src().deviceId(), key);
371 dstLinks.put(newLink.dst().deviceId(), key);
372 return new LinkEvent(LINK_ADDED, newLink);
373 }
374
375 // Updates, if necessary the specified link and returns the appropriate event.
376 // Guarded by linkDescs value (=locking each Link)
377 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700378 // Note: INDIRECT -> DIRECT transition only
379 // so that BDDP discovered Link will not overwrite LDDP Link
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800380 if (oldLink.state() != newLink.state() ||
381 (oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
Madan Jampani2ff05592014-10-10 15:42:47 -0700382 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
383
384 links.put(key, newLink);
385 // strictly speaking following can be ommitted
386 srcLinks.put(oldLink.src().deviceId(), key);
387 dstLinks.put(oldLink.dst().deviceId(), key);
388 return new LinkEvent(LINK_UPDATED, newLink);
389 }
390 return null;
391 }
392
393 @Override
394 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700395 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700396
397 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700398 Timestamp timestamp = null;
399 try {
400 timestamp = deviceClockService.getTimestamp(dstDeviceId);
401 } catch (IllegalStateException e) {
Yuta HIGUCHId6a0ac32014-11-04 09:26:31 -0800402 log.warn("Failed to remove link {}, was not the master", key);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700403 //there are times when this is called before mastership
404 // handoff correctly completes.
405 return null;
406 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700407
408 LinkEvent event = removeLinkInternal(key, timestamp);
409
410 if (event != null) {
411 log.info("Notifying peers of a link removed topology event for a link "
412 + "between src: {} and dst: {}", src, dst);
413 try {
414 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
415 } catch (IOException e) {
416 log.error("Failed to notify peers of a link removed topology event for a link "
417 + "between src: {} and dst: {}", src, dst);
418 }
419 }
420 return event;
421 }
422
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700423 private static Timestamped<LinkDescription> getPrimaryDescription(
424 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
425
Madan Jampani2ff05592014-10-10 15:42:47 -0700426 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700427 for (Entry<ProviderId, Timestamped<LinkDescription>>
428 e : linkDescriptions.entrySet()) {
429
430 if (!e.getKey().isAncillary()) {
431 return e.getValue();
432 }
433 }
434 }
435 return null;
436 }
437
438
439 // TODO: consider slicing out as Timestamp utils
440 /**
441 * Checks is timestamp is more recent than timestamped object.
442 *
443 * @param timestamp to check if this is more recent then other
444 * @param timestamped object to be tested against
445 * @return true if {@code timestamp} is more recent than {@code timestamped}
446 * or {@code timestamped is null}
447 */
448 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
449 checkNotNull(timestamp);
450 if (timestamped == null) {
451 return true;
452 }
453 return timestamp.compareTo(timestamped.timestamp()) > 0;
454 }
455
456 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
457 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
458 = getOrCreateLinkDescriptions(key);
459
460 synchronized (linkDescriptions) {
461 if (linkDescriptions.isEmpty()) {
462 // never seen such link before. keeping timestamp for record
463 removedLinks.put(key, timestamp);
464 return null;
465 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700466 // accept removal request if given timestamp is newer than
467 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700468 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
469 if (!isMoreRecent(timestamp, prim)) {
470 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700471 return null;
472 }
473 removedLinks.put(key, timestamp);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800474 Link link = links.remove(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700475 linkDescriptions.clear();
476 if (link != null) {
477 srcLinks.remove(link.src().deviceId(), key);
478 dstLinks.remove(link.dst().deviceId(), key);
479 return new LinkEvent(LINK_REMOVED, link);
480 }
481 return null;
482 }
483 }
484
485 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
486 return synchronizedSetMultimap(HashMultimap.<K, V>create());
487 }
488
489 /**
490 * @return primary ProviderID, or randomly chosen one if none exists
491 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700492 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700493 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700494
495 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700496 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700497 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700498 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700499 return e.getKey();
500 } else if (fallBackPrimary == null) {
501 // pick randomly as a fallback in case there is no primary
502 fallBackPrimary = e.getKey();
503 }
504 }
505 return fallBackPrimary;
506 }
507
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700508 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700509 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700510 ProviderId baseProviderId = pickBaseProviderId(descs);
511 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700512
513 ConnectPoint src = base.value().src();
514 ConnectPoint dst = base.value().dst();
515 Type type = base.value().type();
516 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
517 annotations = merge(annotations, base.value().annotations());
518
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700519 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700520 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700521 continue;
522 }
523
524 // TODO: should keep track of Description timestamp
525 // and only merge conflicting keys when timestamp is newer
526 // Currently assuming there will never be a key conflict between
527 // providers
528
529 // annotation merging. not so efficient, should revisit later
530 annotations = merge(annotations, e.getValue().value().annotations());
531 }
532
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800533 boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
534 boolean isActive = !Objects.equals(annotations.value(AnnotationKeys.INACTIVE), "true");
535 return new DefaultLink(baseProviderId, src, dst, type,
536 isActive ? ACTIVE : INACTIVE, isDurable, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700537 }
538
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700539 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700540 Map<ProviderId, Timestamped<LinkDescription>> r;
541 r = linkDescs.get(key);
542 if (r != null) {
543 return r;
544 }
545 r = new HashMap<>();
546 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
547 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
548 if (concurrentlyAdded != null) {
549 return concurrentlyAdded;
550 } else {
551 return r;
552 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700553 }
554
555 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800556
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700557 /**
558 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800559 *
560 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700561 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700562 private Function<LinkKey, Link> lookupLink() {
563 return lookupLink;
564 }
565
566 private final class LookupLink implements Function<LinkKey, Link> {
567 @Override
568 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700569 if (input == null) {
570 return null;
571 } else {
572 return links.get(input);
573 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700574 }
575 }
576
Madan Jampani2ff05592014-10-10 15:42:47 -0700577 private void notifyDelegateIfNotNull(LinkEvent event) {
578 if (event != null) {
579 notifyDelegate(event);
580 }
581 }
582
Madan Jampani2ff05592014-10-10 15:42:47 -0700583 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
584 ClusterMessage message = new ClusterMessage(
585 clusterService.getLocalNode().id(),
586 subject,
587 SERIALIZER.encode(event));
588 clusterCommunicator.broadcast(message);
589 }
590
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700591 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
592 ClusterMessage message = new ClusterMessage(
593 clusterService.getLocalNode().id(),
594 subject,
595 SERIALIZER.encode(event));
596 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700597 }
598
Madan Jampani2ff05592014-10-10 15:42:47 -0700599 private void notifyPeers(InternalLinkEvent event) throws IOException {
600 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
601 }
602
603 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
604 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
605 }
606
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700607 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700608 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700609 try {
610 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
611 } catch (IOException e) {
612 log.debug("Failed to notify peer {} with message {}", peer, event);
613 }
Madan Jampania97e8202014-10-10 17:01:33 -0700614 }
615
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700616 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700617 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700618 try {
619 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
620 } catch (IOException e) {
621 log.debug("Failed to notify peer {} with message {}", peer, event);
622 }
Madan Jampania97e8202014-10-10 17:01:33 -0700623 }
624
625 private final class SendAdvertisementTask implements Runnable {
626
627 @Override
628 public void run() {
629 if (Thread.currentThread().isInterrupted()) {
630 log.info("Interrupted, quitting");
631 return;
632 }
633
634 try {
635 final NodeId self = clusterService.getLocalNode().id();
636 Set<ControllerNode> nodes = clusterService.getNodes();
637
638 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
639 .transform(toNodeId())
640 .toList();
641
642 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHI37083082014-10-13 10:38:38 -0700643 log.debug("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700644 return;
645 }
646
647 NodeId peer;
648 do {
649 int idx = RandomUtils.nextInt(0, nodeIds.size());
650 peer = nodeIds.get(idx);
651 } while (peer.equals(self));
652
653 LinkAntiEntropyAdvertisement ad = createAdvertisement();
654
655 if (Thread.currentThread().isInterrupted()) {
656 log.info("Interrupted, quitting");
657 return;
658 }
659
660 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700661 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
662 } catch (IOException e) {
663 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700664 return;
665 }
666 } catch (Exception e) {
667 // catch all Exception to avoid Scheduled task being suppressed.
668 log.error("Exception thrown while sending advertisement", e);
669 }
670 }
671 }
672
673 private LinkAntiEntropyAdvertisement createAdvertisement() {
674 final NodeId self = clusterService.getLocalNode().id();
675
676 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
677 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
678
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700679 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
Madan Jampania97e8202014-10-10 17:01:33 -0700680 provs : linkDescs.entrySet()) {
681
682 final LinkKey linkKey = provs.getKey();
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700683 final Map<ProviderId, Timestamped<LinkDescription>> linkDesc = provs.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700684 synchronized (linkDesc) {
685 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
686 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
687 }
688 }
689 }
690
691 linkTombstones.putAll(removedLinks);
692
693 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
694 }
695
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700696 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700697
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700698 final NodeId sender = ad.sender();
699 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700700
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700701 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
702 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700703
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700704 final LinkKey key = l.getKey();
705 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
706 synchronized (link) {
707 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700708
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700709 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
710 final ProviderId providerId = p.getKey();
711 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700712
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700713 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
714 // remote
715 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
716 if (remoteTimestamp == null) {
717 remoteTimestamp = ad.linkTombstones().get(key);
718 }
719 if (remoteTimestamp == null ||
720 pDesc.isNewer(remoteTimestamp)) {
721 // I have more recent link description. update peer.
722 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
723 } else {
724 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
725 if (remoteLive != null &&
726 remoteLive.compareTo(pDesc.timestamp()) > 0) {
727 // I have something outdated
728 localOutdated = true;
729 }
730 }
731
732 // search local latest along the way
733 if (localLatest == null ||
734 pDesc.isNewer(localLatest)) {
735 localLatest = pDesc.timestamp();
736 }
737 }
738 // Tests if remote remove is more recent then local latest.
739 final Timestamp remoteRemove = ad.linkTombstones().get(key);
740 if (remoteRemove != null) {
741 if (localLatest != null &&
742 localLatest.compareTo(remoteRemove) < 0) {
743 // remote remove is more recent
744 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
745 }
746 }
Madan Jampania97e8202014-10-10 17:01:33 -0700747 }
748 }
749
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700750 // populate remove info if not known locally
751 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
752 final LinkKey key = remoteRm.getKey();
753 final Timestamp remoteRemove = remoteRm.getValue();
754 // relying on removeLinkInternal to ignore stale info
755 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
756 }
Madan Jampania97e8202014-10-10 17:01:33 -0700757
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700758 if (localOutdated) {
759 // send back advertisement to speed up convergence
760 try {
761 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
762 createAdvertisement());
763 } catch (IOException e) {
764 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700765 }
766 }
767 }
768
Madan Jampani2ff05592014-10-10 15:42:47 -0700769 private class InternalLinkEventListener implements ClusterMessageHandler {
770 @Override
771 public void handle(ClusterMessage message) {
772
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700773 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700774 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
775
776 ProviderId providerId = event.providerId();
777 Timestamped<LinkDescription> linkDescription = event.linkDescription();
778
779 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
780 }
781 }
782
783 private class InternalLinkRemovedEventListener implements ClusterMessageHandler {
784 @Override
785 public void handle(ClusterMessage message) {
786
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700787 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700788 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
789
790 LinkKey linkKey = event.linkKey();
791 Timestamp timestamp = event.timestamp();
792
793 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
794 }
795 }
Madan Jampania97e8202014-10-10 17:01:33 -0700796
797 private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler {
798
799 @Override
800 public void handle(ClusterMessage message) {
Yuta HIGUCHI9a0a1d12014-10-13 22:38:02 -0700801 log.debug("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700802 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
803 handleAntiEntropyAdvertisement(advertisement);
804 }
805 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700806}