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