blob: 9810d7a2fbcfd10c8b1fdeb236608360a5f32afa [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;
Thomas Vachuska29a6a782014-11-10 21:31:41 -080068import java.util.Objects;
Madan Jampani2ff05592014-10-10 15:42:47 -070069import java.util.Set;
70import java.util.Map.Entry;
71import 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
Madan Jampania97e8202014-10-10 17:01:33 -070076import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
77import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070078import static org.onlab.onos.net.DefaultAnnotations.union;
79import static org.onlab.onos.net.DefaultAnnotations.merge;
80import static org.onlab.onos.net.Link.Type.DIRECT;
81import static org.onlab.onos.net.Link.Type.INDIRECT;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070082import static org.onlab.onos.net.LinkKey.linkKey;
Madan Jampani2ff05592014-10-10 15:42:47 -070083import static org.onlab.onos.net.link.LinkEvent.Type.*;
Madan Jampania97e8202014-10-10 17:01:33 -070084import static org.onlab.util.Tools.namedThreads;
Madan Jampani2ff05592014-10-10 15:42:47 -070085import static org.slf4j.LoggerFactory.getLogger;
86import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -070087import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampani2ff05592014-10-10 15:42:47 -070088import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -070089import static org.onlab.onos.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Madan Jampani2ff05592014-10-10 15:42:47 -070090
91/**
92 * Manages inventory of infrastructure links in distributed data store
93 * that uses optimistic replication and gossip based techniques.
94 */
95@Component(immediate = true)
96@Service
97public class GossipLinkStore
98 extends AbstractStore<LinkEvent, LinkStoreDelegate>
99 implements LinkStore {
100
101 private final Logger log = getLogger(getClass());
102
103 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700104 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700105 new ConcurrentHashMap<>();
106
107 // Link instance cache
108 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
109
110 // Egress and ingress link sets
111 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
112 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
113
114 // Remove links
115 private final Map<LinkKey, Timestamp> removedLinks = Maps.newHashMap();
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700118 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected ClusterCommunicationService clusterCommunicator;
122
123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected ClusterService clusterService;
125
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800126 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700127 @Override
128 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700129 serializerPool = KryoNamespace.newBuilder()
Madan Jampani2ff05592014-10-10 15:42:47 -0700130 .register(DistributedStoreSerializers.COMMON)
131 .register(InternalLinkEvent.class)
132 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700133 .register(LinkAntiEntropyAdvertisement.class)
134 .register(LinkFragmentId.class)
Madan Jampani2ff05592014-10-10 15:42:47 -0700135 .build()
136 .populate(1);
137 }
138 };
139
Madan Jampania97e8202014-10-10 17:01:33 -0700140 private ScheduledExecutorService executor;
141
Madan Jampani2ff05592014-10-10 15:42:47 -0700142 @Activate
143 public void activate() {
144
145 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700146 GossipLinkStoreMessageSubjects.LINK_UPDATE,
147 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700148 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700149 GossipLinkStoreMessageSubjects.LINK_REMOVED,
150 new InternalLinkRemovedEventListener());
151 clusterCommunicator.addSubscriber(
152 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
153 new InternalLinkAntiEntropyAdvertisementListener());
154
155 executor =
156 newSingleThreadScheduledExecutor(namedThreads("link-anti-entropy-%d"));
157
158 // TODO: Make these configurable
159 long initialDelaySec = 5;
160 long periodSec = 5;
161 // start anti-entropy thread
162 executor.scheduleAtFixedRate(new SendAdvertisementTask(),
163 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700164
165 log.info("Started");
166 }
167
168 @Deactivate
169 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700170
171 executor.shutdownNow();
172 try {
173 if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
174 log.error("Timeout during executor shutdown");
175 }
176 } catch (InterruptedException e) {
177 log.error("Error during executor shutdown", e);
178 }
179
Madan Jampani2ff05592014-10-10 15:42:47 -0700180 linkDescs.clear();
181 links.clear();
182 srcLinks.clear();
183 dstLinks.clear();
184 log.info("Stopped");
185 }
186
187 @Override
188 public int getLinkCount() {
189 return links.size();
190 }
191
192 @Override
193 public Iterable<Link> getLinks() {
194 return Collections.unmodifiableCollection(links.values());
195 }
196
197 @Override
198 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
199 // lock for iteration
200 synchronized (srcLinks) {
201 return FluentIterable.from(srcLinks.get(deviceId))
202 .transform(lookupLink())
203 .filter(notNull())
204 .toSet();
205 }
206 }
207
208 @Override
209 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
210 // lock for iteration
211 synchronized (dstLinks) {
212 return FluentIterable.from(dstLinks.get(deviceId))
213 .transform(lookupLink())
214 .filter(notNull())
215 .toSet();
216 }
217 }
218
219 @Override
220 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700221 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700222 }
223
224 @Override
225 public Set<Link> getEgressLinks(ConnectPoint src) {
226 Set<Link> egress = new HashSet<>();
227 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
228 if (linkKey.src().equals(src)) {
229 egress.add(links.get(linkKey));
230 }
231 }
232 return egress;
233 }
234
235 @Override
236 public Set<Link> getIngressLinks(ConnectPoint dst) {
237 Set<Link> ingress = new HashSet<>();
238 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
239 if (linkKey.dst().equals(dst)) {
240 ingress.add(links.get(linkKey));
241 }
242 }
243 return ingress;
244 }
245
246 @Override
247 public LinkEvent createOrUpdateLink(ProviderId providerId,
248 LinkDescription linkDescription) {
249
250 DeviceId dstDeviceId = linkDescription.dst().deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700251 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700252
253 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
254
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700255 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700256 final LinkEvent event;
257 final Timestamped<LinkDescription> mergedDesc;
alshabibdfc7afb2014-10-21 20:13:27 -0700258 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
259 synchronized (map) {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700260 event = createOrUpdateLinkInternal(providerId, deltaDesc);
alshabibdfc7afb2014-10-21 20:13:27 -0700261 mergedDesc = map.get(providerId);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700262 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700263
alshabibdfc7afb2014-10-21 20:13:27 -0700264
265
Madan Jampani2ff05592014-10-10 15:42:47 -0700266 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
281 private LinkEvent createOrUpdateLinkInternal(
282 ProviderId providerId,
283 Timestamped<LinkDescription> linkDescription) {
284
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700285 final LinkKey key = linkKey(linkDescription.value().src(),
286 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700287 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700288
289 synchronized (descs) {
290 // if the link was previously removed, we should proceed if and
291 // only if this request is more recent.
292 Timestamp linkRemovedTimestamp = removedLinks.get(key);
293 if (linkRemovedTimestamp != null) {
294 if (linkDescription.isNewer(linkRemovedTimestamp)) {
295 removedLinks.remove(key);
296 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700297 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700298 return null;
299 }
300 }
301
302 final Link oldLink = links.get(key);
303 // update description
304 createOrUpdateLinkDescription(descs, providerId, linkDescription);
305 final Link newLink = composeLink(descs);
306 if (oldLink == null) {
307 return createLink(key, newLink);
308 }
309 return updateLink(key, oldLink, newLink);
310 }
311 }
312
313 // Guarded by linkDescs value (=locking each Link)
314 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700315 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700316 ProviderId providerId,
317 Timestamped<LinkDescription> linkDescription) {
318
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700319 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700320 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700321 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700322 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700323 return null;
324 }
325 Timestamped<LinkDescription> newLinkDescription = linkDescription;
326 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700327 // we only allow transition from INDIRECT -> DIRECT
328 final Type newType;
329 if (existingLinkDescription.value().type() == DIRECT) {
330 newType = DIRECT;
331 } else {
332 newType = linkDescription.value().type();
333 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700334 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
335 linkDescription.value().annotations());
336 newLinkDescription = new Timestamped<LinkDescription>(
337 new DefaultLinkDescription(
338 linkDescription.value().src(),
339 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700340 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700341 linkDescription.timestamp());
342 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700343 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700344 }
345
346 // Creates and stores the link and returns the appropriate event.
347 // Guarded by linkDescs value (=locking each Link)
348 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700349 links.put(key, newLink);
350 srcLinks.put(newLink.src().deviceId(), key);
351 dstLinks.put(newLink.dst().deviceId(), key);
352 return new LinkEvent(LINK_ADDED, newLink);
353 }
354
355 // Updates, if necessary the specified link and returns the appropriate event.
356 // Guarded by linkDescs value (=locking each Link)
357 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700358 // Note: INDIRECT -> DIRECT transition only
359 // so that BDDP discovered Link will not overwrite LDDP Link
Madan Jampani2ff05592014-10-10 15:42:47 -0700360 if ((oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
361 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
362
363 links.put(key, newLink);
364 // strictly speaking following can be ommitted
365 srcLinks.put(oldLink.src().deviceId(), key);
366 dstLinks.put(oldLink.dst().deviceId(), key);
367 return new LinkEvent(LINK_UPDATED, newLink);
368 }
369 return null;
370 }
371
372 @Override
373 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700374 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700375
376 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700377 Timestamp timestamp = null;
378 try {
379 timestamp = deviceClockService.getTimestamp(dstDeviceId);
380 } catch (IllegalStateException e) {
Yuta HIGUCHId6a0ac32014-11-04 09:26:31 -0800381 log.warn("Failed to remove link {}, was not the master", key);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700382 //there are times when this is called before mastership
383 // handoff correctly completes.
384 return null;
385 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700386
387 LinkEvent event = removeLinkInternal(key, timestamp);
388
389 if (event != null) {
390 log.info("Notifying peers of a link removed topology event for a link "
391 + "between src: {} and dst: {}", src, dst);
392 try {
393 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
394 } catch (IOException e) {
395 log.error("Failed to notify peers of a link removed topology event for a link "
396 + "between src: {} and dst: {}", src, dst);
397 }
398 }
399 return event;
400 }
401
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700402 private static Timestamped<LinkDescription> getPrimaryDescription(
403 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
404
Madan Jampani2ff05592014-10-10 15:42:47 -0700405 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700406 for (Entry<ProviderId, Timestamped<LinkDescription>>
407 e : linkDescriptions.entrySet()) {
408
409 if (!e.getKey().isAncillary()) {
410 return e.getValue();
411 }
412 }
413 }
414 return null;
415 }
416
417
418 // TODO: consider slicing out as Timestamp utils
419 /**
420 * Checks is timestamp is more recent than timestamped object.
421 *
422 * @param timestamp to check if this is more recent then other
423 * @param timestamped object to be tested against
424 * @return true if {@code timestamp} is more recent than {@code timestamped}
425 * or {@code timestamped is null}
426 */
427 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
428 checkNotNull(timestamp);
429 if (timestamped == null) {
430 return true;
431 }
432 return timestamp.compareTo(timestamped.timestamp()) > 0;
433 }
434
435 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
436 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
437 = getOrCreateLinkDescriptions(key);
438
439 synchronized (linkDescriptions) {
440 if (linkDescriptions.isEmpty()) {
441 // never seen such link before. keeping timestamp for record
442 removedLinks.put(key, timestamp);
443 return null;
444 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700445 // accept removal request if given timestamp is newer than
446 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700447 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
448 if (!isMoreRecent(timestamp, prim)) {
449 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700450 return null;
451 }
Thomas Vachuska29a6a782014-11-10 21:31:41 -0800452 Link link = links.get(key);
453 if (isDurable(link)) {
454 return null;
455 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700456 removedLinks.put(key, timestamp);
Thomas Vachuska29a6a782014-11-10 21:31:41 -0800457 links.remove(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700458 linkDescriptions.clear();
459 if (link != null) {
460 srcLinks.remove(link.src().deviceId(), key);
461 dstLinks.remove(link.dst().deviceId(), key);
462 return new LinkEvent(LINK_REMOVED, link);
463 }
464 return null;
465 }
466 }
467
Thomas Vachuska29a6a782014-11-10 21:31:41 -0800468 // Indicates if the link has been marked as durable via annotations.
469 private boolean isDurable(Link link) {
470 return link != null && Objects.equals(link.annotations().value("durable"), "true");
471 }
472
Madan Jampani2ff05592014-10-10 15:42:47 -0700473 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
474 return synchronizedSetMultimap(HashMultimap.<K, V>create());
475 }
476
477 /**
478 * @return primary ProviderID, or randomly chosen one if none exists
479 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700480 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700481 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700482
483 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700484 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700485 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700486 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700487 return e.getKey();
488 } else if (fallBackPrimary == null) {
489 // pick randomly as a fallback in case there is no primary
490 fallBackPrimary = e.getKey();
491 }
492 }
493 return fallBackPrimary;
494 }
495
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700496 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700497 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700498 ProviderId baseProviderId = pickBaseProviderId(descs);
499 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700500
501 ConnectPoint src = base.value().src();
502 ConnectPoint dst = base.value().dst();
503 Type type = base.value().type();
504 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
505 annotations = merge(annotations, base.value().annotations());
506
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700507 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700508 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700509 continue;
510 }
511
512 // TODO: should keep track of Description timestamp
513 // and only merge conflicting keys when timestamp is newer
514 // Currently assuming there will never be a key conflict between
515 // providers
516
517 // annotation merging. not so efficient, should revisit later
518 annotations = merge(annotations, e.getValue().value().annotations());
519 }
520
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700521 return new DefaultLink(baseProviderId, src, dst, type, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700522 }
523
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700524 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700525 Map<ProviderId, Timestamped<LinkDescription>> r;
526 r = linkDescs.get(key);
527 if (r != null) {
528 return r;
529 }
530 r = new HashMap<>();
531 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
532 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
533 if (concurrentlyAdded != null) {
534 return concurrentlyAdded;
535 } else {
536 return r;
537 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700538 }
539
540 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700541 /**
542 * Returns a Function to lookup Link instance using LinkKey from cache.
543 * @return
544 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700545 private Function<LinkKey, Link> lookupLink() {
546 return lookupLink;
547 }
548
549 private final class LookupLink implements Function<LinkKey, Link> {
550 @Override
551 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700552 if (input == null) {
553 return null;
554 } else {
555 return links.get(input);
556 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700557 }
558 }
559
Madan Jampani2ff05592014-10-10 15:42:47 -0700560 private void notifyDelegateIfNotNull(LinkEvent event) {
561 if (event != null) {
562 notifyDelegate(event);
563 }
564 }
565
Madan Jampani2ff05592014-10-10 15:42:47 -0700566 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
567 ClusterMessage message = new ClusterMessage(
568 clusterService.getLocalNode().id(),
569 subject,
570 SERIALIZER.encode(event));
571 clusterCommunicator.broadcast(message);
572 }
573
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700574 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
575 ClusterMessage message = new ClusterMessage(
576 clusterService.getLocalNode().id(),
577 subject,
578 SERIALIZER.encode(event));
579 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700580 }
581
Madan Jampani2ff05592014-10-10 15:42:47 -0700582 private void notifyPeers(InternalLinkEvent event) throws IOException {
583 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
584 }
585
586 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
587 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
588 }
589
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700590 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700591 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700592 try {
593 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
594 } catch (IOException e) {
595 log.debug("Failed to notify peer {} with message {}", peer, event);
596 }
Madan Jampania97e8202014-10-10 17:01:33 -0700597 }
598
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700599 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700600 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700601 try {
602 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
603 } catch (IOException e) {
604 log.debug("Failed to notify peer {} with message {}", peer, event);
605 }
Madan Jampania97e8202014-10-10 17:01:33 -0700606 }
607
608 private final class SendAdvertisementTask implements Runnable {
609
610 @Override
611 public void run() {
612 if (Thread.currentThread().isInterrupted()) {
613 log.info("Interrupted, quitting");
614 return;
615 }
616
617 try {
618 final NodeId self = clusterService.getLocalNode().id();
619 Set<ControllerNode> nodes = clusterService.getNodes();
620
621 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
622 .transform(toNodeId())
623 .toList();
624
625 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHI37083082014-10-13 10:38:38 -0700626 log.debug("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700627 return;
628 }
629
630 NodeId peer;
631 do {
632 int idx = RandomUtils.nextInt(0, nodeIds.size());
633 peer = nodeIds.get(idx);
634 } while (peer.equals(self));
635
636 LinkAntiEntropyAdvertisement ad = createAdvertisement();
637
638 if (Thread.currentThread().isInterrupted()) {
639 log.info("Interrupted, quitting");
640 return;
641 }
642
643 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700644 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
645 } catch (IOException e) {
646 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700647 return;
648 }
649 } catch (Exception e) {
650 // catch all Exception to avoid Scheduled task being suppressed.
651 log.error("Exception thrown while sending advertisement", e);
652 }
653 }
654 }
655
656 private LinkAntiEntropyAdvertisement createAdvertisement() {
657 final NodeId self = clusterService.getLocalNode().id();
658
659 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
660 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
661
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700662 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
Madan Jampania97e8202014-10-10 17:01:33 -0700663 provs : linkDescs.entrySet()) {
664
665 final LinkKey linkKey = provs.getKey();
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700666 final Map<ProviderId, Timestamped<LinkDescription>> linkDesc = provs.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700667 synchronized (linkDesc) {
668 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
669 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
670 }
671 }
672 }
673
674 linkTombstones.putAll(removedLinks);
675
676 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
677 }
678
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700679 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700680
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700681 final NodeId sender = ad.sender();
682 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700683
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700684 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
685 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700686
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700687 final LinkKey key = l.getKey();
688 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
689 synchronized (link) {
690 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700691
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700692 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
693 final ProviderId providerId = p.getKey();
694 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700695
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700696 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
697 // remote
698 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
699 if (remoteTimestamp == null) {
700 remoteTimestamp = ad.linkTombstones().get(key);
701 }
702 if (remoteTimestamp == null ||
703 pDesc.isNewer(remoteTimestamp)) {
704 // I have more recent link description. update peer.
705 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
706 } else {
707 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
708 if (remoteLive != null &&
709 remoteLive.compareTo(pDesc.timestamp()) > 0) {
710 // I have something outdated
711 localOutdated = true;
712 }
713 }
714
715 // search local latest along the way
716 if (localLatest == null ||
717 pDesc.isNewer(localLatest)) {
718 localLatest = pDesc.timestamp();
719 }
720 }
721 // Tests if remote remove is more recent then local latest.
722 final Timestamp remoteRemove = ad.linkTombstones().get(key);
723 if (remoteRemove != null) {
724 if (localLatest != null &&
725 localLatest.compareTo(remoteRemove) < 0) {
726 // remote remove is more recent
727 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
728 }
729 }
Madan Jampania97e8202014-10-10 17:01:33 -0700730 }
731 }
732
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700733 // populate remove info if not known locally
734 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
735 final LinkKey key = remoteRm.getKey();
736 final Timestamp remoteRemove = remoteRm.getValue();
737 // relying on removeLinkInternal to ignore stale info
738 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
739 }
Madan Jampania97e8202014-10-10 17:01:33 -0700740
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700741 if (localOutdated) {
742 // send back advertisement to speed up convergence
743 try {
744 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
745 createAdvertisement());
746 } catch (IOException e) {
747 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700748 }
749 }
750 }
751
Madan Jampani2ff05592014-10-10 15:42:47 -0700752 private class InternalLinkEventListener implements ClusterMessageHandler {
753 @Override
754 public void handle(ClusterMessage message) {
755
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700756 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700757 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
758
759 ProviderId providerId = event.providerId();
760 Timestamped<LinkDescription> linkDescription = event.linkDescription();
761
762 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
763 }
764 }
765
766 private class InternalLinkRemovedEventListener implements ClusterMessageHandler {
767 @Override
768 public void handle(ClusterMessage message) {
769
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700770 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700771 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
772
773 LinkKey linkKey = event.linkKey();
774 Timestamp timestamp = event.timestamp();
775
776 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
777 }
778 }
Madan Jampania97e8202014-10-10 17:01:33 -0700779
780 private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler {
781
782 @Override
783 public void handle(ClusterMessage message) {
Yuta HIGUCHI9a0a1d12014-10-13 22:38:02 -0700784 log.debug("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700785 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
786 handleAntiEntropyAdvertisement(advertisement);
787 }
788 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700789}