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.device.impl; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 18 | import com.google.common.base.Function; |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 19 | import com.google.common.base.Predicate; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 20 | import com.google.common.collect.FluentIterable; |
| 21 | import com.google.common.collect.ImmutableList; |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.Sets; |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 24 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 25 | import org.apache.commons.lang3.RandomUtils; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.cluster.ClusterService; |
| 33 | import org.onosproject.cluster.ControllerNode; |
| 34 | import org.onosproject.cluster.NodeId; |
| 35 | import org.onosproject.mastership.MastershipService; |
| 36 | import org.onosproject.mastership.MastershipTerm; |
| 37 | import org.onosproject.mastership.MastershipTermService; |
| 38 | import org.onosproject.net.AnnotationsUtil; |
| 39 | import org.onosproject.net.DefaultAnnotations; |
| 40 | import org.onosproject.net.DefaultDevice; |
| 41 | import org.onosproject.net.DefaultPort; |
| 42 | import org.onosproject.net.Device; |
| 43 | import org.onosproject.net.Device.Type; |
| 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.onosproject.net.MastershipRole; |
| 46 | import org.onosproject.net.Port; |
| 47 | import org.onosproject.net.PortNumber; |
| 48 | import org.onosproject.net.device.DeviceClockService; |
| 49 | import org.onosproject.net.device.DeviceDescription; |
| 50 | import org.onosproject.net.device.DeviceEvent; |
| 51 | import org.onosproject.net.device.DeviceStore; |
| 52 | import org.onosproject.net.device.DeviceStoreDelegate; |
| 53 | import org.onosproject.net.device.PortDescription; |
| 54 | import org.onosproject.net.provider.ProviderId; |
| 55 | import org.onosproject.store.AbstractStore; |
| 56 | import org.onosproject.store.Timestamp; |
| 57 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 58 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 59 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 60 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 61 | import org.onosproject.store.impl.Timestamped; |
| 62 | import org.onosproject.store.serializers.KryoSerializer; |
| 63 | import org.onosproject.store.serializers.impl.DistributedStoreSerializers; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 64 | import org.onlab.packet.ChassisId; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 65 | import org.onlab.util.KryoNamespace; |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 66 | import org.onlab.util.NewConcurrentHashMap; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 67 | import org.slf4j.Logger; |
| 68 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 69 | import java.io.IOException; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 70 | import java.util.ArrayList; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 71 | import java.util.Collection; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 72 | import java.util.Collections; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 73 | import java.util.HashMap; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 74 | import java.util.HashSet; |
| 75 | import java.util.Iterator; |
| 76 | import java.util.List; |
| 77 | import java.util.Map; |
| 78 | import java.util.Map.Entry; |
| 79 | import java.util.Objects; |
| 80 | import java.util.Set; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 81 | import java.util.concurrent.ConcurrentMap; |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 82 | import java.util.concurrent.ExecutorService; |
| 83 | import java.util.concurrent.Executors; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 84 | import java.util.concurrent.ScheduledExecutorService; |
| 85 | import java.util.concurrent.TimeUnit; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 86 | |
| 87 | import static com.google.common.base.Preconditions.checkArgument; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 88 | import static com.google.common.base.Predicates.notNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 89 | import static org.onosproject.cluster.ControllerNodeToNodeId.toNodeId; |
| 90 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 91 | import static org.slf4j.LoggerFactory.getLogger; |
| 92 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 93 | import static org.onosproject.net.DefaultAnnotations.merge; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 94 | import static com.google.common.base.Verify.verify; |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 95 | import static org.onlab.util.Tools.minPriority; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 96 | import static org.onlab.util.Tools.namedThreads; |
| 97 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 98 | import static org.onosproject.store.device.impl.GossipDeviceStoreMessageSubjects.DEVICE_ADVERTISE; |
| 99 | import static org.onosproject.store.device.impl.GossipDeviceStoreMessageSubjects.DEVICE_REMOVE_REQ; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 100 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Manages inventory of infrastructure devices using gossip protocol to distribute |
| 103 | * information. |
| 104 | */ |
| 105 | @Component(immediate = true) |
| 106 | @Service |
| 107 | public class GossipDeviceStore |
| 108 | extends AbstractStore<DeviceEvent, DeviceStoreDelegate> |
| 109 | implements DeviceStore { |
| 110 | |
| 111 | private final Logger log = getLogger(getClass()); |
| 112 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 113 | private static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 114 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 115 | // innerMap is used to lock a Device, thus instance should never be replaced. |
| 116 | // collection of Description given from various providers |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 117 | private final ConcurrentMap<DeviceId, Map<ProviderId, DeviceDescriptions>> |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 118 | deviceDescs = Maps.newConcurrentMap(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 119 | |
| 120 | // cache of Device and Ports generated by compositing descriptions from providers |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 121 | private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap(); |
| 122 | private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>> devicePorts = Maps.newConcurrentMap(); |
| 123 | |
| 124 | // to be updated under Device lock |
| 125 | private final Map<DeviceId, Timestamp> offline = Maps.newHashMap(); |
| 126 | private final Map<DeviceId, Timestamp> removalRequest = Maps.newHashMap(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 127 | |
| 128 | // available(=UP) devices |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 129 | private final Set<DeviceId> availableDevices = Sets.newConcurrentHashSet(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 130 | |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 132 | protected DeviceClockService deviceClockService; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 133 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 135 | protected ClusterCommunicationService clusterCommunicator; |
| 136 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 138 | protected ClusterService clusterService; |
| 139 | |
Yuta HIGUCHI | e8252bb | 2014-10-22 09:41:01 -0700 | [diff] [blame] | 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 141 | protected MastershipService mastershipService; |
| 142 | |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 143 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 144 | protected MastershipTermService termService; |
| 145 | |
| 146 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 147 | protected static final KryoSerializer SERIALIZER = new KryoSerializer() { |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 148 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 149 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 150 | serializerPool = KryoNamespace.newBuilder() |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 151 | .register(DistributedStoreSerializers.STORE_COMMON) |
| 152 | .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) |
| 153 | .register(new InternalDeviceEventSerializer(), InternalDeviceEvent.class) |
| 154 | .register(new InternalDeviceOfflineEventSerializer(), InternalDeviceOfflineEvent.class) |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 155 | .register(InternalDeviceRemovedEvent.class) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 156 | .register(new InternalPortEventSerializer(), InternalPortEvent.class) |
| 157 | .register(new InternalPortStatusEventSerializer(), InternalPortStatusEvent.class) |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 158 | .register(DeviceAntiEntropyAdvertisement.class) |
| 159 | .register(DeviceFragmentId.class) |
| 160 | .register(PortFragmentId.class) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 161 | .build(); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 162 | } |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 165 | private ExecutorService executor; |
| 166 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 167 | private ScheduledExecutorService backgroundExecutor; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 168 | |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame^] | 169 | // TODO make these anti-entropy parameters configurable |
| 170 | private long initialDelaySec = 5; |
| 171 | private long periodSec = 5; |
| 172 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 173 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 174 | @Activate |
| 175 | public void activate() { |
Madan Jampani | 2206e01 | 2014-10-06 21:04:20 -0700 | [diff] [blame] | 176 | clusterCommunicator.addSubscriber( |
| 177 | GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, new InternalDeviceEventListener()); |
| 178 | clusterCommunicator.addSubscriber( |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 179 | GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, new InternalDeviceOfflineEventListener()); |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 180 | clusterCommunicator.addSubscriber(DEVICE_REMOVE_REQ, new InternalRemoveRequestListener()); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 181 | clusterCommunicator.addSubscriber( |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 182 | GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, new InternalDeviceRemovedEventListener()); |
| 183 | clusterCommunicator.addSubscriber( |
Madan Jampani | 2206e01 | 2014-10-06 21:04:20 -0700 | [diff] [blame] | 184 | GossipDeviceStoreMessageSubjects.PORT_UPDATE, new InternalPortEventListener()); |
| 185 | clusterCommunicator.addSubscriber( |
| 186 | GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, new InternalPortStatusEventListener()); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 187 | clusterCommunicator.addSubscriber( |
| 188 | GossipDeviceStoreMessageSubjects.DEVICE_ADVERTISE, new InternalDeviceAdvertisementListener()); |
| 189 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 190 | executor = Executors.newCachedThreadPool(namedThreads("device-fg-%d")); |
| 191 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 192 | backgroundExecutor = |
| 193 | newSingleThreadScheduledExecutor(minPriority(namedThreads("device-bg-%d"))); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 194 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 195 | // start anti-entropy thread |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 196 | backgroundExecutor.scheduleAtFixedRate(new SendAdvertisementTask(), |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 197 | initialDelaySec, periodSec, TimeUnit.SECONDS); |
| 198 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 199 | log.info("Started"); |
| 200 | } |
| 201 | |
| 202 | @Deactivate |
| 203 | public void deactivate() { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 204 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 205 | executor.shutdownNow(); |
| 206 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 207 | backgroundExecutor.shutdownNow(); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 208 | try { |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 209 | boolean timedout = backgroundExecutor.awaitTermination(5, TimeUnit.SECONDS); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 210 | if (timedout) { |
| 211 | log.error("Timeout during executor shutdown"); |
| 212 | } |
| 213 | } catch (InterruptedException e) { |
| 214 | log.error("Error during executor shutdown", e); |
| 215 | } |
| 216 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 217 | deviceDescs.clear(); |
| 218 | devices.clear(); |
| 219 | devicePorts.clear(); |
| 220 | availableDevices.clear(); |
| 221 | log.info("Stopped"); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public int getDeviceCount() { |
| 226 | return devices.size(); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public Iterable<Device> getDevices() { |
| 231 | return Collections.unmodifiableCollection(devices.values()); |
| 232 | } |
| 233 | |
| 234 | @Override |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 235 | public Iterable<Device> getAvailableDevices() { |
| 236 | return FluentIterable.from(getDevices()) |
| 237 | .filter(new Predicate<Device>() { |
| 238 | |
| 239 | @Override |
| 240 | public boolean apply(Device input) { |
| 241 | return isAvailable(input.id()); |
| 242 | } |
| 243 | }); |
| 244 | } |
| 245 | |
| 246 | @Override |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 247 | public Device getDevice(DeviceId deviceId) { |
| 248 | return devices.get(deviceId); |
| 249 | } |
| 250 | |
| 251 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 252 | public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, |
| 253 | DeviceId deviceId, |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 254 | DeviceDescription deviceDescription) { |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 255 | final Timestamp newTimestamp = deviceClockService.getTimestamp(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 256 | final Timestamped<DeviceDescription> deltaDesc = new Timestamped<>(deviceDescription, newTimestamp); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 257 | final DeviceEvent event; |
| 258 | final Timestamped<DeviceDescription> mergedDesc; |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 259 | final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); |
| 260 | synchronized (device) { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 261 | event = createOrUpdateDeviceInternal(providerId, deviceId, deltaDesc); |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 262 | mergedDesc = device.get(providerId).getDeviceDesc(); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 263 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 264 | if (event != null) { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 265 | log.info("Notifying peers of a device update topology event for providerId: {} and deviceId: {}", |
| 266 | providerId, deviceId); |
| 267 | try { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 268 | notifyPeers(new InternalDeviceEvent(providerId, deviceId, mergedDesc)); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 269 | } catch (IOException e) { |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 270 | log.error("Failed to notify peers of a device update topology event for providerId: " |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 271 | + providerId + " and deviceId: " + deviceId, e); |
| 272 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 273 | } |
| 274 | return event; |
| 275 | } |
| 276 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 277 | private DeviceEvent createOrUpdateDeviceInternal(ProviderId providerId, |
| 278 | DeviceId deviceId, |
| 279 | Timestamped<DeviceDescription> deltaDesc) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 280 | |
| 281 | // Collection of DeviceDescriptions for a Device |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 282 | Map<ProviderId, DeviceDescriptions> device |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 283 | = getOrCreateDeviceDescriptionsMap(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 284 | |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 285 | synchronized (device) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 286 | // locking per device |
| 287 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 288 | if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) { |
| 289 | log.debug("Ignoring outdated event: {}", deltaDesc); |
| 290 | return null; |
| 291 | } |
| 292 | |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 293 | DeviceDescriptions descs = getOrCreateProviderDeviceDescriptions(device, providerId, deltaDesc); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 294 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 295 | final Device oldDevice = devices.get(deviceId); |
| 296 | final Device newDevice; |
| 297 | |
| 298 | if (deltaDesc == descs.getDeviceDesc() || |
| 299 | deltaDesc.isNewer(descs.getDeviceDesc())) { |
| 300 | // on new device or valid update |
| 301 | descs.putDeviceDesc(deltaDesc); |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 302 | newDevice = composeDevice(deviceId, device); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 303 | } else { |
| 304 | // outdated event, ignored. |
| 305 | return null; |
| 306 | } |
| 307 | if (oldDevice == null) { |
| 308 | // ADD |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 309 | return createDevice(providerId, newDevice, deltaDesc.timestamp()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 310 | } else { |
| 311 | // UPDATE or ignore (no change or stale) |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 312 | return updateDevice(providerId, oldDevice, newDevice, deltaDesc.timestamp()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Creates the device and returns the appropriate event if necessary. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 318 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 319 | private DeviceEvent createDevice(ProviderId providerId, |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 320 | Device newDevice, Timestamp timestamp) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 321 | |
| 322 | // update composed device cache |
| 323 | Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice); |
| 324 | verify(oldDevice == null, |
| 325 | "Unexpected Device in cache. PID:%s [old=%s, new=%s]", |
| 326 | providerId, oldDevice, newDevice); |
| 327 | |
| 328 | if (!providerId.isAncillary()) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 329 | markOnline(newDevice.id(), timestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null); |
| 333 | } |
| 334 | |
| 335 | // Updates the device and returns the appropriate event if necessary. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 336 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 337 | private DeviceEvent updateDevice(ProviderId providerId, |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 338 | Device oldDevice, |
| 339 | Device newDevice, Timestamp newTimestamp) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 340 | // We allow only certain attributes to trigger update |
Thomas Vachuska | 56dbeb1 | 2014-10-22 16:40:44 -0700 | [diff] [blame] | 341 | boolean propertiesChanged = |
| 342 | !Objects.equals(oldDevice.hwVersion(), newDevice.hwVersion()) || |
| 343 | !Objects.equals(oldDevice.swVersion(), newDevice.swVersion()); |
| 344 | boolean annotationsChanged = |
| 345 | !AnnotationsUtil.isEqual(oldDevice.annotations(), newDevice.annotations()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 346 | |
Thomas Vachuska | 56dbeb1 | 2014-10-22 16:40:44 -0700 | [diff] [blame] | 347 | // Primary providers can respond to all changes, but ancillary ones |
| 348 | // should respond only to annotation changes. |
| 349 | if ((providerId.isAncillary() && annotationsChanged) || |
| 350 | (!providerId.isAncillary() && (propertiesChanged || annotationsChanged))) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 351 | boolean replaced = devices.replace(newDevice.id(), oldDevice, newDevice); |
| 352 | if (!replaced) { |
| 353 | verify(replaced, |
| 354 | "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]", |
| 355 | providerId, oldDevice, devices.get(newDevice.id()) |
| 356 | , newDevice); |
| 357 | } |
| 358 | if (!providerId.isAncillary()) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 359 | markOnline(newDevice.id(), newTimestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 360 | } |
| 361 | return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null); |
| 362 | } |
| 363 | |
| 364 | // Otherwise merely attempt to change availability if primary provider |
| 365 | if (!providerId.isAncillary()) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 366 | boolean added = markOnline(newDevice.id(), newTimestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 367 | return !added ? null : |
| 368 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null); |
| 369 | } |
| 370 | return null; |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public DeviceEvent markOffline(DeviceId deviceId) { |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 375 | final Timestamp timestamp = deviceClockService.getTimestamp(deviceId); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 376 | final DeviceEvent event = markOfflineInternal(deviceId, timestamp); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 377 | if (event != null) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 378 | log.info("Notifying peers of a device offline topology event for deviceId: {} {}", |
| 379 | deviceId, timestamp); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 380 | try { |
| 381 | notifyPeers(new InternalDeviceOfflineEvent(deviceId, timestamp)); |
| 382 | } catch (IOException e) { |
| 383 | log.error("Failed to notify peers of a device offline topology event for deviceId: {}", |
| 384 | deviceId); |
| 385 | } |
| 386 | } |
| 387 | return event; |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | private DeviceEvent markOfflineInternal(DeviceId deviceId, Timestamp timestamp) { |
| 391 | |
| 392 | Map<ProviderId, DeviceDescriptions> providerDescs |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 393 | = getOrCreateDeviceDescriptionsMap(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 394 | |
| 395 | // locking device |
| 396 | synchronized (providerDescs) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 397 | |
| 398 | // accept off-line if given timestamp is newer than |
| 399 | // the latest Timestamp from Primary provider |
| 400 | DeviceDescriptions primDescs = getPrimaryDescriptions(providerDescs); |
| 401 | Timestamp lastTimestamp = primDescs.getLatestTimestamp(); |
| 402 | if (timestamp.compareTo(lastTimestamp) <= 0) { |
| 403 | // outdated event ignore |
| 404 | return null; |
| 405 | } |
| 406 | |
| 407 | offline.put(deviceId, timestamp); |
| 408 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 409 | Device device = devices.get(deviceId); |
| 410 | if (device == null) { |
| 411 | return null; |
| 412 | } |
| 413 | boolean removed = availableDevices.remove(deviceId); |
| 414 | if (removed) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 415 | return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 416 | } |
| 417 | return null; |
| 418 | } |
| 419 | } |
| 420 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 421 | /** |
| 422 | * Marks the device as available if the given timestamp is not outdated, |
| 423 | * compared to the time the device has been marked offline. |
| 424 | * |
| 425 | * @param deviceId identifier of the device |
| 426 | * @param timestamp of the event triggering this change. |
| 427 | * @return true if availability change request was accepted and changed the state |
| 428 | */ |
| 429 | // Guarded by deviceDescs value (=Device lock) |
| 430 | private boolean markOnline(DeviceId deviceId, Timestamp timestamp) { |
| 431 | // accept on-line if given timestamp is newer than |
| 432 | // the latest offline request Timestamp |
| 433 | Timestamp offlineTimestamp = offline.get(deviceId); |
| 434 | if (offlineTimestamp == null || |
| 435 | offlineTimestamp.compareTo(timestamp) < 0) { |
| 436 | |
| 437 | offline.remove(deviceId); |
| 438 | return availableDevices.add(deviceId); |
| 439 | } |
| 440 | return false; |
| 441 | } |
| 442 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 443 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 444 | public synchronized List<DeviceEvent> updatePorts(ProviderId providerId, |
| 445 | DeviceId deviceId, |
| 446 | List<PortDescription> portDescriptions) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 447 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 448 | final Timestamp newTimestamp; |
| 449 | try { |
| 450 | newTimestamp = deviceClockService.getTimestamp(deviceId); |
| 451 | } catch (IllegalStateException e) { |
| 452 | log.info("Timestamp was not available for device {}", deviceId); |
| 453 | log.debug(" discarding {}", portDescriptions); |
| 454 | // Failed to generate timestamp. |
| 455 | |
| 456 | // Possible situation: |
| 457 | // Device connected and became master for short period of time, |
| 458 | // but lost mastership before this instance had the chance to |
| 459 | // retrieve term information. |
| 460 | |
| 461 | // Information dropped here is expected to be recoverable by |
| 462 | // device probing after mastership change |
| 463 | |
| 464 | return Collections.emptyList(); |
| 465 | } |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 466 | log.debug("timestamp for {} {}", deviceId, newTimestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 467 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 468 | final Timestamped<List<PortDescription>> timestampedInput |
| 469 | = new Timestamped<>(portDescriptions, newTimestamp); |
| 470 | final List<DeviceEvent> events; |
| 471 | final Timestamped<List<PortDescription>> merged; |
| 472 | |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 473 | final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); |
| 474 | synchronized (device) { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 475 | events = updatePortsInternal(providerId, deviceId, timestampedInput); |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 476 | final DeviceDescriptions descs = device.get(providerId); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 477 | List<PortDescription> mergedList = |
| 478 | FluentIterable.from(portDescriptions) |
| 479 | .transform(new Function<PortDescription, PortDescription>() { |
| 480 | @Override |
| 481 | public PortDescription apply(PortDescription input) { |
| 482 | // lookup merged port description |
| 483 | return descs.getPortDesc(input.portNumber()).value(); |
| 484 | } |
| 485 | }).toList(); |
| 486 | merged = new Timestamped<List<PortDescription>>(mergedList, newTimestamp); |
| 487 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 488 | if (!events.isEmpty()) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 489 | log.info("Notifying peers of a ports update topology event for providerId: {} and deviceId: {}", |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 490 | providerId, deviceId); |
| 491 | try { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 492 | notifyPeers(new InternalPortEvent(providerId, deviceId, merged)); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 493 | } catch (IOException e) { |
| 494 | log.error("Failed to notify peers of a port update topology event or providerId: " |
| 495 | + providerId + " and deviceId: " + deviceId, e); |
| 496 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 497 | } |
| 498 | return events; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 501 | private List<DeviceEvent> updatePortsInternal(ProviderId providerId, |
| 502 | DeviceId deviceId, |
| 503 | Timestamped<List<PortDescription>> portDescriptions) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 504 | |
| 505 | Device device = devices.get(deviceId); |
| 506 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 507 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 508 | Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 509 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 510 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 511 | List<DeviceEvent> events = new ArrayList<>(); |
| 512 | synchronized (descsMap) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 513 | |
| 514 | if (isDeviceRemoved(deviceId, portDescriptions.timestamp())) { |
| 515 | log.debug("Ignoring outdated events: {}", portDescriptions); |
| 516 | return null; |
| 517 | } |
| 518 | |
| 519 | DeviceDescriptions descs = descsMap.get(providerId); |
| 520 | // every provider must provide DeviceDescription. |
| 521 | checkArgument(descs != null, |
| 522 | "Device description for Device ID %s from Provider %s was not found", |
| 523 | deviceId, providerId); |
| 524 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 525 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 526 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 527 | final Timestamp newTimestamp = portDescriptions.timestamp(); |
| 528 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 529 | // Add new ports |
| 530 | Set<PortNumber> processed = new HashSet<>(); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 531 | for (PortDescription portDescription : portDescriptions.value()) { |
| 532 | final PortNumber number = portDescription.portNumber(); |
| 533 | processed.add(number); |
| 534 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 535 | final Port oldPort = ports.get(number); |
| 536 | final Port newPort; |
| 537 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 538 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 539 | final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number); |
| 540 | if (existingPortDesc == null || |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 541 | newTimestamp.compareTo(existingPortDesc.timestamp()) >= 0) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 542 | // on new port or valid update |
| 543 | // update description |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 544 | descs.putPortDesc(new Timestamped<>(portDescription, |
| 545 | portDescriptions.timestamp())); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 546 | newPort = composePort(device, number, descsMap); |
| 547 | } else { |
| 548 | // outdated event, ignored. |
| 549 | continue; |
| 550 | } |
| 551 | |
| 552 | events.add(oldPort == null ? |
| 553 | createPort(device, newPort, ports) : |
| 554 | updatePort(device, oldPort, newPort, ports)); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | events.addAll(pruneOldPorts(device, ports, processed)); |
| 558 | } |
| 559 | return FluentIterable.from(events).filter(notNull()).toList(); |
| 560 | } |
| 561 | |
| 562 | // Creates a new port based on the port description adds it to the map and |
| 563 | // Returns corresponding event. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 564 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 565 | private DeviceEvent createPort(Device device, Port newPort, |
| 566 | Map<PortNumber, Port> ports) { |
| 567 | ports.put(newPort.number(), newPort); |
| 568 | return new DeviceEvent(PORT_ADDED, device, newPort); |
| 569 | } |
| 570 | |
| 571 | // Checks if the specified port requires update and if so, it replaces the |
| 572 | // existing entry in the map and returns corresponding event. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 573 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 574 | private DeviceEvent updatePort(Device device, Port oldPort, |
| 575 | Port newPort, |
| 576 | Map<PortNumber, Port> ports) { |
| 577 | if (oldPort.isEnabled() != newPort.isEnabled() || |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 578 | oldPort.type() != newPort.type() || |
| 579 | oldPort.portSpeed() != newPort.portSpeed() || |
| 580 | !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 581 | ports.put(oldPort.number(), newPort); |
| 582 | return new DeviceEvent(PORT_UPDATED, device, newPort); |
| 583 | } |
| 584 | return null; |
| 585 | } |
| 586 | |
| 587 | // Prunes the specified list of ports based on which ports are in the |
| 588 | // processed list and returns list of corresponding events. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 589 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 590 | private List<DeviceEvent> pruneOldPorts(Device device, |
| 591 | Map<PortNumber, Port> ports, |
| 592 | Set<PortNumber> processed) { |
| 593 | List<DeviceEvent> events = new ArrayList<>(); |
Yuta HIGUCHI | 0264907 | 2014-10-15 23:28:20 -0700 | [diff] [blame] | 594 | Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 595 | while (iterator.hasNext()) { |
Yuta HIGUCHI | 0264907 | 2014-10-15 23:28:20 -0700 | [diff] [blame] | 596 | Entry<PortNumber, Port> e = iterator.next(); |
| 597 | PortNumber portNumber = e.getKey(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 598 | if (!processed.contains(portNumber)) { |
Yuta HIGUCHI | 0264907 | 2014-10-15 23:28:20 -0700 | [diff] [blame] | 599 | events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue())); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 600 | iterator.remove(); |
| 601 | } |
| 602 | } |
| 603 | return events; |
| 604 | } |
| 605 | |
| 606 | // Gets the map of ports for the specified device; if one does not already |
| 607 | // exist, it creates and registers a new one. |
| 608 | private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) { |
| 609 | return createIfAbsentUnchecked(devicePorts, deviceId, |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 610 | NewConcurrentHashMap.<PortNumber, Port>ifNeeded()); |
| 611 | } |
| 612 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 613 | private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptionsMap( |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 614 | DeviceId deviceId) { |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 615 | Map<ProviderId, DeviceDescriptions> r; |
| 616 | r = deviceDescs.get(deviceId); |
| 617 | if (r == null) { |
| 618 | r = new HashMap<ProviderId, DeviceDescriptions>(); |
| 619 | final Map<ProviderId, DeviceDescriptions> concurrentlyAdded; |
| 620 | concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r); |
| 621 | if (concurrentlyAdded != null) { |
| 622 | r = concurrentlyAdded; |
| 623 | } |
| 624 | } |
| 625 | return r; |
| 626 | } |
| 627 | |
| 628 | // Guarded by deviceDescs value (=Device lock) |
| 629 | private DeviceDescriptions getOrCreateProviderDeviceDescriptions( |
| 630 | Map<ProviderId, DeviceDescriptions> device, |
| 631 | ProviderId providerId, Timestamped<DeviceDescription> deltaDesc) { |
| 632 | |
| 633 | synchronized (device) { |
| 634 | DeviceDescriptions r = device.get(providerId); |
| 635 | if (r == null) { |
| 636 | r = new DeviceDescriptions(deltaDesc); |
| 637 | device.put(providerId, r); |
| 638 | } |
| 639 | return r; |
| 640 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | @Override |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 644 | public synchronized DeviceEvent updatePortStatus(ProviderId providerId, |
| 645 | DeviceId deviceId, |
| 646 | PortDescription portDescription) { |
| 647 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 648 | final Timestamp newTimestamp; |
| 649 | try { |
| 650 | newTimestamp = deviceClockService.getTimestamp(deviceId); |
| 651 | } catch (IllegalStateException e) { |
| 652 | log.info("Timestamp was not available for device {}", deviceId); |
| 653 | log.debug(" discarding {}", portDescription); |
| 654 | // Failed to generate timestamp. Ignoring. |
| 655 | // See updatePorts comment |
| 656 | return null; |
| 657 | } |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 658 | final Timestamped<PortDescription> deltaDesc |
| 659 | = new Timestamped<>(portDescription, newTimestamp); |
| 660 | final DeviceEvent event; |
| 661 | final Timestamped<PortDescription> mergedDesc; |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 662 | final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); |
| 663 | synchronized (device) { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 664 | event = updatePortStatusInternal(providerId, deviceId, deltaDesc); |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 665 | mergedDesc = device.get(providerId) |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 666 | .getPortDesc(portDescription.portNumber()); |
| 667 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 668 | if (event != null) { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 669 | log.info("Notifying peers of a port status update topology event for providerId: {} and deviceId: {}", |
| 670 | providerId, deviceId); |
| 671 | try { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 672 | notifyPeers(new InternalPortStatusEvent(providerId, deviceId, mergedDesc)); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 673 | } catch (IOException e) { |
| 674 | log.error("Failed to notify peers of a port status update topology event or providerId: " |
| 675 | + providerId + " and deviceId: " + deviceId, e); |
| 676 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 677 | } |
| 678 | return event; |
| 679 | } |
| 680 | |
| 681 | private DeviceEvent updatePortStatusInternal(ProviderId providerId, DeviceId deviceId, |
| 682 | Timestamped<PortDescription> deltaDesc) { |
| 683 | |
| 684 | Device device = devices.get(deviceId); |
| 685 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 686 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 687 | Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 688 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 689 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 690 | synchronized (descsMap) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 691 | |
| 692 | if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) { |
| 693 | log.debug("Ignoring outdated event: {}", deltaDesc); |
| 694 | return null; |
| 695 | } |
| 696 | |
| 697 | DeviceDescriptions descs = descsMap.get(providerId); |
| 698 | // assuming all providers must to give DeviceDescription |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 699 | verify(descs != null, |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 700 | "Device description for Device ID %s from Provider %s was not found", |
| 701 | deviceId, providerId); |
| 702 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 703 | ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId); |
| 704 | final PortNumber number = deltaDesc.value().portNumber(); |
| 705 | final Port oldPort = ports.get(number); |
| 706 | final Port newPort; |
| 707 | |
| 708 | final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number); |
| 709 | if (existingPortDesc == null || |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 710 | deltaDesc.isNewer(existingPortDesc)) { |
| 711 | // on new port or valid update |
| 712 | // update description |
| 713 | descs.putPortDesc(deltaDesc); |
| 714 | newPort = composePort(device, number, descsMap); |
| 715 | } else { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 716 | // same or outdated event, ignored. |
| 717 | log.trace("ignore same or outdated {} >= {}", existingPortDesc, deltaDesc); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 718 | return null; |
| 719 | } |
| 720 | |
| 721 | if (oldPort == null) { |
| 722 | return createPort(device, newPort, ports); |
| 723 | } else { |
| 724 | return updatePort(device, oldPort, newPort, ports); |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | @Override |
| 730 | public List<Port> getPorts(DeviceId deviceId) { |
| 731 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 732 | if (ports == null) { |
| 733 | return Collections.emptyList(); |
| 734 | } |
| 735 | return ImmutableList.copyOf(ports.values()); |
| 736 | } |
| 737 | |
| 738 | @Override |
| 739 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 740 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 741 | return ports == null ? null : ports.get(portNumber); |
| 742 | } |
| 743 | |
| 744 | @Override |
| 745 | public boolean isAvailable(DeviceId deviceId) { |
| 746 | return availableDevices.contains(deviceId); |
| 747 | } |
| 748 | |
| 749 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 750 | public synchronized DeviceEvent removeDevice(DeviceId deviceId) { |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 751 | final NodeId myId = clusterService.getLocalNode().id(); |
| 752 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 753 | |
| 754 | // if there exist a master, forward |
| 755 | // if there is no master, try to become one and process |
| 756 | |
| 757 | boolean relinquishAtEnd = false; |
| 758 | if (master == null) { |
| 759 | final MastershipRole myRole = mastershipService.getLocalRole(deviceId); |
| 760 | if (myRole != MastershipRole.NONE) { |
| 761 | relinquishAtEnd = true; |
| 762 | } |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 763 | log.debug("Temporarily requesting role for {} to remove", deviceId); |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 764 | mastershipService.requestRoleFor(deviceId); |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 765 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
| 766 | if (myId.equals(term.master())) { |
| 767 | master = myId; |
| 768 | } |
Yuta HIGUCHI | e8252bb | 2014-10-22 09:41:01 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 771 | if (!myId.equals(master)) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 772 | log.debug("{} has control of {}, forwarding remove request", |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 773 | master, deviceId); |
| 774 | |
| 775 | ClusterMessage message = new ClusterMessage( |
| 776 | myId, |
| 777 | DEVICE_REMOVE_REQ, |
| 778 | SERIALIZER.encode(deviceId)); |
| 779 | |
| 780 | try { |
| 781 | clusterCommunicator.unicast(message, master); |
| 782 | } catch (IOException e) { |
| 783 | log.error("Failed to forward {} remove request to {}", deviceId, master, e); |
| 784 | } |
| 785 | |
| 786 | // event will be triggered after master processes it. |
| 787 | return null; |
| 788 | } |
| 789 | |
| 790 | // I have control.. |
| 791 | |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 792 | Timestamp timestamp = deviceClockService.getTimestamp(deviceId); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 793 | DeviceEvent event = removeDeviceInternal(deviceId, timestamp); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 794 | if (event != null) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 795 | log.debug("Notifying peers of a device removed topology event for deviceId: {}", |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 796 | deviceId); |
| 797 | try { |
| 798 | notifyPeers(new InternalDeviceRemovedEvent(deviceId, timestamp)); |
| 799 | } catch (IOException e) { |
| 800 | log.error("Failed to notify peers of a device removed topology event for deviceId: {}", |
| 801 | deviceId); |
| 802 | } |
| 803 | } |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 804 | if (relinquishAtEnd) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 805 | log.debug("Relinquishing temporary role acquired for {}", deviceId); |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 806 | mastershipService.relinquishMastership(deviceId); |
| 807 | } |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 808 | return event; |
| 809 | } |
| 810 | |
| 811 | private DeviceEvent removeDeviceInternal(DeviceId deviceId, |
| 812 | Timestamp timestamp) { |
| 813 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 814 | Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptionsMap(deviceId); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 815 | synchronized (descs) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 816 | // accept removal request if given timestamp is newer than |
| 817 | // the latest Timestamp from Primary provider |
| 818 | DeviceDescriptions primDescs = getPrimaryDescriptions(descs); |
| 819 | Timestamp lastTimestamp = primDescs.getLatestTimestamp(); |
| 820 | if (timestamp.compareTo(lastTimestamp) <= 0) { |
| 821 | // outdated event ignore |
| 822 | return null; |
| 823 | } |
| 824 | removalRequest.put(deviceId, timestamp); |
| 825 | |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 826 | Device device = devices.remove(deviceId); |
| 827 | // should DEVICE_REMOVED carry removed ports? |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 828 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 829 | if (ports != null) { |
| 830 | ports.clear(); |
| 831 | } |
| 832 | markOfflineInternal(deviceId, timestamp); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 833 | descs.clear(); |
| 834 | return device == null ? null : |
| 835 | new DeviceEvent(DEVICE_REMOVED, device, null); |
| 836 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 839 | /** |
| 840 | * Checks if given timestamp is superseded by removal request |
| 841 | * with more recent timestamp. |
| 842 | * |
| 843 | * @param deviceId identifier of a device |
| 844 | * @param timestampToCheck timestamp of an event to check |
| 845 | * @return true if device is already removed |
| 846 | */ |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 847 | private boolean isDeviceRemoved(DeviceId deviceId, Timestamp timestampToCheck) { |
| 848 | Timestamp removalTimestamp = removalRequest.get(deviceId); |
| 849 | if (removalTimestamp != null && |
| 850 | removalTimestamp.compareTo(timestampToCheck) >= 0) { |
| 851 | // removalRequest is more recent |
| 852 | return true; |
| 853 | } |
| 854 | return false; |
| 855 | } |
| 856 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 857 | /** |
| 858 | * Returns a Device, merging description given from multiple Providers. |
| 859 | * |
| 860 | * @param deviceId device identifier |
| 861 | * @param providerDescs Collection of Descriptions from multiple providers |
| 862 | * @return Device instance |
| 863 | */ |
| 864 | private Device composeDevice(DeviceId deviceId, |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 865 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 866 | |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 867 | checkArgument(!providerDescs.isEmpty(), "No device descriptions supplied"); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 868 | |
| 869 | ProviderId primary = pickPrimaryPID(providerDescs); |
| 870 | |
| 871 | DeviceDescriptions desc = providerDescs.get(primary); |
| 872 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 873 | final DeviceDescription base = desc.getDeviceDesc().value(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 874 | Type type = base.type(); |
| 875 | String manufacturer = base.manufacturer(); |
| 876 | String hwVersion = base.hwVersion(); |
| 877 | String swVersion = base.swVersion(); |
| 878 | String serialNumber = base.serialNumber(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 879 | ChassisId chassisId = base.chassisId(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 880 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 881 | annotations = merge(annotations, base.annotations()); |
| 882 | |
| 883 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 884 | if (e.getKey().equals(primary)) { |
| 885 | continue; |
| 886 | } |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame^] | 887 | // Note: should keep track of Description timestamp in the future |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 888 | // and only merge conflicting keys when timestamp is newer. |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 889 | // Currently assuming there will never be a key conflict between |
| 890 | // providers |
| 891 | |
| 892 | // annotation merging. not so efficient, should revisit later |
| 893 | annotations = merge(annotations, e.getValue().getDeviceDesc().value().annotations()); |
| 894 | } |
| 895 | |
| 896 | return new DefaultDevice(primary, deviceId , type, manufacturer, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 897 | hwVersion, swVersion, serialNumber, |
| 898 | chassisId, annotations); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /** |
| 902 | * Returns a Port, merging description given from multiple Providers. |
| 903 | * |
| 904 | * @param device device the port is on |
| 905 | * @param number port number |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 906 | * @param descsMap Collection of Descriptions from multiple providers |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 907 | * @return Port instance |
| 908 | */ |
| 909 | private Port composePort(Device device, PortNumber number, |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 910 | Map<ProviderId, DeviceDescriptions> descsMap) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 911 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 912 | ProviderId primary = pickPrimaryPID(descsMap); |
| 913 | DeviceDescriptions primDescs = descsMap.get(primary); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 914 | // if no primary, assume not enabled |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 915 | boolean isEnabled = false; |
| 916 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 917 | |
| 918 | final Timestamped<PortDescription> portDesc = primDescs.getPortDesc(number); |
| 919 | if (portDesc != null) { |
| 920 | isEnabled = portDesc.value().isEnabled(); |
| 921 | annotations = merge(annotations, portDesc.value().annotations()); |
| 922 | } |
| 923 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 924 | for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 925 | if (e.getKey().equals(primary)) { |
| 926 | continue; |
| 927 | } |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame^] | 928 | // Note: should keep track of Description timestamp in the future |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 929 | // and only merge conflicting keys when timestamp is newer. |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 930 | // Currently assuming there will never be a key conflict between |
| 931 | // providers |
| 932 | |
| 933 | // annotation merging. not so efficient, should revisit later |
| 934 | final Timestamped<PortDescription> otherPortDesc = e.getValue().getPortDesc(number); |
| 935 | if (otherPortDesc != null) { |
| 936 | annotations = merge(annotations, otherPortDesc.value().annotations()); |
| 937 | } |
| 938 | } |
| 939 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 940 | return portDesc == null ? |
| 941 | new DefaultPort(device, number, false, annotations) : |
| 942 | new DefaultPort(device, number, isEnabled, portDesc.value().type(), |
| 943 | portDesc.value().portSpeed(), annotations); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /** |
| 947 | * @return primary ProviderID, or randomly chosen one if none exists |
| 948 | */ |
| 949 | private ProviderId pickPrimaryPID( |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 950 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 951 | ProviderId fallBackPrimary = null; |
| 952 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 953 | if (!e.getKey().isAncillary()) { |
| 954 | return e.getKey(); |
| 955 | } else if (fallBackPrimary == null) { |
| 956 | // pick randomly as a fallback in case there is no primary |
| 957 | fallBackPrimary = e.getKey(); |
| 958 | } |
| 959 | } |
| 960 | return fallBackPrimary; |
| 961 | } |
| 962 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 963 | private DeviceDescriptions getPrimaryDescriptions( |
| 964 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
| 965 | ProviderId pid = pickPrimaryPID(providerDescs); |
| 966 | return providerDescs.get(pid); |
| 967 | } |
| 968 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 969 | private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException { |
| 970 | ClusterMessage message = new ClusterMessage( |
| 971 | clusterService.getLocalNode().id(), |
| 972 | subject, |
| 973 | SERIALIZER.encode(event)); |
| 974 | clusterCommunicator.unicast(message, recipient); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 977 | private void broadcastMessage(MessageSubject subject, Object event) throws IOException { |
| 978 | ClusterMessage message = new ClusterMessage( |
| 979 | clusterService.getLocalNode().id(), |
| 980 | subject, |
| 981 | SERIALIZER.encode(event)); |
| 982 | clusterCommunicator.broadcast(message); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 983 | } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 984 | |
| 985 | private void notifyPeers(InternalDeviceEvent event) throws IOException { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 986 | broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, event); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 989 | private void notifyPeers(InternalDeviceOfflineEvent event) throws IOException { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 990 | broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, event); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 993 | private void notifyPeers(InternalDeviceRemovedEvent event) throws IOException { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 994 | broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, event); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 997 | private void notifyPeers(InternalPortEvent event) throws IOException { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 998 | broadcastMessage(GossipDeviceStoreMessageSubjects.PORT_UPDATE, event); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | private void notifyPeers(InternalPortStatusEvent event) throws IOException { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1002 | broadcastMessage(GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event); |
| 1003 | } |
| 1004 | |
| 1005 | private void notifyPeer(NodeId recipient, InternalDeviceEvent event) { |
| 1006 | try { |
| 1007 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, event); |
| 1008 | } catch (IOException e) { |
| 1009 | log.error("Failed to send" + event + " to " + recipient, e); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | private void notifyPeer(NodeId recipient, InternalDeviceOfflineEvent event) { |
| 1014 | try { |
| 1015 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, event); |
| 1016 | } catch (IOException e) { |
| 1017 | log.error("Failed to send" + event + " to " + recipient, e); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | private void notifyPeer(NodeId recipient, InternalDeviceRemovedEvent event) { |
| 1022 | try { |
| 1023 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, event); |
| 1024 | } catch (IOException e) { |
| 1025 | log.error("Failed to send" + event + " to " + recipient, e); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | private void notifyPeer(NodeId recipient, InternalPortEvent event) { |
| 1030 | try { |
| 1031 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.PORT_UPDATE, event); |
| 1032 | } catch (IOException e) { |
| 1033 | log.error("Failed to send" + event + " to " + recipient, e); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | private void notifyPeer(NodeId recipient, InternalPortStatusEvent event) { |
| 1038 | try { |
| 1039 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event); |
| 1040 | } catch (IOException e) { |
| 1041 | log.error("Failed to send" + event + " to " + recipient, e); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | private DeviceAntiEntropyAdvertisement createAdvertisement() { |
| 1046 | final NodeId self = clusterService.getLocalNode().id(); |
| 1047 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1048 | final int numDevices = deviceDescs.size(); |
| 1049 | Map<DeviceFragmentId, Timestamp> adDevices = new HashMap<>(numDevices); |
| 1050 | final int portsPerDevice = 8; // random factor to minimize reallocation |
| 1051 | Map<PortFragmentId, Timestamp> adPorts = new HashMap<>(numDevices * portsPerDevice); |
| 1052 | Map<DeviceId, Timestamp> adOffline = new HashMap<>(numDevices); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1053 | |
Yuta HIGUCHI | b6cfac3 | 2014-11-25 13:37:27 -0800 | [diff] [blame] | 1054 | deviceDescs.forEach((deviceId, devDescs) -> { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1055 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1056 | // for each Device... |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1057 | synchronized (devDescs) { |
| 1058 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1059 | // send device offline timestamp |
| 1060 | Timestamp lOffline = this.offline.get(deviceId); |
| 1061 | if (lOffline != null) { |
| 1062 | adOffline.put(deviceId, lOffline); |
| 1063 | } |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1064 | |
| 1065 | for (Entry<ProviderId, DeviceDescriptions> |
| 1066 | prov : devDescs.entrySet()) { |
| 1067 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1068 | // for each Provider Descriptions... |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1069 | final ProviderId provId = prov.getKey(); |
| 1070 | final DeviceDescriptions descs = prov.getValue(); |
| 1071 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1072 | adDevices.put(new DeviceFragmentId(deviceId, provId), |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1073 | descs.getDeviceDesc().timestamp()); |
| 1074 | |
| 1075 | for (Entry<PortNumber, Timestamped<PortDescription>> |
| 1076 | portDesc : descs.getPortDescs().entrySet()) { |
| 1077 | |
| 1078 | final PortNumber number = portDesc.getKey(); |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1079 | adPorts.put(new PortFragmentId(deviceId, provId, number), |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1080 | portDesc.getValue().timestamp()); |
| 1081 | } |
| 1082 | } |
| 1083 | } |
Yuta HIGUCHI | b6cfac3 | 2014-11-25 13:37:27 -0800 | [diff] [blame] | 1084 | }); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1085 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1086 | return new DeviceAntiEntropyAdvertisement(self, adDevices, adPorts, adOffline); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /** |
| 1090 | * Responds to anti-entropy advertisement message. |
| 1091 | * <P> |
| 1092 | * Notify sender about out-dated information using regular replication message. |
| 1093 | * Send back advertisement to sender if not in sync. |
| 1094 | * |
| 1095 | * @param advertisement to respond to |
| 1096 | */ |
| 1097 | private void handleAdvertisement(DeviceAntiEntropyAdvertisement advertisement) { |
| 1098 | |
| 1099 | final NodeId sender = advertisement.sender(); |
| 1100 | |
| 1101 | Map<DeviceFragmentId, Timestamp> devAds = new HashMap<>(advertisement.deviceFingerPrints()); |
| 1102 | Map<PortFragmentId, Timestamp> portAds = new HashMap<>(advertisement.ports()); |
| 1103 | Map<DeviceId, Timestamp> offlineAds = new HashMap<>(advertisement.offline()); |
| 1104 | |
| 1105 | // Fragments to request |
| 1106 | Collection<DeviceFragmentId> reqDevices = new ArrayList<>(); |
| 1107 | Collection<PortFragmentId> reqPorts = new ArrayList<>(); |
| 1108 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1109 | for (Entry<DeviceId, Map<ProviderId, DeviceDescriptions>> de : deviceDescs.entrySet()) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1110 | final DeviceId deviceId = de.getKey(); |
| 1111 | final Map<ProviderId, DeviceDescriptions> lDevice = de.getValue(); |
| 1112 | |
| 1113 | synchronized (lDevice) { |
| 1114 | // latestTimestamp across provider |
| 1115 | // Note: can be null initially |
| 1116 | Timestamp localLatest = offline.get(deviceId); |
| 1117 | |
| 1118 | // handle device Ads |
| 1119 | for (Entry<ProviderId, DeviceDescriptions> prov : lDevice.entrySet()) { |
| 1120 | final ProviderId provId = prov.getKey(); |
| 1121 | final DeviceDescriptions lDeviceDescs = prov.getValue(); |
| 1122 | |
| 1123 | final DeviceFragmentId devFragId = new DeviceFragmentId(deviceId, provId); |
| 1124 | |
| 1125 | |
| 1126 | Timestamped<DeviceDescription> lProvDevice = lDeviceDescs.getDeviceDesc(); |
| 1127 | Timestamp advDevTimestamp = devAds.get(devFragId); |
| 1128 | |
| 1129 | if (advDevTimestamp == null || lProvDevice.isNewer(advDevTimestamp)) { |
| 1130 | // remote does not have it or outdated, suggest |
| 1131 | notifyPeer(sender, new InternalDeviceEvent(provId, deviceId, lProvDevice)); |
| 1132 | } else if (!lProvDevice.timestamp().equals(advDevTimestamp)) { |
| 1133 | // local is outdated, request |
| 1134 | reqDevices.add(devFragId); |
| 1135 | } |
| 1136 | |
| 1137 | // handle port Ads |
| 1138 | for (Entry<PortNumber, Timestamped<PortDescription>> |
| 1139 | pe : lDeviceDescs.getPortDescs().entrySet()) { |
| 1140 | |
| 1141 | final PortNumber num = pe.getKey(); |
| 1142 | final Timestamped<PortDescription> lPort = pe.getValue(); |
| 1143 | |
| 1144 | final PortFragmentId portFragId = new PortFragmentId(deviceId, provId, num); |
| 1145 | |
| 1146 | Timestamp advPortTimestamp = portAds.get(portFragId); |
Yuta HIGUCHI | ec76bfe | 2014-10-09 20:17:07 -0700 | [diff] [blame] | 1147 | if (advPortTimestamp == null || lPort.isNewer(advPortTimestamp)) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1148 | // remote does not have it or outdated, suggest |
| 1149 | notifyPeer(sender, new InternalPortStatusEvent(provId, deviceId, lPort)); |
| 1150 | } else if (!lPort.timestamp().equals(advPortTimestamp)) { |
| 1151 | // local is outdated, request |
| 1152 | log.trace("need update {} < {}", lPort.timestamp(), advPortTimestamp); |
| 1153 | reqPorts.add(portFragId); |
| 1154 | } |
| 1155 | |
| 1156 | // remove port Ad already processed |
| 1157 | portAds.remove(portFragId); |
| 1158 | } // end local port loop |
| 1159 | |
| 1160 | // remove device Ad already processed |
| 1161 | devAds.remove(devFragId); |
| 1162 | |
| 1163 | // find latest and update |
| 1164 | final Timestamp providerLatest = lDeviceDescs.getLatestTimestamp(); |
| 1165 | if (localLatest == null || |
| 1166 | providerLatest.compareTo(localLatest) > 0) { |
| 1167 | localLatest = providerLatest; |
| 1168 | } |
| 1169 | } // end local provider loop |
| 1170 | |
| 1171 | // checking if remote timestamp is more recent. |
| 1172 | Timestamp rOffline = offlineAds.get(deviceId); |
| 1173 | if (rOffline != null && |
| 1174 | rOffline.compareTo(localLatest) > 0) { |
| 1175 | // remote offline timestamp suggests that the |
| 1176 | // device is off-line |
| 1177 | markOfflineInternal(deviceId, rOffline); |
| 1178 | } |
| 1179 | |
| 1180 | Timestamp lOffline = offline.get(deviceId); |
| 1181 | if (lOffline != null && rOffline == null) { |
| 1182 | // locally offline, but remote is online, suggest offline |
| 1183 | notifyPeer(sender, new InternalDeviceOfflineEvent(deviceId, lOffline)); |
| 1184 | } |
| 1185 | |
| 1186 | // remove device offline Ad already processed |
| 1187 | offlineAds.remove(deviceId); |
| 1188 | } // end local device loop |
| 1189 | } // device lock |
| 1190 | |
| 1191 | // If there is any Ads left, request them |
| 1192 | log.trace("Ads left {}, {}", devAds, portAds); |
| 1193 | reqDevices.addAll(devAds.keySet()); |
| 1194 | reqPorts.addAll(portAds.keySet()); |
| 1195 | |
| 1196 | if (reqDevices.isEmpty() && reqPorts.isEmpty()) { |
| 1197 | log.trace("Nothing to request to remote peer {}", sender); |
| 1198 | return; |
| 1199 | } |
| 1200 | |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1201 | log.debug("Need to sync {} {}", reqDevices, reqPorts); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1202 | |
| 1203 | // 2-way Anti-Entropy for now |
| 1204 | try { |
| 1205 | unicastMessage(sender, DEVICE_ADVERTISE, createAdvertisement()); |
| 1206 | } catch (IOException e) { |
| 1207 | log.error("Failed to send response advertisement to " + sender, e); |
| 1208 | } |
| 1209 | |
| 1210 | // Sketch of 3-way Anti-Entropy |
| 1211 | // DeviceAntiEntropyRequest request = new DeviceAntiEntropyRequest(self, reqDevices, reqPorts); |
| 1212 | // ClusterMessage message = new ClusterMessage( |
| 1213 | // clusterService.getLocalNode().id(), |
| 1214 | // GossipDeviceStoreMessageSubjects.DEVICE_REQUEST, |
| 1215 | // SERIALIZER.encode(request)); |
| 1216 | // |
| 1217 | // try { |
| 1218 | // clusterCommunicator.unicast(message, advertisement.sender()); |
| 1219 | // } catch (IOException e) { |
| 1220 | // log.error("Failed to send advertisement reply to " |
| 1221 | // + advertisement.sender(), e); |
| 1222 | // } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Madan Jampani | 255a58b | 2014-10-09 12:08:20 -0700 | [diff] [blame] | 1225 | private void notifyDelegateIfNotNull(DeviceEvent event) { |
| 1226 | if (event != null) { |
| 1227 | notifyDelegate(event); |
| 1228 | } |
| 1229 | } |
| 1230 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1231 | private final class SendAdvertisementTask implements Runnable { |
| 1232 | |
| 1233 | @Override |
| 1234 | public void run() { |
| 1235 | if (Thread.currentThread().isInterrupted()) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1236 | log.debug("Interrupted, quitting"); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1237 | return; |
| 1238 | } |
| 1239 | |
| 1240 | try { |
| 1241 | final NodeId self = clusterService.getLocalNode().id(); |
| 1242 | Set<ControllerNode> nodes = clusterService.getNodes(); |
| 1243 | |
| 1244 | ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes) |
| 1245 | .transform(toNodeId()) |
| 1246 | .toList(); |
| 1247 | |
| 1248 | if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) { |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 1249 | log.trace("No other peers in the cluster."); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1250 | return; |
| 1251 | } |
| 1252 | |
| 1253 | NodeId peer; |
| 1254 | do { |
| 1255 | int idx = RandomUtils.nextInt(0, nodeIds.size()); |
| 1256 | peer = nodeIds.get(idx); |
| 1257 | } while (peer.equals(self)); |
| 1258 | |
| 1259 | DeviceAntiEntropyAdvertisement ad = createAdvertisement(); |
| 1260 | |
| 1261 | if (Thread.currentThread().isInterrupted()) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1262 | log.debug("Interrupted, quitting"); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | try { |
| 1267 | unicastMessage(peer, DEVICE_ADVERTISE, ad); |
| 1268 | } catch (IOException e) { |
Yuta HIGUCHI | 78f3a0a | 2014-10-16 17:24:20 -0700 | [diff] [blame] | 1269 | log.debug("Failed to send anti-entropy advertisement to {}", peer); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1270 | return; |
| 1271 | } |
| 1272 | } catch (Exception e) { |
| 1273 | // catch all Exception to avoid Scheduled task being suppressed. |
| 1274 | log.error("Exception thrown while sending advertisement", e); |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1279 | private final class InternalDeviceEventListener |
| 1280 | implements ClusterMessageHandler { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1281 | @Override |
| 1282 | public void handle(ClusterMessage message) { |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1283 | |
Yuta HIGUCHI | cc8e96e | 2014-10-31 17:48:09 -0700 | [diff] [blame] | 1284 | log.debug("Received device update event from peer: {}", message.sender()); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1285 | InternalDeviceEvent event = SERIALIZER.decode(message.payload()); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1286 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1287 | ProviderId providerId = event.providerId(); |
| 1288 | DeviceId deviceId = event.deviceId(); |
| 1289 | Timestamped<DeviceDescription> deviceDescription = event.deviceDescription(); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1290 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1291 | executor.submit(new Runnable() { |
| 1292 | |
| 1293 | @Override |
| 1294 | public void run() { |
| 1295 | try { |
| 1296 | notifyDelegateIfNotNull(createOrUpdateDeviceInternal(providerId, deviceId, deviceDescription)); |
| 1297 | } catch (Exception e) { |
| 1298 | log.warn("Exception thrown handling device update", e); |
| 1299 | } |
| 1300 | } |
| 1301 | }); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1305 | private final class InternalDeviceOfflineEventListener |
| 1306 | implements ClusterMessageHandler { |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1307 | @Override |
| 1308 | public void handle(ClusterMessage message) { |
| 1309 | |
Yuta HIGUCHI | cc8e96e | 2014-10-31 17:48:09 -0700 | [diff] [blame] | 1310 | log.debug("Received device offline event from peer: {}", message.sender()); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1311 | InternalDeviceOfflineEvent event = SERIALIZER.decode(message.payload()); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1312 | |
| 1313 | DeviceId deviceId = event.deviceId(); |
| 1314 | Timestamp timestamp = event.timestamp(); |
| 1315 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1316 | executor.submit(new Runnable() { |
| 1317 | |
| 1318 | @Override |
| 1319 | public void run() { |
| 1320 | try { |
| 1321 | notifyDelegateIfNotNull(markOfflineInternal(deviceId, timestamp)); |
| 1322 | } catch (Exception e) { |
| 1323 | log.warn("Exception thrown handling device offline", e); |
| 1324 | } |
| 1325 | } |
| 1326 | }); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1330 | private final class InternalRemoveRequestListener |
| 1331 | implements ClusterMessageHandler { |
| 1332 | @Override |
| 1333 | public void handle(ClusterMessage message) { |
| 1334 | log.debug("Received device remove request from peer: {}", message.sender()); |
| 1335 | DeviceId did = SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1336 | |
| 1337 | executor.submit(new Runnable() { |
| 1338 | |
| 1339 | @Override |
| 1340 | public void run() { |
| 1341 | try { |
| 1342 | removeDevice(did); |
| 1343 | } catch (Exception e) { |
| 1344 | log.warn("Exception thrown handling device remove", e); |
| 1345 | } |
| 1346 | } |
| 1347 | }); |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1351 | private final class InternalDeviceRemovedEventListener |
| 1352 | implements ClusterMessageHandler { |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 1353 | @Override |
| 1354 | public void handle(ClusterMessage message) { |
| 1355 | |
Yuta HIGUCHI | cc8e96e | 2014-10-31 17:48:09 -0700 | [diff] [blame] | 1356 | log.debug("Received device removed event from peer: {}", message.sender()); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1357 | InternalDeviceRemovedEvent event = SERIALIZER.decode(message.payload()); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 1358 | |
| 1359 | DeviceId deviceId = event.deviceId(); |
| 1360 | Timestamp timestamp = event.timestamp(); |
| 1361 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1362 | executor.submit(new Runnable() { |
| 1363 | |
| 1364 | @Override |
| 1365 | public void run() { |
| 1366 | try { |
| 1367 | notifyDelegateIfNotNull(removeDeviceInternal(deviceId, timestamp)); |
| 1368 | } catch (Exception e) { |
| 1369 | log.warn("Exception thrown handling device removed", e); |
| 1370 | } |
| 1371 | } |
| 1372 | }); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1376 | private final class InternalPortEventListener |
| 1377 | implements ClusterMessageHandler { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1378 | @Override |
| 1379 | public void handle(ClusterMessage message) { |
| 1380 | |
Yuta HIGUCHI | cc8e96e | 2014-10-31 17:48:09 -0700 | [diff] [blame] | 1381 | log.debug("Received port update event from peer: {}", message.sender()); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1382 | InternalPortEvent event = SERIALIZER.decode(message.payload()); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1383 | |
| 1384 | ProviderId providerId = event.providerId(); |
| 1385 | DeviceId deviceId = event.deviceId(); |
| 1386 | Timestamped<List<PortDescription>> portDescriptions = event.portDescriptions(); |
| 1387 | |
Yuta HIGUCHI | 20c0e97 | 2014-10-31 15:22:33 -0700 | [diff] [blame] | 1388 | if (getDevice(deviceId) == null) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1389 | log.debug("{} not found on this node yet, ignoring.", deviceId); |
Yuta HIGUCHI | 20c0e97 | 2014-10-31 15:22:33 -0700 | [diff] [blame] | 1390 | // Note: dropped information will be recovered by anti-entropy |
| 1391 | return; |
| 1392 | } |
| 1393 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1394 | executor.submit(new Runnable() { |
| 1395 | |
| 1396 | @Override |
| 1397 | public void run() { |
| 1398 | try { |
| 1399 | notifyDelegate(updatePortsInternal(providerId, deviceId, portDescriptions)); |
| 1400 | } catch (Exception e) { |
| 1401 | log.warn("Exception thrown handling port update", e); |
| 1402 | } |
| 1403 | } |
| 1404 | }); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1408 | private final class InternalPortStatusEventListener |
| 1409 | implements ClusterMessageHandler { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1410 | @Override |
| 1411 | public void handle(ClusterMessage message) { |
| 1412 | |
Yuta HIGUCHI | cc8e96e | 2014-10-31 17:48:09 -0700 | [diff] [blame] | 1413 | log.debug("Received port status update event from peer: {}", message.sender()); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1414 | InternalPortStatusEvent event = SERIALIZER.decode(message.payload()); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1415 | |
| 1416 | ProviderId providerId = event.providerId(); |
| 1417 | DeviceId deviceId = event.deviceId(); |
| 1418 | Timestamped<PortDescription> portDescription = event.portDescription(); |
| 1419 | |
Yuta HIGUCHI | 20c0e97 | 2014-10-31 15:22:33 -0700 | [diff] [blame] | 1420 | if (getDevice(deviceId) == null) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1421 | log.debug("{} not found on this node yet, ignoring.", deviceId); |
Yuta HIGUCHI | 20c0e97 | 2014-10-31 15:22:33 -0700 | [diff] [blame] | 1422 | // Note: dropped information will be recovered by anti-entropy |
| 1423 | return; |
| 1424 | } |
| 1425 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 1426 | executor.submit(new Runnable() { |
| 1427 | |
| 1428 | @Override |
| 1429 | public void run() { |
| 1430 | try { |
| 1431 | notifyDelegateIfNotNull(updatePortStatusInternal(providerId, deviceId, portDescription)); |
| 1432 | } catch (Exception e) { |
| 1433 | log.warn("Exception thrown handling port update", e); |
| 1434 | } |
| 1435 | } |
| 1436 | }); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1437 | } |
| 1438 | } |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1439 | |
| 1440 | private final class InternalDeviceAdvertisementListener |
| 1441 | implements ClusterMessageHandler { |
| 1442 | |
| 1443 | @Override |
| 1444 | public void handle(ClusterMessage message) { |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 1445 | log.trace("Received Device Anti-Entropy advertisement from peer: {}", message.sender()); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1446 | DeviceAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 1447 | backgroundExecutor.submit(new Runnable() { |
| 1448 | |
| 1449 | @Override |
| 1450 | public void run() { |
| 1451 | try { |
| 1452 | handleAdvertisement(advertisement); |
| 1453 | } catch (Exception e) { |
| 1454 | log.warn("Exception thrown handling Device advertisements.", e); |
| 1455 | } |
| 1456 | } |
| 1457 | }); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1458 | } |
| 1459 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1460 | } |