Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-present Open Networking Foundation |
| 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 | */ |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 16 | package org.onosproject.store.flow.impl; |
| 17 | |
Jordan Halterman | 78c287d | 2019-04-17 11:05:16 -0700 | [diff] [blame^] | 18 | import java.util.Collections; |
| 19 | import java.util.Dictionary; |
| 20 | import java.util.HashSet; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Objects; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.ExecutionException; |
| 27 | import java.util.concurrent.ExecutorService; |
| 28 | import java.util.concurrent.Executors; |
| 29 | import java.util.concurrent.ScheduledExecutorService; |
| 30 | import java.util.concurrent.TimeUnit; |
| 31 | import java.util.concurrent.TimeoutException; |
| 32 | import java.util.function.Function; |
| 33 | import java.util.stream.Collectors; |
| 34 | import java.util.stream.StreamSupport; |
| 35 | |
Jordan Halterman | 8f90d6d | 2018-06-12 11:23:33 -0700 | [diff] [blame] | 36 | import com.google.common.collect.ImmutableList; |
| 37 | import com.google.common.collect.Maps; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 38 | import com.google.common.collect.Streams; |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 39 | import org.apache.commons.lang3.tuple.ImmutablePair; |
| 40 | import org.apache.commons.lang3.tuple.Pair; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 41 | import org.onlab.util.KryoNamespace; |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 42 | import org.onlab.util.OrderedExecutor; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 43 | import org.onlab.util.Tools; |
| 44 | import org.onosproject.cfg.ComponentConfigService; |
| 45 | import org.onosproject.cluster.ClusterService; |
| 46 | import org.onosproject.cluster.NodeId; |
| 47 | import org.onosproject.core.CoreService; |
| 48 | import org.onosproject.core.IdGenerator; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 49 | import org.onosproject.event.AbstractListenerManager; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 50 | import org.onosproject.mastership.MastershipService; |
| 51 | import org.onosproject.net.DeviceId; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 52 | import org.onosproject.net.device.DeviceEvent; |
| 53 | import org.onosproject.net.device.DeviceListener; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 54 | import org.onosproject.net.device.DeviceService; |
| 55 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 56 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 57 | import org.onosproject.net.flow.FlowEntry; |
| 58 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 59 | import org.onosproject.net.flow.FlowRule; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 60 | import org.onosproject.net.flow.FlowRuleEvent; |
| 61 | import org.onosproject.net.flow.FlowRuleEvent.Type; |
| 62 | import org.onosproject.net.flow.FlowRuleService; |
| 63 | import org.onosproject.net.flow.FlowRuleStore; |
| 64 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 65 | import org.onosproject.net.flow.StoredFlowEntry; |
Jordan Halterman | 78c287d | 2019-04-17 11:05:16 -0700 | [diff] [blame^] | 66 | import org.onosproject.net.flow.FlowRuleStoreException; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 67 | import org.onosproject.net.flow.TableStatisticsEntry; |
Jordan Halterman | 8f90d6d | 2018-06-12 11:23:33 -0700 | [diff] [blame] | 68 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 69 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry.FlowRuleOperation; |
| 70 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent; |
| 71 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
| 72 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 73 | import org.onosproject.persistence.PersistenceService; |
| 74 | import org.onosproject.store.AbstractStore; |
| 75 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 76 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 77 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 78 | import org.onosproject.store.flow.ReplicaInfo; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 79 | import org.onosproject.store.flow.ReplicaInfoEvent; |
| 80 | import org.onosproject.store.flow.ReplicaInfoEventListener; |
| 81 | import org.onosproject.store.flow.ReplicaInfoService; |
| 82 | import org.onosproject.store.impl.MastershipBasedTimestamp; |
| 83 | import org.onosproject.store.serializers.KryoNamespaces; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 84 | import org.onosproject.store.service.AsyncConsistentMap; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 85 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 86 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 87 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 88 | import org.onosproject.store.service.MapEvent; |
| 89 | import org.onosproject.store.service.MapEventListener; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 90 | import org.onosproject.store.service.Serializer; |
| 91 | import org.onosproject.store.service.StorageService; |
| 92 | import org.onosproject.store.service.WallClockTimestamp; |
| 93 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 94 | import org.osgi.service.component.annotations.Activate; |
| 95 | import org.osgi.service.component.annotations.Component; |
| 96 | import org.osgi.service.component.annotations.Deactivate; |
| 97 | import org.osgi.service.component.annotations.Modified; |
| 98 | import org.osgi.service.component.annotations.Reference; |
| 99 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 100 | import org.slf4j.Logger; |
| 101 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 102 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 103 | import static java.lang.Math.max; |
| 104 | import static java.lang.Math.min; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 105 | import static org.onlab.util.Tools.get; |
| 106 | import static org.onlab.util.Tools.groupedThreads; |
| 107 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 108 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.APPLY_BATCH_FLOWS; |
| 109 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.FLOW_TABLE_BACKUP; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 110 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.GET_DEVICE_FLOW_COUNT; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 111 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.GET_FLOW_ENTRY; |
| 112 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.REMOTE_APPLY_COMPLETED; |
| 113 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.REMOVE_FLOW_ENTRY; |
| 114 | import static org.slf4j.LoggerFactory.getLogger; |
| 115 | |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 116 | import static org.onosproject.store.OsgiPropertyConstants.*; |
| 117 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Manages inventory of flow rules using a distributed state management protocol. |
| 120 | */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 121 | @Component( |
| 122 | immediate = true, |
| 123 | service = FlowRuleStore.class, |
| 124 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 125 | MESSAGE_HANDLER_THREAD_POOL_SIZE + ":Integer=" + MESSAGE_HANDLER_THREAD_POOL_SIZE_DEFAULT, |
| 126 | BACKUP_PERIOD_MILLIS + ":Integer=" + BACKUP_PERIOD_MILLIS_DEFAULT, |
| 127 | ANTI_ENTROPY_PERIOD_MILLIS + ":Integer=" + ANTI_ENTROPY_PERIOD_MILLIS_DEFAULT, |
| 128 | EC_FLOW_RULE_STORE_PERSISTENCE_ENABLED + ":Boolean=" + EC_FLOW_RULE_STORE_PERSISTENCE_ENABLED_DEFAULT, |
| 129 | MAX_BACKUP_COUNT + ":Integer=" + MAX_BACKUP_COUNT_DEFAULT |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 130 | } |
| 131 | ) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 132 | public class ECFlowRuleStore |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 133 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
| 134 | implements FlowRuleStore { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 135 | |
| 136 | private final Logger log = getLogger(getClass()); |
| 137 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 138 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 139 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 140 | /** Number of threads in the message handler pool. */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 141 | private int msgHandlerPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE_DEFAULT; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 142 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 143 | /** Delay in ms between successive backup runs. */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 144 | private int backupPeriod = BACKUP_PERIOD_MILLIS_DEFAULT; |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 145 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 146 | /** Delay in ms between anti-entropy runs. */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 147 | private int antiEntropyPeriod = ANTI_ENTROPY_PERIOD_MILLIS_DEFAULT; |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 148 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 149 | /** Indicates whether or not changes in the flow table should be persisted to disk. */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 150 | private boolean persistenceEnabled = EC_FLOW_RULE_STORE_PERSISTENCE_ENABLED_DEFAULT; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 151 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 152 | /** Max number of backup copies for each device. */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 153 | private volatile int backupCount = MAX_BACKUP_COUNT_DEFAULT; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 154 | |
| 155 | private InternalFlowTable flowTable = new InternalFlowTable(); |
| 156 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 157 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 158 | protected ReplicaInfoService replicaInfoManager; |
| 159 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 160 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 161 | protected ClusterCommunicationService clusterCommunicator; |
| 162 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 163 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 164 | protected ClusterService clusterService; |
| 165 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 166 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 167 | protected DeviceService deviceService; |
| 168 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 169 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 170 | protected CoreService coreService; |
| 171 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 172 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 173 | protected ComponentConfigService configService; |
| 174 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 175 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 176 | protected MastershipService mastershipService; |
| 177 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 178 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 179 | protected PersistenceService persistenceService; |
| 180 | |
| 181 | private Map<Long, NodeId> pendingResponses = Maps.newConcurrentMap(); |
| 182 | private ExecutorService messageHandlingExecutor; |
| 183 | private ExecutorService eventHandler; |
| 184 | |
Ray Milkey | d1092d6 | 2019-04-02 09:12:00 -0700 | [diff] [blame] | 185 | private ScheduledExecutorService backupScheduler; |
| 186 | private ExecutorService backupExecutor; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 187 | |
| 188 | private EventuallyConsistentMap<DeviceId, List<TableStatisticsEntry>> deviceTableStats; |
| 189 | private final EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> tableStatsListener = |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 190 | new InternalTableStatsListener(); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 191 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 192 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 193 | protected StorageService storageService; |
| 194 | |
Jordan Halterman | b2f5795 | 2018-03-21 12:52:37 -0700 | [diff] [blame] | 195 | protected final Serializer serializer = Serializer.using(KryoNamespace.newBuilder() |
| 196 | .register(KryoNamespaces.API) |
Jordan Halterman | a765d22 | 2018-06-01 00:40:56 -0700 | [diff] [blame] | 197 | .register(BucketId.class) |
Jordan Halterman | b2f5795 | 2018-03-21 12:52:37 -0700 | [diff] [blame] | 198 | .register(FlowBucket.class) |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 199 | .register(ImmutablePair.class) |
Jordan Halterman | b2f5795 | 2018-03-21 12:52:37 -0700 | [diff] [blame] | 200 | .build()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 201 | |
| 202 | protected final KryoNamespace.Builder serializerBuilder = KryoNamespace.newBuilder() |
Jordan Halterman | a765d22 | 2018-06-01 00:40:56 -0700 | [diff] [blame] | 203 | .register(KryoNamespaces.API) |
| 204 | .register(BucketId.class) |
| 205 | .register(MastershipBasedTimestamp.class); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 206 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 207 | protected AsyncConsistentMap<DeviceId, Long> mastershipTermLifecycles; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 208 | |
| 209 | private IdGenerator idGenerator; |
| 210 | private NodeId local; |
| 211 | |
| 212 | @Activate |
| 213 | public void activate(ComponentContext context) { |
| 214 | configService.registerProperties(getClass()); |
| 215 | |
Ray Milkey | d1092d6 | 2019-04-02 09:12:00 -0700 | [diff] [blame] | 216 | backupScheduler = Executors.newSingleThreadScheduledExecutor( |
| 217 | groupedThreads("onos/flow", "backup-scheduler", log)); |
| 218 | backupExecutor = Executors.newFixedThreadPool( |
| 219 | max(min(Runtime.getRuntime().availableProcessors() * 2, 16), 4), |
| 220 | groupedThreads("onos/flow", "backup-%d", log)); |
| 221 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 222 | idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC); |
| 223 | |
| 224 | local = clusterService.getLocalNode().id(); |
| 225 | |
| 226 | eventHandler = Executors.newSingleThreadExecutor( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 227 | groupedThreads("onos/flow", "event-handler", log)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 228 | messageHandlingExecutor = Executors.newFixedThreadPool( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 229 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers", log)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 230 | |
| 231 | registerMessageHandlers(messageHandlingExecutor); |
| 232 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 233 | mastershipTermLifecycles = storageService.<DeviceId, Long>consistentMapBuilder() |
| 234 | .withName("onos-flow-store-terms") |
| 235 | .withSerializer(serializer) |
| 236 | .buildAsyncMap(); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 237 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 238 | deviceTableStats = storageService.<DeviceId, List<TableStatisticsEntry>>eventuallyConsistentMapBuilder() |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 239 | .withName("onos-flow-table-stats") |
| 240 | .withSerializer(serializerBuilder) |
| 241 | .withAntiEntropyPeriod(5, TimeUnit.SECONDS) |
| 242 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 243 | .withTombstonesDisabled() |
| 244 | .build(); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 245 | deviceTableStats.addListener(tableStatsListener); |
| 246 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 247 | deviceService.addListener(flowTable); |
| 248 | deviceService.getDevices().forEach(device -> flowTable.addDevice(device.id())); |
| 249 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 250 | logConfig("Started"); |
| 251 | } |
| 252 | |
| 253 | @Deactivate |
| 254 | public void deactivate(ComponentContext context) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 255 | configService.unregisterProperties(getClass(), false); |
| 256 | unregisterMessageHandlers(); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 257 | deviceService.removeListener(flowTable); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 258 | deviceTableStats.removeListener(tableStatsListener); |
| 259 | deviceTableStats.destroy(); |
| 260 | eventHandler.shutdownNow(); |
| 261 | messageHandlingExecutor.shutdownNow(); |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 262 | backupScheduler.shutdownNow(); |
| 263 | backupExecutor.shutdownNow(); |
Ray Milkey | d1092d6 | 2019-04-02 09:12:00 -0700 | [diff] [blame] | 264 | backupScheduler = null; |
| 265 | backupExecutor = null; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 266 | log.info("Stopped"); |
| 267 | } |
| 268 | |
| 269 | @SuppressWarnings("rawtypes") |
| 270 | @Modified |
| 271 | public void modified(ComponentContext context) { |
| 272 | if (context == null) { |
| 273 | logConfig("Default config"); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | Dictionary properties = context.getProperties(); |
| 278 | int newPoolSize; |
| 279 | int newBackupPeriod; |
| 280 | int newBackupCount; |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 281 | int newAntiEntropyPeriod; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 282 | try { |
| 283 | String s = get(properties, "msgHandlerPoolSize"); |
| 284 | newPoolSize = isNullOrEmpty(s) ? msgHandlerPoolSize : Integer.parseInt(s.trim()); |
| 285 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 286 | s = get(properties, BACKUP_PERIOD_MILLIS); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 287 | newBackupPeriod = isNullOrEmpty(s) ? backupPeriod : Integer.parseInt(s.trim()); |
| 288 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 289 | s = get(properties, MAX_BACKUP_COUNT); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 290 | newBackupCount = isNullOrEmpty(s) ? backupCount : Integer.parseInt(s.trim()); |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 291 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 292 | s = get(properties, ANTI_ENTROPY_PERIOD_MILLIS); |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 293 | newAntiEntropyPeriod = isNullOrEmpty(s) ? antiEntropyPeriod : Integer.parseInt(s.trim()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 294 | } catch (NumberFormatException | ClassCastException e) { |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 295 | newPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE_DEFAULT; |
| 296 | newBackupPeriod = BACKUP_PERIOD_MILLIS_DEFAULT; |
| 297 | newBackupCount = MAX_BACKUP_COUNT_DEFAULT; |
| 298 | newAntiEntropyPeriod = ANTI_ENTROPY_PERIOD_MILLIS_DEFAULT; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 301 | if (newBackupPeriod != backupPeriod) { |
| 302 | backupPeriod = newBackupPeriod; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 303 | flowTable.setBackupPeriod(newBackupPeriod); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 304 | } |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 305 | |
| 306 | if (newAntiEntropyPeriod != antiEntropyPeriod) { |
| 307 | antiEntropyPeriod = newAntiEntropyPeriod; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 308 | flowTable.setAntiEntropyPeriod(newAntiEntropyPeriod); |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 311 | if (newPoolSize != msgHandlerPoolSize) { |
| 312 | msgHandlerPoolSize = newPoolSize; |
| 313 | ExecutorService oldMsgHandler = messageHandlingExecutor; |
| 314 | messageHandlingExecutor = Executors.newFixedThreadPool( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 315 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers", log)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 316 | |
| 317 | // replace previously registered handlers. |
| 318 | registerMessageHandlers(messageHandlingExecutor); |
| 319 | oldMsgHandler.shutdown(); |
| 320 | } |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 321 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 322 | if (backupCount != newBackupCount) { |
| 323 | backupCount = newBackupCount; |
| 324 | } |
| 325 | logConfig("Reconfigured"); |
| 326 | } |
| 327 | |
| 328 | private void registerMessageHandlers(ExecutorService executor) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 329 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new OnStoreBatch(), executor); |
| 330 | clusterCommunicator.<FlowRuleBatchEvent>addSubscriber( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 331 | REMOTE_APPLY_COMPLETED, serializer::decode, this::notifyDelegate, executor); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 332 | clusterCommunicator.addSubscriber( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 333 | GET_FLOW_ENTRY, serializer::decode, flowTable::getFlowEntry, serializer::encode, executor); |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 334 | clusterCommunicator.<Pair<DeviceId, FlowEntryState>, Integer>addSubscriber( |
| 335 | GET_DEVICE_FLOW_COUNT, |
| 336 | serializer::decode, |
| 337 | p -> flowTable.getFlowRuleCount(p.getLeft(), p.getRight()), |
| 338 | serializer::encode, executor); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 339 | clusterCommunicator.addSubscriber( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 340 | REMOVE_FLOW_ENTRY, serializer::decode, this::removeFlowRuleInternal, serializer::encode, executor); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | private void unregisterMessageHandlers() { |
| 344 | clusterCommunicator.removeSubscriber(REMOVE_FLOW_ENTRY); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 345 | clusterCommunicator.removeSubscriber(GET_DEVICE_FLOW_COUNT); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 346 | clusterCommunicator.removeSubscriber(GET_FLOW_ENTRY); |
| 347 | clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS); |
| 348 | clusterCommunicator.removeSubscriber(REMOTE_APPLY_COMPLETED); |
| 349 | clusterCommunicator.removeSubscriber(FLOW_TABLE_BACKUP); |
| 350 | } |
| 351 | |
| 352 | private void logConfig(String prefix) { |
| 353 | log.info("{} with msgHandlerPoolSize = {}; backupPeriod = {}, backupCount = {}", |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 354 | prefix, msgHandlerPoolSize, backupPeriod, backupCount); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 357 | @Override |
| 358 | public int getFlowRuleCount() { |
| 359 | return Streams.stream(deviceService.getDevices()).parallel() |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 360 | .mapToInt(device -> getFlowRuleCount(device.id())) |
| 361 | .sum(); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public int getFlowRuleCount(DeviceId deviceId) { |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 366 | return getFlowRuleCount(deviceId, null); |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public int getFlowRuleCount(DeviceId deviceId, FlowEntryState state) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 371 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 372 | if (master == null) { |
| 373 | log.debug("Failed to getFlowRuleCount: No master for {}", deviceId); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | if (Objects.equals(local, master)) { |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 378 | return flowTable.getFlowRuleCount(deviceId, state); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | log.trace("Forwarding getFlowRuleCount to master {} for device {}", master, deviceId); |
| 382 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive( |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 383 | Pair.of(deviceId, state), |
| 384 | GET_DEVICE_FLOW_COUNT, |
| 385 | serializer::encode, |
| 386 | serializer::decode, |
| 387 | master), |
| 388 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 389 | TimeUnit.MILLISECONDS, |
| 390 | 0); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public FlowEntry getFlowEntry(FlowRule rule) { |
| 395 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
| 396 | |
| 397 | if (master == null) { |
| 398 | log.debug("Failed to getFlowEntry: No master for {}", rule.deviceId()); |
| 399 | return null; |
| 400 | } |
| 401 | |
| 402 | if (Objects.equals(local, master)) { |
| 403 | return flowTable.getFlowEntry(rule); |
| 404 | } |
| 405 | |
| 406 | log.trace("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 407 | master, rule.deviceId()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 408 | |
| 409 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(rule, |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 410 | ECFlowRuleStoreMessageSubjects.GET_FLOW_ENTRY, |
| 411 | serializer::encode, |
| 412 | serializer::decode, |
| 413 | master), |
| 414 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 415 | TimeUnit.MILLISECONDS, |
| 416 | null); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Jordan Halterman | 78c287d | 2019-04-17 11:05:16 -0700 | [diff] [blame^] | 421 | return flowTable.getFlowEntries(deviceId); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void storeFlowRule(FlowRule rule) { |
| 426 | storeBatch(new FlowRuleBatchOperation( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 427 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)), |
| 428 | rule.deviceId(), idGenerator.getNewId())); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public void storeBatch(FlowRuleBatchOperation operation) { |
| 433 | if (operation.getOperations().isEmpty()) { |
| 434 | notifyDelegate(FlowRuleBatchEvent.completed( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 435 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 436 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | |
| 440 | DeviceId deviceId = operation.deviceId(); |
| 441 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 442 | |
| 443 | if (master == null) { |
| 444 | log.warn("No master for {} ", deviceId); |
| 445 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 446 | Set<FlowRule> allFailures = operation.getOperations() |
| 447 | .stream() |
| 448 | .map(op -> op.target()) |
| 449 | .collect(Collectors.toSet()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 450 | notifyDelegate(FlowRuleBatchEvent.completed( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 451 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 452 | new CompletedBatchOperation(false, allFailures, deviceId))); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
| 456 | if (Objects.equals(local, master)) { |
| 457 | storeBatchInternal(operation); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 462 | master, deviceId); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 463 | |
| 464 | clusterCommunicator.unicast(operation, |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 465 | APPLY_BATCH_FLOWS, |
| 466 | serializer::encode, |
| 467 | master) |
| 468 | .whenComplete((result, error) -> { |
| 469 | if (error != null) { |
| 470 | log.warn("Failed to storeBatch: {} to {}", operation, master, error); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 471 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 472 | Set<FlowRule> allFailures = operation.getOperations() |
| 473 | .stream() |
| 474 | .map(op -> op.target()) |
| 475 | .collect(Collectors.toSet()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 476 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 477 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 478 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 479 | new CompletedBatchOperation(false, allFailures, deviceId))); |
| 480 | } |
| 481 | }); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | private void storeBatchInternal(FlowRuleBatchOperation operation) { |
| 485 | |
| 486 | final DeviceId did = operation.deviceId(); |
| 487 | //final Collection<FlowEntry> ft = flowTable.getFlowEntries(did); |
| 488 | Set<FlowRuleBatchEntry> currentOps = updateStoreInternal(operation); |
| 489 | if (currentOps.isEmpty()) { |
| 490 | batchOperationComplete(FlowRuleBatchEvent.completed( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 491 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 492 | new CompletedBatchOperation(true, Collections.emptySet(), did))); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 493 | return; |
| 494 | } |
| 495 | |
| 496 | notifyDelegate(FlowRuleBatchEvent.requested(new |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 497 | FlowRuleBatchRequest(operation.id(), |
| 498 | currentOps), operation.deviceId())); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | private Set<FlowRuleBatchEntry> updateStoreInternal(FlowRuleBatchOperation operation) { |
| 502 | return operation.getOperations().stream().map( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 503 | op -> { |
| 504 | StoredFlowEntry entry; |
| 505 | switch (op.operator()) { |
| 506 | case ADD: |
| 507 | entry = new DefaultFlowEntry(op.target()); |
Jordan Halterman | c88c265 | 2019-01-14 16:23:31 -0800 | [diff] [blame] | 508 | log.debug("Adding flow rule: {}", entry); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 509 | flowTable.add(entry); |
| 510 | return op; |
| 511 | case MODIFY: |
| 512 | entry = new DefaultFlowEntry(op.target()); |
Jordan Halterman | c88c265 | 2019-01-14 16:23:31 -0800 | [diff] [blame] | 513 | log.debug("Updating flow rule: {}", entry); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 514 | flowTable.update(entry); |
| 515 | return op; |
| 516 | case REMOVE: |
| 517 | return flowTable.update(op.target(), stored -> { |
| 518 | stored.setState(FlowEntryState.PENDING_REMOVE); |
| 519 | log.debug("Setting state of rule to pending remove: {}", stored); |
Jordan Halterman | 2edfeef | 2018-01-16 14:59:49 -0800 | [diff] [blame] | 520 | return op; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 521 | }); |
| 522 | default: |
| 523 | log.warn("Unknown flow operation operator: {}", op.operator()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 524 | } |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 525 | return null; |
| 526 | } |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 527 | ).filter(Objects::nonNull).collect(Collectors.toSet()); |
| 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public void deleteFlowRule(FlowRule rule) { |
| 532 | storeBatch( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 533 | new FlowRuleBatchOperation( |
| 534 | Collections.singletonList( |
| 535 | new FlowRuleBatchEntry( |
| 536 | FlowRuleOperation.REMOVE, |
| 537 | rule)), rule.deviceId(), idGenerator.getNewId())); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | @Override |
| 541 | public FlowRuleEvent pendingFlowRule(FlowEntry rule) { |
| 542 | if (mastershipService.isLocalMaster(rule.deviceId())) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 543 | return flowTable.update(rule, stored -> { |
| 544 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 545 | stored.setState(FlowEntryState.PENDING_ADD); |
| 546 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 547 | } |
| 548 | return null; |
| 549 | }); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 550 | } |
| 551 | return null; |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
| 556 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
| 557 | if (Objects.equals(local, master)) { |
| 558 | return addOrUpdateFlowRuleInternal(rule); |
| 559 | } |
| 560 | |
| 561 | log.warn("Tried to update FlowRule {} state," |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 562 | + " while the Node was not the master.", rule); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 563 | return null; |
| 564 | } |
| 565 | |
| 566 | private FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 567 | FlowRuleEvent event = flowTable.update(rule, stored -> { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 568 | stored.setBytes(rule.bytes()); |
| 569 | stored.setLife(rule.life(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS); |
| 570 | stored.setLiveType(rule.liveType()); |
| 571 | stored.setPackets(rule.packets()); |
| 572 | stored.setLastSeen(); |
| 573 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 574 | stored.setState(FlowEntryState.ADDED); |
| 575 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 576 | } |
| 577 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 578 | }); |
| 579 | if (event != null) { |
| 580 | return event; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
| 584 | // TODO: also update backup if the behavior is correct. |
| 585 | flowTable.add(rule); |
| 586 | return null; |
| 587 | } |
| 588 | |
| 589 | @Override |
| 590 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 591 | final DeviceId deviceId = rule.deviceId(); |
| 592 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 593 | |
| 594 | if (Objects.equals(local, master)) { |
| 595 | // bypass and handle it locally |
| 596 | return removeFlowRuleInternal(rule); |
| 597 | } |
| 598 | |
| 599 | if (master == null) { |
| 600 | log.warn("Failed to removeFlowRule: No master for {}", deviceId); |
| 601 | // TODO: revisit if this should be null (="no-op") or Exception |
| 602 | return null; |
| 603 | } |
| 604 | |
| 605 | log.trace("Forwarding removeFlowRule to {}, which is the master for device {}", |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 606 | master, deviceId); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 607 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 608 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive( |
| 609 | rule, |
| 610 | REMOVE_FLOW_ENTRY, |
| 611 | serializer::encode, |
| 612 | serializer::decode, |
| 613 | master), |
| 614 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 615 | TimeUnit.MILLISECONDS, |
| 616 | null); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | private FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 620 | // This is where one could mark a rule as removed and still keep it in the store. |
Jordan Halterman | 2edfeef | 2018-01-16 14:59:49 -0800 | [diff] [blame] | 621 | final FlowEntry removed = flowTable.remove(rule); |
Jordan Halterman | c88c265 | 2019-01-14 16:23:31 -0800 | [diff] [blame] | 622 | log.debug("Removed flow rule: {}", removed); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 623 | // rule may be partial rule that is missing treatment, we should use rule from store instead |
| 624 | return removed != null ? new FlowRuleEvent(RULE_REMOVED, removed) : null; |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public void purgeFlowRule(DeviceId deviceId) { |
| 629 | flowTable.purgeFlowRule(deviceId); |
| 630 | } |
| 631 | |
| 632 | @Override |
| 633 | public void purgeFlowRules() { |
| 634 | flowTable.purgeFlowRules(); |
| 635 | } |
| 636 | |
| 637 | @Override |
| 638 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
| 639 | //FIXME: need a per device pending response |
| 640 | NodeId nodeId = pendingResponses.remove(event.subject().batchId()); |
| 641 | if (nodeId == null) { |
| 642 | notifyDelegate(event); |
| 643 | } else { |
| 644 | // TODO check unicast return value |
| 645 | clusterCommunicator.unicast(event, REMOTE_APPLY_COMPLETED, serializer::encode, nodeId); |
| 646 | //error log: log.warn("Failed to respond to peer for batch operation result"); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | private final class OnStoreBatch implements ClusterMessageHandler { |
| 651 | |
| 652 | @Override |
| 653 | public void handle(final ClusterMessage message) { |
| 654 | FlowRuleBatchOperation operation = serializer.decode(message.payload()); |
| 655 | log.debug("received batch request {}", operation); |
| 656 | |
| 657 | final DeviceId deviceId = operation.deviceId(); |
| 658 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 659 | if (!Objects.equals(local, master)) { |
| 660 | Set<FlowRule> failures = new HashSet<>(operation.size()); |
| 661 | for (FlowRuleBatchEntry op : operation.getOperations()) { |
| 662 | failures.add(op.target()); |
| 663 | } |
| 664 | CompletedBatchOperation allFailed = new CompletedBatchOperation(false, failures, deviceId); |
| 665 | // This node is no longer the master, respond as all failed. |
| 666 | // TODO: we might want to wrap response in envelope |
| 667 | // to distinguish sw programming failure and hand over |
| 668 | // it make sense in the latter case to retry immediately. |
| 669 | message.respond(serializer.encode(allFailed)); |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | pendingResponses.put(operation.id(), message.sender()); |
| 674 | storeBatchInternal(operation); |
| 675 | } |
| 676 | } |
| 677 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 678 | private class InternalFlowTable implements DeviceListener { |
| 679 | private final Map<DeviceId, DeviceFlowTable> flowTables = Maps.newConcurrentMap(); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 680 | |
| 681 | @Override |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 682 | public void event(DeviceEvent event) { |
| 683 | if (event.type() == DeviceEvent.Type.DEVICE_ADDED) { |
| 684 | addDevice(event.subject().id()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 688 | /** |
| 689 | * Adds the given device to the flow table. |
| 690 | * |
| 691 | * @param deviceId the device to add to the table |
| 692 | */ |
| 693 | public void addDevice(DeviceId deviceId) { |
| 694 | flowTables.computeIfAbsent(deviceId, id -> new DeviceFlowTable( |
| 695 | id, |
| 696 | clusterService, |
| 697 | clusterCommunicator, |
| 698 | new InternalLifecycleManager(id), |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 699 | backupScheduler, |
| 700 | new OrderedExecutor(backupExecutor), |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 701 | backupPeriod, |
| 702 | antiEntropyPeriod)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 705 | /** |
| 706 | * Sets the flow table backup period. |
| 707 | * |
| 708 | * @param backupPeriod the flow table backup period |
| 709 | */ |
| 710 | void setBackupPeriod(int backupPeriod) { |
| 711 | flowTables.values().forEach(flowTable -> flowTable.setBackupPeriod(backupPeriod)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 714 | /** |
| 715 | * Sets the flow table anti-entropy period. |
| 716 | * |
| 717 | * @param antiEntropyPeriod the flow table anti-entropy period |
| 718 | */ |
| 719 | void setAntiEntropyPeriod(int antiEntropyPeriod) { |
| 720 | flowTables.values().forEach(flowTable -> flowTable.setAntiEntropyPeriod(antiEntropyPeriod)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 723 | /** |
| 724 | * Returns the flow table for a specific device. |
| 725 | * |
| 726 | * @param deviceId the device identifier |
| 727 | * @return the flow table for the given device |
| 728 | */ |
| 729 | private DeviceFlowTable getFlowTable(DeviceId deviceId) { |
| 730 | DeviceFlowTable flowTable = flowTables.get(deviceId); |
| 731 | return flowTable != null ? flowTable : flowTables.computeIfAbsent(deviceId, id -> new DeviceFlowTable( |
| 732 | deviceId, |
| 733 | clusterService, |
| 734 | clusterCommunicator, |
| 735 | new InternalLifecycleManager(deviceId), |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 736 | backupScheduler, |
| 737 | new OrderedExecutor(backupExecutor), |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 738 | backupPeriod, |
| 739 | antiEntropyPeriod)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 742 | /** |
| 743 | * Returns the flow rule count for the given device. |
| 744 | * |
| 745 | * @param deviceId the device for which to return the flow rule count |
| 746 | * @return the flow rule count for the given device |
| 747 | */ |
| 748 | public int getFlowRuleCount(DeviceId deviceId) { |
| 749 | return getFlowTable(deviceId).count(); |
| 750 | } |
| 751 | |
| 752 | /** |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 753 | * Returns the count of flow rules in the given state for the given device. |
| 754 | * |
| 755 | * @param deviceId the device for which to return the flow rule count |
| 756 | * @return the flow rule count for the given device |
| 757 | */ |
| 758 | public int getFlowRuleCount(DeviceId deviceId, FlowEntryState state) { |
| 759 | if (state == null) { |
| 760 | return getFlowRuleCount(deviceId); |
| 761 | } |
Jordan Halterman | 78c287d | 2019-04-17 11:05:16 -0700 | [diff] [blame^] | 762 | return (int) StreamSupport.stream(getFlowEntries(deviceId).spliterator(), false) |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 763 | .filter(rule -> rule.state() == state) |
| 764 | .count(); |
| 765 | } |
| 766 | |
| 767 | /** |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 768 | * Returns the flow entry for the given rule. |
| 769 | * |
| 770 | * @param rule the rule for which to return the flow entry |
| 771 | * @return the flow entry for the given rule |
| 772 | */ |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 773 | public StoredFlowEntry getFlowEntry(FlowRule rule) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 774 | return getFlowTable(rule.deviceId()).getFlowEntry(rule); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 777 | /** |
| 778 | * Returns the set of flow entries for the given device. |
| 779 | * |
| 780 | * @param deviceId the device for which to lookup flow entries |
| 781 | * @return the set of flow entries for the given device |
| 782 | */ |
Jordan Halterman | 78c287d | 2019-04-17 11:05:16 -0700 | [diff] [blame^] | 783 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 784 | try { |
| 785 | return getFlowTable(deviceId).getFlowEntries() |
| 786 | .get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 787 | } catch (ExecutionException e) { |
| 788 | throw new FlowRuleStoreException(e.getCause()); |
| 789 | } catch (TimeoutException e) { |
| 790 | throw new FlowRuleStoreException.Timeout(); |
| 791 | } catch (InterruptedException e) { |
| 792 | throw new FlowRuleStoreException.Interrupted(); |
| 793 | } |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 796 | /** |
| 797 | * Adds the given flow rule. |
| 798 | * |
| 799 | * @param rule the rule to add |
| 800 | */ |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 801 | public void add(FlowEntry rule) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 802 | Tools.futureGetOrElse( |
| 803 | getFlowTable(rule.deviceId()).add(rule), |
| 804 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 805 | TimeUnit.MILLISECONDS, |
| 806 | null); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 809 | /** |
| 810 | * Updates the given flow rule. |
| 811 | * |
| 812 | * @param rule the rule to update |
| 813 | */ |
Jordan Halterman | 2edfeef | 2018-01-16 14:59:49 -0800 | [diff] [blame] | 814 | public void update(FlowEntry rule) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 815 | Tools.futureGetOrElse( |
| 816 | getFlowTable(rule.deviceId()).update(rule), |
| 817 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 818 | TimeUnit.MILLISECONDS, |
| 819 | null); |
Jordan Halterman | 2edfeef | 2018-01-16 14:59:49 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 822 | /** |
| 823 | * Applies the given update function to the rule. |
| 824 | * |
| 825 | * @param function the update function to apply |
| 826 | * @return a future to be completed with the update event or {@code null} if the rule was not updated |
| 827 | */ |
| 828 | public <T> T update(FlowRule rule, Function<StoredFlowEntry, T> function) { |
| 829 | return Tools.futureGetOrElse( |
| 830 | getFlowTable(rule.deviceId()).update(rule, function), |
| 831 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 832 | TimeUnit.MILLISECONDS, |
| 833 | null); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * Removes the given flow rule. |
| 838 | * |
| 839 | * @param rule the rule to remove |
| 840 | */ |
Jordan Halterman | 2edfeef | 2018-01-16 14:59:49 -0800 | [diff] [blame] | 841 | public FlowEntry remove(FlowEntry rule) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 842 | return Tools.futureGetOrElse( |
| 843 | getFlowTable(rule.deviceId()).remove(rule), |
| 844 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 845 | TimeUnit.MILLISECONDS, |
| 846 | null); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 849 | /** |
| 850 | * Purges flow rules for the given device. |
| 851 | * |
| 852 | * @param deviceId the device for which to purge flow rules |
| 853 | */ |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 854 | public void purgeFlowRule(DeviceId deviceId) { |
Jordan Halterman | da3b9f0 | 2018-10-05 13:28:51 -0700 | [diff] [blame] | 855 | // If the device is still present in the store, purge the underlying DeviceFlowTable. |
| 856 | // Otherwise, remove the DeviceFlowTable and unregister message handlers. |
| 857 | if (deviceService.getDevice(deviceId) != null) { |
| 858 | DeviceFlowTable flowTable = flowTables.get(deviceId); |
| 859 | if (flowTable != null) { |
| 860 | flowTable.purge(); |
| 861 | } |
| 862 | } else { |
| 863 | DeviceFlowTable flowTable = flowTables.remove(deviceId); |
| 864 | if (flowTable != null) { |
| 865 | flowTable.close(); |
| 866 | } |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 867 | } |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 870 | /** |
| 871 | * Purges all flow rules from the table. |
| 872 | */ |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 873 | public void purgeFlowRules() { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 874 | Iterator<DeviceFlowTable> iterator = flowTables.values().iterator(); |
| 875 | while (iterator.hasNext()) { |
| 876 | iterator.next().close(); |
| 877 | iterator.remove(); |
Jordan Halterman | 8f90d6d | 2018-06-12 11:23:33 -0700 | [diff] [blame] | 878 | } |
Jordan Halterman | b2f5795 | 2018-03-21 12:52:37 -0700 | [diff] [blame] | 879 | } |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | @Override |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 883 | public FlowRuleEvent updateTableStatistics(DeviceId deviceId, List<TableStatisticsEntry> tableStats) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 884 | deviceTableStats.put(deviceId, tableStats); |
| 885 | return null; |
| 886 | } |
| 887 | |
| 888 | @Override |
| 889 | public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) { |
| 890 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 891 | |
| 892 | if (master == null) { |
| 893 | log.debug("Failed to getTableStats: No master for {}", deviceId); |
| 894 | return Collections.emptyList(); |
| 895 | } |
| 896 | |
| 897 | List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId); |
| 898 | if (tableStats == null) { |
| 899 | return Collections.emptyList(); |
| 900 | } |
| 901 | return ImmutableList.copyOf(tableStats); |
| 902 | } |
| 903 | |
| 904 | @Override |
| 905 | public long getActiveFlowRuleCount(DeviceId deviceId) { |
| 906 | return Streams.stream(getTableStatistics(deviceId)) |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 907 | .mapToLong(TableStatisticsEntry::activeFlowEntries) |
| 908 | .sum(); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | private class InternalTableStatsListener |
| 912 | implements EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> { |
| 913 | @Override |
| 914 | public void event(EventuallyConsistentMapEvent<DeviceId, |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 915 | List<TableStatisticsEntry>> event) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 916 | //TODO: Generate an event to listeners (do we need?) |
| 917 | } |
| 918 | } |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 919 | |
| 920 | /** |
| 921 | * Device lifecycle manager implementation. |
| 922 | */ |
| 923 | private final class InternalLifecycleManager |
| 924 | extends AbstractListenerManager<LifecycleEvent, LifecycleEventListener> |
| 925 | implements LifecycleManager, ReplicaInfoEventListener, MapEventListener<DeviceId, Long> { |
| 926 | |
| 927 | private final DeviceId deviceId; |
| 928 | |
| 929 | private volatile DeviceReplicaInfo replicaInfo; |
| 930 | |
| 931 | InternalLifecycleManager(DeviceId deviceId) { |
| 932 | this.deviceId = deviceId; |
| 933 | replicaInfoManager.addListener(this); |
| 934 | mastershipTermLifecycles.addListener(this); |
| 935 | replicaInfo = toDeviceReplicaInfo(replicaInfoManager.getReplicaInfoFor(deviceId)); |
| 936 | } |
| 937 | |
| 938 | @Override |
| 939 | public DeviceReplicaInfo getReplicaInfo() { |
| 940 | return replicaInfo; |
| 941 | } |
| 942 | |
| 943 | @Override |
| 944 | public void activate(long term) { |
| 945 | final ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 946 | if (replicaInfo != null && replicaInfo.term() == term) { |
| 947 | mastershipTermLifecycles.put(deviceId, term); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | @Override |
| 952 | public void event(ReplicaInfoEvent event) { |
Jordan Halterman | e4bf856 | 2018-06-22 16:58:08 -0700 | [diff] [blame] | 953 | if (event.subject().equals(deviceId)) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 954 | onReplicaInfoChange(event.replicaInfo()); |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | @Override |
| 959 | public void event(MapEvent<DeviceId, Long> event) { |
| 960 | if (event.key().equals(deviceId) && event.newValue() != null) { |
| 961 | onActivate(event.newValue().value()); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * Handles a term activation event. |
| 967 | * |
| 968 | * @param term the term that was activated |
| 969 | */ |
| 970 | private void onActivate(long term) { |
| 971 | final ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 972 | if (replicaInfo != null && replicaInfo.term() == term) { |
| 973 | NodeId master = replicaInfo.master().orElse(null); |
| 974 | List<NodeId> backups = replicaInfo.backups() |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 975 | .subList(0, min(replicaInfo.backups().size(), backupCount)); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 976 | listenerRegistry.process(new LifecycleEvent( |
| 977 | LifecycleEvent.Type.TERM_ACTIVE, |
| 978 | new DeviceReplicaInfo(term, master, backups))); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * Handles a replica info change event. |
| 984 | * |
| 985 | * @param replicaInfo the updated replica info |
| 986 | */ |
| 987 | private synchronized void onReplicaInfoChange(ReplicaInfo replicaInfo) { |
| 988 | DeviceReplicaInfo oldReplicaInfo = this.replicaInfo; |
| 989 | this.replicaInfo = toDeviceReplicaInfo(replicaInfo); |
| 990 | if (oldReplicaInfo == null || oldReplicaInfo.term() < replicaInfo.term()) { |
| 991 | if (oldReplicaInfo != null) { |
| 992 | listenerRegistry.process(new LifecycleEvent(LifecycleEvent.Type.TERM_END, oldReplicaInfo)); |
| 993 | } |
| 994 | listenerRegistry.process(new LifecycleEvent(LifecycleEvent.Type.TERM_START, this.replicaInfo)); |
Jordan Halterman | e4bf856 | 2018-06-22 16:58:08 -0700 | [diff] [blame] | 995 | } else if (oldReplicaInfo.term() == replicaInfo.term()) { |
| 996 | listenerRegistry.process(new LifecycleEvent(LifecycleEvent.Type.TERM_UPDATE, this.replicaInfo)); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * Converts the given replica info into a {@link DeviceReplicaInfo} instance. |
| 1002 | * |
| 1003 | * @param replicaInfo the replica info to convert |
| 1004 | * @return the converted replica info |
| 1005 | */ |
| 1006 | private DeviceReplicaInfo toDeviceReplicaInfo(ReplicaInfo replicaInfo) { |
| 1007 | NodeId master = replicaInfo.master().orElse(null); |
| 1008 | List<NodeId> backups = replicaInfo.backups() |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 1009 | .subList(0, min(replicaInfo.backups().size(), backupCount)); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 1010 | return new DeviceReplicaInfo(replicaInfo.term(), master, backups); |
| 1011 | } |
| 1012 | |
| 1013 | @Override |
| 1014 | public void close() { |
| 1015 | replicaInfoManager.removeListener(this); |
| 1016 | mastershipTermLifecycles.removeListener(this); |
| 1017 | } |
| 1018 | } |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 1019 | |
| 1020 | private static class CountMessage { |
| 1021 | private final DeviceId deviceId; |
| 1022 | private final FlowEntryState state; |
| 1023 | |
| 1024 | CountMessage(DeviceId deviceId, FlowEntryState state) { |
| 1025 | this.deviceId = deviceId; |
| 1026 | this.state = state; |
| 1027 | } |
| 1028 | } |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 1029 | } |