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