blob: 2a601204732e1f4e1fa54137544615e11d73f9b6 [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;
Yuta HIGUCHI80d56592014-11-25 15:11:13 -080074import java.util.concurrent.ExecutorService;
75import java.util.concurrent.Executors;
Madan Jampania97e8202014-10-10 17:01:33 -070076import 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;
83import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070084import static org.onlab.onos.net.DefaultAnnotations.merge;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080085import static org.onlab.onos.net.DefaultAnnotations.union;
86import static org.onlab.onos.net.Link.State.ACTIVE;
87import static org.onlab.onos.net.Link.State.INACTIVE;
Madan Jampani2ff05592014-10-10 15:42:47 -070088import static org.onlab.onos.net.Link.Type.DIRECT;
89import static org.onlab.onos.net.Link.Type.INDIRECT;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070090import static org.onlab.onos.net.LinkKey.linkKey;
Madan Jampani2ff05592014-10-10 15:42:47 -070091import static org.onlab.onos.net.link.LinkEvent.Type.*;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080092import static org.onlab.onos.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Yuta HIGUCHI06586272014-11-25 14:27:03 -080093import static org.onlab.util.Tools.minPriority;
Madan Jampania97e8202014-10-10 17:01:33 -070094import static org.onlab.util.Tools.namedThreads;
Madan Jampani2ff05592014-10-10 15:42:47 -070095import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani2ff05592014-10-10 15:42:47 -070096
97/**
98 * Manages inventory of infrastructure links in distributed data store
99 * that uses optimistic replication and gossip based techniques.
100 */
101@Component(immediate = true)
102@Service
103public class GossipLinkStore
104 extends AbstractStore<LinkEvent, LinkStoreDelegate>
105 implements LinkStore {
106
107 private final Logger log = getLogger(getClass());
108
109 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700110 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700111 new ConcurrentHashMap<>();
112
113 // Link instance cache
114 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
115
116 // Egress and ingress link sets
117 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
118 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
119
120 // Remove links
121 private final Map<LinkKey, Timestamp> removedLinks = Maps.newHashMap();
122
123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700124 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
127 protected ClusterCommunicationService clusterCommunicator;
128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
130 protected ClusterService clusterService;
131
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800132 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700133 @Override
134 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700135 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800136 .register(DistributedStoreSerializers.STORE_COMMON)
137 .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
Madan Jampani2ff05592014-10-10 15:42:47 -0700138 .register(InternalLinkEvent.class)
139 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700140 .register(LinkAntiEntropyAdvertisement.class)
141 .register(LinkFragmentId.class)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800142 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700143 }
144 };
145
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800146 private ExecutorService executor;
147
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800148 private ScheduledExecutorService backgroundExecutors;
Madan Jampania97e8202014-10-10 17:01:33 -0700149
Madan Jampani2ff05592014-10-10 15:42:47 -0700150 @Activate
151 public void activate() {
152
153 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700154 GossipLinkStoreMessageSubjects.LINK_UPDATE,
155 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700156 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700157 GossipLinkStoreMessageSubjects.LINK_REMOVED,
158 new InternalLinkRemovedEventListener());
159 clusterCommunicator.addSubscriber(
160 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
161 new InternalLinkAntiEntropyAdvertisementListener());
162
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800163 executor = Executors.newCachedThreadPool(namedThreads("link-fg-%d"));
164
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800165 backgroundExecutors =
166 newSingleThreadScheduledExecutor(minPriority(namedThreads("link-bg-%d")));
Madan Jampania97e8202014-10-10 17:01:33 -0700167
168 // TODO: Make these configurable
169 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)) {
241 egress.add(links.get(linkKey));
242 }
243 }
244 return egress;
245 }
246
247 @Override
248 public Set<Link> getIngressLinks(ConnectPoint dst) {
249 Set<Link> ingress = new HashSet<>();
250 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
251 if (linkKey.dst().equals(dst)) {
252 ingress.add(links.get(linkKey));
253 }
254 }
255 return ingress;
256 }
257
258 @Override
259 public LinkEvent createOrUpdateLink(ProviderId providerId,
260 LinkDescription linkDescription) {
261
262 DeviceId dstDeviceId = linkDescription.dst().deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700263 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700264
265 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
266
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700267 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700268 final LinkEvent event;
269 final Timestamped<LinkDescription> mergedDesc;
alshabibdfc7afb2014-10-21 20:13:27 -0700270 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
271 synchronized (map) {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700272 event = createOrUpdateLinkInternal(providerId, deltaDesc);
alshabibdfc7afb2014-10-21 20:13:27 -0700273 mergedDesc = map.get(providerId);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700274 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700275
276 if (event != null) {
277 log.info("Notifying peers of a link update topology event from providerId: "
278 + "{} between src: {} and dst: {}",
279 providerId, linkDescription.src(), linkDescription.dst());
280 try {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700281 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
Madan Jampani2ff05592014-10-10 15:42:47 -0700282 } catch (IOException e) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800283 log.debug("Failed to notify peers of a link update topology event from providerId: "
alshabibdfc7afb2014-10-21 20:13:27 -0700284 + "{} between src: {} and dst: {}",
285 providerId, linkDescription.src(), linkDescription.dst());
Madan Jampani2ff05592014-10-10 15:42:47 -0700286 }
287 }
288 return event;
289 }
290
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800291 @Override
292 public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) {
293 Link link = getLink(src, dst);
294 if (link == null) {
295 return null;
296 }
297
298 if (link.isDurable()) {
299 // FIXME: this is not the right thing to call for the gossip store; will not sync link state!!!
300 return link.state() == INACTIVE ? null :
301 updateLink(linkKey(link.src(), link.dst()), link,
302 new DefaultLink(link.providerId(),
303 link.src(), link.dst(),
304 link.type(), INACTIVE,
305 link.isDurable(),
306 link.annotations()));
307 }
308 return removeLink(src, dst);
309 }
310
Madan Jampani2ff05592014-10-10 15:42:47 -0700311 private LinkEvent createOrUpdateLinkInternal(
312 ProviderId providerId,
313 Timestamped<LinkDescription> linkDescription) {
314
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700315 final LinkKey key = linkKey(linkDescription.value().src(),
316 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700317 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700318
319 synchronized (descs) {
320 // if the link was previously removed, we should proceed if and
321 // only if this request is more recent.
322 Timestamp linkRemovedTimestamp = removedLinks.get(key);
323 if (linkRemovedTimestamp != null) {
324 if (linkDescription.isNewer(linkRemovedTimestamp)) {
325 removedLinks.remove(key);
326 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700327 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700328 return null;
329 }
330 }
331
332 final Link oldLink = links.get(key);
333 // update description
334 createOrUpdateLinkDescription(descs, providerId, linkDescription);
335 final Link newLink = composeLink(descs);
336 if (oldLink == null) {
337 return createLink(key, newLink);
338 }
339 return updateLink(key, oldLink, newLink);
340 }
341 }
342
343 // Guarded by linkDescs value (=locking each Link)
344 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700345 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700346 ProviderId providerId,
347 Timestamped<LinkDescription> linkDescription) {
348
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700349 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700350 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700351 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700352 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700353 return null;
354 }
355 Timestamped<LinkDescription> newLinkDescription = linkDescription;
356 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700357 // we only allow transition from INDIRECT -> DIRECT
358 final Type newType;
359 if (existingLinkDescription.value().type() == DIRECT) {
360 newType = DIRECT;
361 } else {
362 newType = linkDescription.value().type();
363 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700364 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
365 linkDescription.value().annotations());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800366 newLinkDescription = new Timestamped<>(
Madan Jampani2ff05592014-10-10 15:42:47 -0700367 new DefaultLinkDescription(
368 linkDescription.value().src(),
369 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700370 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700371 linkDescription.timestamp());
372 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700373 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700374 }
375
376 // Creates and stores the link and returns the appropriate event.
377 // Guarded by linkDescs value (=locking each Link)
378 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700379 links.put(key, newLink);
380 srcLinks.put(newLink.src().deviceId(), key);
381 dstLinks.put(newLink.dst().deviceId(), key);
382 return new LinkEvent(LINK_ADDED, newLink);
383 }
384
385 // Updates, if necessary the specified link and returns the appropriate event.
386 // Guarded by linkDescs value (=locking each Link)
387 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700388 // Note: INDIRECT -> DIRECT transition only
389 // so that BDDP discovered Link will not overwrite LDDP Link
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800390 if (oldLink.state() != newLink.state() ||
391 (oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
Madan Jampani2ff05592014-10-10 15:42:47 -0700392 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
393
394 links.put(key, newLink);
395 // strictly speaking following can be ommitted
396 srcLinks.put(oldLink.src().deviceId(), key);
397 dstLinks.put(oldLink.dst().deviceId(), key);
398 return new LinkEvent(LINK_UPDATED, newLink);
399 }
400 return null;
401 }
402
403 @Override
404 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700405 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700406
407 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700408 Timestamp timestamp = null;
409 try {
410 timestamp = deviceClockService.getTimestamp(dstDeviceId);
411 } catch (IllegalStateException e) {
Yuta HIGUCHId6a0ac32014-11-04 09:26:31 -0800412 log.warn("Failed to remove link {}, was not the master", key);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700413 //there are times when this is called before mastership
414 // handoff correctly completes.
415 return null;
416 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700417
418 LinkEvent event = removeLinkInternal(key, timestamp);
419
420 if (event != null) {
421 log.info("Notifying peers of a link removed topology event for a link "
422 + "between src: {} and dst: {}", src, dst);
423 try {
424 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
425 } catch (IOException e) {
426 log.error("Failed to notify peers of a link removed topology event for a link "
427 + "between src: {} and dst: {}", src, dst);
428 }
429 }
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
495 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
496 return synchronizedSetMultimap(HashMultimap.<K, V>create());
497 }
498
499 /**
500 * @return primary ProviderID, or randomly chosen one if none exists
501 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700502 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700503 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700504
505 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700506 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700507 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700508 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700509 return e.getKey();
510 } else if (fallBackPrimary == null) {
511 // pick randomly as a fallback in case there is no primary
512 fallBackPrimary = e.getKey();
513 }
514 }
515 return fallBackPrimary;
516 }
517
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700518 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700519 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700520 ProviderId baseProviderId = pickBaseProviderId(descs);
521 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700522
523 ConnectPoint src = base.value().src();
524 ConnectPoint dst = base.value().dst();
525 Type type = base.value().type();
526 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
527 annotations = merge(annotations, base.value().annotations());
528
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700529 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700530 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700531 continue;
532 }
533
534 // TODO: should keep track of Description timestamp
535 // and only merge conflicting keys when timestamp is newer
536 // Currently assuming there will never be a key conflict between
537 // providers
538
539 // annotation merging. not so efficient, should revisit later
540 annotations = merge(annotations, e.getValue().value().annotations());
541 }
542
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800543 boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800544 return new DefaultLink(baseProviderId, src, dst, type, ACTIVE, isDurable, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700545 }
546
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700547 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700548 Map<ProviderId, Timestamped<LinkDescription>> r;
549 r = linkDescs.get(key);
550 if (r != null) {
551 return r;
552 }
553 r = new HashMap<>();
554 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
555 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
556 if (concurrentlyAdded != null) {
557 return concurrentlyAdded;
558 } else {
559 return r;
560 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700561 }
562
563 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800564
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700565 /**
566 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800567 *
568 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700569 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700570 private Function<LinkKey, Link> lookupLink() {
571 return lookupLink;
572 }
573
574 private final class LookupLink implements Function<LinkKey, Link> {
575 @Override
576 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700577 if (input == null) {
578 return null;
579 } else {
580 return links.get(input);
581 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700582 }
583 }
584
Madan Jampani2ff05592014-10-10 15:42:47 -0700585 private void notifyDelegateIfNotNull(LinkEvent event) {
586 if (event != null) {
587 notifyDelegate(event);
588 }
589 }
590
Madan Jampani2ff05592014-10-10 15:42:47 -0700591 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
592 ClusterMessage message = new ClusterMessage(
593 clusterService.getLocalNode().id(),
594 subject,
595 SERIALIZER.encode(event));
596 clusterCommunicator.broadcast(message);
597 }
598
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700599 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
600 ClusterMessage message = new ClusterMessage(
601 clusterService.getLocalNode().id(),
602 subject,
603 SERIALIZER.encode(event));
604 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700605 }
606
Madan Jampani2ff05592014-10-10 15:42:47 -0700607 private void notifyPeers(InternalLinkEvent event) throws IOException {
608 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
609 }
610
611 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
612 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
613 }
614
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700615 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700616 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700617 try {
618 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
619 } catch (IOException e) {
620 log.debug("Failed to notify peer {} with message {}", peer, event);
621 }
Madan Jampania97e8202014-10-10 17:01:33 -0700622 }
623
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700624 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700625 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700626 try {
627 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
628 } catch (IOException e) {
629 log.debug("Failed to notify peer {} with message {}", peer, event);
630 }
Madan Jampania97e8202014-10-10 17:01:33 -0700631 }
632
633 private final class SendAdvertisementTask implements Runnable {
634
635 @Override
636 public void run() {
637 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800638 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700639 return;
640 }
641
642 try {
643 final NodeId self = clusterService.getLocalNode().id();
644 Set<ControllerNode> nodes = clusterService.getNodes();
645
646 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
647 .transform(toNodeId())
648 .toList();
649
650 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800651 log.trace("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700652 return;
653 }
654
655 NodeId peer;
656 do {
657 int idx = RandomUtils.nextInt(0, nodeIds.size());
658 peer = nodeIds.get(idx);
659 } while (peer.equals(self));
660
661 LinkAntiEntropyAdvertisement ad = createAdvertisement();
662
663 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800664 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700665 return;
666 }
667
668 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700669 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
670 } catch (IOException e) {
671 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700672 return;
673 }
674 } catch (Exception e) {
675 // catch all Exception to avoid Scheduled task being suppressed.
676 log.error("Exception thrown while sending advertisement", e);
677 }
678 }
679 }
680
681 private LinkAntiEntropyAdvertisement createAdvertisement() {
682 final NodeId self = clusterService.getLocalNode().id();
683
684 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
685 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
686
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800687 linkDescs.forEach((linkKey, linkDesc) -> {
Madan Jampania97e8202014-10-10 17:01:33 -0700688 synchronized (linkDesc) {
689 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
690 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
691 }
692 }
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800693 });
Madan Jampania97e8202014-10-10 17:01:33 -0700694
695 linkTombstones.putAll(removedLinks);
696
697 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
698 }
699
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700700 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700701
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700702 final NodeId sender = ad.sender();
703 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700704
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700705 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
706 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700707
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700708 final LinkKey key = l.getKey();
709 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
710 synchronized (link) {
711 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700712
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700713 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
714 final ProviderId providerId = p.getKey();
715 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700716
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700717 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
718 // remote
719 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
720 if (remoteTimestamp == null) {
721 remoteTimestamp = ad.linkTombstones().get(key);
722 }
723 if (remoteTimestamp == null ||
724 pDesc.isNewer(remoteTimestamp)) {
725 // I have more recent link description. update peer.
726 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
727 } else {
728 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
729 if (remoteLive != null &&
730 remoteLive.compareTo(pDesc.timestamp()) > 0) {
731 // I have something outdated
732 localOutdated = true;
733 }
734 }
735
736 // search local latest along the way
737 if (localLatest == null ||
738 pDesc.isNewer(localLatest)) {
739 localLatest = pDesc.timestamp();
740 }
741 }
742 // Tests if remote remove is more recent then local latest.
743 final Timestamp remoteRemove = ad.linkTombstones().get(key);
744 if (remoteRemove != null) {
745 if (localLatest != null &&
746 localLatest.compareTo(remoteRemove) < 0) {
747 // remote remove is more recent
748 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
749 }
750 }
Madan Jampania97e8202014-10-10 17:01:33 -0700751 }
752 }
753
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700754 // populate remove info if not known locally
755 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
756 final LinkKey key = remoteRm.getKey();
757 final Timestamp remoteRemove = remoteRm.getValue();
758 // relying on removeLinkInternal to ignore stale info
759 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
760 }
Madan Jampania97e8202014-10-10 17:01:33 -0700761
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700762 if (localOutdated) {
763 // send back advertisement to speed up convergence
764 try {
765 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
766 createAdvertisement());
767 } catch (IOException e) {
768 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700769 }
770 }
771 }
772
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800773 private final class InternalLinkEventListener
774 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700775 @Override
776 public void handle(ClusterMessage message) {
777
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700778 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700779 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
780
781 ProviderId providerId = event.providerId();
782 Timestamped<LinkDescription> linkDescription = event.linkDescription();
783
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800784 executor.submit(new Runnable() {
785
786 @Override
787 public void run() {
788 try {
789 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
790 } catch (Exception e) {
791 log.warn("Exception thrown handling link event", e);
792 }
793 }
794 });
Madan Jampani2ff05592014-10-10 15:42:47 -0700795 }
796 }
797
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800798 private final class InternalLinkRemovedEventListener
799 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700800 @Override
801 public void handle(ClusterMessage message) {
802
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700803 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700804 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
805
806 LinkKey linkKey = event.linkKey();
807 Timestamp timestamp = event.timestamp();
808
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800809 executor.submit(new Runnable() {
810
811 @Override
812 public void run() {
813 try {
814 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
815 } catch (Exception e) {
816 log.warn("Exception thrown handling link removed", e);
817 }
818 }
819 });
Madan Jampani2ff05592014-10-10 15:42:47 -0700820 }
821 }
Madan Jampania97e8202014-10-10 17:01:33 -0700822
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800823 private final class InternalLinkAntiEntropyAdvertisementListener
824 implements ClusterMessageHandler {
Madan Jampania97e8202014-10-10 17:01:33 -0700825
826 @Override
827 public void handle(ClusterMessage message) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800828 log.trace("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700829 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800830 backgroundExecutors.submit(new Runnable() {
831
832 @Override
833 public void run() {
834 try {
835 handleAntiEntropyAdvertisement(advertisement);
836 } catch (Exception e) {
837 log.warn("Exception thrown while handling Link advertisements", e);
838 throw e;
839 }
840 }
841 });
Madan Jampania97e8202014-10-10 17:01:33 -0700842 }
843 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700844}