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