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