blob: 01bdc3614e8ddc05e2be8599a6890f00e9215dff [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
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080018import java.io.IOException;
19import java.util.Collections;
20import java.util.HashMap;
21import java.util.HashSet;
22import java.util.Map;
23import java.util.Map.Entry;
24import java.util.Objects;
25import java.util.Set;
26import java.util.concurrent.ConcurrentHashMap;
27import java.util.concurrent.ConcurrentMap;
28import java.util.concurrent.ExecutorService;
29import java.util.concurrent.Executors;
30import java.util.concurrent.ScheduledExecutorService;
31import java.util.concurrent.TimeUnit;
Yuta HIGUCHI06586272014-11-25 14:27:03 -080032
Madan Jampania97e8202014-10-10 17:01:33 -070033import org.apache.commons.lang3.RandomUtils;
Madan Jampani2ff05592014-10-10 15:42:47 -070034import org.apache.felix.scr.annotations.Activate;
35import org.apache.felix.scr.annotations.Component;
36import org.apache.felix.scr.annotations.Deactivate;
37import org.apache.felix.scr.annotations.Reference;
38import org.apache.felix.scr.annotations.ReferenceCardinality;
39import org.apache.felix.scr.annotations.Service;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080040import org.onlab.util.KryoNamespace;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.cluster.ClusterService;
42import org.onosproject.cluster.ControllerNode;
43import org.onosproject.cluster.NodeId;
44import org.onosproject.net.AnnotationKeys;
45import org.onosproject.net.AnnotationsUtil;
46import org.onosproject.net.ConnectPoint;
47import org.onosproject.net.DefaultAnnotations;
48import org.onosproject.net.DefaultLink;
49import org.onosproject.net.DeviceId;
50import org.onosproject.net.Link;
51import org.onosproject.net.Link.Type;
52import org.onosproject.net.LinkKey;
53import org.onosproject.net.SparseAnnotations;
54import org.onosproject.net.device.DeviceClockService;
55import org.onosproject.net.link.DefaultLinkDescription;
56import org.onosproject.net.link.LinkDescription;
57import org.onosproject.net.link.LinkEvent;
58import org.onosproject.net.link.LinkStore;
59import org.onosproject.net.link.LinkStoreDelegate;
60import org.onosproject.net.provider.ProviderId;
61import org.onosproject.store.AbstractStore;
62import org.onosproject.store.Timestamp;
63import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
64import org.onosproject.store.cluster.messaging.ClusterMessage;
65import org.onosproject.store.cluster.messaging.ClusterMessageHandler;
66import org.onosproject.store.cluster.messaging.MessageSubject;
67import org.onosproject.store.impl.Timestamped;
68import org.onosproject.store.serializers.KryoSerializer;
69import org.onosproject.store.serializers.impl.DistributedStoreSerializers;
Madan Jampani2ff05592014-10-10 15:42:47 -070070import org.slf4j.Logger;
71
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080072import com.google.common.base.Function;
73import com.google.common.collect.FluentIterable;
74import com.google.common.collect.HashMultimap;
75import com.google.common.collect.ImmutableList;
76import com.google.common.collect.SetMultimap;
Madan Jampani2ff05592014-10-10 15:42:47 -070077
Thomas Vachuska57126fe2014-11-11 17:13:24 -080078import static com.google.common.base.Preconditions.checkNotNull;
79import static com.google.common.base.Predicates.notNull;
80import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Madan Jampania97e8202014-10-10 17:01:33 -070081import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080082import static org.onlab.util.Tools.minPriority;
83import static org.onlab.util.Tools.namedThreads;
Brian O'Connorabafb502014-12-02 22:26:20 -080084import static org.onosproject.cluster.ControllerNodeToNodeId.toNodeId;
85import static org.onosproject.net.DefaultAnnotations.merge;
86import static org.onosproject.net.DefaultAnnotations.union;
87import static org.onosproject.net.Link.State.ACTIVE;
88import static org.onosproject.net.Link.State.INACTIVE;
89import static org.onosproject.net.Link.Type.DIRECT;
90import static org.onosproject.net.Link.Type.INDIRECT;
91import static org.onosproject.net.LinkKey.linkKey;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080092import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
93import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
94import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED;
Brian O'Connorabafb502014-12-02 22:26:20 -080095import static org.onosproject.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Madan Jampani2ff05592014-10-10 15:42:47 -070096import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani2ff05592014-10-10 15:42:47 -070097
98/**
99 * Manages inventory of infrastructure links in distributed data store
100 * that uses optimistic replication and gossip based techniques.
101 */
102@Component(immediate = true)
103@Service
104public class GossipLinkStore
105 extends AbstractStore<LinkEvent, LinkStoreDelegate>
106 implements LinkStore {
107
108 private final Logger log = getLogger(getClass());
109
110 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700111 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700112 new ConcurrentHashMap<>();
113
114 // Link instance cache
115 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
116
117 // Egress and ingress link sets
118 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
119 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
120
121 // Remove links
Yuta HIGUCHIb9125562014-12-01 23:28:22 -0800122 private final Map<LinkKey, Timestamp> removedLinks = new ConcurrentHashMap<>();
Madan Jampani2ff05592014-10-10 15:42:47 -0700123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700125 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
128 protected ClusterCommunicationService clusterCommunicator;
129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 protected ClusterService clusterService;
132
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800133 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700134 @Override
135 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700136 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800137 .register(DistributedStoreSerializers.STORE_COMMON)
138 .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
Madan Jampani2ff05592014-10-10 15:42:47 -0700139 .register(InternalLinkEvent.class)
140 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700141 .register(LinkAntiEntropyAdvertisement.class)
142 .register(LinkFragmentId.class)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800143 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700144 }
145 };
146
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800147 private ExecutorService executor;
148
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800149 private ScheduledExecutorService backgroundExecutors;
Madan Jampania97e8202014-10-10 17:01:33 -0700150
Madan Jampani2ff05592014-10-10 15:42:47 -0700151 @Activate
152 public void activate() {
153
154 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700155 GossipLinkStoreMessageSubjects.LINK_UPDATE,
156 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700157 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700158 GossipLinkStoreMessageSubjects.LINK_REMOVED,
159 new InternalLinkRemovedEventListener());
160 clusterCommunicator.addSubscriber(
161 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
162 new InternalLinkAntiEntropyAdvertisementListener());
163
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800164 executor = Executors.newCachedThreadPool(namedThreads("link-fg-%d"));
165
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800166 backgroundExecutors =
167 newSingleThreadScheduledExecutor(minPriority(namedThreads("link-bg-%d")));
Madan Jampania97e8202014-10-10 17:01:33 -0700168
Madan Jampania97e8202014-10-10 17:01:33 -0700169 long initialDelaySec = 5;
170 long periodSec = 5;
171 // start anti-entropy thread
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800172 backgroundExecutors.scheduleAtFixedRate(new SendAdvertisementTask(),
Madan Jampania97e8202014-10-10 17:01:33 -0700173 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700174
175 log.info("Started");
176 }
177
178 @Deactivate
179 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700180
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800181 executor.shutdownNow();
182
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800183 backgroundExecutors.shutdownNow();
Madan Jampani3ffbb272014-10-13 11:19:37 -0700184 try {
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800185 if (!backgroundExecutors.awaitTermination(5, TimeUnit.SECONDS)) {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700186 log.error("Timeout during executor shutdown");
187 }
188 } catch (InterruptedException e) {
189 log.error("Error during executor shutdown", e);
190 }
191
Madan Jampani2ff05592014-10-10 15:42:47 -0700192 linkDescs.clear();
193 links.clear();
194 srcLinks.clear();
195 dstLinks.clear();
196 log.info("Stopped");
197 }
198
199 @Override
200 public int getLinkCount() {
201 return links.size();
202 }
203
204 @Override
205 public Iterable<Link> getLinks() {
206 return Collections.unmodifiableCollection(links.values());
207 }
208
209 @Override
210 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
211 // lock for iteration
212 synchronized (srcLinks) {
213 return FluentIterable.from(srcLinks.get(deviceId))
214 .transform(lookupLink())
215 .filter(notNull())
216 .toSet();
217 }
218 }
219
220 @Override
221 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
222 // lock for iteration
223 synchronized (dstLinks) {
224 return FluentIterable.from(dstLinks.get(deviceId))
225 .transform(lookupLink())
226 .filter(notNull())
227 .toSet();
228 }
229 }
230
231 @Override
232 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700233 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700234 }
235
236 @Override
237 public Set<Link> getEgressLinks(ConnectPoint src) {
238 Set<Link> egress = new HashSet<>();
239 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
240 if (linkKey.src().equals(src)) {
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800241 Link link = links.get(linkKey);
242 if (link != null) {
243 egress.add(link);
244 } else {
245 log.debug("Egress link for {} was null, skipped", linkKey);
246 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700247 }
248 }
249 return egress;
250 }
251
252 @Override
253 public Set<Link> getIngressLinks(ConnectPoint dst) {
254 Set<Link> ingress = new HashSet<>();
255 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
256 if (linkKey.dst().equals(dst)) {
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800257 Link link = links.get(linkKey);
258 if (link != null) {
259 ingress.add(link);
260 } else {
261 log.debug("Ingress link for {} was null, skipped", linkKey);
262 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700263 }
264 }
265 return ingress;
266 }
267
268 @Override
269 public LinkEvent createOrUpdateLink(ProviderId providerId,
270 LinkDescription linkDescription) {
271
272 DeviceId dstDeviceId = linkDescription.dst().deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700273 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700274
275 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
276
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700277 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700278 final LinkEvent event;
279 final Timestamped<LinkDescription> mergedDesc;
alshabibdfc7afb2014-10-21 20:13:27 -0700280 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
281 synchronized (map) {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700282 event = createOrUpdateLinkInternal(providerId, deltaDesc);
alshabibdfc7afb2014-10-21 20:13:27 -0700283 mergedDesc = map.get(providerId);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700284 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700285
286 if (event != null) {
287 log.info("Notifying peers of a link update topology event from providerId: "
288 + "{} between src: {} and dst: {}",
289 providerId, linkDescription.src(), linkDescription.dst());
290 try {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700291 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
Madan Jampani2ff05592014-10-10 15:42:47 -0700292 } catch (IOException e) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800293 log.debug("Failed to notify peers of a link update topology event from providerId: "
alshabibdfc7afb2014-10-21 20:13:27 -0700294 + "{} between src: {} and dst: {}",
295 providerId, linkDescription.src(), linkDescription.dst());
Madan Jampani2ff05592014-10-10 15:42:47 -0700296 }
297 }
298 return event;
299 }
300
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800301 @Override
302 public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) {
303 Link link = getLink(src, dst);
304 if (link == null) {
305 return null;
306 }
307
308 if (link.isDurable()) {
309 // FIXME: this is not the right thing to call for the gossip store; will not sync link state!!!
310 return link.state() == INACTIVE ? null :
311 updateLink(linkKey(link.src(), link.dst()), link,
312 new DefaultLink(link.providerId(),
313 link.src(), link.dst(),
314 link.type(), INACTIVE,
315 link.isDurable(),
316 link.annotations()));
317 }
318 return removeLink(src, dst);
319 }
320
Madan Jampani2ff05592014-10-10 15:42:47 -0700321 private LinkEvent createOrUpdateLinkInternal(
322 ProviderId providerId,
323 Timestamped<LinkDescription> linkDescription) {
324
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700325 final LinkKey key = linkKey(linkDescription.value().src(),
326 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700327 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700328
329 synchronized (descs) {
330 // if the link was previously removed, we should proceed if and
331 // only if this request is more recent.
332 Timestamp linkRemovedTimestamp = removedLinks.get(key);
333 if (linkRemovedTimestamp != null) {
334 if (linkDescription.isNewer(linkRemovedTimestamp)) {
335 removedLinks.remove(key);
336 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700337 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700338 return null;
339 }
340 }
341
342 final Link oldLink = links.get(key);
343 // update description
344 createOrUpdateLinkDescription(descs, providerId, linkDescription);
345 final Link newLink = composeLink(descs);
346 if (oldLink == null) {
347 return createLink(key, newLink);
348 }
349 return updateLink(key, oldLink, newLink);
350 }
351 }
352
353 // Guarded by linkDescs value (=locking each Link)
354 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700355 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700356 ProviderId providerId,
357 Timestamped<LinkDescription> linkDescription) {
358
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700359 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700360 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700361 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700362 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700363 return null;
364 }
365 Timestamped<LinkDescription> newLinkDescription = linkDescription;
366 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700367 // we only allow transition from INDIRECT -> DIRECT
368 final Type newType;
369 if (existingLinkDescription.value().type() == DIRECT) {
370 newType = DIRECT;
371 } else {
372 newType = linkDescription.value().type();
373 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700374 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
375 linkDescription.value().annotations());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800376 newLinkDescription = new Timestamped<>(
Madan Jampani2ff05592014-10-10 15:42:47 -0700377 new DefaultLinkDescription(
378 linkDescription.value().src(),
379 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700380 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700381 linkDescription.timestamp());
382 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700383 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700384 }
385
386 // Creates and stores the link and returns the appropriate event.
387 // Guarded by linkDescs value (=locking each Link)
388 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700389 links.put(key, newLink);
390 srcLinks.put(newLink.src().deviceId(), key);
391 dstLinks.put(newLink.dst().deviceId(), key);
392 return new LinkEvent(LINK_ADDED, newLink);
393 }
394
395 // Updates, if necessary the specified link and returns the appropriate event.
396 // Guarded by linkDescs value (=locking each Link)
397 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700398 // Note: INDIRECT -> DIRECT transition only
399 // so that BDDP discovered Link will not overwrite LDDP Link
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800400 if (oldLink.state() != newLink.state() ||
401 (oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
Madan Jampani2ff05592014-10-10 15:42:47 -0700402 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
403
404 links.put(key, newLink);
405 // strictly speaking following can be ommitted
406 srcLinks.put(oldLink.src().deviceId(), key);
407 dstLinks.put(oldLink.dst().deviceId(), key);
408 return new LinkEvent(LINK_UPDATED, newLink);
409 }
410 return null;
411 }
412
413 @Override
414 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700415 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700416
417 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700418 Timestamp timestamp = null;
419 try {
420 timestamp = deviceClockService.getTimestamp(dstDeviceId);
421 } catch (IllegalStateException e) {
Yuta HIGUCHId6a0ac32014-11-04 09:26:31 -0800422 log.warn("Failed to remove link {}, was not the master", key);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700423 //there are times when this is called before mastership
424 // handoff correctly completes.
425 return null;
426 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700427
428 LinkEvent event = removeLinkInternal(key, timestamp);
429
430 if (event != null) {
431 log.info("Notifying peers of a link removed topology event for a link "
432 + "between src: {} and dst: {}", src, dst);
433 try {
434 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
435 } catch (IOException e) {
436 log.error("Failed to notify peers of a link removed topology event for a link "
437 + "between src: {} and dst: {}", src, dst);
438 }
439 }
440 return event;
441 }
442
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700443 private static Timestamped<LinkDescription> getPrimaryDescription(
444 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
445
Madan Jampani2ff05592014-10-10 15:42:47 -0700446 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700447 for (Entry<ProviderId, Timestamped<LinkDescription>>
448 e : linkDescriptions.entrySet()) {
449
450 if (!e.getKey().isAncillary()) {
451 return e.getValue();
452 }
453 }
454 }
455 return null;
456 }
457
458
459 // TODO: consider slicing out as Timestamp utils
460 /**
461 * Checks is timestamp is more recent than timestamped object.
462 *
463 * @param timestamp to check if this is more recent then other
464 * @param timestamped object to be tested against
465 * @return true if {@code timestamp} is more recent than {@code timestamped}
466 * or {@code timestamped is null}
467 */
468 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
469 checkNotNull(timestamp);
470 if (timestamped == null) {
471 return true;
472 }
473 return timestamp.compareTo(timestamped.timestamp()) > 0;
474 }
475
476 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
477 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
478 = getOrCreateLinkDescriptions(key);
479
480 synchronized (linkDescriptions) {
481 if (linkDescriptions.isEmpty()) {
482 // never seen such link before. keeping timestamp for record
483 removedLinks.put(key, timestamp);
484 return null;
485 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700486 // accept removal request if given timestamp is newer than
487 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700488 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
489 if (!isMoreRecent(timestamp, prim)) {
490 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700491 return null;
492 }
493 removedLinks.put(key, timestamp);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800494 Link link = links.remove(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700495 linkDescriptions.clear();
496 if (link != null) {
497 srcLinks.remove(link.src().deviceId(), key);
498 dstLinks.remove(link.dst().deviceId(), key);
499 return new LinkEvent(LINK_REMOVED, link);
500 }
501 return null;
502 }
503 }
504
505 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
506 return synchronizedSetMultimap(HashMultimap.<K, V>create());
507 }
508
509 /**
510 * @return primary ProviderID, or randomly chosen one if none exists
511 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700512 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700513 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700514
515 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700516 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700517 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700518 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700519 return e.getKey();
520 } else if (fallBackPrimary == null) {
521 // pick randomly as a fallback in case there is no primary
522 fallBackPrimary = e.getKey();
523 }
524 }
525 return fallBackPrimary;
526 }
527
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700528 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700529 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700530 ProviderId baseProviderId = pickBaseProviderId(descs);
531 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700532
533 ConnectPoint src = base.value().src();
534 ConnectPoint dst = base.value().dst();
535 Type type = base.value().type();
536 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
537 annotations = merge(annotations, base.value().annotations());
538
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700539 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700540 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700541 continue;
542 }
543
Yuta HIGUCHI65934892014-12-04 17:47:44 -0800544 // Note: In the long run we should keep track of Description timestamp
Madan Jampani2ff05592014-10-10 15:42:47 -0700545 // and only merge conflicting keys when timestamp is newer
546 // Currently assuming there will never be a key conflict between
547 // providers
548
549 // annotation merging. not so efficient, should revisit later
550 annotations = merge(annotations, e.getValue().value().annotations());
551 }
552
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800553 boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800554 return new DefaultLink(baseProviderId, src, dst, type, ACTIVE, isDurable, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700555 }
556
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700557 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700558 Map<ProviderId, Timestamped<LinkDescription>> r;
559 r = linkDescs.get(key);
560 if (r != null) {
561 return r;
562 }
563 r = new HashMap<>();
564 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
565 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
566 if (concurrentlyAdded != null) {
567 return concurrentlyAdded;
568 } else {
569 return r;
570 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700571 }
572
573 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800574
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700575 /**
576 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800577 *
578 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700579 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700580 private Function<LinkKey, Link> lookupLink() {
581 return lookupLink;
582 }
583
584 private final class LookupLink implements Function<LinkKey, Link> {
585 @Override
586 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700587 if (input == null) {
588 return null;
589 } else {
590 return links.get(input);
591 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700592 }
593 }
594
Madan Jampani2ff05592014-10-10 15:42:47 -0700595 private void notifyDelegateIfNotNull(LinkEvent event) {
596 if (event != null) {
597 notifyDelegate(event);
598 }
599 }
600
Madan Jampani2ff05592014-10-10 15:42:47 -0700601 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
602 ClusterMessage message = new ClusterMessage(
603 clusterService.getLocalNode().id(),
604 subject,
605 SERIALIZER.encode(event));
606 clusterCommunicator.broadcast(message);
607 }
608
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700609 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
610 ClusterMessage message = new ClusterMessage(
611 clusterService.getLocalNode().id(),
612 subject,
613 SERIALIZER.encode(event));
614 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700615 }
616
Madan Jampani2ff05592014-10-10 15:42:47 -0700617 private void notifyPeers(InternalLinkEvent event) throws IOException {
618 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
619 }
620
621 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
622 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
623 }
624
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700625 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700626 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700627 try {
628 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
629 } catch (IOException e) {
630 log.debug("Failed to notify peer {} with message {}", peer, event);
631 }
Madan Jampania97e8202014-10-10 17:01:33 -0700632 }
633
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700634 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700635 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700636 try {
637 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
638 } catch (IOException e) {
639 log.debug("Failed to notify peer {} with message {}", peer, event);
640 }
Madan Jampania97e8202014-10-10 17:01:33 -0700641 }
642
643 private final class SendAdvertisementTask implements Runnable {
644
645 @Override
646 public void run() {
647 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800648 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700649 return;
650 }
651
652 try {
653 final NodeId self = clusterService.getLocalNode().id();
654 Set<ControllerNode> nodes = clusterService.getNodes();
655
656 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
657 .transform(toNodeId())
658 .toList();
659
660 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800661 log.trace("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700662 return;
663 }
664
665 NodeId peer;
666 do {
667 int idx = RandomUtils.nextInt(0, nodeIds.size());
668 peer = nodeIds.get(idx);
669 } while (peer.equals(self));
670
671 LinkAntiEntropyAdvertisement ad = createAdvertisement();
672
673 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800674 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700675 return;
676 }
677
678 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700679 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
680 } catch (IOException e) {
681 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700682 return;
683 }
684 } catch (Exception e) {
685 // catch all Exception to avoid Scheduled task being suppressed.
686 log.error("Exception thrown while sending advertisement", e);
687 }
688 }
689 }
690
691 private LinkAntiEntropyAdvertisement createAdvertisement() {
692 final NodeId self = clusterService.getLocalNode().id();
693
694 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
695 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
696
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800697 linkDescs.forEach((linkKey, linkDesc) -> {
Madan Jampania97e8202014-10-10 17:01:33 -0700698 synchronized (linkDesc) {
699 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
700 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
701 }
702 }
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800703 });
Madan Jampania97e8202014-10-10 17:01:33 -0700704
705 linkTombstones.putAll(removedLinks);
706
707 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
708 }
709
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700710 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700711
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700712 final NodeId sender = ad.sender();
713 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700714
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700715 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
716 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700717
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700718 final LinkKey key = l.getKey();
719 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
720 synchronized (link) {
721 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700722
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700723 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
724 final ProviderId providerId = p.getKey();
725 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700726
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700727 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
728 // remote
729 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
730 if (remoteTimestamp == null) {
731 remoteTimestamp = ad.linkTombstones().get(key);
732 }
733 if (remoteTimestamp == null ||
734 pDesc.isNewer(remoteTimestamp)) {
735 // I have more recent link description. update peer.
736 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
737 } else {
738 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
739 if (remoteLive != null &&
740 remoteLive.compareTo(pDesc.timestamp()) > 0) {
741 // I have something outdated
742 localOutdated = true;
743 }
744 }
745
746 // search local latest along the way
747 if (localLatest == null ||
748 pDesc.isNewer(localLatest)) {
749 localLatest = pDesc.timestamp();
750 }
751 }
752 // Tests if remote remove is more recent then local latest.
753 final Timestamp remoteRemove = ad.linkTombstones().get(key);
754 if (remoteRemove != null) {
755 if (localLatest != null &&
756 localLatest.compareTo(remoteRemove) < 0) {
757 // remote remove is more recent
758 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
759 }
760 }
Madan Jampania97e8202014-10-10 17:01:33 -0700761 }
762 }
763
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700764 // populate remove info if not known locally
765 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
766 final LinkKey key = remoteRm.getKey();
767 final Timestamp remoteRemove = remoteRm.getValue();
768 // relying on removeLinkInternal to ignore stale info
769 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
770 }
Madan Jampania97e8202014-10-10 17:01:33 -0700771
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700772 if (localOutdated) {
773 // send back advertisement to speed up convergence
774 try {
775 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
776 createAdvertisement());
777 } catch (IOException e) {
778 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700779 }
780 }
781 }
782
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800783 private final class InternalLinkEventListener
784 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700785 @Override
786 public void handle(ClusterMessage message) {
787
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700788 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700789 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
790
791 ProviderId providerId = event.providerId();
792 Timestamped<LinkDescription> linkDescription = event.linkDescription();
793
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800794 executor.submit(new Runnable() {
795
796 @Override
797 public void run() {
798 try {
799 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
800 } catch (Exception e) {
801 log.warn("Exception thrown handling link event", e);
802 }
803 }
804 });
Madan Jampani2ff05592014-10-10 15:42:47 -0700805 }
806 }
807
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800808 private final class InternalLinkRemovedEventListener
809 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700810 @Override
811 public void handle(ClusterMessage message) {
812
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700813 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700814 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
815
816 LinkKey linkKey = event.linkKey();
817 Timestamp timestamp = event.timestamp();
818
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800819 executor.submit(new Runnable() {
820
821 @Override
822 public void run() {
823 try {
824 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
825 } catch (Exception e) {
826 log.warn("Exception thrown handling link removed", e);
827 }
828 }
829 });
Madan Jampani2ff05592014-10-10 15:42:47 -0700830 }
831 }
Madan Jampania97e8202014-10-10 17:01:33 -0700832
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800833 private final class InternalLinkAntiEntropyAdvertisementListener
834 implements ClusterMessageHandler {
Madan Jampania97e8202014-10-10 17:01:33 -0700835
836 @Override
837 public void handle(ClusterMessage message) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800838 log.trace("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700839 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800840 backgroundExecutors.submit(new Runnable() {
841
842 @Override
843 public void run() {
844 try {
845 handleAntiEntropyAdvertisement(advertisement);
846 } catch (Exception e) {
847 log.warn("Exception thrown while handling Link advertisements", e);
848 throw e;
849 }
850 }
851 });
Madan Jampania97e8202014-10-10 17:01:33 -0700852 }
853 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700854}