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