blob: c4658663a187cfdb5754152410091e725b9c1d3a [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;
Yuta HIGUCHI06586272014-11-25 14:27:03 -080024
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;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080035import org.onlab.onos.net.AnnotationKeys;
Madan Jampani2ff05592014-10-10 15:42:47 -070036import org.onlab.onos.net.AnnotationsUtil;
37import org.onlab.onos.net.ConnectPoint;
38import org.onlab.onos.net.DefaultAnnotations;
39import org.onlab.onos.net.DefaultLink;
40import org.onlab.onos.net.DeviceId;
41import org.onlab.onos.net.Link;
Madan Jampani2ff05592014-10-10 15:42:47 -070042import org.onlab.onos.net.Link.Type;
43import org.onlab.onos.net.LinkKey;
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -070044import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070045import org.onlab.onos.net.device.DeviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -070046import org.onlab.onos.net.link.DefaultLinkDescription;
47import org.onlab.onos.net.link.LinkDescription;
48import org.onlab.onos.net.link.LinkEvent;
49import org.onlab.onos.net.link.LinkStore;
50import org.onlab.onos.net.link.LinkStoreDelegate;
51import org.onlab.onos.net.provider.ProviderId;
52import org.onlab.onos.store.AbstractStore;
Madan Jampani2ff05592014-10-10 15:42:47 -070053import org.onlab.onos.store.Timestamp;
54import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
55import org.onlab.onos.store.cluster.messaging.ClusterMessage;
56import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler;
57import org.onlab.onos.store.cluster.messaging.MessageSubject;
Yuta HIGUCHIeecee552014-10-16 14:09:01 -070058import org.onlab.onos.store.impl.Timestamped;
Madan Jampani2ff05592014-10-10 15:42:47 -070059import org.onlab.onos.store.serializers.KryoSerializer;
Yuta HIGUCHI60a190b2014-11-07 16:24:47 -080060import org.onlab.onos.store.serializers.impl.DistributedStoreSerializers;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070061import org.onlab.util.KryoNamespace;
Madan Jampani2ff05592014-10-10 15:42:47 -070062import org.slf4j.Logger;
63
64import java.io.IOException;
65import java.util.Collections;
Madan Jampania97e8202014-10-10 17:01:33 -070066import java.util.HashMap;
Madan Jampani2ff05592014-10-10 15:42:47 -070067import java.util.HashSet;
68import java.util.Map;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080069import java.util.Map.Entry;
Thomas Vachuska29a6a782014-11-10 21:31:41 -080070import java.util.Objects;
Madan Jampani2ff05592014-10-10 15:42:47 -070071import java.util.Set;
Madan Jampani2ff05592014-10-10 15:42:47 -070072import java.util.concurrent.ConcurrentHashMap;
73import java.util.concurrent.ConcurrentMap;
Madan Jampania97e8202014-10-10 17:01:33 -070074import java.util.concurrent.ScheduledExecutorService;
75import java.util.concurrent.TimeUnit;
Madan Jampani2ff05592014-10-10 15:42:47 -070076
Thomas Vachuska57126fe2014-11-11 17:13:24 -080077import static com.google.common.base.Preconditions.checkNotNull;
78import static com.google.common.base.Predicates.notNull;
79import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Madan Jampania97e8202014-10-10 17:01:33 -070080import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
81import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070082import static org.onlab.onos.net.DefaultAnnotations.merge;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080083import static org.onlab.onos.net.DefaultAnnotations.union;
84import static org.onlab.onos.net.Link.State.ACTIVE;
85import static org.onlab.onos.net.Link.State.INACTIVE;
Madan Jampani2ff05592014-10-10 15:42:47 -070086import static org.onlab.onos.net.Link.Type.DIRECT;
87import static org.onlab.onos.net.Link.Type.INDIRECT;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070088import static org.onlab.onos.net.LinkKey.linkKey;
Madan Jampani2ff05592014-10-10 15:42:47 -070089import static org.onlab.onos.net.link.LinkEvent.Type.*;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080090import static org.onlab.onos.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Yuta HIGUCHI06586272014-11-25 14:27:03 -080091import static org.onlab.util.Tools.minPriority;
Madan Jampania97e8202014-10-10 17:01:33 -070092import static org.onlab.util.Tools.namedThreads;
Madan Jampani2ff05592014-10-10 15:42:47 -070093import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani2ff05592014-10-10 15:42:47 -070094
95/**
96 * Manages inventory of infrastructure links in distributed data store
97 * that uses optimistic replication and gossip based techniques.
98 */
99@Component(immediate = true)
100@Service
101public class GossipLinkStore
102 extends AbstractStore<LinkEvent, LinkStoreDelegate>
103 implements LinkStore {
104
105 private final Logger log = getLogger(getClass());
106
107 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700108 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700109 new ConcurrentHashMap<>();
110
111 // Link instance cache
112 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
113
114 // Egress and ingress link sets
115 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
116 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
117
118 // Remove links
119 private final Map<LinkKey, Timestamp> removedLinks = Maps.newHashMap();
120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700122 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
125 protected ClusterCommunicationService clusterCommunicator;
126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
128 protected ClusterService clusterService;
129
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800130 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700131 @Override
132 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700133 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800134 .register(DistributedStoreSerializers.STORE_COMMON)
135 .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
Madan Jampani2ff05592014-10-10 15:42:47 -0700136 .register(InternalLinkEvent.class)
137 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700138 .register(LinkAntiEntropyAdvertisement.class)
139 .register(LinkFragmentId.class)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800140 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700141 }
142 };
143
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800144 private ScheduledExecutorService backgroundExecutors;
Madan Jampania97e8202014-10-10 17:01:33 -0700145
Madan Jampani2ff05592014-10-10 15:42:47 -0700146 @Activate
147 public void activate() {
148
149 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700150 GossipLinkStoreMessageSubjects.LINK_UPDATE,
151 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700152 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700153 GossipLinkStoreMessageSubjects.LINK_REMOVED,
154 new InternalLinkRemovedEventListener());
155 clusterCommunicator.addSubscriber(
156 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
157 new InternalLinkAntiEntropyAdvertisementListener());
158
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800159 backgroundExecutors =
160 newSingleThreadScheduledExecutor(minPriority(namedThreads("link-bg-%d")));
Madan Jampania97e8202014-10-10 17:01:33 -0700161
162 // TODO: Make these configurable
163 long initialDelaySec = 5;
164 long periodSec = 5;
165 // start anti-entropy thread
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800166 backgroundExecutors.scheduleAtFixedRate(new SendAdvertisementTask(),
Madan Jampania97e8202014-10-10 17:01:33 -0700167 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700168
169 log.info("Started");
170 }
171
172 @Deactivate
173 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700174
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800175 backgroundExecutors.shutdownNow();
Madan Jampani3ffbb272014-10-13 11:19:37 -0700176 try {
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800177 if (!backgroundExecutors.awaitTermination(5, TimeUnit.SECONDS)) {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700178 log.error("Timeout during executor shutdown");
179 }
180 } catch (InterruptedException e) {
181 log.error("Error during executor shutdown", e);
182 }
183
Madan Jampani2ff05592014-10-10 15:42:47 -0700184 linkDescs.clear();
185 links.clear();
186 srcLinks.clear();
187 dstLinks.clear();
188 log.info("Stopped");
189 }
190
191 @Override
192 public int getLinkCount() {
193 return links.size();
194 }
195
196 @Override
197 public Iterable<Link> getLinks() {
198 return Collections.unmodifiableCollection(links.values());
199 }
200
201 @Override
202 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
203 // lock for iteration
204 synchronized (srcLinks) {
205 return FluentIterable.from(srcLinks.get(deviceId))
206 .transform(lookupLink())
207 .filter(notNull())
208 .toSet();
209 }
210 }
211
212 @Override
213 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
214 // lock for iteration
215 synchronized (dstLinks) {
216 return FluentIterable.from(dstLinks.get(deviceId))
217 .transform(lookupLink())
218 .filter(notNull())
219 .toSet();
220 }
221 }
222
223 @Override
224 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700225 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700226 }
227
228 @Override
229 public Set<Link> getEgressLinks(ConnectPoint src) {
230 Set<Link> egress = new HashSet<>();
231 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
232 if (linkKey.src().equals(src)) {
233 egress.add(links.get(linkKey));
234 }
235 }
236 return egress;
237 }
238
239 @Override
240 public Set<Link> getIngressLinks(ConnectPoint dst) {
241 Set<Link> ingress = new HashSet<>();
242 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
243 if (linkKey.dst().equals(dst)) {
244 ingress.add(links.get(linkKey));
245 }
246 }
247 return ingress;
248 }
249
250 @Override
251 public LinkEvent createOrUpdateLink(ProviderId providerId,
252 LinkDescription linkDescription) {
253
254 DeviceId dstDeviceId = linkDescription.dst().deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700255 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700256
257 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
258
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700259 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700260 final LinkEvent event;
261 final Timestamped<LinkDescription> mergedDesc;
alshabibdfc7afb2014-10-21 20:13:27 -0700262 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
263 synchronized (map) {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700264 event = createOrUpdateLinkInternal(providerId, deltaDesc);
alshabibdfc7afb2014-10-21 20:13:27 -0700265 mergedDesc = map.get(providerId);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700266 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700267
268 if (event != null) {
269 log.info("Notifying peers of a link update topology event from providerId: "
270 + "{} between src: {} and dst: {}",
271 providerId, linkDescription.src(), linkDescription.dst());
272 try {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700273 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
Madan Jampani2ff05592014-10-10 15:42:47 -0700274 } catch (IOException e) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800275 log.debug("Failed to notify peers of a link update topology event from providerId: "
alshabibdfc7afb2014-10-21 20:13:27 -0700276 + "{} between src: {} and dst: {}",
277 providerId, linkDescription.src(), linkDescription.dst());
Madan Jampani2ff05592014-10-10 15:42:47 -0700278 }
279 }
280 return event;
281 }
282
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800283 @Override
284 public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) {
285 Link link = getLink(src, dst);
286 if (link == null) {
287 return null;
288 }
289
290 if (link.isDurable()) {
291 // FIXME: this is not the right thing to call for the gossip store; will not sync link state!!!
292 return link.state() == INACTIVE ? null :
293 updateLink(linkKey(link.src(), link.dst()), link,
294 new DefaultLink(link.providerId(),
295 link.src(), link.dst(),
296 link.type(), INACTIVE,
297 link.isDurable(),
298 link.annotations()));
299 }
300 return removeLink(src, dst);
301 }
302
Madan Jampani2ff05592014-10-10 15:42:47 -0700303 private LinkEvent createOrUpdateLinkInternal(
304 ProviderId providerId,
305 Timestamped<LinkDescription> linkDescription) {
306
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700307 final LinkKey key = linkKey(linkDescription.value().src(),
308 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700309 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700310
311 synchronized (descs) {
312 // if the link was previously removed, we should proceed if and
313 // only if this request is more recent.
314 Timestamp linkRemovedTimestamp = removedLinks.get(key);
315 if (linkRemovedTimestamp != null) {
316 if (linkDescription.isNewer(linkRemovedTimestamp)) {
317 removedLinks.remove(key);
318 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700319 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700320 return null;
321 }
322 }
323
324 final Link oldLink = links.get(key);
325 // update description
326 createOrUpdateLinkDescription(descs, providerId, linkDescription);
327 final Link newLink = composeLink(descs);
328 if (oldLink == null) {
329 return createLink(key, newLink);
330 }
331 return updateLink(key, oldLink, newLink);
332 }
333 }
334
335 // Guarded by linkDescs value (=locking each Link)
336 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700337 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700338 ProviderId providerId,
339 Timestamped<LinkDescription> linkDescription) {
340
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700341 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700342 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700343 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700344 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700345 return null;
346 }
347 Timestamped<LinkDescription> newLinkDescription = linkDescription;
348 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700349 // we only allow transition from INDIRECT -> DIRECT
350 final Type newType;
351 if (existingLinkDescription.value().type() == DIRECT) {
352 newType = DIRECT;
353 } else {
354 newType = linkDescription.value().type();
355 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700356 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
357 linkDescription.value().annotations());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800358 newLinkDescription = new Timestamped<>(
Madan Jampani2ff05592014-10-10 15:42:47 -0700359 new DefaultLinkDescription(
360 linkDescription.value().src(),
361 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700362 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700363 linkDescription.timestamp());
364 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700365 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700366 }
367
368 // Creates and stores the link and returns the appropriate event.
369 // Guarded by linkDescs value (=locking each Link)
370 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700371 links.put(key, newLink);
372 srcLinks.put(newLink.src().deviceId(), key);
373 dstLinks.put(newLink.dst().deviceId(), key);
374 return new LinkEvent(LINK_ADDED, newLink);
375 }
376
377 // Updates, if necessary the specified link and returns the appropriate event.
378 // Guarded by linkDescs value (=locking each Link)
379 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700380 // Note: INDIRECT -> DIRECT transition only
381 // so that BDDP discovered Link will not overwrite LDDP Link
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800382 if (oldLink.state() != newLink.state() ||
383 (oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
Madan Jampani2ff05592014-10-10 15:42:47 -0700384 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
385
386 links.put(key, newLink);
387 // strictly speaking following can be ommitted
388 srcLinks.put(oldLink.src().deviceId(), key);
389 dstLinks.put(oldLink.dst().deviceId(), key);
390 return new LinkEvent(LINK_UPDATED, newLink);
391 }
392 return null;
393 }
394
395 @Override
396 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700397 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700398
399 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700400 Timestamp timestamp = null;
401 try {
402 timestamp = deviceClockService.getTimestamp(dstDeviceId);
403 } catch (IllegalStateException e) {
Yuta HIGUCHId6a0ac32014-11-04 09:26:31 -0800404 log.warn("Failed to remove link {}, was not the master", key);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700405 //there are times when this is called before mastership
406 // handoff correctly completes.
407 return null;
408 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700409
410 LinkEvent event = removeLinkInternal(key, timestamp);
411
412 if (event != null) {
413 log.info("Notifying peers of a link removed topology event for a link "
414 + "between src: {} and dst: {}", src, dst);
415 try {
416 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
417 } catch (IOException e) {
418 log.error("Failed to notify peers of a link removed topology event for a link "
419 + "between src: {} and dst: {}", src, dst);
420 }
421 }
422 return event;
423 }
424
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700425 private static Timestamped<LinkDescription> getPrimaryDescription(
426 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
427
Madan Jampani2ff05592014-10-10 15:42:47 -0700428 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700429 for (Entry<ProviderId, Timestamped<LinkDescription>>
430 e : linkDescriptions.entrySet()) {
431
432 if (!e.getKey().isAncillary()) {
433 return e.getValue();
434 }
435 }
436 }
437 return null;
438 }
439
440
441 // TODO: consider slicing out as Timestamp utils
442 /**
443 * Checks is timestamp is more recent than timestamped object.
444 *
445 * @param timestamp to check if this is more recent then other
446 * @param timestamped object to be tested against
447 * @return true if {@code timestamp} is more recent than {@code timestamped}
448 * or {@code timestamped is null}
449 */
450 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
451 checkNotNull(timestamp);
452 if (timestamped == null) {
453 return true;
454 }
455 return timestamp.compareTo(timestamped.timestamp()) > 0;
456 }
457
458 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
459 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
460 = getOrCreateLinkDescriptions(key);
461
462 synchronized (linkDescriptions) {
463 if (linkDescriptions.isEmpty()) {
464 // never seen such link before. keeping timestamp for record
465 removedLinks.put(key, timestamp);
466 return null;
467 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700468 // accept removal request if given timestamp is newer than
469 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700470 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
471 if (!isMoreRecent(timestamp, prim)) {
472 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700473 return null;
474 }
475 removedLinks.put(key, timestamp);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800476 Link link = links.remove(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700477 linkDescriptions.clear();
478 if (link != null) {
479 srcLinks.remove(link.src().deviceId(), key);
480 dstLinks.remove(link.dst().deviceId(), key);
481 return new LinkEvent(LINK_REMOVED, link);
482 }
483 return null;
484 }
485 }
486
487 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
488 return synchronizedSetMultimap(HashMultimap.<K, V>create());
489 }
490
491 /**
492 * @return primary ProviderID, or randomly chosen one if none exists
493 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700494 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700495 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700496
497 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700498 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700499 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700500 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700501 return e.getKey();
502 } else if (fallBackPrimary == null) {
503 // pick randomly as a fallback in case there is no primary
504 fallBackPrimary = e.getKey();
505 }
506 }
507 return fallBackPrimary;
508 }
509
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700510 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700511 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700512 ProviderId baseProviderId = pickBaseProviderId(descs);
513 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700514
515 ConnectPoint src = base.value().src();
516 ConnectPoint dst = base.value().dst();
517 Type type = base.value().type();
518 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
519 annotations = merge(annotations, base.value().annotations());
520
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700521 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700522 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700523 continue;
524 }
525
526 // TODO: should keep track of Description timestamp
527 // and only merge conflicting keys when timestamp is newer
528 // Currently assuming there will never be a key conflict between
529 // providers
530
531 // annotation merging. not so efficient, should revisit later
532 annotations = merge(annotations, e.getValue().value().annotations());
533 }
534
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800535 boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800536 return new DefaultLink(baseProviderId, src, dst, type, ACTIVE, isDurable, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700537 }
538
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700539 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700540 Map<ProviderId, Timestamped<LinkDescription>> r;
541 r = linkDescs.get(key);
542 if (r != null) {
543 return r;
544 }
545 r = new HashMap<>();
546 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
547 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
548 if (concurrentlyAdded != null) {
549 return concurrentlyAdded;
550 } else {
551 return r;
552 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700553 }
554
555 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800556
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700557 /**
558 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800559 *
560 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700561 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700562 private Function<LinkKey, Link> lookupLink() {
563 return lookupLink;
564 }
565
566 private final class LookupLink implements Function<LinkKey, Link> {
567 @Override
568 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700569 if (input == null) {
570 return null;
571 } else {
572 return links.get(input);
573 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700574 }
575 }
576
Madan Jampani2ff05592014-10-10 15:42:47 -0700577 private void notifyDelegateIfNotNull(LinkEvent event) {
578 if (event != null) {
579 notifyDelegate(event);
580 }
581 }
582
Madan Jampani2ff05592014-10-10 15:42:47 -0700583 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
584 ClusterMessage message = new ClusterMessage(
585 clusterService.getLocalNode().id(),
586 subject,
587 SERIALIZER.encode(event));
588 clusterCommunicator.broadcast(message);
589 }
590
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700591 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
592 ClusterMessage message = new ClusterMessage(
593 clusterService.getLocalNode().id(),
594 subject,
595 SERIALIZER.encode(event));
596 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700597 }
598
Madan Jampani2ff05592014-10-10 15:42:47 -0700599 private void notifyPeers(InternalLinkEvent event) throws IOException {
600 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
601 }
602
603 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
604 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
605 }
606
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700607 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700608 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700609 try {
610 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
611 } catch (IOException e) {
612 log.debug("Failed to notify peer {} with message {}", peer, event);
613 }
Madan Jampania97e8202014-10-10 17:01:33 -0700614 }
615
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700616 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700617 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700618 try {
619 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
620 } catch (IOException e) {
621 log.debug("Failed to notify peer {} with message {}", peer, event);
622 }
Madan Jampania97e8202014-10-10 17:01:33 -0700623 }
624
625 private final class SendAdvertisementTask implements Runnable {
626
627 @Override
628 public void run() {
629 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800630 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700631 return;
632 }
633
634 try {
635 final NodeId self = clusterService.getLocalNode().id();
636 Set<ControllerNode> nodes = clusterService.getNodes();
637
638 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
639 .transform(toNodeId())
640 .toList();
641
642 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800643 log.trace("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700644 return;
645 }
646
647 NodeId peer;
648 do {
649 int idx = RandomUtils.nextInt(0, nodeIds.size());
650 peer = nodeIds.get(idx);
651 } while (peer.equals(self));
652
653 LinkAntiEntropyAdvertisement ad = createAdvertisement();
654
655 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800656 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700657 return;
658 }
659
660 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700661 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
662 } catch (IOException e) {
663 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700664 return;
665 }
666 } catch (Exception e) {
667 // catch all Exception to avoid Scheduled task being suppressed.
668 log.error("Exception thrown while sending advertisement", e);
669 }
670 }
671 }
672
673 private LinkAntiEntropyAdvertisement createAdvertisement() {
674 final NodeId self = clusterService.getLocalNode().id();
675
676 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
677 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
678
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800679 linkDescs.forEach((linkKey, linkDesc) -> {
Madan Jampania97e8202014-10-10 17:01:33 -0700680 synchronized (linkDesc) {
681 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
682 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
683 }
684 }
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800685 });
Madan Jampania97e8202014-10-10 17:01:33 -0700686
687 linkTombstones.putAll(removedLinks);
688
689 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
690 }
691
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700692 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700693
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700694 final NodeId sender = ad.sender();
695 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700696
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700697 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
698 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700699
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700700 final LinkKey key = l.getKey();
701 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
702 synchronized (link) {
703 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700704
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700705 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
706 final ProviderId providerId = p.getKey();
707 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700708
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700709 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
710 // remote
711 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
712 if (remoteTimestamp == null) {
713 remoteTimestamp = ad.linkTombstones().get(key);
714 }
715 if (remoteTimestamp == null ||
716 pDesc.isNewer(remoteTimestamp)) {
717 // I have more recent link description. update peer.
718 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
719 } else {
720 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
721 if (remoteLive != null &&
722 remoteLive.compareTo(pDesc.timestamp()) > 0) {
723 // I have something outdated
724 localOutdated = true;
725 }
726 }
727
728 // search local latest along the way
729 if (localLatest == null ||
730 pDesc.isNewer(localLatest)) {
731 localLatest = pDesc.timestamp();
732 }
733 }
734 // Tests if remote remove is more recent then local latest.
735 final Timestamp remoteRemove = ad.linkTombstones().get(key);
736 if (remoteRemove != null) {
737 if (localLatest != null &&
738 localLatest.compareTo(remoteRemove) < 0) {
739 // remote remove is more recent
740 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
741 }
742 }
Madan Jampania97e8202014-10-10 17:01:33 -0700743 }
744 }
745
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700746 // populate remove info if not known locally
747 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
748 final LinkKey key = remoteRm.getKey();
749 final Timestamp remoteRemove = remoteRm.getValue();
750 // relying on removeLinkInternal to ignore stale info
751 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
752 }
Madan Jampania97e8202014-10-10 17:01:33 -0700753
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700754 if (localOutdated) {
755 // send back advertisement to speed up convergence
756 try {
757 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
758 createAdvertisement());
759 } catch (IOException e) {
760 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700761 }
762 }
763 }
764
Madan Jampani2ff05592014-10-10 15:42:47 -0700765 private class InternalLinkEventListener implements ClusterMessageHandler {
766 @Override
767 public void handle(ClusterMessage message) {
768
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700769 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700770 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
771
772 ProviderId providerId = event.providerId();
773 Timestamped<LinkDescription> linkDescription = event.linkDescription();
774
775 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
776 }
777 }
778
779 private class InternalLinkRemovedEventListener implements ClusterMessageHandler {
780 @Override
781 public void handle(ClusterMessage message) {
782
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700783 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700784 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
785
786 LinkKey linkKey = event.linkKey();
787 Timestamp timestamp = event.timestamp();
788
789 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
790 }
791 }
Madan Jampania97e8202014-10-10 17:01:33 -0700792
793 private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler {
794
795 @Override
796 public void handle(ClusterMessage message) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800797 log.trace("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700798 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800799 backgroundExecutors.submit(new Runnable() {
800
801 @Override
802 public void run() {
803 try {
804 handleAntiEntropyAdvertisement(advertisement);
805 } catch (Exception e) {
806 log.warn("Exception thrown while handling Link advertisements", e);
807 throw e;
808 }
809 }
810 });
Madan Jampania97e8202014-10-10 17:01:33 -0700811 }
812 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700813}