Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.link.impl; |
| 17 | |
| 18 | import com.google.common.base.Function; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 19 | import com.google.common.collect.FluentIterable; |
| 20 | import com.google.common.collect.HashMultimap; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.SetMultimap; |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 24 | |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 25 | import org.apache.commons.lang3.RandomUtils; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
| 32 | import org.onlab.onos.cluster.ClusterService; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 33 | import org.onlab.onos.cluster.ControllerNode; |
| 34 | import org.onlab.onos.cluster.NodeId; |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 35 | import org.onlab.onos.net.AnnotationKeys; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.AnnotationsUtil; |
| 37 | import org.onlab.onos.net.ConnectPoint; |
| 38 | import org.onlab.onos.net.DefaultAnnotations; |
| 39 | import org.onlab.onos.net.DefaultLink; |
| 40 | import org.onlab.onos.net.DeviceId; |
| 41 | import org.onlab.onos.net.Link; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.Link.Type; |
| 43 | import org.onlab.onos.net.LinkKey; |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 44 | import org.onlab.onos.net.SparseAnnotations; |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.device.DeviceClockService; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 46 | import org.onlab.onos.net.link.DefaultLinkDescription; |
| 47 | import org.onlab.onos.net.link.LinkDescription; |
| 48 | import org.onlab.onos.net.link.LinkEvent; |
| 49 | import org.onlab.onos.net.link.LinkStore; |
| 50 | import org.onlab.onos.net.link.LinkStoreDelegate; |
| 51 | import org.onlab.onos.net.provider.ProviderId; |
| 52 | import org.onlab.onos.store.AbstractStore; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 53 | import org.onlab.onos.store.Timestamp; |
| 54 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 55 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
| 56 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
| 57 | import org.onlab.onos.store.cluster.messaging.MessageSubject; |
Yuta HIGUCHI | eecee55 | 2014-10-16 14:09:01 -0700 | [diff] [blame] | 58 | import org.onlab.onos.store.impl.Timestamped; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 59 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 60a190b | 2014-11-07 16:24:47 -0800 | [diff] [blame] | 60 | import org.onlab.onos.store.serializers.impl.DistributedStoreSerializers; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 61 | import org.onlab.util.KryoNamespace; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | |
| 64 | import java.io.IOException; |
| 65 | import java.util.Collections; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 66 | import java.util.HashMap; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 67 | import java.util.HashSet; |
| 68 | import java.util.Map; |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 69 | import java.util.Map.Entry; |
Thomas Vachuska | 29a6a78 | 2014-11-10 21:31:41 -0800 | [diff] [blame] | 70 | import java.util.Objects; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 71 | import java.util.Set; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 72 | import java.util.concurrent.ConcurrentHashMap; |
| 73 | import java.util.concurrent.ConcurrentMap; |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 74 | import java.util.concurrent.ExecutorService; |
| 75 | import java.util.concurrent.Executors; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 76 | import java.util.concurrent.ScheduledExecutorService; |
| 77 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 78 | |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 79 | import static com.google.common.base.Preconditions.checkNotNull; |
| 80 | import static com.google.common.base.Predicates.notNull; |
| 81 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 82 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 83 | import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 84 | import static org.onlab.onos.net.DefaultAnnotations.merge; |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 85 | import static org.onlab.onos.net.DefaultAnnotations.union; |
| 86 | import static org.onlab.onos.net.Link.State.ACTIVE; |
| 87 | import static org.onlab.onos.net.Link.State.INACTIVE; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 88 | import static org.onlab.onos.net.Link.Type.DIRECT; |
| 89 | import static org.onlab.onos.net.Link.Type.INDIRECT; |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 90 | import static org.onlab.onos.net.LinkKey.linkKey; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 91 | import static org.onlab.onos.net.link.LinkEvent.Type.*; |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 92 | import static org.onlab.onos.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT; |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 93 | import static org.onlab.util.Tools.minPriority; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 94 | import static org.onlab.util.Tools.namedThreads; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 95 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 96 | |
| 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 |
| 103 | public class GossipLinkStore |
| 104 | extends AbstractStore<LinkEvent, LinkStoreDelegate> |
| 105 | implements LinkStore { |
| 106 | |
| 107 | private final Logger log = getLogger(getClass()); |
| 108 | |
| 109 | // Link inventory |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 110 | private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs = |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 111 | 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 HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 124 | protected DeviceClockService deviceClockService; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 127 | protected ClusterCommunicationService clusterCommunicator; |
| 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 130 | protected ClusterService clusterService; |
| 131 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 132 | protected static final KryoSerializer SERIALIZER = new KryoSerializer() { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 133 | @Override |
| 134 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 135 | serializerPool = KryoNamespace.newBuilder() |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 136 | .register(DistributedStoreSerializers.STORE_COMMON) |
| 137 | .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 138 | .register(InternalLinkEvent.class) |
| 139 | .register(InternalLinkRemovedEvent.class) |
Madan Jampani | d2054d4 | 2014-10-10 17:27:06 -0700 | [diff] [blame] | 140 | .register(LinkAntiEntropyAdvertisement.class) |
| 141 | .register(LinkFragmentId.class) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 142 | .build(); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 143 | } |
| 144 | }; |
| 145 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 146 | private ExecutorService executor; |
| 147 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 148 | private ScheduledExecutorService backgroundExecutors; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 149 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 150 | @Activate |
| 151 | public void activate() { |
| 152 | |
| 153 | clusterCommunicator.addSubscriber( |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 154 | GossipLinkStoreMessageSubjects.LINK_UPDATE, |
| 155 | new InternalLinkEventListener()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 156 | clusterCommunicator.addSubscriber( |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 157 | GossipLinkStoreMessageSubjects.LINK_REMOVED, |
| 158 | new InternalLinkRemovedEventListener()); |
| 159 | clusterCommunicator.addSubscriber( |
| 160 | GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT, |
| 161 | new InternalLinkAntiEntropyAdvertisementListener()); |
| 162 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 163 | executor = Executors.newCachedThreadPool(namedThreads("link-fg-%d")); |
| 164 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 165 | backgroundExecutors = |
| 166 | newSingleThreadScheduledExecutor(minPriority(namedThreads("link-bg-%d"))); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 167 | |
| 168 | // TODO: Make these configurable |
| 169 | long initialDelaySec = 5; |
| 170 | long periodSec = 5; |
| 171 | // start anti-entropy thread |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 172 | backgroundExecutors.scheduleAtFixedRate(new SendAdvertisementTask(), |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 173 | initialDelaySec, periodSec, TimeUnit.SECONDS); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 174 | |
| 175 | log.info("Started"); |
| 176 | } |
| 177 | |
| 178 | @Deactivate |
| 179 | public void deactivate() { |
Madan Jampani | 3ffbb27 | 2014-10-13 11:19:37 -0700 | [diff] [blame] | 180 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 181 | executor.shutdownNow(); |
| 182 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 183 | backgroundExecutors.shutdownNow(); |
Madan Jampani | 3ffbb27 | 2014-10-13 11:19:37 -0700 | [diff] [blame] | 184 | try { |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 185 | if (!backgroundExecutors.awaitTermination(5, TimeUnit.SECONDS)) { |
Madan Jampani | 3ffbb27 | 2014-10-13 11:19:37 -0700 | [diff] [blame] | 186 | log.error("Timeout during executor shutdown"); |
| 187 | } |
| 188 | } catch (InterruptedException e) { |
| 189 | log.error("Error during executor shutdown", e); |
| 190 | } |
| 191 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 192 | 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 HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 233 | return links.get(linkKey(src, dst)); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 234 | } |
| 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 HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 263 | Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 264 | |
| 265 | final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp); |
| 266 | |
Yuta HIGUCHI | 990aecc | 2014-10-13 22:21:42 -0700 | [diff] [blame] | 267 | LinkKey key = linkKey(linkDescription.src(), linkDescription.dst()); |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 268 | final LinkEvent event; |
| 269 | final Timestamped<LinkDescription> mergedDesc; |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 270 | Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key); |
| 271 | synchronized (map) { |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 272 | event = createOrUpdateLinkInternal(providerId, deltaDesc); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 273 | mergedDesc = map.get(providerId); |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 274 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 275 | |
| 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 HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 281 | notifyPeers(new InternalLinkEvent(providerId, mergedDesc)); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 282 | } catch (IOException e) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 283 | log.debug("Failed to notify peers of a link update topology event from providerId: " |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 284 | + "{} between src: {} and dst: {}", |
| 285 | providerId, linkDescription.src(), linkDescription.dst()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | return event; |
| 289 | } |
| 290 | |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 291 | @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 Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 311 | private LinkEvent createOrUpdateLinkInternal( |
| 312 | ProviderId providerId, |
| 313 | Timestamped<LinkDescription> linkDescription) { |
| 314 | |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 315 | final LinkKey key = linkKey(linkDescription.value().src(), |
| 316 | linkDescription.value().dst()); |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 317 | Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 318 | |
| 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 HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 327 | log.trace("Link {} was already removed ignoring.", key); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 328 | 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 HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 345 | Map<ProviderId, Timestamped<LinkDescription>> descs, |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 346 | ProviderId providerId, |
| 347 | Timestamped<LinkDescription> linkDescription) { |
| 348 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 349 | // merge existing annotations |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 350 | Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 351 | if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) { |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 352 | log.trace("local info is more up-to-date, ignoring {}.", linkDescription); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 353 | return null; |
| 354 | } |
| 355 | Timestamped<LinkDescription> newLinkDescription = linkDescription; |
| 356 | if (existingLinkDescription != null) { |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 357 | // 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 Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 364 | SparseAnnotations merged = union(existingLinkDescription.value().annotations(), |
| 365 | linkDescription.value().annotations()); |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 366 | newLinkDescription = new Timestamped<>( |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 367 | new DefaultLinkDescription( |
| 368 | linkDescription.value().src(), |
| 369 | linkDescription.value().dst(), |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 370 | newType, merged), |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 371 | linkDescription.timestamp()); |
| 372 | } |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 373 | return descs.put(providerId, newLinkDescription); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 374 | } |
| 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 Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 379 | 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 HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 388 | // Note: INDIRECT -> DIRECT transition only |
| 389 | // so that BDDP discovered Link will not overwrite LDDP Link |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 390 | if (oldLink.state() != newLink.state() || |
| 391 | (oldLink.type() == INDIRECT && newLink.type() == DIRECT) || |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 392 | !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 HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 405 | final LinkKey key = linkKey(src, dst); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 406 | |
| 407 | DeviceId dstDeviceId = dst.deviceId(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 408 | Timestamp timestamp = null; |
| 409 | try { |
| 410 | timestamp = deviceClockService.getTimestamp(dstDeviceId); |
| 411 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | d6a0ac3 | 2014-11-04 09:26:31 -0800 | [diff] [blame] | 412 | log.warn("Failed to remove link {}, was not the master", key); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 413 | //there are times when this is called before mastership |
| 414 | // handoff correctly completes. |
| 415 | return null; |
| 416 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 417 | |
| 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 HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 433 | private static Timestamped<LinkDescription> getPrimaryDescription( |
| 434 | Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) { |
| 435 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 436 | synchronized (linkDescriptions) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 437 | 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 Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 476 | // accept removal request if given timestamp is newer than |
| 477 | // the latest Timestamp from Primary provider |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 478 | Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions); |
| 479 | if (!isMoreRecent(timestamp, prim)) { |
| 480 | // outdated remove request, ignore |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 481 | return null; |
| 482 | } |
| 483 | removedLinks.put(key, timestamp); |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 484 | Link link = links.remove(key); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 485 | 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 HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 502 | private static ProviderId pickBaseProviderId( |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 503 | Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 504 | |
| 505 | ProviderId fallBackPrimary = null; |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 506 | for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 507 | if (!e.getKey().isAncillary()) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 508 | // found primary |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 509 | 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 HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 518 | // Guarded by linkDescs value (=locking each Link) |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 519 | private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 520 | ProviderId baseProviderId = pickBaseProviderId(descs); |
| 521 | Timestamped<LinkDescription> base = descs.get(baseProviderId); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 522 | |
| 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 HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 529 | for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 530 | if (baseProviderId.equals(e.getKey())) { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 531 | 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 Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 543 | boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true"); |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 544 | return new DefaultLink(baseProviderId, src, dst, type, ACTIVE, isDurable, annotations); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 547 | private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) { |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 548 | 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 Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | private final Function<LinkKey, Link> lookupLink = new LookupLink(); |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 564 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 565 | /** |
| 566 | * Returns a Function to lookup Link instance using LinkKey from cache. |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 567 | * |
| 568 | * @return lookup link function |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 569 | */ |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 570 | 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 HIGUCHI | 023295a | 2014-10-15 23:29:46 -0700 | [diff] [blame] | 577 | if (input == null) { |
| 578 | return null; |
| 579 | } else { |
| 580 | return links.get(input); |
| 581 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 585 | private void notifyDelegateIfNotNull(LinkEvent event) { |
| 586 | if (event != null) { |
| 587 | notifyDelegate(event); |
| 588 | } |
| 589 | } |
| 590 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 591 | 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 HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 599 | 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 Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 607 | 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 HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 615 | // notify peer, silently ignoring error |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 616 | private void notifyPeer(NodeId peer, InternalLinkEvent event) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 617 | 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 Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 624 | // notify peer, silently ignoring error |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 625 | private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 626 | 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 Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | private final class SendAdvertisementTask implements Runnable { |
| 634 | |
| 635 | @Override |
| 636 | public void run() { |
| 637 | if (Thread.currentThread().isInterrupted()) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 638 | log.debug("Interrupted, quitting"); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 639 | 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 HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 651 | log.trace("No other peers in the cluster."); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 652 | 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 HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 664 | log.debug("Interrupted, quitting"); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 665 | return; |
| 666 | } |
| 667 | |
| 668 | try { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 669 | unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad); |
| 670 | } catch (IOException e) { |
| 671 | log.debug("Failed to send anti-entropy advertisement to {}", peer); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 672 | 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 HIGUCHI | b6cfac3 | 2014-11-25 13:37:27 -0800 | [diff] [blame] | 687 | linkDescs.forEach((linkKey, linkDesc) -> { |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 688 | 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 HIGUCHI | b6cfac3 | 2014-11-25 13:37:27 -0800 | [diff] [blame] | 693 | }); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 694 | |
| 695 | linkTombstones.putAll(removedLinks); |
| 696 | |
| 697 | return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones); |
| 698 | } |
| 699 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 700 | private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) { |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 701 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 702 | final NodeId sender = ad.sender(); |
| 703 | boolean localOutdated = false; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 704 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 705 | for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> |
| 706 | l : linkDescs.entrySet()) { |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 707 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 708 | final LinkKey key = l.getKey(); |
| 709 | final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue(); |
| 710 | synchronized (link) { |
| 711 | Timestamp localLatest = removedLinks.get(key); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 712 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 713 | for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) { |
| 714 | final ProviderId providerId = p.getKey(); |
| 715 | final Timestamped<LinkDescription> pDesc = p.getValue(); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 716 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 717 | 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 Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 754 | // 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 Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 761 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 762 | 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 Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 773 | private final class InternalLinkEventListener |
| 774 | implements ClusterMessageHandler { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 775 | @Override |
| 776 | public void handle(ClusterMessage message) { |
| 777 | |
Yuta HIGUCHI | c01d2aa | 2014-10-19 01:19:34 -0700 | [diff] [blame] | 778 | log.trace("Received link event from peer: {}", message.sender()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 779 | InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload()); |
| 780 | |
| 781 | ProviderId providerId = event.providerId(); |
| 782 | Timestamped<LinkDescription> linkDescription = event.linkDescription(); |
| 783 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 784 | 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 Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 798 | private final class InternalLinkRemovedEventListener |
| 799 | implements ClusterMessageHandler { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 800 | @Override |
| 801 | public void handle(ClusterMessage message) { |
| 802 | |
Yuta HIGUCHI | c01d2aa | 2014-10-19 01:19:34 -0700 | [diff] [blame] | 803 | log.trace("Received link removed event from peer: {}", message.sender()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 804 | InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload()); |
| 805 | |
| 806 | LinkKey linkKey = event.linkKey(); |
| 807 | Timestamp timestamp = event.timestamp(); |
| 808 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 809 | 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 Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 822 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 823 | private final class InternalLinkAntiEntropyAdvertisementListener |
| 824 | implements ClusterMessageHandler { |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 825 | |
| 826 | @Override |
| 827 | public void handle(ClusterMessage message) { |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 828 | log.trace("Received Link Anti-Entropy advertisement from peer: {}", message.sender()); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 829 | LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 830 | 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 Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 844 | } |