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