Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.flow.impl; |
| 17 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.Iterables; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import com.google.common.util.concurrent.Futures; |
| 24 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Modified; |
| 29 | import org.apache.felix.scr.annotations.Property; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.apache.felix.scr.annotations.Service; |
| 33 | import org.onlab.util.KryoNamespace; |
| 34 | import org.onlab.util.Tools; |
| 35 | import org.onosproject.cfg.ComponentConfigService; |
| 36 | import org.onosproject.cluster.ClusterService; |
| 37 | import org.onosproject.cluster.NodeId; |
| 38 | import org.onosproject.core.CoreService; |
| 39 | import org.onosproject.core.IdGenerator; |
| 40 | import org.onosproject.mastership.MastershipService; |
| 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.device.DeviceService; |
| 43 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 44 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 45 | import org.onosproject.net.flow.FlowEntry; |
| 46 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 47 | import org.onosproject.net.flow.FlowId; |
| 48 | import org.onosproject.net.flow.FlowRule; |
| 49 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 50 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 51 | import org.onosproject.net.flow.FlowRuleBatchEvent; |
| 52 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 53 | import org.onosproject.net.flow.FlowRuleBatchRequest; |
| 54 | import org.onosproject.net.flow.FlowRuleEvent; |
| 55 | import org.onosproject.net.flow.FlowRuleEvent.Type; |
| 56 | import org.onosproject.net.flow.FlowRuleService; |
| 57 | import org.onosproject.net.flow.FlowRuleStore; |
| 58 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 59 | import org.onosproject.net.flow.StoredFlowEntry; |
| 60 | import org.onosproject.net.flow.TableStatisticsEntry; |
| 61 | import org.onosproject.persistence.PersistenceService; |
| 62 | import org.onosproject.store.AbstractStore; |
| 63 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 64 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 65 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 66 | import org.onosproject.store.flow.ReplicaInfoEvent; |
| 67 | import org.onosproject.store.flow.ReplicaInfoEventListener; |
| 68 | import org.onosproject.store.flow.ReplicaInfoService; |
| 69 | import org.onosproject.store.impl.MastershipBasedTimestamp; |
| 70 | import org.onosproject.store.serializers.KryoNamespaces; |
| 71 | import org.onosproject.store.serializers.StoreSerializer; |
| 72 | import org.onosproject.store.serializers.custom.DistributedStoreSerializers; |
| 73 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 74 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 75 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 76 | import org.onosproject.store.service.Serializer; |
| 77 | import org.onosproject.store.service.StorageService; |
| 78 | import org.onosproject.store.service.WallClockTimestamp; |
| 79 | import org.osgi.service.component.ComponentContext; |
| 80 | import org.slf4j.Logger; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 81 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 82 | import java.util.Collections; |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 83 | import java.util.Dictionary; |
| 84 | import java.util.HashSet; |
| 85 | import java.util.List; |
| 86 | import java.util.Map; |
| 87 | import java.util.Objects; |
| 88 | import java.util.Set; |
| 89 | import java.util.concurrent.ExecutorService; |
| 90 | import java.util.concurrent.Executors; |
| 91 | import java.util.concurrent.ScheduledExecutorService; |
| 92 | import java.util.concurrent.ScheduledFuture; |
| 93 | import java.util.concurrent.TimeUnit; |
| 94 | import java.util.concurrent.atomic.AtomicInteger; |
| 95 | import java.util.concurrent.atomic.AtomicReference; |
| 96 | import java.util.stream.Collectors; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 97 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 98 | import static com.google.common.base.Strings.isNullOrEmpty; |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 99 | import static org.onlab.util.Tools.get; |
| 100 | import static org.onlab.util.Tools.groupedThreads; |
| 101 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 102 | import static org.onosproject.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED; |
| 103 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.*; |
| 104 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Manages inventory of flow rules using a distributed state management protocol. |
| 108 | */ |
Sho SHIMIZU | 5c396e3 | 2016-08-12 15:19:12 -0700 | [diff] [blame] | 109 | @Component(immediate = true) |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 110 | @Service |
Madan Jampani | 37d04c6 | 2016-04-25 15:53:55 -0700 | [diff] [blame] | 111 | public class DistributedFlowRuleStore |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 112 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
| 113 | implements FlowRuleStore { |
| 114 | |
| 115 | private final Logger log = getLogger(getClass()); |
| 116 | |
| 117 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 8; |
| 118 | private static final boolean DEFAULT_BACKUP_ENABLED = true; |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 119 | private static final int DEFAULT_MAX_BACKUP_COUNT = 2; |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 120 | private static final boolean DEFAULT_PERSISTENCE_ENABLED = false; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 121 | private static final int DEFAULT_BACKUP_PERIOD_MILLIS = 2000; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 122 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
Madan Jampani | adea890 | 2015-06-04 17:39:45 -0700 | [diff] [blame] | 123 | // number of devices whose flow entries will be backed up in one communication round |
| 124 | private static final int FLOW_TABLE_BACKUP_BATCH_SIZE = 1; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 125 | |
| 126 | @Property(name = "msgHandlerPoolSize", intValue = MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 127 | label = "Number of threads in the message handler pool") |
| 128 | private int msgHandlerPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 129 | |
| 130 | @Property(name = "backupEnabled", boolValue = DEFAULT_BACKUP_ENABLED, |
| 131 | label = "Indicates whether backups are enabled or not") |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 132 | private volatile boolean backupEnabled = DEFAULT_BACKUP_ENABLED; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 133 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 134 | @Property(name = "backupPeriod", intValue = DEFAULT_BACKUP_PERIOD_MILLIS, |
| 135 | label = "Delay in ms between successive backup runs") |
| 136 | private int backupPeriod = DEFAULT_BACKUP_PERIOD_MILLIS; |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 137 | @Property(name = "persistenceEnabled", boolValue = false, |
| 138 | label = "Indicates whether or not changes in the flow table should be persisted to disk.") |
| 139 | private boolean persistenceEnabled = DEFAULT_PERSISTENCE_ENABLED; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 140 | |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 141 | @Property(name = "backupCount", intValue = DEFAULT_MAX_BACKUP_COUNT, |
| 142 | label = "Max number of backup copies for each device") |
| 143 | private volatile int backupCount = DEFAULT_MAX_BACKUP_COUNT; |
| 144 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 145 | private InternalFlowTable flowTable = new InternalFlowTable(); |
| 146 | |
| 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 148 | protected ReplicaInfoService replicaInfoManager; |
| 149 | |
| 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 151 | protected ClusterCommunicationService clusterCommunicator; |
| 152 | |
| 153 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 154 | protected ClusterService clusterService; |
| 155 | |
| 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 157 | protected DeviceService deviceService; |
| 158 | |
| 159 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 160 | protected CoreService coreService; |
| 161 | |
| 162 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 163 | protected ComponentConfigService configService; |
| 164 | |
| 165 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 166 | protected MastershipService mastershipService; |
| 167 | |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 168 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 169 | protected PersistenceService persistenceService; |
| 170 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 171 | private Map<Long, NodeId> pendingResponses = Maps.newConcurrentMap(); |
| 172 | private ExecutorService messageHandlingExecutor; |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 173 | private ExecutorService eventHandler; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 174 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 175 | private ScheduledFuture<?> backupTask; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 176 | private final ScheduledExecutorService backupSenderExecutor = |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 177 | Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/flow", "backup-sender", log)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 178 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 179 | private EventuallyConsistentMap<DeviceId, List<TableStatisticsEntry>> deviceTableStats; |
| 180 | private final EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> tableStatsListener = |
| 181 | new InternalTableStatsListener(); |
| 182 | |
| 183 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 184 | protected StorageService storageService; |
| 185 | |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 186 | protected static final StoreSerializer SERIALIZER = StoreSerializer.using( |
| 187 | KryoNamespace.newBuilder() |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 188 | .register(DistributedStoreSerializers.STORE_COMMON) |
| 189 | .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 190 | .build("FlowRuleStore")); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 191 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 192 | protected static final KryoNamespace.Builder SERIALIZER_BUILDER = KryoNamespace.newBuilder() |
| 193 | .register(KryoNamespaces.API) |
| 194 | .register(MastershipBasedTimestamp.class); |
| 195 | |
| 196 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 197 | private IdGenerator idGenerator; |
| 198 | private NodeId local; |
| 199 | |
| 200 | @Activate |
| 201 | public void activate(ComponentContext context) { |
| 202 | configService.registerProperties(getClass()); |
| 203 | |
| 204 | idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC); |
| 205 | |
| 206 | local = clusterService.getLocalNode().id(); |
| 207 | |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 208 | eventHandler = Executors.newSingleThreadExecutor( |
| 209 | groupedThreads("onos/flow", "event-handler", log)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 210 | messageHandlingExecutor = Executors.newFixedThreadPool( |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 211 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers", log)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 212 | |
| 213 | registerMessageHandlers(messageHandlingExecutor); |
| 214 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 215 | if (backupEnabled) { |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 216 | replicaInfoManager.addListener(flowTable); |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 217 | backupTask = backupSenderExecutor.scheduleWithFixedDelay( |
| 218 | flowTable::backup, |
| 219 | 0, |
| 220 | backupPeriod, |
| 221 | TimeUnit.MILLISECONDS); |
| 222 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 223 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 224 | deviceTableStats = storageService.<DeviceId, List<TableStatisticsEntry>>eventuallyConsistentMapBuilder() |
| 225 | .withName("onos-flow-table-stats") |
| 226 | .withSerializer(SERIALIZER_BUILDER) |
| 227 | .withAntiEntropyPeriod(5, TimeUnit.SECONDS) |
| 228 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 229 | .withTombstonesDisabled() |
| 230 | .build(); |
| 231 | deviceTableStats.addListener(tableStatsListener); |
| 232 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 233 | logConfig("Started"); |
| 234 | } |
| 235 | |
| 236 | @Deactivate |
| 237 | public void deactivate(ComponentContext context) { |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 238 | if (backupEnabled) { |
| 239 | replicaInfoManager.removeListener(flowTable); |
| 240 | backupTask.cancel(true); |
| 241 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 242 | configService.unregisterProperties(getClass(), false); |
| 243 | unregisterMessageHandlers(); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 244 | deviceTableStats.removeListener(tableStatsListener); |
| 245 | deviceTableStats.destroy(); |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 246 | eventHandler.shutdownNow(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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 | backupEnabled = DEFAULT_BACKUP_ENABLED; |
| 257 | logConfig("Default config"); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | Dictionary properties = context.getProperties(); |
| 262 | int newPoolSize; |
| 263 | boolean newBackupEnabled; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 264 | int newBackupPeriod; |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 265 | int newBackupCount; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 266 | try { |
| 267 | String s = get(properties, "msgHandlerPoolSize"); |
| 268 | newPoolSize = isNullOrEmpty(s) ? msgHandlerPoolSize : Integer.parseInt(s.trim()); |
| 269 | |
| 270 | s = get(properties, "backupEnabled"); |
| 271 | newBackupEnabled = isNullOrEmpty(s) ? backupEnabled : Boolean.parseBoolean(s.trim()); |
| 272 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 273 | s = get(properties, "backupPeriod"); |
| 274 | newBackupPeriod = isNullOrEmpty(s) ? backupPeriod : Integer.parseInt(s.trim()); |
| 275 | |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 276 | s = get(properties, "backupCount"); |
| 277 | newBackupCount = isNullOrEmpty(s) ? backupCount : Integer.parseInt(s.trim()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 278 | } catch (NumberFormatException | ClassCastException e) { |
| 279 | newPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 280 | newBackupEnabled = DEFAULT_BACKUP_ENABLED; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 281 | newBackupPeriod = DEFAULT_BACKUP_PERIOD_MILLIS; |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 282 | newBackupCount = DEFAULT_MAX_BACKUP_COUNT; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 285 | boolean restartBackupTask = false; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 286 | if (newBackupEnabled != backupEnabled) { |
| 287 | backupEnabled = newBackupEnabled; |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 288 | if (!backupEnabled) { |
| 289 | replicaInfoManager.removeListener(flowTable); |
| 290 | if (backupTask != null) { |
| 291 | backupTask.cancel(false); |
| 292 | backupTask = null; |
| 293 | } |
| 294 | } else { |
| 295 | replicaInfoManager.addListener(flowTable); |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 296 | } |
| 297 | restartBackupTask = backupEnabled; |
| 298 | } |
| 299 | if (newBackupPeriod != backupPeriod) { |
| 300 | backupPeriod = newBackupPeriod; |
| 301 | restartBackupTask = backupEnabled; |
| 302 | } |
| 303 | if (restartBackupTask) { |
| 304 | if (backupTask != null) { |
| 305 | // cancel previously running task |
| 306 | backupTask.cancel(false); |
| 307 | } |
| 308 | backupTask = backupSenderExecutor.scheduleWithFixedDelay( |
| 309 | flowTable::backup, |
| 310 | 0, |
| 311 | backupPeriod, |
| 312 | TimeUnit.MILLISECONDS); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 313 | } |
| 314 | if (newPoolSize != msgHandlerPoolSize) { |
| 315 | msgHandlerPoolSize = newPoolSize; |
| 316 | ExecutorService oldMsgHandler = messageHandlingExecutor; |
| 317 | messageHandlingExecutor = Executors.newFixedThreadPool( |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 318 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers", log)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 319 | |
| 320 | // replace previously registered handlers. |
| 321 | registerMessageHandlers(messageHandlingExecutor); |
| 322 | oldMsgHandler.shutdown(); |
| 323 | } |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 324 | if (backupCount != newBackupCount) { |
| 325 | backupCount = newBackupCount; |
| 326 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 327 | logConfig("Reconfigured"); |
| 328 | } |
| 329 | |
| 330 | private void registerMessageHandlers(ExecutorService executor) { |
| 331 | |
| 332 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new OnStoreBatch(), executor); |
| 333 | clusterCommunicator.<FlowRuleBatchEvent>addSubscriber( |
| 334 | REMOTE_APPLY_COMPLETED, SERIALIZER::decode, this::notifyDelegate, executor); |
| 335 | clusterCommunicator.addSubscriber( |
| 336 | GET_FLOW_ENTRY, SERIALIZER::decode, flowTable::getFlowEntry, SERIALIZER::encode, executor); |
| 337 | clusterCommunicator.addSubscriber( |
| 338 | GET_DEVICE_FLOW_ENTRIES, SERIALIZER::decode, flowTable::getFlowEntries, SERIALIZER::encode, executor); |
| 339 | clusterCommunicator.addSubscriber( |
| 340 | REMOVE_FLOW_ENTRY, SERIALIZER::decode, this::removeFlowRuleInternal, SERIALIZER::encode, executor); |
| 341 | clusterCommunicator.addSubscriber( |
| 342 | REMOVE_FLOW_ENTRY, SERIALIZER::decode, this::removeFlowRuleInternal, SERIALIZER::encode, executor); |
| 343 | clusterCommunicator.addSubscriber( |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 344 | FLOW_TABLE_BACKUP, SERIALIZER::decode, flowTable::onBackupReceipt, SERIALIZER::encode, executor); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | private void unregisterMessageHandlers() { |
| 348 | clusterCommunicator.removeSubscriber(REMOVE_FLOW_ENTRY); |
| 349 | clusterCommunicator.removeSubscriber(GET_DEVICE_FLOW_ENTRIES); |
| 350 | clusterCommunicator.removeSubscriber(GET_FLOW_ENTRY); |
| 351 | clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS); |
| 352 | clusterCommunicator.removeSubscriber(REMOTE_APPLY_COMPLETED); |
| 353 | clusterCommunicator.removeSubscriber(FLOW_TABLE_BACKUP); |
| 354 | } |
| 355 | |
| 356 | private void logConfig(String prefix) { |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 357 | log.info("{} with msgHandlerPoolSize = {}; backupEnabled = {}, backupPeriod = {}, backupCount = {}", |
| 358 | prefix, msgHandlerPoolSize, backupEnabled, backupPeriod, backupCount); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // This is not a efficient operation on a distributed sharded |
| 362 | // flow store. We need to revisit the need for this operation or at least |
| 363 | // make it device specific. |
| 364 | @Override |
| 365 | public int getFlowRuleCount() { |
| 366 | AtomicInteger sum = new AtomicInteger(0); |
| 367 | deviceService.getDevices().forEach(device -> sum.addAndGet(Iterables.size(getFlowEntries(device.id())))); |
| 368 | return sum.get(); |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public FlowEntry getFlowEntry(FlowRule rule) { |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 373 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 374 | |
| 375 | if (master == null) { |
Thomas Vachuska | 88fd690 | 2015-08-04 10:08:34 -0700 | [diff] [blame] | 376 | log.debug("Failed to getFlowEntry: No master for {}", rule.deviceId()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 377 | return null; |
| 378 | } |
| 379 | |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 380 | if (Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 381 | return flowTable.getFlowEntry(rule); |
| 382 | } |
| 383 | |
| 384 | log.trace("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
| 385 | master, rule.deviceId()); |
| 386 | |
| 387 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(rule, |
| 388 | FlowStoreMessageSubjects.GET_FLOW_ENTRY, |
| 389 | SERIALIZER::encode, |
| 390 | SERIALIZER::decode, |
| 391 | master), |
| 392 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 393 | TimeUnit.MILLISECONDS, |
| 394 | null); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 399 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 400 | |
| 401 | if (master == null) { |
Thomas Vachuska | 88fd690 | 2015-08-04 10:08:34 -0700 | [diff] [blame] | 402 | log.debug("Failed to getFlowEntries: No master for {}", deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 403 | return Collections.emptyList(); |
| 404 | } |
| 405 | |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 406 | if (Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 407 | return flowTable.getFlowEntries(deviceId); |
| 408 | } |
| 409 | |
| 410 | log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", |
| 411 | master, deviceId); |
| 412 | |
| 413 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(deviceId, |
| 414 | FlowStoreMessageSubjects.GET_DEVICE_FLOW_ENTRIES, |
| 415 | SERIALIZER::encode, |
| 416 | SERIALIZER::decode, |
| 417 | master), |
| 418 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 419 | TimeUnit.MILLISECONDS, |
| 420 | Collections.emptyList()); |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public void storeFlowRule(FlowRule rule) { |
| 425 | storeBatch(new FlowRuleBatchOperation( |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 426 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)), |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 427 | rule.deviceId(), idGenerator.getNewId())); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public void storeBatch(FlowRuleBatchOperation operation) { |
| 432 | if (operation.getOperations().isEmpty()) { |
| 433 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 434 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 435 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | DeviceId deviceId = operation.deviceId(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 440 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 441 | |
| 442 | if (master == null) { |
| 443 | log.warn("No master for {} : flows will be marked for removal", deviceId); |
| 444 | |
| 445 | updateStoreInternal(operation); |
| 446 | |
| 447 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 448 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 449 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
| 450 | return; |
| 451 | } |
| 452 | |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 453 | if (Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 454 | storeBatchInternal(operation); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
| 459 | master, deviceId); |
| 460 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 461 | clusterCommunicator.unicast(operation, |
| 462 | APPLY_BATCH_FLOWS, |
| 463 | SERIALIZER::encode, |
| 464 | master) |
| 465 | .whenComplete((result, error) -> { |
Thomas Vachuska | a267ce4 | 2015-05-27 16:14:23 -0700 | [diff] [blame] | 466 | if (error != null) { |
Madan Jampani | 15f1bc4 | 2015-05-28 10:51:52 -0700 | [diff] [blame] | 467 | log.warn("Failed to storeBatch: {} to {}", operation, master, error); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 468 | |
Thomas Vachuska | a267ce4 | 2015-05-27 16:14:23 -0700 | [diff] [blame] | 469 | Set<FlowRule> allFailures = operation.getOperations() |
| 470 | .stream() |
| 471 | .map(op -> op.target()) |
| 472 | .collect(Collectors.toSet()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 473 | |
Thomas Vachuska | a267ce4 | 2015-05-27 16:14:23 -0700 | [diff] [blame] | 474 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 475 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 476 | new CompletedBatchOperation(false, allFailures, deviceId))); |
| 477 | } |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 478 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | private void storeBatchInternal(FlowRuleBatchOperation operation) { |
| 482 | |
| 483 | final DeviceId did = operation.deviceId(); |
| 484 | //final Collection<FlowEntry> ft = flowTable.getFlowEntries(did); |
| 485 | Set<FlowRuleBatchEntry> currentOps = updateStoreInternal(operation); |
| 486 | if (currentOps.isEmpty()) { |
| 487 | batchOperationComplete(FlowRuleBatchEvent.completed( |
| 488 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 489 | new CompletedBatchOperation(true, Collections.emptySet(), did))); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | notifyDelegate(FlowRuleBatchEvent.requested(new |
| 494 | FlowRuleBatchRequest(operation.id(), |
| 495 | currentOps), operation.deviceId())); |
| 496 | } |
| 497 | |
| 498 | private Set<FlowRuleBatchEntry> updateStoreInternal(FlowRuleBatchOperation operation) { |
| 499 | return operation.getOperations().stream().map( |
| 500 | op -> { |
| 501 | StoredFlowEntry entry; |
| 502 | switch (op.operator()) { |
| 503 | case ADD: |
| 504 | entry = new DefaultFlowEntry(op.target()); |
| 505 | // always add requested FlowRule |
| 506 | // Note: 2 equal FlowEntry may have different treatment |
| 507 | flowTable.remove(entry.deviceId(), entry); |
| 508 | flowTable.add(entry); |
| 509 | |
| 510 | return op; |
| 511 | case REMOVE: |
| 512 | entry = flowTable.getFlowEntry(op.target()); |
| 513 | if (entry != null) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 514 | //FIXME modification of "stored" flow entry outside of flow table |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 515 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 516 | log.debug("Setting state of rule to pending remove: {}", entry); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 517 | return op; |
| 518 | } |
| 519 | break; |
| 520 | case MODIFY: |
| 521 | //TODO: figure this out at some point |
| 522 | break; |
| 523 | default: |
| 524 | log.warn("Unknown flow operation operator: {}", op.operator()); |
| 525 | } |
| 526 | return null; |
| 527 | } |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 528 | ).filter(Objects::nonNull).collect(Collectors.toSet()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | @Override |
| 532 | public void deleteFlowRule(FlowRule rule) { |
| 533 | storeBatch( |
| 534 | new FlowRuleBatchOperation( |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 535 | Collections.singletonList( |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 536 | new FlowRuleBatchEntry( |
| 537 | FlowRuleOperation.REMOVE, |
| 538 | rule)), rule.deviceId(), idGenerator.getNewId())); |
| 539 | } |
| 540 | |
| 541 | @Override |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 542 | public FlowRuleEvent pendingFlowRule(FlowEntry rule) { |
| 543 | if (mastershipService.isLocalMaster(rule.deviceId())) { |
| 544 | StoredFlowEntry stored = flowTable.getFlowEntry(rule); |
| 545 | if (stored != null && |
| 546 | stored.state() != FlowEntryState.PENDING_ADD) { |
| 547 | stored.setState(FlowEntryState.PENDING_ADD); |
| 548 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 549 | } |
| 550 | } |
| 551 | return null; |
| 552 | } |
| 553 | |
| 554 | @Override |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 555 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 556 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 557 | if (Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 558 | return addOrUpdateFlowRuleInternal(rule); |
| 559 | } |
| 560 | |
| 561 | log.warn("Tried to update FlowRule {} state," |
| 562 | + " while the Node was not the master.", rule); |
| 563 | return null; |
| 564 | } |
| 565 | |
| 566 | private FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
| 567 | // check if this new rule is an update to an existing entry |
| 568 | StoredFlowEntry stored = flowTable.getFlowEntry(rule); |
| 569 | if (stored != null) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 570 | //FIXME modification of "stored" flow entry outside of flow table |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 571 | stored.setBytes(rule.bytes()); |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 572 | stored.setLife(rule.life(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 573 | stored.setPackets(rule.packets()); |
Jonathan Hart | 89e981f | 2016-01-04 13:59:55 -0800 | [diff] [blame] | 574 | stored.setLastSeen(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 575 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 576 | stored.setState(FlowEntryState.ADDED); |
| 577 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 578 | } |
| 579 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 580 | } |
| 581 | |
| 582 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
| 583 | // TODO: also update backup if the behavior is correct. |
| 584 | flowTable.add(rule); |
| 585 | return null; |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 590 | final DeviceId deviceId = rule.deviceId(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 591 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 592 | |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 593 | if (Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 594 | // bypass and handle it locally |
| 595 | return removeFlowRuleInternal(rule); |
| 596 | } |
| 597 | |
| 598 | if (master == null) { |
| 599 | log.warn("Failed to removeFlowRule: No master for {}", deviceId); |
| 600 | // TODO: revisit if this should be null (="no-op") or Exception |
| 601 | return null; |
| 602 | } |
| 603 | |
| 604 | log.trace("Forwarding removeFlowRule to {}, which is the master for device {}", |
| 605 | master, deviceId); |
| 606 | |
Madan Jampani | 222229e | 2016-07-14 10:43:25 -0700 | [diff] [blame] | 607 | return Futures.getUnchecked(clusterCommunicator.sendAndReceive( |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 608 | rule, |
| 609 | REMOVE_FLOW_ENTRY, |
| 610 | SERIALIZER::encode, |
| 611 | SERIALIZER::decode, |
Madan Jampani | 222229e | 2016-07-14 10:43:25 -0700 | [diff] [blame] | 612 | master)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | private FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
| 616 | final DeviceId deviceId = rule.deviceId(); |
| 617 | // This is where one could mark a rule as removed and still keep it in the store. |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 618 | final FlowEntry removed = flowTable.remove(deviceId, rule); |
| 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; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | @Override |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 624 | public void purgeFlowRule(DeviceId deviceId) { |
| 625 | flowTable.purgeFlowRule(deviceId); |
| 626 | } |
| 627 | |
| 628 | @Override |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 629 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
| 630 | //FIXME: need a per device pending response |
| 631 | NodeId nodeId = pendingResponses.remove(event.subject().batchId()); |
| 632 | if (nodeId == null) { |
| 633 | notifyDelegate(event); |
| 634 | } else { |
| 635 | // TODO check unicast return value |
| 636 | clusterCommunicator.unicast(event, REMOTE_APPLY_COMPLETED, SERIALIZER::encode, nodeId); |
| 637 | //error log: log.warn("Failed to respond to peer for batch operation result"); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | private final class OnStoreBatch implements ClusterMessageHandler { |
| 642 | |
| 643 | @Override |
| 644 | public void handle(final ClusterMessage message) { |
| 645 | FlowRuleBatchOperation operation = SERIALIZER.decode(message.payload()); |
| 646 | log.debug("received batch request {}", operation); |
| 647 | |
| 648 | final DeviceId deviceId = operation.deviceId(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 649 | NodeId master = mastershipService.getMasterFor(deviceId); |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 650 | if (!Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 651 | Set<FlowRule> failures = new HashSet<>(operation.size()); |
| 652 | for (FlowRuleBatchEntry op : operation.getOperations()) { |
| 653 | failures.add(op.target()); |
| 654 | } |
| 655 | CompletedBatchOperation allFailed = new CompletedBatchOperation(false, failures, deviceId); |
| 656 | // This node is no longer the master, respond as all failed. |
| 657 | // TODO: we might want to wrap response in envelope |
| 658 | // to distinguish sw programming failure and hand over |
| 659 | // it make sense in the latter case to retry immediately. |
| 660 | message.respond(SERIALIZER.encode(allFailed)); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | pendingResponses.put(operation.id(), message.sender()); |
| 665 | storeBatchInternal(operation); |
| 666 | } |
| 667 | } |
| 668 | |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 669 | private class BackupOperation { |
| 670 | private final NodeId nodeId; |
| 671 | private final DeviceId deviceId; |
| 672 | |
| 673 | public BackupOperation(NodeId nodeId, DeviceId deviceId) { |
| 674 | this.nodeId = nodeId; |
| 675 | this.deviceId = deviceId; |
| 676 | } |
| 677 | |
| 678 | @Override |
| 679 | public int hashCode() { |
| 680 | return Objects.hash(nodeId, deviceId); |
| 681 | } |
| 682 | |
| 683 | @Override |
| 684 | public boolean equals(Object other) { |
| 685 | if (other != null && other instanceof BackupOperation) { |
| 686 | BackupOperation that = (BackupOperation) other; |
| 687 | return this.nodeId.equals(that.nodeId) && |
| 688 | this.deviceId.equals(that.deviceId); |
| 689 | } else { |
| 690 | return false; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 695 | private class InternalFlowTable implements ReplicaInfoEventListener { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 696 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 697 | //TODO replace the Map<V,V> with ExtendedSet |
| 698 | private final Map<DeviceId, Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>> |
Madan Jampani | 5c3766c | 2015-06-02 15:54:41 -0700 | [diff] [blame] | 699 | flowEntries = Maps.newConcurrentMap(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 700 | |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 701 | private final Map<BackupOperation, Long> lastBackupTimes = Maps.newConcurrentMap(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 702 | private final Map<DeviceId, Long> lastUpdateTimes = Maps.newConcurrentMap(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 703 | |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 704 | @Override |
| 705 | public void event(ReplicaInfoEvent event) { |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 706 | eventHandler.execute(() -> handleEvent(event)); |
| 707 | } |
| 708 | |
| 709 | private void handleEvent(ReplicaInfoEvent event) { |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 710 | DeviceId deviceId = event.subject(); |
| 711 | if (!backupEnabled || !mastershipService.isLocalMaster(deviceId)) { |
Madan Jampani | a98bf93 | 2015-06-02 12:01:36 -0700 | [diff] [blame] | 712 | return; |
| 713 | } |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 714 | if (event.type() == MASTER_CHANGED) { |
| 715 | lastUpdateTimes.put(deviceId, System.currentTimeMillis()); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 716 | } |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 717 | backupSenderExecutor.schedule(this::backup, 0, TimeUnit.SECONDS); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Madan Jampani | adea890 | 2015-06-04 17:39:45 -0700 | [diff] [blame] | 720 | private void sendBackups(NodeId nodeId, Set<DeviceId> deviceIds) { |
| 721 | // split up the devices into smaller batches and send them separately. |
| 722 | Iterables.partition(deviceIds, FLOW_TABLE_BACKUP_BATCH_SIZE) |
| 723 | .forEach(ids -> backupFlowEntries(nodeId, Sets.newHashSet(ids))); |
| 724 | } |
| 725 | |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 726 | private void backupFlowEntries(NodeId nodeId, Set<DeviceId> deviceIds) { |
Madan Jampani | adea890 | 2015-06-04 17:39:45 -0700 | [diff] [blame] | 727 | if (deviceIds.isEmpty()) { |
| 728 | return; |
| 729 | } |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 730 | log.debug("Sending flowEntries for devices {} to {} for backup.", deviceIds, nodeId); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 731 | Map<DeviceId, Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>> |
| 732 | deviceFlowEntries = Maps.newConcurrentMap(); |
Madan Jampani | 85a9b0d | 2015-06-03 17:15:44 -0700 | [diff] [blame] | 733 | deviceIds.forEach(id -> deviceFlowEntries.put(id, ImmutableMap.copyOf(getFlowTable(id)))); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 734 | clusterCommunicator.<Map<DeviceId, |
| 735 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>>, |
| 736 | Set<DeviceId>> |
| 737 | sendAndReceive(deviceFlowEntries, |
| 738 | FLOW_TABLE_BACKUP, |
| 739 | SERIALIZER::encode, |
| 740 | SERIALIZER::decode, |
| 741 | nodeId) |
| 742 | .whenComplete((backedupDevices, error) -> { |
| 743 | Set<DeviceId> devicesNotBackedup = error != null ? |
| 744 | deviceFlowEntries.keySet() : |
| 745 | Sets.difference(deviceFlowEntries.keySet(), backedupDevices); |
| 746 | if (devicesNotBackedup.size() > 0) { |
| 747 | log.warn("Failed to backup devices: {}. Reason: {}", |
Thomas Vachuska | c10a948 | 2016-08-03 10:31:11 -0700 | [diff] [blame] | 748 | devicesNotBackedup, error != null ? error.getMessage() : "none"); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 749 | } |
| 750 | if (backedupDevices != null) { |
| 751 | backedupDevices.forEach(id -> { |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 752 | lastBackupTimes.put(new BackupOperation(nodeId, id), System.currentTimeMillis()); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 753 | }); |
| 754 | } |
| 755 | }); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 758 | /** |
| 759 | * Returns the flow table for specified device. |
| 760 | * |
| 761 | * @param deviceId identifier of the device |
| 762 | * @return Map representing Flow Table of given device. |
| 763 | */ |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 764 | private Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>> getFlowTable(DeviceId deviceId) { |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 765 | if (persistenceEnabled) { |
| 766 | return flowEntries.computeIfAbsent(deviceId, id -> persistenceService |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 767 | .<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>persistentMapBuilder() |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 768 | .withName("FlowTable:" + deviceId.toString()) |
| 769 | .withSerializer(new Serializer() { |
| 770 | @Override |
| 771 | public <T> byte[] encode(T object) { |
| 772 | return SERIALIZER.encode(object); |
| 773 | } |
| 774 | |
| 775 | @Override |
| 776 | public <T> T decode(byte[] bytes) { |
| 777 | return SERIALIZER.decode(bytes); |
| 778 | } |
| 779 | }) |
| 780 | .build()); |
| 781 | } else { |
| 782 | return flowEntries.computeIfAbsent(deviceId, id -> Maps.newConcurrentMap()); |
| 783 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 786 | private Map<StoredFlowEntry, StoredFlowEntry> getFlowEntriesInternal(DeviceId deviceId, FlowId flowId) { |
| 787 | return getFlowTable(deviceId).computeIfAbsent(flowId, id -> Maps.newConcurrentMap()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | private StoredFlowEntry getFlowEntryInternal(FlowRule rule) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 791 | return getFlowEntriesInternal(rule.deviceId(), rule.id()).get(rule); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 795 | return getFlowTable(deviceId).values().stream() |
| 796 | .flatMap(m -> m.values().stream()) |
| 797 | .collect(Collectors.toSet()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | public StoredFlowEntry getFlowEntry(FlowRule rule) { |
| 801 | return getFlowEntryInternal(rule); |
| 802 | } |
| 803 | |
| 804 | public Set<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 805 | return getFlowEntriesInternal(deviceId); |
| 806 | } |
| 807 | |
| 808 | public void add(FlowEntry rule) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 809 | getFlowEntriesInternal(rule.deviceId(), rule.id()) |
| 810 | .compute((StoredFlowEntry) rule, (k, stored) -> { |
| 811 | //TODO compare stored and rule timestamps |
| 812 | //TODO the key is not updated |
| 813 | return (StoredFlowEntry) rule; |
| 814 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 815 | lastUpdateTimes.put(rule.deviceId(), System.currentTimeMillis()); |
| 816 | } |
| 817 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 818 | public FlowEntry remove(DeviceId deviceId, FlowEntry rule) { |
| 819 | final AtomicReference<FlowEntry> removedRule = new AtomicReference<>(); |
| 820 | getFlowEntriesInternal(rule.deviceId(), rule.id()) |
| 821 | .computeIfPresent((StoredFlowEntry) rule, (k, stored) -> { |
| 822 | if (rule instanceof DefaultFlowEntry) { |
| 823 | DefaultFlowEntry toRemove = (DefaultFlowEntry) rule; |
| 824 | if (stored instanceof DefaultFlowEntry) { |
| 825 | DefaultFlowEntry storedEntry = (DefaultFlowEntry) stored; |
| 826 | if (toRemove.created() < storedEntry.created()) { |
| 827 | log.debug("Trying to remove more recent flow entry {} (stored: {})", |
| 828 | toRemove, stored); |
| 829 | // the key is not updated, removedRule remains null |
| 830 | return stored; |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | removedRule.set(stored); |
| 835 | return null; |
| 836 | }); |
| 837 | |
| 838 | if (removedRule.get() != null) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 839 | lastUpdateTimes.put(deviceId, System.currentTimeMillis()); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 840 | return removedRule.get(); |
| 841 | } else { |
| 842 | return null; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 846 | public void purgeFlowRule(DeviceId deviceId) { |
| 847 | flowEntries.remove(deviceId); |
| 848 | } |
| 849 | |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 850 | private List<NodeId> getBackupNodes(DeviceId deviceId) { |
| 851 | // The returned backup node list is in the order of preference i.e. next likely master first. |
| 852 | List<NodeId> allPossibleBackupNodes = replicaInfoManager.getReplicaInfoFor(deviceId).backups(); |
| 853 | return ImmutableList.copyOf(allPossibleBackupNodes) |
| 854 | .subList(0, Math.min(allPossibleBackupNodes.size(), backupCount)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | private void backup() { |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 858 | if (!backupEnabled) { |
| 859 | return; |
| 860 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 861 | try { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 862 | // compute a mapping from node to the set of devices whose flow entries it should backup |
| 863 | Map<NodeId, Set<DeviceId>> devicesToBackupByNode = Maps.newHashMap(); |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 864 | flowEntries.keySet().forEach(deviceId -> { |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 865 | List<NodeId> backupNodes = getBackupNodes(deviceId); |
| 866 | backupNodes.forEach(backupNode -> { |
| 867 | if (lastBackupTimes.getOrDefault(new BackupOperation(backupNode, deviceId), 0L) |
| 868 | < lastUpdateTimes.getOrDefault(deviceId, 0L)) { |
| 869 | devicesToBackupByNode.computeIfAbsent(backupNode, |
| 870 | nodeId -> Sets.newHashSet()).add(deviceId); |
| 871 | } |
| 872 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 873 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 874 | // send the device flow entries to their respective backup nodes |
Madan Jampani | adea890 | 2015-06-04 17:39:45 -0700 | [diff] [blame] | 875 | devicesToBackupByNode.forEach(this::sendBackups); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 876 | } catch (Exception e) { |
| 877 | log.error("Backup failed.", e); |
| 878 | } |
| 879 | } |
| 880 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 881 | private Set<DeviceId> onBackupReceipt(Map<DeviceId, |
| 882 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>> flowTables) { |
Madan Jampani | 7267c55 | 2015-05-20 22:39:17 -0700 | [diff] [blame] | 883 | log.debug("Received flowEntries for {} to backup", flowTables.keySet()); |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 884 | Set<DeviceId> backedupDevices = Sets.newHashSet(); |
| 885 | try { |
Madan Jampani | a98bf93 | 2015-06-02 12:01:36 -0700 | [diff] [blame] | 886 | flowTables.forEach((deviceId, deviceFlowTable) -> { |
| 887 | // Only process those devices are that not managed by the local node. |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 888 | if (!Objects.equals(local, mastershipService.getMasterFor(deviceId))) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 889 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>> backupFlowTable = |
| 890 | getFlowTable(deviceId); |
Madan Jampani | a98bf93 | 2015-06-02 12:01:36 -0700 | [diff] [blame] | 891 | backupFlowTable.clear(); |
| 892 | backupFlowTable.putAll(deviceFlowTable); |
| 893 | backedupDevices.add(deviceId); |
| 894 | } |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 895 | }); |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 896 | } catch (Exception e) { |
| 897 | log.warn("Failure processing backup request", e); |
| 898 | } |
| 899 | return backedupDevices; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 900 | } |
| 901 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 902 | |
| 903 | @Override |
| 904 | public FlowRuleEvent updateTableStatistics(DeviceId deviceId, |
| 905 | List<TableStatisticsEntry> tableStats) { |
| 906 | deviceTableStats.put(deviceId, tableStats); |
| 907 | return null; |
| 908 | } |
| 909 | |
| 910 | @Override |
| 911 | public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) { |
| 912 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 913 | |
| 914 | if (master == null) { |
| 915 | log.debug("Failed to getTableStats: No master for {}", deviceId); |
| 916 | return Collections.emptyList(); |
| 917 | } |
| 918 | |
| 919 | List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId); |
| 920 | if (tableStats == null) { |
| 921 | return Collections.emptyList(); |
| 922 | } |
| 923 | return ImmutableList.copyOf(tableStats); |
| 924 | } |
| 925 | |
| 926 | private class InternalTableStatsListener |
| 927 | implements EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> { |
| 928 | @Override |
| 929 | public void event(EventuallyConsistentMapEvent<DeviceId, |
| 930 | List<TableStatisticsEntry>> event) { |
| 931 | //TODO: Generate an event to listeners (do we need?) |
| 932 | } |
| 933 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 934 | } |