blob: 0332a7e751375707a0fa51a2b1502427bf1e5c08 [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.SetMultimap;
Yuta HIGUCHI06586272014-11-25 14:27:03 -080023
Madan Jampania97e8202014-10-10 17:01:33 -070024import org.apache.commons.lang3.RandomUtils;
Madan Jampani2ff05592014-10-10 15:42:47 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
31import org.onlab.onos.cluster.ClusterService;
Madan Jampania97e8202014-10-10 17:01:33 -070032import org.onlab.onos.cluster.ControllerNode;
33import org.onlab.onos.cluster.NodeId;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080034import org.onlab.onos.net.AnnotationKeys;
Madan Jampani2ff05592014-10-10 15:42:47 -070035import org.onlab.onos.net.AnnotationsUtil;
36import org.onlab.onos.net.ConnectPoint;
37import org.onlab.onos.net.DefaultAnnotations;
38import org.onlab.onos.net.DefaultLink;
39import org.onlab.onos.net.DeviceId;
40import org.onlab.onos.net.Link;
Madan Jampani2ff05592014-10-10 15:42:47 -070041import org.onlab.onos.net.Link.Type;
42import org.onlab.onos.net.LinkKey;
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -070043import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070044import org.onlab.onos.net.device.DeviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -070045import org.onlab.onos.net.link.DefaultLinkDescription;
46import org.onlab.onos.net.link.LinkDescription;
47import org.onlab.onos.net.link.LinkEvent;
48import org.onlab.onos.net.link.LinkStore;
49import org.onlab.onos.net.link.LinkStoreDelegate;
50import org.onlab.onos.net.provider.ProviderId;
51import org.onlab.onos.store.AbstractStore;
Madan Jampani2ff05592014-10-10 15:42:47 -070052import org.onlab.onos.store.Timestamp;
53import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
54import org.onlab.onos.store.cluster.messaging.ClusterMessage;
55import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler;
56import org.onlab.onos.store.cluster.messaging.MessageSubject;
Yuta HIGUCHIeecee552014-10-16 14:09:01 -070057import org.onlab.onos.store.impl.Timestamped;
Madan Jampani2ff05592014-10-10 15:42:47 -070058import org.onlab.onos.store.serializers.KryoSerializer;
Yuta HIGUCHI60a190b2014-11-07 16:24:47 -080059import org.onlab.onos.store.serializers.impl.DistributedStoreSerializers;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070060import org.onlab.util.KryoNamespace;
Madan Jampani2ff05592014-10-10 15:42:47 -070061import org.slf4j.Logger;
62
63import java.io.IOException;
64import java.util.Collections;
Madan Jampania97e8202014-10-10 17:01:33 -070065import java.util.HashMap;
Madan Jampani2ff05592014-10-10 15:42:47 -070066import java.util.HashSet;
67import java.util.Map;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080068import java.util.Map.Entry;
Thomas Vachuska29a6a782014-11-10 21:31:41 -080069import java.util.Objects;
Madan Jampani2ff05592014-10-10 15:42:47 -070070import java.util.Set;
Madan Jampani2ff05592014-10-10 15:42:47 -070071import java.util.concurrent.ConcurrentHashMap;
72import java.util.concurrent.ConcurrentMap;
Yuta HIGUCHI80d56592014-11-25 15:11:13 -080073import java.util.concurrent.ExecutorService;
74import java.util.concurrent.Executors;
Madan Jampania97e8202014-10-10 17:01:33 -070075import java.util.concurrent.ScheduledExecutorService;
76import java.util.concurrent.TimeUnit;
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;
82import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId;
Madan Jampani2ff05592014-10-10 15:42:47 -070083import static org.onlab.onos.net.DefaultAnnotations.merge;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080084import static org.onlab.onos.net.DefaultAnnotations.union;
85import static org.onlab.onos.net.Link.State.ACTIVE;
86import static org.onlab.onos.net.Link.State.INACTIVE;
Madan Jampani2ff05592014-10-10 15:42:47 -070087import static org.onlab.onos.net.Link.Type.DIRECT;
88import static org.onlab.onos.net.Link.Type.INDIRECT;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070089import static org.onlab.onos.net.LinkKey.linkKey;
Madan Jampani2ff05592014-10-10 15:42:47 -070090import static org.onlab.onos.net.link.LinkEvent.Type.*;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080091import static org.onlab.onos.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Yuta HIGUCHI06586272014-11-25 14:27:03 -080092import static org.onlab.util.Tools.minPriority;
Madan Jampania97e8202014-10-10 17:01:33 -070093import static org.onlab.util.Tools.namedThreads;
Madan Jampani2ff05592014-10-10 15:42:47 -070094import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani2ff05592014-10-10 15:42:47 -070095
96/**
97 * Manages inventory of infrastructure links in distributed data store
98 * that uses optimistic replication and gossip based techniques.
99 */
100@Component(immediate = true)
101@Service
102public class GossipLinkStore
103 extends AbstractStore<LinkEvent, LinkStoreDelegate>
104 implements LinkStore {
105
106 private final Logger log = getLogger(getClass());
107
108 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700109 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700110 new ConcurrentHashMap<>();
111
112 // Link instance cache
113 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
114
115 // Egress and ingress link sets
116 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
117 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
118
119 // Remove links
Yuta HIGUCHIb9125562014-12-01 23:28:22 -0800120 private final Map<LinkKey, Timestamp> removedLinks = new ConcurrentHashMap<>();
Madan Jampani2ff05592014-10-10 15:42:47 -0700121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700123 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected ClusterCommunicationService clusterCommunicator;
127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected ClusterService clusterService;
130
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800131 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700132 @Override
133 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700134 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800135 .register(DistributedStoreSerializers.STORE_COMMON)
136 .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
Madan Jampani2ff05592014-10-10 15:42:47 -0700137 .register(InternalLinkEvent.class)
138 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700139 .register(LinkAntiEntropyAdvertisement.class)
140 .register(LinkFragmentId.class)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800141 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700142 }
143 };
144
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800145 private ExecutorService executor;
146
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800147 private ScheduledExecutorService backgroundExecutors;
Madan Jampania97e8202014-10-10 17:01:33 -0700148
Madan Jampani2ff05592014-10-10 15:42:47 -0700149 @Activate
150 public void activate() {
151
152 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700153 GossipLinkStoreMessageSubjects.LINK_UPDATE,
154 new InternalLinkEventListener());
Madan Jampani2ff05592014-10-10 15:42:47 -0700155 clusterCommunicator.addSubscriber(
Madan Jampania97e8202014-10-10 17:01:33 -0700156 GossipLinkStoreMessageSubjects.LINK_REMOVED,
157 new InternalLinkRemovedEventListener());
158 clusterCommunicator.addSubscriber(
159 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
160 new InternalLinkAntiEntropyAdvertisementListener());
161
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800162 executor = Executors.newCachedThreadPool(namedThreads("link-fg-%d"));
163
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800164 backgroundExecutors =
165 newSingleThreadScheduledExecutor(minPriority(namedThreads("link-bg-%d")));
Madan Jampania97e8202014-10-10 17:01:33 -0700166
167 // TODO: Make these configurable
168 long initialDelaySec = 5;
169 long periodSec = 5;
170 // start anti-entropy thread
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800171 backgroundExecutors.scheduleAtFixedRate(new SendAdvertisementTask(),
Madan Jampania97e8202014-10-10 17:01:33 -0700172 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700173
174 log.info("Started");
175 }
176
177 @Deactivate
178 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700179
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800180 executor.shutdownNow();
181
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800182 backgroundExecutors.shutdownNow();
Madan Jampani3ffbb272014-10-13 11:19:37 -0700183 try {
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800184 if (!backgroundExecutors.awaitTermination(5, TimeUnit.SECONDS)) {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700185 log.error("Timeout during executor shutdown");
186 }
187 } catch (InterruptedException e) {
188 log.error("Error during executor shutdown", e);
189 }
190
Madan Jampani2ff05592014-10-10 15:42:47 -0700191 linkDescs.clear();
192 links.clear();
193 srcLinks.clear();
194 dstLinks.clear();
195 log.info("Stopped");
196 }
197
198 @Override
199 public int getLinkCount() {
200 return links.size();
201 }
202
203 @Override
204 public Iterable<Link> getLinks() {
205 return Collections.unmodifiableCollection(links.values());
206 }
207
208 @Override
209 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
210 // lock for iteration
211 synchronized (srcLinks) {
212 return FluentIterable.from(srcLinks.get(deviceId))
213 .transform(lookupLink())
214 .filter(notNull())
215 .toSet();
216 }
217 }
218
219 @Override
220 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
221 // lock for iteration
222 synchronized (dstLinks) {
223 return FluentIterable.from(dstLinks.get(deviceId))
224 .transform(lookupLink())
225 .filter(notNull())
226 .toSet();
227 }
228 }
229
230 @Override
231 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700232 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700233 }
234
235 @Override
236 public Set<Link> getEgressLinks(ConnectPoint src) {
237 Set<Link> egress = new HashSet<>();
238 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
239 if (linkKey.src().equals(src)) {
240 egress.add(links.get(linkKey));
241 }
242 }
243 return egress;
244 }
245
246 @Override
247 public Set<Link> getIngressLinks(ConnectPoint dst) {
248 Set<Link> ingress = new HashSet<>();
249 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
250 if (linkKey.dst().equals(dst)) {
251 ingress.add(links.get(linkKey));
252 }
253 }
254 return ingress;
255 }
256
257 @Override
258 public LinkEvent createOrUpdateLink(ProviderId providerId,
259 LinkDescription linkDescription) {
260
261 DeviceId dstDeviceId = linkDescription.dst().deviceId();
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700262 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700263
264 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
265
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700266 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700267 final LinkEvent event;
268 final Timestamped<LinkDescription> mergedDesc;
alshabibdfc7afb2014-10-21 20:13:27 -0700269 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
270 synchronized (map) {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700271 event = createOrUpdateLinkInternal(providerId, deltaDesc);
alshabibdfc7afb2014-10-21 20:13:27 -0700272 mergedDesc = map.get(providerId);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700273 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700274
275 if (event != null) {
276 log.info("Notifying peers of a link update topology event from providerId: "
277 + "{} between src: {} and dst: {}",
278 providerId, linkDescription.src(), linkDescription.dst());
279 try {
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700280 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
Madan Jampani2ff05592014-10-10 15:42:47 -0700281 } catch (IOException e) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800282 log.debug("Failed to notify peers of a link update topology event from providerId: "
alshabibdfc7afb2014-10-21 20:13:27 -0700283 + "{} between src: {} and dst: {}",
284 providerId, linkDescription.src(), linkDescription.dst());
Madan Jampani2ff05592014-10-10 15:42:47 -0700285 }
286 }
287 return event;
288 }
289
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800290 @Override
291 public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) {
292 Link link = getLink(src, dst);
293 if (link == null) {
294 return null;
295 }
296
297 if (link.isDurable()) {
298 // FIXME: this is not the right thing to call for the gossip store; will not sync link state!!!
299 return link.state() == INACTIVE ? null :
300 updateLink(linkKey(link.src(), link.dst()), link,
301 new DefaultLink(link.providerId(),
302 link.src(), link.dst(),
303 link.type(), INACTIVE,
304 link.isDurable(),
305 link.annotations()));
306 }
307 return removeLink(src, dst);
308 }
309
Madan Jampani2ff05592014-10-10 15:42:47 -0700310 private LinkEvent createOrUpdateLinkInternal(
311 ProviderId providerId,
312 Timestamped<LinkDescription> linkDescription) {
313
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700314 final LinkKey key = linkKey(linkDescription.value().src(),
315 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700316 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700317
318 synchronized (descs) {
319 // if the link was previously removed, we should proceed if and
320 // only if this request is more recent.
321 Timestamp linkRemovedTimestamp = removedLinks.get(key);
322 if (linkRemovedTimestamp != null) {
323 if (linkDescription.isNewer(linkRemovedTimestamp)) {
324 removedLinks.remove(key);
325 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700326 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700327 return null;
328 }
329 }
330
331 final Link oldLink = links.get(key);
332 // update description
333 createOrUpdateLinkDescription(descs, providerId, linkDescription);
334 final Link newLink = composeLink(descs);
335 if (oldLink == null) {
336 return createLink(key, newLink);
337 }
338 return updateLink(key, oldLink, newLink);
339 }
340 }
341
342 // Guarded by linkDescs value (=locking each Link)
343 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700344 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700345 ProviderId providerId,
346 Timestamped<LinkDescription> linkDescription) {
347
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700348 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700349 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700350 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700351 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700352 return null;
353 }
354 Timestamped<LinkDescription> newLinkDescription = linkDescription;
355 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700356 // we only allow transition from INDIRECT -> DIRECT
357 final Type newType;
358 if (existingLinkDescription.value().type() == DIRECT) {
359 newType = DIRECT;
360 } else {
361 newType = linkDescription.value().type();
362 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700363 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
364 linkDescription.value().annotations());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800365 newLinkDescription = new Timestamped<>(
Madan Jampani2ff05592014-10-10 15:42:47 -0700366 new DefaultLinkDescription(
367 linkDescription.value().src(),
368 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700369 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700370 linkDescription.timestamp());
371 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700372 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700373 }
374
375 // Creates and stores the link and returns the appropriate event.
376 // Guarded by linkDescs value (=locking each Link)
377 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700378 links.put(key, newLink);
379 srcLinks.put(newLink.src().deviceId(), key);
380 dstLinks.put(newLink.dst().deviceId(), key);
381 return new LinkEvent(LINK_ADDED, newLink);
382 }
383
384 // Updates, if necessary the specified link and returns the appropriate event.
385 // Guarded by linkDescs value (=locking each Link)
386 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700387 // Note: INDIRECT -> DIRECT transition only
388 // so that BDDP discovered Link will not overwrite LDDP Link
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800389 if (oldLink.state() != newLink.state() ||
390 (oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
Madan Jampani2ff05592014-10-10 15:42:47 -0700391 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
392
393 links.put(key, newLink);
394 // strictly speaking following can be ommitted
395 srcLinks.put(oldLink.src().deviceId(), key);
396 dstLinks.put(oldLink.dst().deviceId(), key);
397 return new LinkEvent(LINK_UPDATED, newLink);
398 }
399 return null;
400 }
401
402 @Override
403 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700404 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700405
406 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700407 Timestamp timestamp = null;
408 try {
409 timestamp = deviceClockService.getTimestamp(dstDeviceId);
410 } catch (IllegalStateException e) {
Yuta HIGUCHId6a0ac32014-11-04 09:26:31 -0800411 log.warn("Failed to remove link {}, was not the master", key);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700412 //there are times when this is called before mastership
413 // handoff correctly completes.
414 return null;
415 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700416
417 LinkEvent event = removeLinkInternal(key, timestamp);
418
419 if (event != null) {
420 log.info("Notifying peers of a link removed topology event for a link "
421 + "between src: {} and dst: {}", src, dst);
422 try {
423 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
424 } catch (IOException e) {
425 log.error("Failed to notify peers of a link removed topology event for a link "
426 + "between src: {} and dst: {}", src, dst);
427 }
428 }
429 return event;
430 }
431
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700432 private static Timestamped<LinkDescription> getPrimaryDescription(
433 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
434
Madan Jampani2ff05592014-10-10 15:42:47 -0700435 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700436 for (Entry<ProviderId, Timestamped<LinkDescription>>
437 e : linkDescriptions.entrySet()) {
438
439 if (!e.getKey().isAncillary()) {
440 return e.getValue();
441 }
442 }
443 }
444 return null;
445 }
446
447
448 // TODO: consider slicing out as Timestamp utils
449 /**
450 * Checks is timestamp is more recent than timestamped object.
451 *
452 * @param timestamp to check if this is more recent then other
453 * @param timestamped object to be tested against
454 * @return true if {@code timestamp} is more recent than {@code timestamped}
455 * or {@code timestamped is null}
456 */
457 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
458 checkNotNull(timestamp);
459 if (timestamped == null) {
460 return true;
461 }
462 return timestamp.compareTo(timestamped.timestamp()) > 0;
463 }
464
465 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
466 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
467 = getOrCreateLinkDescriptions(key);
468
469 synchronized (linkDescriptions) {
470 if (linkDescriptions.isEmpty()) {
471 // never seen such link before. keeping timestamp for record
472 removedLinks.put(key, timestamp);
473 return null;
474 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700475 // accept removal request if given timestamp is newer than
476 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700477 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
478 if (!isMoreRecent(timestamp, prim)) {
479 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700480 return null;
481 }
482 removedLinks.put(key, timestamp);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800483 Link link = links.remove(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700484 linkDescriptions.clear();
485 if (link != null) {
486 srcLinks.remove(link.src().deviceId(), key);
487 dstLinks.remove(link.dst().deviceId(), key);
488 return new LinkEvent(LINK_REMOVED, link);
489 }
490 return null;
491 }
492 }
493
494 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
495 return synchronizedSetMultimap(HashMultimap.<K, V>create());
496 }
497
498 /**
499 * @return primary ProviderID, or randomly chosen one if none exists
500 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700501 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700502 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700503
504 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700505 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700506 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700507 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700508 return e.getKey();
509 } else if (fallBackPrimary == null) {
510 // pick randomly as a fallback in case there is no primary
511 fallBackPrimary = e.getKey();
512 }
513 }
514 return fallBackPrimary;
515 }
516
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700517 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700518 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700519 ProviderId baseProviderId = pickBaseProviderId(descs);
520 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700521
522 ConnectPoint src = base.value().src();
523 ConnectPoint dst = base.value().dst();
524 Type type = base.value().type();
525 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
526 annotations = merge(annotations, base.value().annotations());
527
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700528 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700529 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700530 continue;
531 }
532
533 // TODO: should keep track of Description timestamp
534 // and only merge conflicting keys when timestamp is newer
535 // Currently assuming there will never be a key conflict between
536 // providers
537
538 // annotation merging. not so efficient, should revisit later
539 annotations = merge(annotations, e.getValue().value().annotations());
540 }
541
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800542 boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800543 return new DefaultLink(baseProviderId, src, dst, type, ACTIVE, isDurable, annotations);
Madan Jampani2ff05592014-10-10 15:42:47 -0700544 }
545
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700546 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700547 Map<ProviderId, Timestamped<LinkDescription>> r;
548 r = linkDescs.get(key);
549 if (r != null) {
550 return r;
551 }
552 r = new HashMap<>();
553 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
554 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
555 if (concurrentlyAdded != null) {
556 return concurrentlyAdded;
557 } else {
558 return r;
559 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700560 }
561
562 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800563
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700564 /**
565 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800566 *
567 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700568 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700569 private Function<LinkKey, Link> lookupLink() {
570 return lookupLink;
571 }
572
573 private final class LookupLink implements Function<LinkKey, Link> {
574 @Override
575 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700576 if (input == null) {
577 return null;
578 } else {
579 return links.get(input);
580 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700581 }
582 }
583
Madan Jampani2ff05592014-10-10 15:42:47 -0700584 private void notifyDelegateIfNotNull(LinkEvent event) {
585 if (event != null) {
586 notifyDelegate(event);
587 }
588 }
589
Madan Jampani2ff05592014-10-10 15:42:47 -0700590 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
591 ClusterMessage message = new ClusterMessage(
592 clusterService.getLocalNode().id(),
593 subject,
594 SERIALIZER.encode(event));
595 clusterCommunicator.broadcast(message);
596 }
597
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700598 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
599 ClusterMessage message = new ClusterMessage(
600 clusterService.getLocalNode().id(),
601 subject,
602 SERIALIZER.encode(event));
603 clusterCommunicator.unicast(message, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700604 }
605
Madan Jampani2ff05592014-10-10 15:42:47 -0700606 private void notifyPeers(InternalLinkEvent event) throws IOException {
607 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
608 }
609
610 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
611 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
612 }
613
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700614 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700615 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700616 try {
617 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
618 } catch (IOException e) {
619 log.debug("Failed to notify peer {} with message {}", peer, event);
620 }
Madan Jampania97e8202014-10-10 17:01:33 -0700621 }
622
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700623 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700624 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700625 try {
626 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
627 } catch (IOException e) {
628 log.debug("Failed to notify peer {} with message {}", peer, event);
629 }
Madan Jampania97e8202014-10-10 17:01:33 -0700630 }
631
632 private final class SendAdvertisementTask implements Runnable {
633
634 @Override
635 public void run() {
636 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800637 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700638 return;
639 }
640
641 try {
642 final NodeId self = clusterService.getLocalNode().id();
643 Set<ControllerNode> nodes = clusterService.getNodes();
644
645 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
646 .transform(toNodeId())
647 .toList();
648
649 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800650 log.trace("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700651 return;
652 }
653
654 NodeId peer;
655 do {
656 int idx = RandomUtils.nextInt(0, nodeIds.size());
657 peer = nodeIds.get(idx);
658 } while (peer.equals(self));
659
660 LinkAntiEntropyAdvertisement ad = createAdvertisement();
661
662 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800663 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700664 return;
665 }
666
667 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700668 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
669 } catch (IOException e) {
670 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700671 return;
672 }
673 } catch (Exception e) {
674 // catch all Exception to avoid Scheduled task being suppressed.
675 log.error("Exception thrown while sending advertisement", e);
676 }
677 }
678 }
679
680 private LinkAntiEntropyAdvertisement createAdvertisement() {
681 final NodeId self = clusterService.getLocalNode().id();
682
683 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
684 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
685
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800686 linkDescs.forEach((linkKey, linkDesc) -> {
Madan Jampania97e8202014-10-10 17:01:33 -0700687 synchronized (linkDesc) {
688 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
689 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
690 }
691 }
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800692 });
Madan Jampania97e8202014-10-10 17:01:33 -0700693
694 linkTombstones.putAll(removedLinks);
695
696 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
697 }
698
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700699 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700700
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700701 final NodeId sender = ad.sender();
702 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700703
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700704 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
705 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700706
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700707 final LinkKey key = l.getKey();
708 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
709 synchronized (link) {
710 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700711
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700712 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
713 final ProviderId providerId = p.getKey();
714 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700715
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700716 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
717 // remote
718 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
719 if (remoteTimestamp == null) {
720 remoteTimestamp = ad.linkTombstones().get(key);
721 }
722 if (remoteTimestamp == null ||
723 pDesc.isNewer(remoteTimestamp)) {
724 // I have more recent link description. update peer.
725 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
726 } else {
727 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
728 if (remoteLive != null &&
729 remoteLive.compareTo(pDesc.timestamp()) > 0) {
730 // I have something outdated
731 localOutdated = true;
732 }
733 }
734
735 // search local latest along the way
736 if (localLatest == null ||
737 pDesc.isNewer(localLatest)) {
738 localLatest = pDesc.timestamp();
739 }
740 }
741 // Tests if remote remove is more recent then local latest.
742 final Timestamp remoteRemove = ad.linkTombstones().get(key);
743 if (remoteRemove != null) {
744 if (localLatest != null &&
745 localLatest.compareTo(remoteRemove) < 0) {
746 // remote remove is more recent
747 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
748 }
749 }
Madan Jampania97e8202014-10-10 17:01:33 -0700750 }
751 }
752
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700753 // populate remove info if not known locally
754 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
755 final LinkKey key = remoteRm.getKey();
756 final Timestamp remoteRemove = remoteRm.getValue();
757 // relying on removeLinkInternal to ignore stale info
758 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
759 }
Madan Jampania97e8202014-10-10 17:01:33 -0700760
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700761 if (localOutdated) {
762 // send back advertisement to speed up convergence
763 try {
764 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
765 createAdvertisement());
766 } catch (IOException e) {
767 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700768 }
769 }
770 }
771
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800772 private final class InternalLinkEventListener
773 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700774 @Override
775 public void handle(ClusterMessage message) {
776
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700777 log.trace("Received link event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700778 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
779
780 ProviderId providerId = event.providerId();
781 Timestamped<LinkDescription> linkDescription = event.linkDescription();
782
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800783 executor.submit(new Runnable() {
784
785 @Override
786 public void run() {
787 try {
788 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
789 } catch (Exception e) {
790 log.warn("Exception thrown handling link event", e);
791 }
792 }
793 });
Madan Jampani2ff05592014-10-10 15:42:47 -0700794 }
795 }
796
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800797 private final class InternalLinkRemovedEventListener
798 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700799 @Override
800 public void handle(ClusterMessage message) {
801
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700802 log.trace("Received link removed event from peer: {}", message.sender());
Madan Jampani2ff05592014-10-10 15:42:47 -0700803 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
804
805 LinkKey linkKey = event.linkKey();
806 Timestamp timestamp = event.timestamp();
807
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800808 executor.submit(new Runnable() {
809
810 @Override
811 public void run() {
812 try {
813 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
814 } catch (Exception e) {
815 log.warn("Exception thrown handling link removed", e);
816 }
817 }
818 });
Madan Jampani2ff05592014-10-10 15:42:47 -0700819 }
820 }
Madan Jampania97e8202014-10-10 17:01:33 -0700821
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800822 private final class InternalLinkAntiEntropyAdvertisementListener
823 implements ClusterMessageHandler {
Madan Jampania97e8202014-10-10 17:01:33 -0700824
825 @Override
826 public void handle(ClusterMessage message) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800827 log.trace("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700828 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800829 backgroundExecutors.submit(new Runnable() {
830
831 @Override
832 public void run() {
833 try {
834 handleAntiEntropyAdvertisement(advertisement);
835 } catch (Exception e) {
836 log.warn("Exception thrown while handling Link advertisements", e);
837 throw e;
838 }
839 }
840 });
Madan Jampania97e8202014-10-10 17:01:33 -0700841 }
842 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700843}