Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.link.impl; |
| 2 | |
| 3 | import com.google.common.base.Function; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 4 | import com.google.common.collect.FluentIterable; |
| 5 | import com.google.common.collect.HashMultimap; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 6 | import com.google.common.collect.ImmutableList; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 7 | import com.google.common.collect.Maps; |
| 8 | import com.google.common.collect.SetMultimap; |
| 9 | |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 10 | import org.apache.commons.lang3.RandomUtils; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 11 | import org.apache.felix.scr.annotations.Activate; |
| 12 | import org.apache.felix.scr.annotations.Component; |
| 13 | import org.apache.felix.scr.annotations.Deactivate; |
| 14 | import org.apache.felix.scr.annotations.Reference; |
| 15 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 16 | import org.apache.felix.scr.annotations.Service; |
| 17 | import org.onlab.onos.cluster.ClusterService; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 18 | import org.onlab.onos.cluster.ControllerNode; |
| 19 | import org.onlab.onos.cluster.NodeId; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.AnnotationsUtil; |
| 21 | import org.onlab.onos.net.ConnectPoint; |
| 22 | import org.onlab.onos.net.DefaultAnnotations; |
| 23 | import org.onlab.onos.net.DefaultLink; |
| 24 | import org.onlab.onos.net.DeviceId; |
| 25 | import org.onlab.onos.net.Link; |
| 26 | import org.onlab.onos.net.SparseAnnotations; |
| 27 | import org.onlab.onos.net.Link.Type; |
| 28 | import org.onlab.onos.net.LinkKey; |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.device.DeviceClockService; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.link.DefaultLinkDescription; |
| 31 | import org.onlab.onos.net.link.LinkDescription; |
| 32 | import org.onlab.onos.net.link.LinkEvent; |
| 33 | import org.onlab.onos.net.link.LinkStore; |
| 34 | import org.onlab.onos.net.link.LinkStoreDelegate; |
| 35 | import org.onlab.onos.net.provider.ProviderId; |
| 36 | import org.onlab.onos.store.AbstractStore; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 37 | import org.onlab.onos.store.Timestamp; |
| 38 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 39 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
| 40 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
| 41 | import org.onlab.onos.store.cluster.messaging.MessageSubject; |
Yuta HIGUCHI | eecee55 | 2014-10-16 14:09:01 -0700 | [diff] [blame] | 42 | import org.onlab.onos.store.impl.Timestamped; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 43 | import org.onlab.onos.store.serializers.DistributedStoreSerializers; |
| 44 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 45 | import org.onlab.util.KryoNamespace; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | import java.io.IOException; |
| 49 | import java.util.Collections; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 50 | import java.util.HashMap; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 51 | import java.util.HashSet; |
| 52 | import java.util.Map; |
| 53 | import java.util.Set; |
| 54 | import java.util.Map.Entry; |
| 55 | import java.util.concurrent.ConcurrentHashMap; |
| 56 | import java.util.concurrent.ConcurrentMap; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 57 | import java.util.concurrent.ScheduledExecutorService; |
| 58 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 59 | |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 60 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 61 | import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 62 | import static org.onlab.onos.net.DefaultAnnotations.union; |
| 63 | import static org.onlab.onos.net.DefaultAnnotations.merge; |
| 64 | import static org.onlab.onos.net.Link.Type.DIRECT; |
| 65 | import static org.onlab.onos.net.Link.Type.INDIRECT; |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 66 | import static org.onlab.onos.net.LinkKey.linkKey; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 67 | import static org.onlab.onos.net.link.LinkEvent.Type.*; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 68 | import static org.onlab.util.Tools.namedThreads; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 69 | import static org.slf4j.LoggerFactory.getLogger; |
| 70 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 71 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 72 | import static com.google.common.base.Predicates.notNull; |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 73 | import static org.onlab.onos.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Manages inventory of infrastructure links in distributed data store |
| 77 | * that uses optimistic replication and gossip based techniques. |
| 78 | */ |
| 79 | @Component(immediate = true) |
| 80 | @Service |
| 81 | public class GossipLinkStore |
| 82 | extends AbstractStore<LinkEvent, LinkStoreDelegate> |
| 83 | implements LinkStore { |
| 84 | |
| 85 | private final Logger log = getLogger(getClass()); |
| 86 | |
| 87 | // Link inventory |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 88 | private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs = |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 89 | new ConcurrentHashMap<>(); |
| 90 | |
| 91 | // Link instance cache |
| 92 | private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>(); |
| 93 | |
| 94 | // Egress and ingress link sets |
| 95 | private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap(); |
| 96 | private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap(); |
| 97 | |
| 98 | // Remove links |
| 99 | private final Map<LinkKey, Timestamp> removedLinks = Maps.newHashMap(); |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 102 | protected DeviceClockService deviceClockService; |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected ClusterCommunicationService clusterCommunicator; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected ClusterService clusterService; |
| 109 | |
| 110 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 111 | @Override |
| 112 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 113 | serializerPool = KryoNamespace.newBuilder() |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 114 | .register(DistributedStoreSerializers.COMMON) |
| 115 | .register(InternalLinkEvent.class) |
| 116 | .register(InternalLinkRemovedEvent.class) |
Madan Jampani | d2054d4 | 2014-10-10 17:27:06 -0700 | [diff] [blame] | 117 | .register(LinkAntiEntropyAdvertisement.class) |
| 118 | .register(LinkFragmentId.class) |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 119 | .build() |
| 120 | .populate(1); |
| 121 | } |
| 122 | }; |
| 123 | |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 124 | private ScheduledExecutorService executor; |
| 125 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 126 | @Activate |
| 127 | public void activate() { |
| 128 | |
| 129 | clusterCommunicator.addSubscriber( |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 130 | GossipLinkStoreMessageSubjects.LINK_UPDATE, |
| 131 | new InternalLinkEventListener()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 132 | clusterCommunicator.addSubscriber( |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 133 | GossipLinkStoreMessageSubjects.LINK_REMOVED, |
| 134 | new InternalLinkRemovedEventListener()); |
| 135 | clusterCommunicator.addSubscriber( |
| 136 | GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT, |
| 137 | new InternalLinkAntiEntropyAdvertisementListener()); |
| 138 | |
| 139 | executor = |
| 140 | newSingleThreadScheduledExecutor(namedThreads("link-anti-entropy-%d")); |
| 141 | |
| 142 | // TODO: Make these configurable |
| 143 | long initialDelaySec = 5; |
| 144 | long periodSec = 5; |
| 145 | // start anti-entropy thread |
| 146 | executor.scheduleAtFixedRate(new SendAdvertisementTask(), |
| 147 | initialDelaySec, periodSec, TimeUnit.SECONDS); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 148 | |
| 149 | log.info("Started"); |
| 150 | } |
| 151 | |
| 152 | @Deactivate |
| 153 | public void deactivate() { |
Madan Jampani | 3ffbb27 | 2014-10-13 11:19:37 -0700 | [diff] [blame] | 154 | |
| 155 | executor.shutdownNow(); |
| 156 | try { |
| 157 | if (!executor.awaitTermination(5, TimeUnit.SECONDS)) { |
| 158 | log.error("Timeout during executor shutdown"); |
| 159 | } |
| 160 | } catch (InterruptedException e) { |
| 161 | log.error("Error during executor shutdown", e); |
| 162 | } |
| 163 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 164 | linkDescs.clear(); |
| 165 | links.clear(); |
| 166 | srcLinks.clear(); |
| 167 | dstLinks.clear(); |
| 168 | log.info("Stopped"); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public int getLinkCount() { |
| 173 | return links.size(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public Iterable<Link> getLinks() { |
| 178 | return Collections.unmodifiableCollection(links.values()); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public Set<Link> getDeviceEgressLinks(DeviceId deviceId) { |
| 183 | // lock for iteration |
| 184 | synchronized (srcLinks) { |
| 185 | return FluentIterable.from(srcLinks.get(deviceId)) |
| 186 | .transform(lookupLink()) |
| 187 | .filter(notNull()) |
| 188 | .toSet(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { |
| 194 | // lock for iteration |
| 195 | synchronized (dstLinks) { |
| 196 | return FluentIterable.from(dstLinks.get(deviceId)) |
| 197 | .transform(lookupLink()) |
| 198 | .filter(notNull()) |
| 199 | .toSet(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Link getLink(ConnectPoint src, ConnectPoint dst) { |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 205 | return links.get(linkKey(src, dst)); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public Set<Link> getEgressLinks(ConnectPoint src) { |
| 210 | Set<Link> egress = new HashSet<>(); |
| 211 | for (LinkKey linkKey : srcLinks.get(src.deviceId())) { |
| 212 | if (linkKey.src().equals(src)) { |
| 213 | egress.add(links.get(linkKey)); |
| 214 | } |
| 215 | } |
| 216 | return egress; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public Set<Link> getIngressLinks(ConnectPoint dst) { |
| 221 | Set<Link> ingress = new HashSet<>(); |
| 222 | for (LinkKey linkKey : dstLinks.get(dst.deviceId())) { |
| 223 | if (linkKey.dst().equals(dst)) { |
| 224 | ingress.add(links.get(linkKey)); |
| 225 | } |
| 226 | } |
| 227 | return ingress; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public LinkEvent createOrUpdateLink(ProviderId providerId, |
| 232 | LinkDescription linkDescription) { |
| 233 | |
| 234 | DeviceId dstDeviceId = linkDescription.dst().deviceId(); |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 235 | Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 236 | |
| 237 | final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp); |
| 238 | |
Yuta HIGUCHI | 990aecc | 2014-10-13 22:21:42 -0700 | [diff] [blame] | 239 | LinkKey key = linkKey(linkDescription.src(), linkDescription.dst()); |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 240 | final LinkEvent event; |
| 241 | final Timestamped<LinkDescription> mergedDesc; |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame^] | 242 | Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key); |
| 243 | synchronized (map) { |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 244 | event = createOrUpdateLinkInternal(providerId, deltaDesc); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame^] | 245 | mergedDesc = map.get(providerId); |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 246 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 247 | |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame^] | 248 | |
| 249 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 250 | if (event != null) { |
| 251 | log.info("Notifying peers of a link update topology event from providerId: " |
| 252 | + "{} between src: {} and dst: {}", |
| 253 | providerId, linkDescription.src(), linkDescription.dst()); |
| 254 | try { |
Yuta HIGUCHI | 92cd51e | 2014-10-13 10:51:45 -0700 | [diff] [blame] | 255 | notifyPeers(new InternalLinkEvent(providerId, mergedDesc)); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 256 | } catch (IOException e) { |
| 257 | log.info("Failed to notify peers of a link update topology event from providerId: " |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame^] | 258 | + "{} between src: {} and dst: {}", |
| 259 | providerId, linkDescription.src(), linkDescription.dst()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | return event; |
| 263 | } |
| 264 | |
| 265 | private LinkEvent createOrUpdateLinkInternal( |
| 266 | ProviderId providerId, |
| 267 | Timestamped<LinkDescription> linkDescription) { |
| 268 | |
Yuta HIGUCHI | 990aecc | 2014-10-13 22:21:42 -0700 | [diff] [blame] | 269 | LinkKey key = linkKey(linkDescription.value().src(), |
| 270 | linkDescription.value().dst()); |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 271 | Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 272 | |
| 273 | synchronized (descs) { |
| 274 | // if the link was previously removed, we should proceed if and |
| 275 | // only if this request is more recent. |
| 276 | Timestamp linkRemovedTimestamp = removedLinks.get(key); |
| 277 | if (linkRemovedTimestamp != null) { |
| 278 | if (linkDescription.isNewer(linkRemovedTimestamp)) { |
| 279 | removedLinks.remove(key); |
| 280 | } else { |
| 281 | return null; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | final Link oldLink = links.get(key); |
| 286 | // update description |
| 287 | createOrUpdateLinkDescription(descs, providerId, linkDescription); |
| 288 | final Link newLink = composeLink(descs); |
| 289 | if (oldLink == null) { |
| 290 | return createLink(key, newLink); |
| 291 | } |
| 292 | return updateLink(key, oldLink, newLink); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Guarded by linkDescs value (=locking each Link) |
| 297 | private Timestamped<LinkDescription> createOrUpdateLinkDescription( |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 298 | Map<ProviderId, Timestamped<LinkDescription>> descs, |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 299 | ProviderId providerId, |
| 300 | Timestamped<LinkDescription> linkDescription) { |
| 301 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 302 | // merge existing annotations |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 303 | Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 304 | if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) { |
| 305 | return null; |
| 306 | } |
| 307 | Timestamped<LinkDescription> newLinkDescription = linkDescription; |
| 308 | if (existingLinkDescription != null) { |
| 309 | SparseAnnotations merged = union(existingLinkDescription.value().annotations(), |
| 310 | linkDescription.value().annotations()); |
| 311 | newLinkDescription = new Timestamped<LinkDescription>( |
| 312 | new DefaultLinkDescription( |
| 313 | linkDescription.value().src(), |
| 314 | linkDescription.value().dst(), |
| 315 | linkDescription.value().type(), merged), |
| 316 | linkDescription.timestamp()); |
| 317 | } |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 318 | return descs.put(providerId, newLinkDescription); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // Creates and stores the link and returns the appropriate event. |
| 322 | // Guarded by linkDescs value (=locking each Link) |
| 323 | private LinkEvent createLink(LinkKey key, Link newLink) { |
| 324 | |
| 325 | if (newLink.providerId().isAncillary()) { |
| 326 | // TODO: revisit ancillary only Link handling |
| 327 | |
| 328 | // currently treating ancillary only as down (not visible outside) |
| 329 | return null; |
| 330 | } |
| 331 | |
| 332 | links.put(key, newLink); |
| 333 | srcLinks.put(newLink.src().deviceId(), key); |
| 334 | dstLinks.put(newLink.dst().deviceId(), key); |
| 335 | return new LinkEvent(LINK_ADDED, newLink); |
| 336 | } |
| 337 | |
| 338 | // Updates, if necessary the specified link and returns the appropriate event. |
| 339 | // Guarded by linkDescs value (=locking each Link) |
| 340 | private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) { |
| 341 | |
| 342 | if (newLink.providerId().isAncillary()) { |
| 343 | // TODO: revisit ancillary only Link handling |
| 344 | |
| 345 | // currently treating ancillary only as down (not visible outside) |
| 346 | return null; |
| 347 | } |
| 348 | |
| 349 | if ((oldLink.type() == INDIRECT && newLink.type() == DIRECT) || |
| 350 | !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) { |
| 351 | |
| 352 | links.put(key, newLink); |
| 353 | // strictly speaking following can be ommitted |
| 354 | srcLinks.put(oldLink.src().deviceId(), key); |
| 355 | dstLinks.put(oldLink.dst().deviceId(), key); |
| 356 | return new LinkEvent(LINK_UPDATED, newLink); |
| 357 | } |
| 358 | return null; |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) { |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 363 | final LinkKey key = linkKey(src, dst); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 364 | |
| 365 | DeviceId dstDeviceId = dst.deviceId(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 366 | Timestamp timestamp = null; |
| 367 | try { |
| 368 | timestamp = deviceClockService.getTimestamp(dstDeviceId); |
| 369 | } catch (IllegalStateException e) { |
| 370 | //there are times when this is called before mastership |
| 371 | // handoff correctly completes. |
| 372 | return null; |
| 373 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 374 | |
| 375 | LinkEvent event = removeLinkInternal(key, timestamp); |
| 376 | |
| 377 | if (event != null) { |
| 378 | log.info("Notifying peers of a link removed topology event for a link " |
| 379 | + "between src: {} and dst: {}", src, dst); |
| 380 | try { |
| 381 | notifyPeers(new InternalLinkRemovedEvent(key, timestamp)); |
| 382 | } catch (IOException e) { |
| 383 | log.error("Failed to notify peers of a link removed topology event for a link " |
| 384 | + "between src: {} and dst: {}", src, dst); |
| 385 | } |
| 386 | } |
| 387 | return event; |
| 388 | } |
| 389 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 390 | private static Timestamped<LinkDescription> getPrimaryDescription( |
| 391 | Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) { |
| 392 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 393 | synchronized (linkDescriptions) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 394 | for (Entry<ProviderId, Timestamped<LinkDescription>> |
| 395 | e : linkDescriptions.entrySet()) { |
| 396 | |
| 397 | if (!e.getKey().isAncillary()) { |
| 398 | return e.getValue(); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | return null; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | // TODO: consider slicing out as Timestamp utils |
| 407 | /** |
| 408 | * Checks is timestamp is more recent than timestamped object. |
| 409 | * |
| 410 | * @param timestamp to check if this is more recent then other |
| 411 | * @param timestamped object to be tested against |
| 412 | * @return true if {@code timestamp} is more recent than {@code timestamped} |
| 413 | * or {@code timestamped is null} |
| 414 | */ |
| 415 | private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) { |
| 416 | checkNotNull(timestamp); |
| 417 | if (timestamped == null) { |
| 418 | return true; |
| 419 | } |
| 420 | return timestamp.compareTo(timestamped.timestamp()) > 0; |
| 421 | } |
| 422 | |
| 423 | private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) { |
| 424 | Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions |
| 425 | = getOrCreateLinkDescriptions(key); |
| 426 | |
| 427 | synchronized (linkDescriptions) { |
| 428 | if (linkDescriptions.isEmpty()) { |
| 429 | // never seen such link before. keeping timestamp for record |
| 430 | removedLinks.put(key, timestamp); |
| 431 | return null; |
| 432 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 433 | // accept removal request if given timestamp is newer than |
| 434 | // the latest Timestamp from Primary provider |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 435 | Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions); |
| 436 | if (!isMoreRecent(timestamp, prim)) { |
| 437 | // outdated remove request, ignore |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 438 | return null; |
| 439 | } |
| 440 | removedLinks.put(key, timestamp); |
| 441 | Link link = links.remove(key); |
| 442 | linkDescriptions.clear(); |
| 443 | if (link != null) { |
| 444 | srcLinks.remove(link.src().deviceId(), key); |
| 445 | dstLinks.remove(link.dst().deviceId(), key); |
| 446 | return new LinkEvent(LINK_REMOVED, link); |
| 447 | } |
| 448 | return null; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() { |
| 453 | return synchronizedSetMultimap(HashMultimap.<K, V>create()); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * @return primary ProviderID, or randomly chosen one if none exists |
| 458 | */ |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 459 | private static ProviderId pickBaseProviderId( |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 460 | Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 461 | |
| 462 | ProviderId fallBackPrimary = null; |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 463 | for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 464 | if (!e.getKey().isAncillary()) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 465 | // found primary |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 466 | return e.getKey(); |
| 467 | } else if (fallBackPrimary == null) { |
| 468 | // pick randomly as a fallback in case there is no primary |
| 469 | fallBackPrimary = e.getKey(); |
| 470 | } |
| 471 | } |
| 472 | return fallBackPrimary; |
| 473 | } |
| 474 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 475 | // Guarded by linkDescs value (=locking each Link) |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 476 | private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 477 | ProviderId baseProviderId = pickBaseProviderId(descs); |
| 478 | Timestamped<LinkDescription> base = descs.get(baseProviderId); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 479 | |
| 480 | ConnectPoint src = base.value().src(); |
| 481 | ConnectPoint dst = base.value().dst(); |
| 482 | Type type = base.value().type(); |
| 483 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 484 | annotations = merge(annotations, base.value().annotations()); |
| 485 | |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 486 | for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 487 | if (baseProviderId.equals(e.getKey())) { |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 488 | continue; |
| 489 | } |
| 490 | |
| 491 | // TODO: should keep track of Description timestamp |
| 492 | // and only merge conflicting keys when timestamp is newer |
| 493 | // Currently assuming there will never be a key conflict between |
| 494 | // providers |
| 495 | |
| 496 | // annotation merging. not so efficient, should revisit later |
| 497 | annotations = merge(annotations, e.getValue().value().annotations()); |
| 498 | } |
| 499 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 500 | return new DefaultLink(baseProviderId, src, dst, type, annotations); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 503 | private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) { |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 504 | Map<ProviderId, Timestamped<LinkDescription>> r; |
| 505 | r = linkDescs.get(key); |
| 506 | if (r != null) { |
| 507 | return r; |
| 508 | } |
| 509 | r = new HashMap<>(); |
| 510 | final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded; |
| 511 | concurrentlyAdded = linkDescs.putIfAbsent(key, r); |
| 512 | if (concurrentlyAdded != null) { |
| 513 | return concurrentlyAdded; |
| 514 | } else { |
| 515 | return r; |
| 516 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | private final Function<LinkKey, Link> lookupLink = new LookupLink(); |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 520 | /** |
| 521 | * Returns a Function to lookup Link instance using LinkKey from cache. |
| 522 | * @return |
| 523 | */ |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 524 | private Function<LinkKey, Link> lookupLink() { |
| 525 | return lookupLink; |
| 526 | } |
| 527 | |
| 528 | private final class LookupLink implements Function<LinkKey, Link> { |
| 529 | @Override |
| 530 | public Link apply(LinkKey input) { |
Yuta HIGUCHI | 023295a | 2014-10-15 23:29:46 -0700 | [diff] [blame] | 531 | if (input == null) { |
| 532 | return null; |
| 533 | } else { |
| 534 | return links.get(input); |
| 535 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 539 | private void notifyDelegateIfNotNull(LinkEvent event) { |
| 540 | if (event != null) { |
| 541 | notifyDelegate(event); |
| 542 | } |
| 543 | } |
| 544 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 545 | private void broadcastMessage(MessageSubject subject, Object event) throws IOException { |
| 546 | ClusterMessage message = new ClusterMessage( |
| 547 | clusterService.getLocalNode().id(), |
| 548 | subject, |
| 549 | SERIALIZER.encode(event)); |
| 550 | clusterCommunicator.broadcast(message); |
| 551 | } |
| 552 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 553 | private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException { |
| 554 | ClusterMessage message = new ClusterMessage( |
| 555 | clusterService.getLocalNode().id(), |
| 556 | subject, |
| 557 | SERIALIZER.encode(event)); |
| 558 | clusterCommunicator.unicast(message, recipient); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 561 | private void notifyPeers(InternalLinkEvent event) throws IOException { |
| 562 | broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event); |
| 563 | } |
| 564 | |
| 565 | private void notifyPeers(InternalLinkRemovedEvent event) throws IOException { |
| 566 | broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event); |
| 567 | } |
| 568 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 569 | // notify peer, silently ignoring error |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 570 | private void notifyPeer(NodeId peer, InternalLinkEvent event) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 571 | try { |
| 572 | unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event); |
| 573 | } catch (IOException e) { |
| 574 | log.debug("Failed to notify peer {} with message {}", peer, event); |
| 575 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 578 | // notify peer, silently ignoring error |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 579 | private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 580 | try { |
| 581 | unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event); |
| 582 | } catch (IOException e) { |
| 583 | log.debug("Failed to notify peer {} with message {}", peer, event); |
| 584 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | private final class SendAdvertisementTask implements Runnable { |
| 588 | |
| 589 | @Override |
| 590 | public void run() { |
| 591 | if (Thread.currentThread().isInterrupted()) { |
| 592 | log.info("Interrupted, quitting"); |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | try { |
| 597 | final NodeId self = clusterService.getLocalNode().id(); |
| 598 | Set<ControllerNode> nodes = clusterService.getNodes(); |
| 599 | |
| 600 | ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes) |
| 601 | .transform(toNodeId()) |
| 602 | .toList(); |
| 603 | |
| 604 | if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) { |
Yuta HIGUCHI | 3708308 | 2014-10-13 10:38:38 -0700 | [diff] [blame] | 605 | log.debug("No other peers in the cluster."); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | |
| 609 | NodeId peer; |
| 610 | do { |
| 611 | int idx = RandomUtils.nextInt(0, nodeIds.size()); |
| 612 | peer = nodeIds.get(idx); |
| 613 | } while (peer.equals(self)); |
| 614 | |
| 615 | LinkAntiEntropyAdvertisement ad = createAdvertisement(); |
| 616 | |
| 617 | if (Thread.currentThread().isInterrupted()) { |
| 618 | log.info("Interrupted, quitting"); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | try { |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 623 | unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad); |
| 624 | } catch (IOException e) { |
| 625 | log.debug("Failed to send anti-entropy advertisement to {}", peer); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 626 | return; |
| 627 | } |
| 628 | } catch (Exception e) { |
| 629 | // catch all Exception to avoid Scheduled task being suppressed. |
| 630 | log.error("Exception thrown while sending advertisement", e); |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | private LinkAntiEntropyAdvertisement createAdvertisement() { |
| 636 | final NodeId self = clusterService.getLocalNode().id(); |
| 637 | |
| 638 | Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size()); |
| 639 | Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size()); |
| 640 | |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 641 | for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 642 | provs : linkDescs.entrySet()) { |
| 643 | |
| 644 | final LinkKey linkKey = provs.getKey(); |
Yuta HIGUCHI | 3e1a5bf | 2014-10-14 19:39:58 -0700 | [diff] [blame] | 645 | final Map<ProviderId, Timestamped<LinkDescription>> linkDesc = provs.getValue(); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 646 | synchronized (linkDesc) { |
| 647 | for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) { |
| 648 | linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp()); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | linkTombstones.putAll(removedLinks); |
| 654 | |
| 655 | return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones); |
| 656 | } |
| 657 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 658 | private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) { |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 659 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 660 | final NodeId sender = ad.sender(); |
| 661 | boolean localOutdated = false; |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 662 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 663 | for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> |
| 664 | l : linkDescs.entrySet()) { |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 665 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 666 | final LinkKey key = l.getKey(); |
| 667 | final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue(); |
| 668 | synchronized (link) { |
| 669 | Timestamp localLatest = removedLinks.get(key); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 670 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 671 | for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) { |
| 672 | final ProviderId providerId = p.getKey(); |
| 673 | final Timestamped<LinkDescription> pDesc = p.getValue(); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 674 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 675 | final LinkFragmentId fragId = new LinkFragmentId(key, providerId); |
| 676 | // remote |
| 677 | Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId); |
| 678 | if (remoteTimestamp == null) { |
| 679 | remoteTimestamp = ad.linkTombstones().get(key); |
| 680 | } |
| 681 | if (remoteTimestamp == null || |
| 682 | pDesc.isNewer(remoteTimestamp)) { |
| 683 | // I have more recent link description. update peer. |
| 684 | notifyPeer(sender, new InternalLinkEvent(providerId, pDesc)); |
| 685 | } else { |
| 686 | final Timestamp remoteLive = ad.linkTimestamps().get(fragId); |
| 687 | if (remoteLive != null && |
| 688 | remoteLive.compareTo(pDesc.timestamp()) > 0) { |
| 689 | // I have something outdated |
| 690 | localOutdated = true; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | // search local latest along the way |
| 695 | if (localLatest == null || |
| 696 | pDesc.isNewer(localLatest)) { |
| 697 | localLatest = pDesc.timestamp(); |
| 698 | } |
| 699 | } |
| 700 | // Tests if remote remove is more recent then local latest. |
| 701 | final Timestamp remoteRemove = ad.linkTombstones().get(key); |
| 702 | if (remoteRemove != null) { |
| 703 | if (localLatest != null && |
| 704 | localLatest.compareTo(remoteRemove) < 0) { |
| 705 | // remote remove is more recent |
| 706 | notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove)); |
| 707 | } |
| 708 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 712 | // populate remove info if not known locally |
| 713 | for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) { |
| 714 | final LinkKey key = remoteRm.getKey(); |
| 715 | final Timestamp remoteRemove = remoteRm.getValue(); |
| 716 | // relying on removeLinkInternal to ignore stale info |
| 717 | notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove)); |
| 718 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 719 | |
Yuta HIGUCHI | 2fe46a2 | 2014-10-15 22:51:02 -0700 | [diff] [blame] | 720 | if (localOutdated) { |
| 721 | // send back advertisement to speed up convergence |
| 722 | try { |
| 723 | unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT, |
| 724 | createAdvertisement()); |
| 725 | } catch (IOException e) { |
| 726 | log.debug("Failed to send back active advertisement"); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 731 | private class InternalLinkEventListener implements ClusterMessageHandler { |
| 732 | @Override |
| 733 | public void handle(ClusterMessage message) { |
| 734 | |
Yuta HIGUCHI | c01d2aa | 2014-10-19 01:19:34 -0700 | [diff] [blame] | 735 | log.trace("Received link event from peer: {}", message.sender()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 736 | InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload()); |
| 737 | |
| 738 | ProviderId providerId = event.providerId(); |
| 739 | Timestamped<LinkDescription> linkDescription = event.linkDescription(); |
| 740 | |
| 741 | notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription)); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | private class InternalLinkRemovedEventListener implements ClusterMessageHandler { |
| 746 | @Override |
| 747 | public void handle(ClusterMessage message) { |
| 748 | |
Yuta HIGUCHI | c01d2aa | 2014-10-19 01:19:34 -0700 | [diff] [blame] | 749 | log.trace("Received link removed event from peer: {}", message.sender()); |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 750 | InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload()); |
| 751 | |
| 752 | LinkKey linkKey = event.linkKey(); |
| 753 | Timestamp timestamp = event.timestamp(); |
| 754 | |
| 755 | notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp)); |
| 756 | } |
| 757 | } |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 758 | |
| 759 | private final class InternalLinkAntiEntropyAdvertisementListener implements ClusterMessageHandler { |
| 760 | |
| 761 | @Override |
| 762 | public void handle(ClusterMessage message) { |
Yuta HIGUCHI | 9a0a1d1 | 2014-10-13 22:38:02 -0700 | [diff] [blame] | 763 | log.debug("Received Link Anti-Entropy advertisement from peer: {}", message.sender()); |
Madan Jampani | a97e820 | 2014-10-10 17:01:33 -0700 | [diff] [blame] | 764 | LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload()); |
| 765 | handleAntiEntropyAdvertisement(advertisement); |
| 766 | } |
| 767 | } |
Madan Jampani | 2ff0559 | 2014-10-10 15:42:47 -0700 | [diff] [blame] | 768 | } |