blob: d4b0916fc17b21c70126829307973266f084778b [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");
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800534 return new DefaultLink(baseProviderId, src, dst, type, ACTIVE, isDurable, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700535 }
536
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700537 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700538 Map<ProviderId, Timestamped<LinkDescription>> r;
539 r = linkDescs.get(key);
540 if (r != null) {
541 return r;
542 }
543 r = new HashMap<>();
544 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
545 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
546 if (concurrentlyAdded != null) {
547 return concurrentlyAdded;
548 } else {
549 return r;
550 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700551 }
552
553 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800554
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700555 /**
556 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800557 *
558 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700559 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700560 private Function<LinkKey, Link> lookupLink() {
561 return lookupLink;
562 }
563
564 private final class LookupLink implements Function<LinkKey, Link> {
565 @Override
566 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700567 if (input == null) {
568 return null;
569 } else {
570 return links.get(input);
571 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700572 }
573 }
574
Madan Jampani2ff05592014-10-10 15:42:47 -0700575 private void notifyDelegateIfNotNull(LinkEvent event) {
576 if (event != null) {
577 notifyDelegate(event);
578 }
579 }
580
Madan Jampani2ff05592014-10-10 15:42:47 -0700581 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
582 ClusterMessage message = new ClusterMessage(
583 clusterService.getLocalNode().id(),
584 subject,
585 SERIALIZER.encode(event));
586 clusterCommunicator.broadcast(message);
587 }
588
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700589 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
590 ClusterMessage message = new ClusterMessage(
591 clusterService.getLocalNode().id(),
592 subject,
593 SERIALIZER.encode(event));
594 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700595 }
596
Madan Jampani2ff05592014-10-10 15:42:47 -0700597 private void notifyPeers(InternalLinkEvent event) throws IOException {
598 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
599 }
600
601 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
602 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
603 }
604
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700605 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700606 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700607 try {
608 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
609 } catch (IOException e) {
610 log.debug("Failed to notify peer {} with message {}", peer, event);
611 }
Madan Jampania97e8202014-10-10 17:01:33 -0700612 }
613
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700614 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700615 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700616 try {
617 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
618 } catch (IOException e) {
619 log.debug("Failed to notify peer {} with message {}", peer, event);
620 }
Madan Jampania97e8202014-10-10 17:01:33 -0700621 }
622
623 private final class SendAdvertisementTask implements Runnable {
624
625 @Override
626 public void run() {
627 if (Thread.currentThread().isInterrupted()) {
628 log.info("Interrupted, quitting");
629 return;
630 }
631
632 try {
633 final NodeId self = clusterService.getLocalNode().id();
634 Set<ControllerNode> nodes = clusterService.getNodes();
635
636 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
637 .transform(toNodeId())
638 .toList();
639
640 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHI37083082014-10-13 10:38:38 -0700641 log.debug("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700642 return;
643 }
644
645 NodeId peer;
646 do {
647 int idx = RandomUtils.nextInt(0, nodeIds.size());
648 peer = nodeIds.get(idx);
649 } while (peer.equals(self));
650
651 LinkAntiEntropyAdvertisement ad = createAdvertisement();
652
653 if (Thread.currentThread().isInterrupted()) {
654 log.info("Interrupted, quitting");
655 return;
656 }
657
658 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700659 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
660 } catch (IOException e) {
661 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700662 return;
663 }
664 } catch (Exception e) {
665 // catch all Exception to avoid Scheduled task being suppressed.
666 log.error("Exception thrown while sending advertisement", e);
667 }
668 }
669 }
670
671 private LinkAntiEntropyAdvertisement createAdvertisement() {
672 final NodeId self = clusterService.getLocalNode().id();
673
674 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
675 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
676
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700677 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
Madan Jampania97e8202014-10-10 17:01:33 -0700678 provs : linkDescs.entrySet()) {
679
680 final LinkKey linkKey = provs.getKey();
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700681 final Map<ProviderId, Timestamped<LinkDescription>> linkDesc = provs.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700682 synchronized (linkDesc) {
683 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
684 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
685 }
686 }
687 }
688
689 linkTombstones.putAll(removedLinks);
690
691 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
692 }
693
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700694 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700695
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700696 final NodeId sender = ad.sender();
697 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700698
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700699 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
700 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700701
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700702 final LinkKey key = l.getKey();
703 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
704 synchronized (link) {
705 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700706
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700707 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
708 final ProviderId providerId = p.getKey();
709 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700710
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700711 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
712 // remote
713 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
714 if (remoteTimestamp == null) {
715 remoteTimestamp = ad.linkTombstones().get(key);
716 }
717 if (remoteTimestamp == null ||
718 pDesc.isNewer(remoteTimestamp)) {
719 // I have more recent link description. update peer.
720 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
721 } else {
722 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
723 if (remoteLive != null &&
724 remoteLive.compareTo(pDesc.timestamp()) > 0) {
725 // I have something outdated
726 localOutdated = true;
727 }
728 }
729
730 // search local latest along the way
731 if (localLatest == null ||
732 pDesc.isNewer(localLatest)) {
733 localLatest = pDesc.timestamp();
734 }
735 }
736 // Tests if remote remove is more recent then local latest.
737 final Timestamp remoteRemove = ad.linkTombstones().get(key);
738 if (remoteRemove != null) {
739 if (localLatest != null &&
740 localLatest.compareTo(remoteRemove) < 0) {
741 // remote remove is more recent
742 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
743 }
744 }
Madan Jampania97e8202014-10-10 17:01:33 -0700745 }
746 }
747
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700748 // populate remove info if not known locally
749 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
750 final LinkKey key = remoteRm.getKey();
751 final Timestamp remoteRemove = remoteRm.getValue();
752 // relying on removeLinkInternal to ignore stale info
753 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
754 }
Madan Jampania97e8202014-10-10 17:01:33 -0700755
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700756 if (localOutdated) {
757 // send back advertisement to speed up convergence
758 try {
759 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
760 createAdvertisement());
761 } catch (IOException e) {
762 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700763 }
764 }
765 }
766
Madan Jampani2ff05592014-10-10 15:42:47 -0700767 private class InternalLinkEventListener implements ClusterMessageHandler {
768 @Override
769 public void handle(ClusterMessage message) {
770
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700771 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700772 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
773
774 ProviderId providerId = event.providerId();
775 Timestamped<LinkDescription> linkDescription = event.linkDescription();
776
777 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
778 }
779 }
780
781 private class InternalLinkRemovedEventListener implements ClusterMessageHandler {
782 @Override
783 public void handle(ClusterMessage message) {
784
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700785 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700786 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
787
788 LinkKey linkKey = event.linkKey();
789 Timestamp timestamp = event.timestamp();
790
791 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
792 }
793 }
Madan Jampania97e8202014-10-10 17:01:33 -0700794
795 private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler {
796
797 @Override
798 public void handle(ClusterMessage message) {
Yuta HIGUCHI9a0a1d12014-10-13 22:38:02 -0700799 log.debug("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700800 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
801 handleAntiEntropyAdvertisement(advertisement);
802 }
803 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700804}