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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.host.impl; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
| 20 | import static com.google.common.collect.Multimaps.newSetMultimap; |
| 21 | import static com.google.common.collect.Sets.newConcurrentHashSet; |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 22 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import static org.onosproject.cluster.ControllerNodeToNodeId.toNodeId; |
| 24 | import static org.onosproject.net.DefaultAnnotations.merge; |
| 25 | import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED; |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 27 | import static org.onosproject.store.host.impl.GossipHostStoreMessageSubjects.*; |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 28 | import static org.onlab.util.Tools.namedThreads; |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 29 | import static org.onlab.util.Tools.minPriority; |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | import java.util.Collections; |
| 34 | import java.util.HashMap; |
| 35 | import java.util.HashSet; |
| 36 | import java.util.Map; |
| 37 | import java.util.Map.Entry; |
| 38 | import java.util.Set; |
| 39 | import java.util.concurrent.ConcurrentHashMap; |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 40 | import java.util.concurrent.ExecutorService; |
| 41 | import java.util.concurrent.Executors; |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 42 | import java.util.concurrent.ScheduledExecutorService; |
| 43 | import java.util.concurrent.TimeUnit; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 44 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 45 | import org.apache.commons.lang3.RandomUtils; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 46 | import org.apache.felix.scr.annotations.Activate; |
| 47 | import org.apache.felix.scr.annotations.Component; |
| 48 | import org.apache.felix.scr.annotations.Deactivate; |
| 49 | import org.apache.felix.scr.annotations.Reference; |
| 50 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 51 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.cluster.ClusterService; |
| 53 | import org.onosproject.cluster.ControllerNode; |
| 54 | import org.onosproject.cluster.NodeId; |
| 55 | import org.onosproject.net.Annotations; |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 56 | import org.onosproject.net.AnnotationsUtil; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 57 | import org.onosproject.net.ConnectPoint; |
| 58 | import org.onosproject.net.DefaultAnnotations; |
| 59 | import org.onosproject.net.DefaultHost; |
| 60 | import org.onosproject.net.DeviceId; |
| 61 | import org.onosproject.net.Host; |
| 62 | import org.onosproject.net.HostId; |
| 63 | import org.onosproject.net.HostLocation; |
| 64 | import org.onosproject.net.host.DefaultHostDescription; |
| 65 | import org.onosproject.net.host.HostClockService; |
| 66 | import org.onosproject.net.host.HostDescription; |
| 67 | import org.onosproject.net.host.HostEvent; |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 68 | import org.onosproject.net.host.HostEvent.Type; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 69 | import org.onosproject.net.host.HostStore; |
| 70 | import org.onosproject.net.host.HostStoreDelegate; |
| 71 | import org.onosproject.net.host.PortAddresses; |
| 72 | import org.onosproject.net.provider.ProviderId; |
| 73 | import org.onosproject.store.AbstractStore; |
| 74 | import org.onosproject.store.Timestamp; |
| 75 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 76 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 77 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 78 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 79 | import org.onosproject.store.impl.Timestamped; |
| 80 | import org.onosproject.store.serializers.KryoSerializer; |
| 81 | import org.onosproject.store.serializers.impl.DistributedStoreSerializers; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 82 | import org.onlab.packet.IpAddress; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 83 | import org.onlab.packet.MacAddress; |
| 84 | import org.onlab.packet.VlanId; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 85 | import org.onlab.util.KryoNamespace; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 86 | import org.slf4j.Logger; |
| 87 | |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 88 | import com.google.common.collect.FluentIterable; |
| 89 | import com.google.common.collect.HashMultimap; |
| 90 | import com.google.common.collect.ImmutableList; |
| 91 | import com.google.common.collect.ImmutableSet; |
| 92 | import com.google.common.collect.Multimap; |
| 93 | import com.google.common.collect.Multimaps; |
| 94 | import com.google.common.collect.SetMultimap; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 95 | |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Manages inventory of end-station hosts in distributed data store |
| 98 | * that uses optimistic replication and gossip based techniques. |
| 99 | */ |
| 100 | @Component(immediate = true) |
| 101 | @Service |
| 102 | public class GossipHostStore |
| 103 | extends AbstractStore<HostEvent, HostStoreDelegate> |
| 104 | implements HostStore { |
| 105 | |
| 106 | private final Logger log = getLogger(getClass()); |
| 107 | |
Yuta HIGUCHI | 4423acc | 2014-11-25 12:34:44 -0800 | [diff] [blame] | 108 | // TODO: make this configurable |
| 109 | private int hostsExpected = 2000000; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 110 | |
Yuta HIGUCHI | 4423acc | 2014-11-25 12:34:44 -0800 | [diff] [blame] | 111 | // Host inventory |
| 112 | private final Map<HostId, StoredHost> hosts = new ConcurrentHashMap<>(hostsExpected, 0.75f, 16); |
| 113 | |
| 114 | private final Map<HostId, Timestamped<Host>> removedHosts = new ConcurrentHashMap<>(hostsExpected, 0.75f, 16); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 115 | |
| 116 | // Hosts tracked by their location |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 117 | private final Multimap<ConnectPoint, Host> locations |
| 118 | = synchronizedSetMultimap(newSetMultimap(new ConcurrentHashMap<>(), |
| 119 | () -> newConcurrentHashSet())); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 120 | |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 121 | private final SetMultimap<ConnectPoint, PortAddresses> portAddresses = |
| 122 | Multimaps.synchronizedSetMultimap( |
| 123 | HashMultimap.<ConnectPoint, PortAddresses>create()); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 126 | protected HostClockService hostClockService; |
| 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | protected ClusterCommunicationService clusterCommunicator; |
| 130 | |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 132 | protected ClusterService clusterService; |
| 133 | |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 134 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 135 | @Override |
| 136 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 137 | serializerPool = KryoNamespace.newBuilder() |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 138 | .register(DistributedStoreSerializers.STORE_COMMON) |
| 139 | .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 140 | .register(InternalHostEvent.class) |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 141 | .register(InternalHostRemovedEvent.class) |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 142 | .register(HostFragmentId.class) |
| 143 | .register(HostAntiEntropyAdvertisement.class) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 144 | .build(); |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 145 | } |
| 146 | }; |
| 147 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 148 | private ExecutorService executor; |
| 149 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 150 | private ScheduledExecutorService backgroundExecutor; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 151 | |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 152 | // TODO: Make these anti-entropy params configurable |
| 153 | private long initialDelaySec = 5; |
| 154 | private long periodSec = 5; |
| 155 | |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 156 | @Activate |
| 157 | public void activate() { |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 158 | clusterCommunicator.addSubscriber( |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 159 | HOST_UPDATED_MSG, |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 160 | new InternalHostEventListener()); |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 161 | clusterCommunicator.addSubscriber( |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 162 | HOST_REMOVED_MSG, |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 163 | new InternalHostRemovedEventListener()); |
| 164 | clusterCommunicator.addSubscriber( |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 165 | HOST_ANTI_ENTROPY_ADVERTISEMENT, |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 166 | new InternalHostAntiEntropyAdvertisementListener()); |
| 167 | |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 168 | executor = Executors.newCachedThreadPool(namedThreads("onos-host-fg-%d")); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 169 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 170 | backgroundExecutor = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 171 | newSingleThreadScheduledExecutor(minPriority(namedThreads("onos-host-bg-%d"))); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 172 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 173 | // start anti-entropy thread |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 174 | backgroundExecutor.scheduleAtFixedRate(new SendAdvertisementTask(), |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 175 | initialDelaySec, periodSec, TimeUnit.SECONDS); |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 176 | |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 177 | log.info("Started"); |
| 178 | } |
| 179 | |
| 180 | @Deactivate |
| 181 | public void deactivate() { |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 182 | executor.shutdownNow(); |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 183 | backgroundExecutor.shutdownNow(); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 184 | try { |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 185 | if (!backgroundExecutor.awaitTermination(5, TimeUnit.SECONDS)) { |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -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 | |
| 192 | hosts.clear(); |
| 193 | removedHosts.clear(); |
| 194 | locations.clear(); |
| 195 | portAddresses.clear(); |
| 196 | |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 197 | log.info("Stopped"); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId, |
| 202 | HostDescription hostDescription) { |
| 203 | Timestamp timestamp = hostClockService.getTimestamp(hostId); |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 204 | HostEvent event = createOrUpdateHostInternal(providerId, hostId, hostDescription, timestamp); |
| 205 | if (event != null) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 206 | log.debug("Notifying peers of a host topology event for providerId: " |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 207 | + "{}; hostId: {}; hostDescription: {}", providerId, hostId, hostDescription); |
| 208 | try { |
| 209 | notifyPeers(new InternalHostEvent(providerId, hostId, hostDescription, timestamp)); |
| 210 | } catch (IOException e) { |
| 211 | log.error("Failed to notify peers of a host topology event for providerId: " |
| 212 | + "{}; hostId: {}; hostDescription: {}", providerId, hostId, hostDescription); |
| 213 | } |
| 214 | } |
| 215 | return event; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | private HostEvent createOrUpdateHostInternal(ProviderId providerId, HostId hostId, |
| 219 | HostDescription hostDescription, Timestamp timestamp) { |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 220 | // If this host was previously removed, first ensure |
| 221 | // this new request is "newer" |
| 222 | if (isHostRemoved(hostId, timestamp)) { |
| 223 | log.debug("Ignoring update for removed host {}@{}", |
| 224 | hostDescription, timestamp); |
| 225 | return null; |
| 226 | } |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 227 | StoredHost host = hosts.get(hostId); |
| 228 | if (host == null) { |
| 229 | return createHost(providerId, hostId, hostDescription, timestamp); |
| 230 | } |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 231 | return updateHost(providerId, hostId, host, hostDescription, timestamp); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @param hostId host identifier |
| 236 | * @param timestamp timstamp to compare with |
| 237 | * @return true if given timestamp is more recent timestamp compared to |
| 238 | * the timestamp Host was removed. |
| 239 | */ |
| 240 | private boolean isHostRemoved(HostId hostId, Timestamp timestamp) { |
| 241 | Timestamped<Host> removedInfo = removedHosts.get(hostId); |
| 242 | if (removedInfo != null) { |
| 243 | if (removedInfo.isNewer(timestamp)) { |
| 244 | return true; |
| 245 | } |
| 246 | removedHosts.remove(hostId, removedInfo); |
| 247 | } |
| 248 | return false; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // creates a new host and sends HOST_ADDED |
| 252 | private HostEvent createHost(ProviderId providerId, HostId hostId, |
| 253 | HostDescription descr, Timestamp timestamp) { |
| 254 | synchronized (this) { |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 255 | StoredHost newhost = new StoredHost(timestamp, providerId, hostId, |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 256 | descr.hwAddress(), |
| 257 | descr.vlan(), |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 258 | descr.location(), |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 259 | ImmutableSet.copyOf(descr.ipAddress())); |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 260 | StoredHost concAdd = hosts.putIfAbsent(hostId, newhost); |
| 261 | if (concAdd != null) { |
| 262 | // concurrent add detected, retry from start |
| 263 | return updateHost(providerId, hostId, concAdd, descr, timestamp); |
| 264 | } |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 265 | locations.put(descr.location(), newhost); |
| 266 | return new HostEvent(HOST_ADDED, newhost); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // checks for type of update to host, sends appropriate event |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 271 | private HostEvent updateHost(ProviderId providerId, HostId hostId, StoredHost oldHost, |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 272 | HostDescription descr, Timestamp timestamp) { |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 273 | |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 274 | if (timestamp.compareTo(oldHost.timestamp()) < 0) { |
| 275 | // new timestamp is older |
| 276 | log.debug("Ignoring outdated host update {}@{}", descr, timestamp); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 277 | return null; |
| 278 | } |
| 279 | |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 280 | final boolean hostMoved = !oldHost.location().equals(descr.location()); |
| 281 | if (hostMoved || |
| 282 | !oldHost.ipAddresses().containsAll(descr.ipAddress()) || |
| 283 | !descr.annotations().keys().isEmpty()) { |
| 284 | |
| 285 | Set<IpAddress> addresses = new HashSet<>(oldHost.ipAddresses()); |
| 286 | addresses.addAll(descr.ipAddress()); |
| 287 | Annotations annotations = merge((DefaultAnnotations) oldHost.annotations(), |
| 288 | descr.annotations()); |
| 289 | |
| 290 | Timestamp newTimestamp = timestamp; |
| 291 | // if merged Set/Annotation differ from description... |
| 292 | final boolean deltaUpdate = !descr.ipAddress().equals(addresses) || |
| 293 | !AnnotationsUtil.isEqual(descr.annotations(), annotations); |
| 294 | if (deltaUpdate) { |
| 295 | // ..then local existing info had something description didn't |
| 296 | newTimestamp = hostClockService.getTimestamp(hostId); |
| 297 | log.debug("delta update detected on {}, substepping timestamp to {}", |
| 298 | hostId, newTimestamp); |
| 299 | } |
| 300 | |
| 301 | StoredHost updated = new StoredHost(newTimestamp, |
| 302 | providerId, oldHost.id(), |
| 303 | oldHost.mac(), oldHost.vlan(), |
| 304 | descr.location(), |
| 305 | addresses, |
| 306 | annotations); |
| 307 | synchronized (this) { |
| 308 | boolean replaced = hosts.replace(hostId, oldHost, updated); |
| 309 | if (!replaced) { |
| 310 | // concurrent update, retry |
| 311 | return createOrUpdateHostInternal(providerId, hostId, descr, timestamp); |
| 312 | } |
| 313 | locations.remove(oldHost.location(), oldHost); |
| 314 | locations.put(updated.location(), updated); |
| 315 | |
| 316 | HostEvent.Type eventType; |
| 317 | if (hostMoved) { |
| 318 | eventType = Type.HOST_MOVED; |
| 319 | } else { |
| 320 | eventType = Type.HOST_UPDATED; |
| 321 | } |
| 322 | return new HostEvent(eventType, updated); |
| 323 | } |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 324 | } |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 325 | return null; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public HostEvent removeHost(HostId hostId) { |
| 330 | Timestamp timestamp = hostClockService.getTimestamp(hostId); |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 331 | HostEvent event = removeHostInternal(hostId, timestamp); |
| 332 | if (event != null) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 333 | log.debug("Notifying peers of a host removed topology event for hostId: {}", hostId); |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 334 | try { |
| 335 | notifyPeers(new InternalHostRemovedEvent(hostId, timestamp)); |
| 336 | } catch (IOException e) { |
| 337 | log.info("Failed to notify peers of a host removed topology event for hostId: {}", hostId); |
| 338 | } |
| 339 | } |
| 340 | return event; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | private HostEvent removeHostInternal(HostId hostId, Timestamp timestamp) { |
| 344 | synchronized (this) { |
| 345 | Host host = hosts.remove(hostId); |
| 346 | if (host != null) { |
| 347 | locations.remove((host.location()), host); |
| 348 | removedHosts.put(hostId, new Timestamped<>(host, timestamp)); |
| 349 | return new HostEvent(HOST_REMOVED, host); |
| 350 | } |
| 351 | return null; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public int getHostCount() { |
| 357 | return hosts.size(); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public Iterable<Host> getHosts() { |
| 362 | return ImmutableSet.<Host>copyOf(hosts.values()); |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public Host getHost(HostId hostId) { |
| 367 | return hosts.get(hostId); |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public Set<Host> getHosts(VlanId vlanId) { |
| 372 | Set<Host> vlanset = new HashSet<>(); |
| 373 | for (Host h : hosts.values()) { |
| 374 | if (h.vlan().equals(vlanId)) { |
| 375 | vlanset.add(h); |
| 376 | } |
| 377 | } |
| 378 | return vlanset; |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public Set<Host> getHosts(MacAddress mac) { |
| 383 | Set<Host> macset = new HashSet<>(); |
| 384 | for (Host h : hosts.values()) { |
| 385 | if (h.mac().equals(mac)) { |
| 386 | macset.add(h); |
| 387 | } |
| 388 | } |
| 389 | return macset; |
| 390 | } |
| 391 | |
| 392 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 393 | public Set<Host> getHosts(IpAddress ip) { |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 394 | Set<Host> ipset = new HashSet<>(); |
| 395 | for (Host h : hosts.values()) { |
| 396 | if (h.ipAddresses().contains(ip)) { |
| 397 | ipset.add(h); |
| 398 | } |
| 399 | } |
| 400 | return ipset; |
| 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
| 405 | return ImmutableSet.copyOf(locations.get(connectPoint)); |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
| 410 | Set<Host> hostset = new HashSet<>(); |
| 411 | for (ConnectPoint p : locations.keySet()) { |
| 412 | if (p.deviceId().equals(deviceId)) { |
| 413 | hostset.addAll(locations.get(p)); |
| 414 | } |
| 415 | } |
| 416 | return hostset; |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public void updateAddressBindings(PortAddresses addresses) { |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 421 | portAddresses.put(addresses.connectPoint(), addresses); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void removeAddressBindings(PortAddresses addresses) { |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 426 | portAddresses.remove(addresses.connectPoint(), addresses); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public void clearAddressBindings(ConnectPoint connectPoint) { |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 431 | portAddresses.removeAll(connectPoint); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public Set<PortAddresses> getAddressBindings() { |
| 436 | synchronized (portAddresses) { |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 437 | return ImmutableSet.copyOf(portAddresses.values()); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | @Override |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 442 | public Set<PortAddresses> getAddressBindingsForPort(ConnectPoint connectPoint) { |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 443 | synchronized (portAddresses) { |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 444 | Set<PortAddresses> addresses = portAddresses.get(connectPoint); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 445 | |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 446 | if (addresses == null) { |
| 447 | return Collections.emptySet(); |
| 448 | } else { |
| 449 | return ImmutableSet.copyOf(addresses); |
| 450 | } |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 451 | } |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 454 | private static final class StoredHost extends DefaultHost { |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 455 | private final Timestamp timestamp; |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * Creates an end-station host using the supplied information. |
| 459 | * |
| 460 | * @param providerId provider identity |
| 461 | * @param id host identifier |
| 462 | * @param mac host MAC address |
| 463 | * @param vlan host VLAN identifier |
| 464 | * @param location host location |
| 465 | * @param ips host IP addresses |
| 466 | * @param annotations optional key/value annotations |
| 467 | */ |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 468 | public StoredHost(Timestamp timestamp, ProviderId providerId, HostId id, |
| 469 | MacAddress mac, VlanId vlan, HostLocation location, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 470 | Set<IpAddress> ips, Annotations... annotations) { |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 471 | super(providerId, id, mac, vlan, location, ips, annotations); |
| 472 | this.timestamp = checkNotNull(timestamp); |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 473 | } |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 474 | |
| 475 | public Timestamp timestamp() { |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 476 | return timestamp; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 477 | } |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 478 | } |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 479 | |
| 480 | private void notifyPeers(InternalHostRemovedEvent event) throws IOException { |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 481 | broadcastMessage(HOST_REMOVED_MSG, event); |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | private void notifyPeers(InternalHostEvent event) throws IOException { |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 485 | broadcastMessage(HOST_UPDATED_MSG, event); |
Madan Jampani | 312a298 | 2014-10-14 21:07:16 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | private void broadcastMessage(MessageSubject subject, Object event) throws IOException { |
| 489 | ClusterMessage message = new ClusterMessage( |
| 490 | clusterService.getLocalNode().id(), |
| 491 | subject, |
| 492 | SERIALIZER.encode(event)); |
| 493 | clusterCommunicator.broadcast(message); |
| 494 | } |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 495 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 496 | private void unicastMessage(NodeId peer, |
| 497 | MessageSubject subject, |
| 498 | Object event) throws IOException { |
| 499 | ClusterMessage message = new ClusterMessage( |
| 500 | clusterService.getLocalNode().id(), |
| 501 | subject, |
| 502 | SERIALIZER.encode(event)); |
| 503 | clusterCommunicator.unicast(message, peer); |
| 504 | } |
| 505 | |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 506 | private void notifyDelegateIfNotNull(HostEvent event) { |
| 507 | if (event != null) { |
| 508 | notifyDelegate(event); |
| 509 | } |
| 510 | } |
| 511 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 512 | private final class InternalHostEventListener |
| 513 | implements ClusterMessageHandler { |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 514 | @Override |
| 515 | public void handle(ClusterMessage message) { |
| 516 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 517 | log.debug("Received host update event from peer: {}", message.sender()); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 518 | InternalHostEvent event = SERIALIZER.decode(message.payload()); |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 519 | |
| 520 | ProviderId providerId = event.providerId(); |
| 521 | HostId hostId = event.hostId(); |
| 522 | HostDescription hostDescription = event.hostDescription(); |
| 523 | Timestamp timestamp = event.timestamp(); |
| 524 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 525 | executor.submit(new Runnable() { |
| 526 | |
| 527 | @Override |
| 528 | public void run() { |
| 529 | try { |
| 530 | notifyDelegateIfNotNull(createOrUpdateHostInternal(providerId, |
| 531 | hostId, |
| 532 | hostDescription, |
| 533 | timestamp)); |
| 534 | } catch (Exception e) { |
| 535 | log.warn("Exception thrown handling host removed", e); |
| 536 | } |
| 537 | } |
| 538 | }); |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 542 | private final class InternalHostRemovedEventListener |
| 543 | implements ClusterMessageHandler { |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 544 | @Override |
| 545 | public void handle(ClusterMessage message) { |
| 546 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 547 | log.debug("Received host removed event from peer: {}", message.sender()); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 548 | InternalHostRemovedEvent event = SERIALIZER.decode(message.payload()); |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 549 | |
| 550 | HostId hostId = event.hostId(); |
| 551 | Timestamp timestamp = event.timestamp(); |
| 552 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 553 | executor.submit(new Runnable() { |
| 554 | |
| 555 | @Override |
| 556 | public void run() { |
| 557 | try { |
| 558 | notifyDelegateIfNotNull(removeHostInternal(hostId, timestamp)); |
| 559 | } catch (Exception e) { |
| 560 | log.warn("Exception thrown handling host removed", e); |
| 561 | } |
| 562 | } |
| 563 | }); |
Madan Jampani | 255618f | 2014-10-14 23:27:57 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 566 | |
| 567 | private final class SendAdvertisementTask implements Runnable { |
| 568 | |
| 569 | @Override |
| 570 | public void run() { |
| 571 | if (Thread.currentThread().isInterrupted()) { |
| 572 | log.info("Interrupted, quitting"); |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | try { |
| 577 | final NodeId self = clusterService.getLocalNode().id(); |
| 578 | Set<ControllerNode> nodes = clusterService.getNodes(); |
| 579 | |
| 580 | ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes) |
| 581 | .transform(toNodeId()) |
| 582 | .toList(); |
| 583 | |
| 584 | if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) { |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 585 | log.trace("No other peers in the cluster."); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 586 | return; |
| 587 | } |
| 588 | |
| 589 | NodeId peer; |
| 590 | do { |
| 591 | int idx = RandomUtils.nextInt(0, nodeIds.size()); |
| 592 | peer = nodeIds.get(idx); |
| 593 | } while (peer.equals(self)); |
| 594 | |
| 595 | HostAntiEntropyAdvertisement ad = createAdvertisement(); |
| 596 | |
| 597 | if (Thread.currentThread().isInterrupted()) { |
| 598 | log.info("Interrupted, quitting"); |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | try { |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 603 | unicastMessage(peer, HOST_ANTI_ENTROPY_ADVERTISEMENT, ad); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 604 | } catch (IOException e) { |
Yuta HIGUCHI | 78f3a0a | 2014-10-16 17:24:20 -0700 | [diff] [blame] | 605 | log.debug("Failed to send anti-entropy advertisement to {}", peer); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | } catch (Exception e) { |
| 609 | // catch all Exception to avoid Scheduled task being suppressed. |
| 610 | log.error("Exception thrown while sending advertisement", e); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | private HostAntiEntropyAdvertisement createAdvertisement() { |
| 616 | final NodeId self = clusterService.getLocalNode().id(); |
| 617 | |
| 618 | Map<HostFragmentId, Timestamp> timestamps = new HashMap<>(hosts.size()); |
| 619 | Map<HostId, Timestamp> tombstones = new HashMap<>(removedHosts.size()); |
| 620 | |
Yuta HIGUCHI | 4423acc | 2014-11-25 12:34:44 -0800 | [diff] [blame] | 621 | hosts.forEach((hostId, hostInfo) -> { |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 622 | final ProviderId providerId = hostInfo.providerId(); |
| 623 | timestamps.put(new HostFragmentId(hostId, providerId), hostInfo.timestamp()); |
Yuta HIGUCHI | 4423acc | 2014-11-25 12:34:44 -0800 | [diff] [blame] | 624 | }); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 625 | |
Yuta HIGUCHI | 4423acc | 2014-11-25 12:34:44 -0800 | [diff] [blame] | 626 | removedHosts.forEach((hostId, timestamped) -> { |
| 627 | tombstones.put(hostId, timestamped.timestamp()); |
| 628 | }); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 629 | |
| 630 | return new HostAntiEntropyAdvertisement(self, timestamps, tombstones); |
| 631 | } |
| 632 | |
| 633 | private synchronized void handleAntiEntropyAdvertisement(HostAntiEntropyAdvertisement ad) { |
| 634 | |
| 635 | final NodeId sender = ad.sender(); |
| 636 | |
| 637 | for (Entry<HostId, StoredHost> host : hosts.entrySet()) { |
| 638 | // for each locally live Hosts... |
| 639 | final HostId hostId = host.getKey(); |
| 640 | final StoredHost localHost = host.getValue(); |
| 641 | final ProviderId providerId = localHost.providerId(); |
| 642 | final HostFragmentId hostFragId = new HostFragmentId(hostId, providerId); |
| 643 | final Timestamp localLiveTimestamp = localHost.timestamp(); |
| 644 | |
| 645 | Timestamp remoteTimestamp = ad.timestamps().get(hostFragId); |
| 646 | if (remoteTimestamp == null) { |
| 647 | remoteTimestamp = ad.tombstones().get(hostId); |
| 648 | } |
| 649 | if (remoteTimestamp == null || |
| 650 | localLiveTimestamp.compareTo(remoteTimestamp) > 0) { |
| 651 | |
| 652 | // local is more recent, push |
| 653 | // TODO: annotation is lost |
| 654 | final HostDescription desc = new DefaultHostDescription( |
| 655 | localHost.mac(), |
| 656 | localHost.vlan(), |
| 657 | localHost.location(), |
| 658 | localHost.ipAddresses()); |
| 659 | try { |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 660 | unicastMessage(sender, HOST_UPDATED_MSG, |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 661 | new InternalHostEvent(providerId, hostId, desc, localHost.timestamp())); |
| 662 | } catch (IOException e1) { |
| 663 | log.debug("Failed to send advertisement response", e1); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | final Timestamp remoteDeadTimestamp = ad.tombstones().get(hostId); |
| 668 | if (remoteDeadTimestamp != null && |
| 669 | remoteDeadTimestamp.compareTo(localLiveTimestamp) > 0) { |
| 670 | // sender has recent remove |
| 671 | notifyDelegateIfNotNull(removeHostInternal(hostId, remoteDeadTimestamp)); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | for (Entry<HostId, Timestamped<Host>> dead : removedHosts.entrySet()) { |
| 676 | // for each locally dead Hosts |
| 677 | final HostId hostId = dead.getKey(); |
| 678 | final Timestamp localDeadTimestamp = dead.getValue().timestamp(); |
| 679 | |
| 680 | // TODO: pick proper ProviderId, when supporting multi-provider |
| 681 | final ProviderId providerId = dead.getValue().value().providerId(); |
| 682 | final HostFragmentId hostFragId = new HostFragmentId(hostId, providerId); |
| 683 | |
| 684 | final Timestamp remoteLiveTimestamp = ad.timestamps().get(hostFragId); |
| 685 | if (remoteLiveTimestamp != null && |
| 686 | localDeadTimestamp.compareTo(remoteLiveTimestamp) > 0) { |
| 687 | // sender has zombie, push |
| 688 | try { |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 689 | unicastMessage(sender, HOST_REMOVED_MSG, |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 690 | new InternalHostRemovedEvent(hostId, localDeadTimestamp)); |
| 691 | } catch (IOException e1) { |
| 692 | log.debug("Failed to send advertisement response", e1); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 697 | for (Entry<HostId, Timestamp> e : ad.tombstones().entrySet()) { |
| 698 | // for each remote tombstone advertisement... |
| 699 | final HostId hostId = e.getKey(); |
| 700 | final Timestamp adRemoveTimestamp = e.getValue(); |
| 701 | |
| 702 | final StoredHost storedHost = hosts.get(hostId); |
| 703 | if (storedHost == null) { |
| 704 | continue; |
| 705 | } |
| 706 | if (adRemoveTimestamp.compareTo(storedHost.timestamp()) > 0) { |
| 707 | // sender has recent remove info, locally remove |
| 708 | notifyDelegateIfNotNull(removeHostInternal(hostId, adRemoveTimestamp)); |
| 709 | } |
| 710 | } |
Yuta HIGUCHI | e59c7e3 | 2014-12-08 22:07:31 -0800 | [diff] [blame] | 711 | |
| 712 | // if remote ad has something unknown, actively sync |
| 713 | for (HostFragmentId key : ad.timestamps().keySet()) { |
| 714 | if (!hosts.containsKey(key.hostId())) { |
| 715 | HostAntiEntropyAdvertisement myAd = createAdvertisement(); |
| 716 | try { |
| 717 | unicastMessage(sender, HOST_ANTI_ENTROPY_ADVERTISEMENT, myAd); |
| 718 | break; |
| 719 | } catch (IOException e) { |
| 720 | log.debug("Failed to send reactive anti-entropy advertisement to {}", sender); |
| 721 | } |
| 722 | } |
| 723 | } |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 726 | private final class InternalHostAntiEntropyAdvertisementListener |
| 727 | implements ClusterMessageHandler { |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 728 | |
| 729 | @Override |
| 730 | public void handle(ClusterMessage message) { |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 731 | log.trace("Received Host Anti-Entropy advertisement from peer: {}", message.sender()); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 732 | HostAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 733 | backgroundExecutor.submit(new Runnable() { |
| 734 | |
| 735 | @Override |
| 736 | public void run() { |
| 737 | try { |
| 738 | handleAntiEntropyAdvertisement(advertisement); |
| 739 | } catch (Exception e) { |
| 740 | log.warn("Exception thrown handling Host advertisements", e); |
| 741 | } |
| 742 | } |
| 743 | }); |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
Madan Jampani | ab99404 | 2014-10-13 15:34:04 -0700 | [diff] [blame] | 746 | } |