blob: 6cc45eaa9ed45b2a6afbca74506ee296f15690f7 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.link.impl;
Madan Jampani2ff05592014-10-10 15:42:47 -070017
Jonathan Hart7d656f42015-01-27 14:07:23 -080018import com.google.common.base.Function;
19import com.google.common.collect.FluentIterable;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Multimaps;
22import com.google.common.collect.SetMultimap;
23import com.google.common.collect.Sets;
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;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080031import org.onlab.util.KryoNamespace;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.cluster.ClusterService;
33import org.onosproject.cluster.ControllerNode;
34import org.onosproject.cluster.NodeId;
35import org.onosproject.net.AnnotationKeys;
36import org.onosproject.net.AnnotationsUtil;
37import org.onosproject.net.ConnectPoint;
38import org.onosproject.net.DefaultAnnotations;
39import org.onosproject.net.DefaultLink;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.Link;
42import org.onosproject.net.Link.Type;
43import org.onosproject.net.LinkKey;
44import org.onosproject.net.SparseAnnotations;
45import org.onosproject.net.device.DeviceClockService;
46import org.onosproject.net.link.DefaultLinkDescription;
47import org.onosproject.net.link.LinkDescription;
48import org.onosproject.net.link.LinkEvent;
49import org.onosproject.net.link.LinkStore;
50import org.onosproject.net.link.LinkStoreDelegate;
51import org.onosproject.net.provider.ProviderId;
52import org.onosproject.store.AbstractStore;
53import org.onosproject.store.Timestamp;
54import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
55import org.onosproject.store.cluster.messaging.ClusterMessage;
56import org.onosproject.store.cluster.messaging.ClusterMessageHandler;
57import org.onosproject.store.cluster.messaging.MessageSubject;
58import org.onosproject.store.impl.Timestamped;
59import org.onosproject.store.serializers.KryoSerializer;
60import org.onosproject.store.serializers.impl.DistributedStoreSerializers;
Madan Jampani2ff05592014-10-10 15:42:47 -070061import org.slf4j.Logger;
62
Jonathan Hart7d656f42015-01-27 14:07:23 -080063import java.io.IOException;
64import java.util.Collection;
65import java.util.Collections;
66import java.util.HashMap;
67import java.util.HashSet;
68import java.util.Map;
69import java.util.Map.Entry;
70import java.util.Objects;
71import java.util.Set;
72import java.util.concurrent.ConcurrentHashMap;
73import java.util.concurrent.ConcurrentMap;
74import java.util.concurrent.ExecutorService;
75import java.util.concurrent.Executors;
76import java.util.concurrent.ScheduledExecutorService;
77import java.util.concurrent.TimeUnit;
Madan Jampani2ff05592014-10-10 15:42:47 -070078
Thomas Vachuska57126fe2014-11-11 17:13:24 -080079import static com.google.common.base.Preconditions.checkNotNull;
80import static com.google.common.base.Predicates.notNull;
81import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Madan Jampania97e8202014-10-10 17:01:33 -070082import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080083import static org.onlab.util.Tools.minPriority;
84import static org.onlab.util.Tools.namedThreads;
Brian O'Connorabafb502014-12-02 22:26:20 -080085import static org.onosproject.cluster.ControllerNodeToNodeId.toNodeId;
86import static org.onosproject.net.DefaultAnnotations.merge;
87import static org.onosproject.net.DefaultAnnotations.union;
88import static org.onosproject.net.Link.State.ACTIVE;
89import static org.onosproject.net.Link.State.INACTIVE;
90import static org.onosproject.net.Link.Type.DIRECT;
91import static org.onosproject.net.Link.Type.INDIRECT;
92import static org.onosproject.net.LinkKey.linkKey;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080093import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
94import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
95import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED;
Brian O'Connorabafb502014-12-02 22:26:20 -080096import static org.onosproject.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Madan Jampani2ff05592014-10-10 15:42:47 -070097import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani2ff05592014-10-10 15:42:47 -070098
99/**
100 * Manages inventory of infrastructure links in distributed data store
101 * that uses optimistic replication and gossip based techniques.
102 */
103@Component(immediate = true)
104@Service
105public class GossipLinkStore
106 extends AbstractStore<LinkEvent, LinkStoreDelegate>
107 implements LinkStore {
108
109 private final Logger log = getLogger(getClass());
110
111 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700112 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700113 new ConcurrentHashMap<>();
114
115 // Link instance cache
116 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
117
118 // Egress and ingress link sets
119 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
120 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
121
122 // Remove links
Yuta HIGUCHIb9125562014-12-01 23:28:22 -0800123 private final Map<LinkKey, Timestamp> removedLinks = new ConcurrentHashMap<>();
Madan Jampani2ff05592014-10-10 15:42:47 -0700124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700126 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected ClusterCommunicationService clusterCommunicator;
130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected ClusterService clusterService;
133
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800134 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700135 @Override
136 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700137 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800138 .register(DistributedStoreSerializers.STORE_COMMON)
139 .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
Madan Jampani2ff05592014-10-10 15:42:47 -0700140 .register(InternalLinkEvent.class)
141 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700142 .register(LinkAntiEntropyAdvertisement.class)
143 .register(LinkFragmentId.class)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800144 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700145 }
146 };
147
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800148 private ExecutorService executor;
149
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800150 private ScheduledExecutorService backgroundExecutors;
Madan Jampania97e8202014-10-10 17:01:33 -0700151
Madan Jampani2ff05592014-10-10 15:42:47 -0700152 @Activate
153 public void activate() {
154
155 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700156 GossipLinkStoreMessageSubjects.LINK_UPDATE,
157 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700158 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700159 GossipLinkStoreMessageSubjects.LINK_REMOVED,
160 new InternalLinkRemovedEventListener());
161 clusterCommunicator.addSubscriber(
162 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
163 new InternalLinkAntiEntropyAdvertisementListener());
164
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -0800165 executor = Executors.newCachedThreadPool(namedThreads("onos-link-fg-%d"));
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800166
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800167 backgroundExecutors =
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -0800168 newSingleThreadScheduledExecutor(minPriority(namedThreads("onos-link-bg-%d")));
Madan Jampania97e8202014-10-10 17:01:33 -0700169
Madan Jampania97e8202014-10-10 17:01:33 -0700170 long initialDelaySec = 5;
171 long periodSec = 5;
172 // start anti-entropy thread
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800173 backgroundExecutors.scheduleAtFixedRate(new SendAdvertisementTask(),
Madan Jampania97e8202014-10-10 17:01:33 -0700174 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700175
176 log.info("Started");
177 }
178
179 @Deactivate
180 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700181
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800182 executor.shutdownNow();
183
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800184 backgroundExecutors.shutdownNow();
Madan Jampani3ffbb272014-10-13 11:19:37 -0700185 try {
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800186 if (!backgroundExecutors.awaitTermination(5, TimeUnit.SECONDS)) {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700187 log.error("Timeout during executor shutdown");
188 }
189 } catch (InterruptedException e) {
190 log.error("Error during executor shutdown", e);
191 }
192
Madan Jampani2ff05592014-10-10 15:42:47 -0700193 linkDescs.clear();
194 links.clear();
195 srcLinks.clear();
196 dstLinks.clear();
197 log.info("Stopped");
198 }
199
200 @Override
201 public int getLinkCount() {
202 return links.size();
203 }
204
205 @Override
206 public Iterable<Link> getLinks() {
207 return Collections.unmodifiableCollection(links.values());
208 }
209
210 @Override
211 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
212 // lock for iteration
213 synchronized (srcLinks) {
214 return FluentIterable.from(srcLinks.get(deviceId))
215 .transform(lookupLink())
216 .filter(notNull())
217 .toSet();
218 }
219 }
220
221 @Override
222 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
223 // lock for iteration
224 synchronized (dstLinks) {
225 return FluentIterable.from(dstLinks.get(deviceId))
226 .transform(lookupLink())
227 .filter(notNull())
228 .toSet();
229 }
230 }
231
232 @Override
233 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700234 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700235 }
236
237 @Override
238 public Set<Link> getEgressLinks(ConnectPoint src) {
239 Set<Link> egress = new HashSet<>();
240 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
241 if (linkKey.src().equals(src)) {
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800242 Link link = links.get(linkKey);
243 if (link != null) {
244 egress.add(link);
245 } else {
246 log.debug("Egress link for {} was null, skipped", linkKey);
247 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700248 }
249 }
250 return egress;
251 }
252
253 @Override
254 public Set<Link> getIngressLinks(ConnectPoint dst) {
255 Set<Link> ingress = new HashSet<>();
256 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
257 if (linkKey.dst().equals(dst)) {
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800258 Link link = links.get(linkKey);
259 if (link != null) {
260 ingress.add(link);
261 } else {
262 log.debug("Ingress link for {} was null, skipped", linkKey);
263 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700264 }
265 }
266 return ingress;
267 }
268
269 @Override
270 public LinkEvent createOrUpdateLink(ProviderId providerId,
271 LinkDescription linkDescription) {
272
273 DeviceId dstDeviceId = linkDescription.dst().deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700274 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700275
276 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
277
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700278 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700279 final LinkEvent event;
280 final Timestamped<LinkDescription> mergedDesc;
alshabibdfc7afb2014-10-21 20:13:27 -0700281 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
282 synchronized (map) {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700283 event = createOrUpdateLinkInternal(providerId, deltaDesc);
alshabibdfc7afb2014-10-21 20:13:27 -0700284 mergedDesc = map.get(providerId);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700285 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700286
287 if (event != null) {
288 log.info("Notifying peers of a link update topology event from providerId: "
289 + "{} between src: {} and dst: {}",
290 providerId, linkDescription.src(), linkDescription.dst());
Jonathan Hart7d656f42015-01-27 14:07:23 -0800291 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
Madan Jampani2ff05592014-10-10 15:42:47 -0700292 }
293 return event;
294 }
295
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800296 @Override
297 public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) {
298 Link link = getLink(src, dst);
299 if (link == null) {
300 return null;
301 }
302
303 if (link.isDurable()) {
304 // FIXME: this is not the right thing to call for the gossip store; will not sync link state!!!
305 return link.state() == INACTIVE ? null :
306 updateLink(linkKey(link.src(), link.dst()), link,
307 new DefaultLink(link.providerId(),
308 link.src(), link.dst(),
309 link.type(), INACTIVE,
310 link.isDurable(),
311 link.annotations()));
312 }
313 return removeLink(src, dst);
314 }
315
Madan Jampani2ff05592014-10-10 15:42:47 -0700316 private LinkEvent createOrUpdateLinkInternal(
317 ProviderId providerId,
318 Timestamped<LinkDescription> linkDescription) {
319
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700320 final LinkKey key = linkKey(linkDescription.value().src(),
321 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700322 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700323
324 synchronized (descs) {
325 // if the link was previously removed, we should proceed if and
326 // only if this request is more recent.
327 Timestamp linkRemovedTimestamp = removedLinks.get(key);
328 if (linkRemovedTimestamp != null) {
329 if (linkDescription.isNewer(linkRemovedTimestamp)) {
330 removedLinks.remove(key);
331 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700332 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700333 return null;
334 }
335 }
336
337 final Link oldLink = links.get(key);
338 // update description
339 createOrUpdateLinkDescription(descs, providerId, linkDescription);
340 final Link newLink = composeLink(descs);
341 if (oldLink == null) {
342 return createLink(key, newLink);
343 }
344 return updateLink(key, oldLink, newLink);
345 }
346 }
347
348 // Guarded by linkDescs value (=locking each Link)
349 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700350 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700351 ProviderId providerId,
352 Timestamped<LinkDescription> linkDescription) {
353
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700354 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700355 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700356 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700357 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700358 return null;
359 }
360 Timestamped<LinkDescription> newLinkDescription = linkDescription;
361 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700362 // we only allow transition from INDIRECT -> DIRECT
363 final Type newType;
364 if (existingLinkDescription.value().type() == DIRECT) {
365 newType = DIRECT;
366 } else {
367 newType = linkDescription.value().type();
368 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700369 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
370 linkDescription.value().annotations());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800371 newLinkDescription = new Timestamped<>(
Madan Jampani2ff05592014-10-10 15:42:47 -0700372 new DefaultLinkDescription(
373 linkDescription.value().src(),
374 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700375 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700376 linkDescription.timestamp());
377 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700378 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700379 }
380
381 // Creates and stores the link and returns the appropriate event.
382 // Guarded by linkDescs value (=locking each Link)
383 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700384 links.put(key, newLink);
385 srcLinks.put(newLink.src().deviceId(), key);
386 dstLinks.put(newLink.dst().deviceId(), key);
387 return new LinkEvent(LINK_ADDED, newLink);
388 }
389
390 // Updates, if necessary the specified link and returns the appropriate event.
391 // Guarded by linkDescs value (=locking each Link)
392 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700393 // Note: INDIRECT -> DIRECT transition only
394 // so that BDDP discovered Link will not overwrite LDDP Link
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800395 if (oldLink.state() != newLink.state() ||
396 (oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
Madan Jampani2ff05592014-10-10 15:42:47 -0700397 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
398
399 links.put(key, newLink);
400 // strictly speaking following can be ommitted
401 srcLinks.put(oldLink.src().deviceId(), key);
402 dstLinks.put(oldLink.dst().deviceId(), key);
403 return new LinkEvent(LINK_UPDATED, newLink);
404 }
405 return null;
406 }
407
408 @Override
409 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700410 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700411
412 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700413 Timestamp timestamp = null;
414 try {
415 timestamp = deviceClockService.getTimestamp(dstDeviceId);
416 } catch (IllegalStateException e) {
Yuta HIGUCHId6a0ac32014-11-04 09:26:31 -0800417 log.warn("Failed to remove link {}, was not the master", key);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700418 //there are times when this is called before mastership
419 // handoff correctly completes.
420 return null;
421 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700422
423 LinkEvent event = removeLinkInternal(key, timestamp);
424
425 if (event != null) {
426 log.info("Notifying peers of a link removed topology event for a link "
427 + "between src: {} and dst: {}", src, dst);
Jonathan Hart7d656f42015-01-27 14:07:23 -0800428 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
Madan Jampani2ff05592014-10-10 15:42:47 -0700429 }
430 return event;
431 }
432
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700433 private static Timestamped<LinkDescription> getPrimaryDescription(
434 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
435
Madan Jampani2ff05592014-10-10 15:42:47 -0700436 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700437 for (Entry<ProviderId, Timestamped<LinkDescription>>
438 e : linkDescriptions.entrySet()) {
439
440 if (!e.getKey().isAncillary()) {
441 return e.getValue();
442 }
443 }
444 }
445 return null;
446 }
447
448
449 // TODO: consider slicing out as Timestamp utils
450 /**
451 * Checks is timestamp is more recent than timestamped object.
452 *
453 * @param timestamp to check if this is more recent then other
454 * @param timestamped object to be tested against
455 * @return true if {@code timestamp} is more recent than {@code timestamped}
456 * or {@code timestamped is null}
457 */
458 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
459 checkNotNull(timestamp);
460 if (timestamped == null) {
461 return true;
462 }
463 return timestamp.compareTo(timestamped.timestamp()) > 0;
464 }
465
466 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
467 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
468 = getOrCreateLinkDescriptions(key);
469
470 synchronized (linkDescriptions) {
471 if (linkDescriptions.isEmpty()) {
472 // never seen such link before. keeping timestamp for record
473 removedLinks.put(key, timestamp);
474 return null;
475 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700476 // accept removal request if given timestamp is newer than
477 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700478 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
479 if (!isMoreRecent(timestamp, prim)) {
480 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700481 return null;
482 }
483 removedLinks.put(key, timestamp);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800484 Link link = links.remove(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700485 linkDescriptions.clear();
486 if (link != null) {
487 srcLinks.remove(link.src().deviceId(), key);
488 dstLinks.remove(link.dst().deviceId(), key);
489 return new LinkEvent(LINK_REMOVED, link);
490 }
491 return null;
492 }
493 }
494
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800495 /**
496 * Creates concurrent readable, synchronized HashMultimap.
497 *
498 * @return SetMultimap
499 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700500 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800501 return synchronizedSetMultimap(
502 Multimaps.newSetMultimap(new ConcurrentHashMap<K, Collection<V>>(),
503 () -> Sets.newConcurrentHashSet()));
Madan Jampani2ff05592014-10-10 15:42:47 -0700504 }
505
506 /**
507 * @return primary ProviderID, or randomly chosen one if none exists
508 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700509 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700510 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700511
512 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700513 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700514 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700515 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700516 return e.getKey();
517 } else if (fallBackPrimary == null) {
518 // pick randomly as a fallback in case there is no primary
519 fallBackPrimary = e.getKey();
520 }
521 }
522 return fallBackPrimary;
523 }
524
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700525 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700526 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700527 ProviderId baseProviderId = pickBaseProviderId(descs);
528 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700529
530 ConnectPoint src = base.value().src();
531 ConnectPoint dst = base.value().dst();
532 Type type = base.value().type();
533 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
534 annotations = merge(annotations, base.value().annotations());
535
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700536 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700537 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700538 continue;
539 }
540
Yuta HIGUCHI65934892014-12-04 17:47:44 -0800541 // Note: In the long run we should keep track of Description timestamp
Madan Jampani2ff05592014-10-10 15:42:47 -0700542 // and only merge conflicting keys when timestamp is newer
543 // Currently assuming there will never be a key conflict between
544 // providers
545
546 // annotation merging. not so efficient, should revisit later
547 annotations = merge(annotations, e.getValue().value().annotations());
548 }
549
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800550 boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800551 return new DefaultLink(baseProviderId, src, dst, type, ACTIVE, isDurable, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700552 }
553
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700554 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700555 Map<ProviderId, Timestamped<LinkDescription>> r;
556 r = linkDescs.get(key);
557 if (r != null) {
558 return r;
559 }
560 r = new HashMap<>();
561 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
562 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
563 if (concurrentlyAdded != null) {
564 return concurrentlyAdded;
565 } else {
566 return r;
567 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700568 }
569
570 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800571
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700572 /**
573 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800574 *
575 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700576 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700577 private Function<LinkKey, Link> lookupLink() {
578 return lookupLink;
579 }
580
581 private final class LookupLink implements Function<LinkKey, Link> {
582 @Override
583 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700584 if (input == null) {
585 return null;
586 } else {
587 return links.get(input);
588 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700589 }
590 }
591
Madan Jampani2ff05592014-10-10 15:42:47 -0700592 private void notifyDelegateIfNotNull(LinkEvent event) {
593 if (event != null) {
594 notifyDelegate(event);
595 }
596 }
597
Jonathan Hart7d656f42015-01-27 14:07:23 -0800598 private void broadcastMessage(MessageSubject subject, Object event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700599 ClusterMessage message = new ClusterMessage(
600 clusterService.getLocalNode().id(),
601 subject,
602 SERIALIZER.encode(event));
603 clusterCommunicator.broadcast(message);
604 }
605
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700606 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
607 ClusterMessage message = new ClusterMessage(
608 clusterService.getLocalNode().id(),
609 subject,
610 SERIALIZER.encode(event));
611 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700612 }
613
Jonathan Hart7d656f42015-01-27 14:07:23 -0800614 private void notifyPeers(InternalLinkEvent event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700615 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
616 }
617
Jonathan Hart7d656f42015-01-27 14:07:23 -0800618 private void notifyPeers(InternalLinkRemovedEvent event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700619 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
620 }
621
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700622 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700623 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700624 try {
625 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
626 } catch (IOException e) {
627 log.debug("Failed to notify peer {} with message {}", peer, event);
628 }
Madan Jampania97e8202014-10-10 17:01:33 -0700629 }
630
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700631 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700632 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700633 try {
634 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
635 } catch (IOException e) {
636 log.debug("Failed to notify peer {} with message {}", peer, event);
637 }
Madan Jampania97e8202014-10-10 17:01:33 -0700638 }
639
640 private final class SendAdvertisementTask implements Runnable {
641
642 @Override
643 public void run() {
644 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800645 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700646 return;
647 }
648
649 try {
650 final NodeId self = clusterService.getLocalNode().id();
651 Set<ControllerNode> nodes = clusterService.getNodes();
652
653 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
654 .transform(toNodeId())
655 .toList();
656
657 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800658 log.trace("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700659 return;
660 }
661
662 NodeId peer;
663 do {
664 int idx = RandomUtils.nextInt(0, nodeIds.size());
665 peer = nodeIds.get(idx);
666 } while (peer.equals(self));
667
668 LinkAntiEntropyAdvertisement ad = createAdvertisement();
669
670 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800671 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700672 return;
673 }
674
675 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700676 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
677 } catch (IOException e) {
678 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700679 return;
680 }
681 } catch (Exception e) {
682 // catch all Exception to avoid Scheduled task being suppressed.
683 log.error("Exception thrown while sending advertisement", e);
684 }
685 }
686 }
687
688 private LinkAntiEntropyAdvertisement createAdvertisement() {
689 final NodeId self = clusterService.getLocalNode().id();
690
691 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
692 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
693
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800694 linkDescs.forEach((linkKey, linkDesc) -> {
Madan Jampania97e8202014-10-10 17:01:33 -0700695 synchronized (linkDesc) {
696 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
697 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
698 }
699 }
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800700 });
Madan Jampania97e8202014-10-10 17:01:33 -0700701
702 linkTombstones.putAll(removedLinks);
703
704 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
705 }
706
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700707 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700708
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700709 final NodeId sender = ad.sender();
710 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700711
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700712 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
713 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700714
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700715 final LinkKey key = l.getKey();
716 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
717 synchronized (link) {
718 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700719
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700720 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
721 final ProviderId providerId = p.getKey();
722 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700723
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700724 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
725 // remote
726 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
727 if (remoteTimestamp == null) {
728 remoteTimestamp = ad.linkTombstones().get(key);
729 }
730 if (remoteTimestamp == null ||
731 pDesc.isNewer(remoteTimestamp)) {
732 // I have more recent link description. update peer.
733 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
734 } else {
735 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
736 if (remoteLive != null &&
737 remoteLive.compareTo(pDesc.timestamp()) > 0) {
738 // I have something outdated
739 localOutdated = true;
740 }
741 }
742
743 // search local latest along the way
744 if (localLatest == null ||
745 pDesc.isNewer(localLatest)) {
746 localLatest = pDesc.timestamp();
747 }
748 }
749 // Tests if remote remove is more recent then local latest.
750 final Timestamp remoteRemove = ad.linkTombstones().get(key);
751 if (remoteRemove != null) {
752 if (localLatest != null &&
753 localLatest.compareTo(remoteRemove) < 0) {
754 // remote remove is more recent
755 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
756 }
757 }
Madan Jampania97e8202014-10-10 17:01:33 -0700758 }
759 }
760
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700761 // populate remove info if not known locally
762 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
763 final LinkKey key = remoteRm.getKey();
764 final Timestamp remoteRemove = remoteRm.getValue();
765 // relying on removeLinkInternal to ignore stale info
766 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
767 }
Madan Jampania97e8202014-10-10 17:01:33 -0700768
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700769 if (localOutdated) {
770 // send back advertisement to speed up convergence
771 try {
772 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
773 createAdvertisement());
774 } catch (IOException e) {
775 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700776 }
777 }
778 }
779
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800780 private final class InternalLinkEventListener
781 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700782 @Override
783 public void handle(ClusterMessage message) {
784
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700785 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700786 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
787
788 ProviderId providerId = event.providerId();
789 Timestamped<LinkDescription> linkDescription = event.linkDescription();
790
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800791 executor.submit(new Runnable() {
792
793 @Override
794 public void run() {
795 try {
796 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
797 } catch (Exception e) {
798 log.warn("Exception thrown handling link event", e);
799 }
800 }
801 });
Madan Jampani2ff05592014-10-10 15:42:47 -0700802 }
803 }
804
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800805 private final class InternalLinkRemovedEventListener
806 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700807 @Override
808 public void handle(ClusterMessage message) {
809
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700810 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700811 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
812
813 LinkKey linkKey = event.linkKey();
814 Timestamp timestamp = event.timestamp();
815
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800816 executor.submit(new Runnable() {
817
818 @Override
819 public void run() {
820 try {
821 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
822 } catch (Exception e) {
823 log.warn("Exception thrown handling link removed", e);
824 }
825 }
826 });
Madan Jampani2ff05592014-10-10 15:42:47 -0700827 }
828 }
Madan Jampania97e8202014-10-10 17:01:33 -0700829
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800830 private final class InternalLinkAntiEntropyAdvertisementListener
831 implements ClusterMessageHandler {
Madan Jampania97e8202014-10-10 17:01:33 -0700832
833 @Override
834 public void handle(ClusterMessage message) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800835 log.trace("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700836 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800837 backgroundExecutors.submit(new Runnable() {
838
839 @Override
840 public void run() {
841 try {
842 handleAntiEntropyAdvertisement(advertisement);
843 } catch (Exception e) {
844 log.warn("Exception thrown while handling Link advertisements", e);
845 throw e;
846 }
847 }
848 });
Madan Jampania97e8202014-10-10 17:01:33 -0700849 }
850 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700851}