Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 19 | import java.util.List; |
| 20 | import java.util.Map; |
| 21 | import java.util.Objects; |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 22 | import java.util.Random; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 23 | import java.util.Set; |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 24 | import java.util.concurrent.CompletableFuture; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 25 | import java.util.concurrent.ExecutorService; |
| 26 | import java.util.concurrent.Executors; |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 27 | import java.util.concurrent.ScheduledExecutorService; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 28 | import java.util.concurrent.TimeUnit; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 29 | import java.util.function.Supplier; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 30 | import java.util.stream.Collectors; |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 31 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 32 | import com.google.common.collect.ImmutableList; |
| 33 | import com.google.common.collect.Iterables; |
| 34 | import com.google.common.collect.Maps; |
| 35 | import com.google.common.collect.Sets; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 36 | import com.google.common.collect.Streams; |
| 37 | import org.apache.felix.scr.annotations.Activate; |
| 38 | import org.apache.felix.scr.annotations.Component; |
| 39 | import org.apache.felix.scr.annotations.Deactivate; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 40 | import org.apache.felix.scr.annotations.Reference; |
| 41 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 42 | import org.apache.felix.scr.annotations.Service; |
| 43 | import org.onlab.util.KryoNamespace; |
| 44 | import org.onlab.util.Tools; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 45 | import org.onosproject.cluster.ClusterService; |
| 46 | import org.onosproject.cluster.NodeId; |
| 47 | import org.onosproject.core.CoreService; |
| 48 | import org.onosproject.core.IdGenerator; |
| 49 | import org.onosproject.mastership.MastershipService; |
| 50 | import org.onosproject.net.DeviceId; |
| 51 | import org.onosproject.net.device.DeviceService; |
| 52 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 53 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 54 | import org.onosproject.net.flow.FlowEntry; |
| 55 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 56 | import org.onosproject.net.flow.FlowId; |
| 57 | import org.onosproject.net.flow.FlowRule; |
| 58 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 59 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 60 | import org.onosproject.net.flow.FlowRuleBatchEvent; |
| 61 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 62 | import org.onosproject.net.flow.FlowRuleBatchRequest; |
| 63 | import org.onosproject.net.flow.FlowRuleEvent; |
| 64 | import org.onosproject.net.flow.FlowRuleEvent.Type; |
| 65 | import org.onosproject.net.flow.FlowRuleService; |
| 66 | import org.onosproject.net.flow.FlowRuleStore; |
| 67 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 68 | import org.onosproject.net.flow.StoredFlowEntry; |
| 69 | import org.onosproject.net.flow.TableStatisticsEntry; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 70 | import org.onosproject.store.AbstractStore; |
| 71 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 72 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 73 | import org.onosproject.store.impl.MastershipBasedTimestamp; |
| 74 | import org.onosproject.store.serializers.KryoNamespaces; |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 75 | import org.onosproject.store.service.AsyncDocumentTree; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 76 | import org.onosproject.store.service.DocumentPath; |
| 77 | import org.onosproject.store.service.DocumentTree; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 78 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 79 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 80 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Jordan Halterman | 6fd3291 | 2017-09-12 15:07:18 -0700 | [diff] [blame] | 81 | import org.onosproject.store.service.IllegalDocumentModificationException; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 82 | import org.onosproject.store.service.NoSuchDocumentPathException; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 83 | import org.onosproject.store.service.Serializer; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 84 | import org.onosproject.store.service.StorageException; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 85 | import org.onosproject.store.service.StorageService; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 86 | import org.onosproject.store.service.Versioned; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 87 | import org.onosproject.store.service.WallClockTimestamp; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 88 | import org.slf4j.Logger; |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 89 | |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 90 | import static org.onlab.util.Tools.groupedThreads; |
| 91 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 92 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_UPDATED; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 93 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Manages inventory of flow rules using a distributed state management protocol. |
| 97 | */ |
Sho SHIMIZU | 5c396e3 | 2016-08-12 15:19:12 -0700 | [diff] [blame] | 98 | @Component(immediate = true) |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 99 | @Service |
Madan Jampani | 37d04c6 | 2016-04-25 15:53:55 -0700 | [diff] [blame] | 100 | public class DistributedFlowRuleStore |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 101 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
| 102 | implements FlowRuleStore { |
| 103 | |
| 104 | private final Logger log = getLogger(getClass()); |
| 105 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 106 | // Constant exception used to indicate an atomic read-modify-write operation needs to be retried. |
| 107 | // We don't want to populate a stack trace every time an optimistic lock is retried. |
| 108 | private static final StorageException.ConcurrentModification RETRY; |
| 109 | |
| 110 | // Initialize retry exception with an empty stack trace. |
| 111 | static { |
| 112 | RETRY = new StorageException.ConcurrentModification(); |
| 113 | RETRY.setStackTrace(new StackTraceElement[0]); |
| 114 | } |
| 115 | |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 116 | private static final int SCHEDULED_THREAD_POOL_SIZE = 8; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 117 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 8; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 118 | private static final int MAX_RETRY_DELAY_MILLIS = 50; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 119 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 120 | private static final String FLOW_TABLE = "onos-flow-table"; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 121 | |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 122 | private static final MessageSubject APPLY_BATCH_FLOWS = new MessageSubject("onos-flow-apply"); |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 123 | private static final MessageSubject COMPLETE_BATCH = new MessageSubject("onos-flow-batch-complete"); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 126 | protected DeviceService deviceService; |
| 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | protected CoreService coreService; |
| 130 | |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 132 | protected MastershipService mastershipService; |
| 133 | |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 135 | protected ClusterCommunicationService clusterCommunicator; |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 136 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 138 | protected ClusterService clusterService; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 139 | |
| 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 141 | protected StorageService storageService; |
| 142 | |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 143 | protected final Serializer serializer = Serializer.using(KryoNamespaces.API); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 144 | |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 145 | protected final KryoNamespace.Builder serializerBuilder = KryoNamespace.newBuilder() |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 146 | .register(KryoNamespaces.API) |
| 147 | .register(MastershipBasedTimestamp.class); |
| 148 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 149 | private EventuallyConsistentMap<DeviceId, List<TableStatisticsEntry>> deviceTableStats; |
| 150 | private final EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> tableStatsListener = |
| 151 | new InternalTableStatsListener(); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 152 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 153 | private Set<Long> pendingBatches = Sets.newConcurrentHashSet(); |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 154 | private ScheduledExecutorService scheduledExecutor; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 155 | private ExecutorService messageHandlingExecutor; |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 156 | private final Random random = new Random(); |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 157 | |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 158 | private AsyncDocumentTree<Map<StoredFlowEntry, StoredFlowEntry>> asyncFlows; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 159 | private DocumentTree<Map<StoredFlowEntry, StoredFlowEntry>> flows; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 160 | private IdGenerator idGenerator; |
| 161 | private NodeId local; |
| 162 | |
| 163 | @Activate |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 164 | public void activate() { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 165 | idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC); |
| 166 | |
| 167 | local = clusterService.getLocalNode().id(); |
| 168 | |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 169 | scheduledExecutor = Executors.newScheduledThreadPool( |
| 170 | SCHEDULED_THREAD_POOL_SIZE, |
| 171 | groupedThreads("onos/store/flow", "schedulers", log)); |
| 172 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 173 | messageHandlingExecutor = Executors.newFixedThreadPool( |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 174 | MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 175 | groupedThreads("onos/store/flow", "message-handlers", log)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 176 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 177 | deviceTableStats = storageService.<DeviceId, List<TableStatisticsEntry>>eventuallyConsistentMapBuilder() |
| 178 | .withName("onos-flow-table-stats") |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 179 | .withSerializer(serializerBuilder) |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 180 | .withAntiEntropyPeriod(5, TimeUnit.SECONDS) |
| 181 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 182 | .withTombstonesDisabled() |
| 183 | .build(); |
| 184 | deviceTableStats.addListener(tableStatsListener); |
| 185 | |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 186 | asyncFlows = storageService.<Map<StoredFlowEntry, StoredFlowEntry>>documentTreeBuilder() |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 187 | .withName(FLOW_TABLE) |
| 188 | .withSerializer(serializer) |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 189 | .buildDocumentTree(); |
| 190 | flows = asyncFlows.asDocumentTree(); |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 191 | |
| 192 | clusterCommunicator.addSubscriber( |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 193 | APPLY_BATCH_FLOWS, |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 194 | serializer::decode, |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 195 | this::applyBatchFlows, |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 196 | messageHandlingExecutor); |
| 197 | clusterCommunicator.addSubscriber( |
| 198 | COMPLETE_BATCH, |
| 199 | serializer::decode, |
| 200 | this::completeBatch, |
| 201 | messageHandlingExecutor); |
| 202 | |
| 203 | log.info("Started"); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Deactivate |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 207 | public void deactivate() { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 208 | deviceTableStats.removeListener(tableStatsListener); |
| 209 | deviceTableStats.destroy(); |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 210 | clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS); |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 211 | clusterCommunicator.removeSubscriber(COMPLETE_BATCH); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 212 | messageHandlingExecutor.shutdownNow(); |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 213 | scheduledExecutor.shutdownNow(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 214 | log.info("Stopped"); |
| 215 | } |
| 216 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Retries the given supplier until successful. |
| 219 | * <p> |
| 220 | * This method retries the given supplier until no {@code ConcurrentModification} exceptions are thrown. In |
| 221 | * between retries, it waits a semi-random interval to attempt to avoid transaction conflicts with other processes. |
| 222 | * |
| 223 | * @param supplier the supplier to retry |
| 224 | * @param <T> the return type |
| 225 | * @return the return value of the given supplier once it runs successfully |
| 226 | */ |
| 227 | private <T> T retryUntilSuccess(Supplier<T> supplier) { |
| 228 | return Tools.retryable( |
| 229 | supplier, |
| 230 | StorageException.ConcurrentModification.class, |
| 231 | Integer.MAX_VALUE, |
| 232 | MAX_RETRY_DELAY_MILLIS) |
| 233 | .get(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 236 | /** |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 237 | * Retries the given asynchronous supplier until successful. |
| 238 | * <p> |
| 239 | * This method retries the given supplier until no {@code ConcurrentModification} exceptions are thrown. In |
| 240 | * between retries, it waits a semi-random interval to attempt to avoid transaction conflicts with other processes. |
| 241 | * |
| 242 | * @param supplier the supplier to retry |
| 243 | * @param <T> the return type |
| 244 | * @return the return value of the given supplier once it runs successfully |
| 245 | */ |
| 246 | private <T> CompletableFuture<T> retryAsyncUntilSuccess(Supplier<CompletableFuture<T>> supplier) { |
| 247 | return retryAsyncUntilSuccess(supplier, new CompletableFuture<>()); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Retries the given asynchronous supplier until successful. |
| 252 | * <p> |
| 253 | * This method retries the given supplier until no {@code ConcurrentModification} exceptions are thrown. In |
| 254 | * between retries, it waits a semi-random interval to attempt to avoid transaction conflicts with other processes. |
| 255 | * |
| 256 | * @param supplier the supplier to retry |
| 257 | * @param future future to be completed once the operation has been successful |
| 258 | * @param <T> the return type |
| 259 | * @return the return value of the given supplier once it runs successfully |
| 260 | */ |
| 261 | private <T> CompletableFuture<T> retryAsyncUntilSuccess( |
| 262 | Supplier<CompletableFuture<T>> supplier, |
| 263 | CompletableFuture<T> future) { |
| 264 | supplier.get().whenComplete((result, error) -> { |
| 265 | if (error == null) { |
| 266 | future.complete(result); |
| 267 | } else { |
| 268 | Throwable cause = error.getCause() != null ? error.getCause() : error; |
| 269 | if (cause instanceof StorageException.ConcurrentModification) { |
| 270 | scheduledExecutor.schedule( |
| 271 | () -> retryAsyncUntilSuccess(supplier, future), |
| 272 | random.nextInt(50), |
| 273 | TimeUnit.MILLISECONDS); |
| 274 | } else { |
| 275 | future.completeExceptionally(error); |
| 276 | } |
| 277 | } |
| 278 | }); |
| 279 | return future; |
| 280 | } |
| 281 | |
| 282 | /** |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 283 | * Return method for {@link #retryUntilSuccess(Supplier)} callbacks to indicate that the callback needs to be |
| 284 | * retried after a randomized delay. |
| 285 | * |
| 286 | * @param <T> the return type |
| 287 | * @return nothing |
| 288 | * @throws StorageException.ConcurrentModification to force a retry of the callback |
| 289 | */ |
| 290 | private <T> T retry() { |
| 291 | throw RETRY; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 294 | /** |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 295 | * Handles a completed batch event received from the master node. |
| 296 | * <p> |
| 297 | * If this node is the source of the batch, notifies event listeners to complete the operations. |
| 298 | * |
| 299 | * @param event the event to handle |
| 300 | */ |
| 301 | private void completeBatch(FlowRuleBatchEvent event) { |
| 302 | if (pendingBatches.remove(event.subject().batchId())) { |
| 303 | notifyDelegate(event); |
| 304 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // This is not a efficient operation on a distributed sharded |
| 308 | // flow store. We need to revisit the need for this operation or at least |
| 309 | // make it device specific. |
| 310 | @Override |
| 311 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 10e91fb | 2017-06-15 13:30:50 -0700 | [diff] [blame] | 312 | return Streams.stream(deviceService.getDevices()).parallel() |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 313 | .mapToInt(device -> Iterables.size(getFlowEntries(device.id()))) |
| 314 | .sum(); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Returns the {@link DocumentPath} for the given {@link DeviceId}. |
| 319 | * |
| 320 | * @param deviceId the device identifier for which to return a path |
| 321 | * @return the path for the given device |
| 322 | */ |
| 323 | private DocumentPath getPathFor(DeviceId deviceId) { |
| 324 | return DocumentPath.from("root", deviceId.toString()); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Returns the {@link DocumentPath} for the given {@link DeviceId} and {@link FlowId}. |
| 329 | * |
| 330 | * @param deviceId the device identifier for which to return the path |
| 331 | * @param flowId the flow identifier for which to return the path |
| 332 | * @return the path for the given device/flow |
| 333 | */ |
| 334 | private DocumentPath getPathFor(DeviceId deviceId, FlowId flowId) { |
| 335 | return DocumentPath.from("root", deviceId.toString(), flowId.toString()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 339 | @SuppressWarnings("unchecked") |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 340 | public FlowEntry getFlowEntry(FlowRule rule) { |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 341 | DocumentPath path = getPathFor(rule.deviceId(), rule.id()); |
| 342 | Versioned<Map<StoredFlowEntry, StoredFlowEntry>> flowEntries = flows.get(path); |
| 343 | return flowEntries != null ? flowEntries.value().get(rule) : null; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 348 | DocumentPath path = getPathFor(deviceId); |
| 349 | try { |
| 350 | return getFlowEntries(path); |
| 351 | } catch (NoSuchDocumentPathException e) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 352 | return Collections.emptyList(); |
| 353 | } |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 354 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 355 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 356 | @SuppressWarnings("unchecked") |
| 357 | private Iterable<FlowEntry> getFlowEntries(DocumentPath path) { |
| 358 | return flows.getChildren(path) |
| 359 | .values() |
| 360 | .stream() |
| 361 | .flatMap(v -> v.value().values().stream()) |
| 362 | .collect(Collectors.toList()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | @Override |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 366 | @SuppressWarnings("unchecked") |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 367 | public void storeFlowRule(FlowRule rule) { |
| 368 | storeBatch(new FlowRuleBatchOperation( |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 369 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)), |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 370 | rule.deviceId(), idGenerator.getNewId())); |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public void storeBatch(FlowRuleBatchOperation operation) { |
| 375 | if (operation.getOperations().isEmpty()) { |
| 376 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 377 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 378 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | DeviceId deviceId = operation.deviceId(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 383 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 384 | |
| 385 | if (master == null) { |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 386 | log.warn("No master for {} ", deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 387 | |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 388 | updateStoreInternal(operation).whenComplete((result, error) -> { |
| 389 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 390 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 391 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
| 392 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 396 | pendingBatches.add(operation.id()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 397 | |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 398 | // If the local node is the master, apply the flows. Otherwise, send them to the master. |
| 399 | if (Objects.equals(local, master)) { |
| 400 | applyBatchFlows(operation); |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 401 | } else { |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 402 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", master, deviceId); |
| 403 | clusterCommunicator.unicast( |
| 404 | operation, |
| 405 | APPLY_BATCH_FLOWS, |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 406 | serializer::encode, |
| 407 | master); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 408 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 411 | /** |
| 412 | * Asynchronously applies a batch of flows to the store. |
| 413 | * <p> |
| 414 | * This operation is performed on the master node to ensure that events occur <em>after</em> flows have been stored |
| 415 | * and are visible to the master node. If a non-master node stores flows and then triggers events on the master, |
| 416 | * the flows may not yet be visible to the master node due to the nature of sequentially consistent reads on the |
| 417 | * underlying {@code DocumentTree} primitive. |
| 418 | */ |
| 419 | private void applyBatchFlows(FlowRuleBatchOperation operation) { |
| 420 | updateStoreInternal(operation).whenComplete((operations, error) -> { |
| 421 | if (error == null) { |
| 422 | if (operations.isEmpty()) { |
| 423 | batchOperationComplete(FlowRuleBatchEvent.completed( |
| 424 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 425 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 426 | } else { |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 427 | notifyDelegate(FlowRuleBatchEvent.requested( |
| 428 | new FlowRuleBatchRequest(operation.id(), operations), |
| 429 | operation.deviceId())); |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 430 | } |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 431 | } |
| 432 | }); |
| 433 | } |
| 434 | |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 435 | private CompletableFuture<Set<FlowRuleBatchEntry>> updateStoreInternal(FlowRuleBatchOperation operation) { |
| 436 | return Tools.allOf(operation.getOperations().stream().map(op -> { |
| 437 | switch (op.operator()) { |
| 438 | case ADD: |
| 439 | return addBatchEntry(op).thenApply(succeeded -> succeeded ? op : null); |
| 440 | case REMOVE: |
| 441 | return removeBatchEntry(op).thenApply(succeeded -> succeeded ? op : null); |
| 442 | case MODIFY: |
| 443 | return CompletableFuture.<FlowRuleBatchEntry>completedFuture(null); |
| 444 | default: |
| 445 | log.warn("Unknown flow operation operator: {}", op.operator()); |
| 446 | return CompletableFuture.<FlowRuleBatchEntry>completedFuture(null); |
| 447 | } |
| 448 | }).collect(Collectors.toList())) |
| 449 | .thenApply(results -> results.stream() |
| 450 | .filter(Objects::nonNull) |
| 451 | .collect(Collectors.toSet())); |
| 452 | } |
| 453 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 454 | @SuppressWarnings("unchecked") |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 455 | private CompletableFuture<Boolean> addBatchEntry(FlowRuleBatchEntry batchEntry) { |
| 456 | StoredFlowEntry entry = new DefaultFlowEntry(batchEntry.target()); |
| 457 | DocumentPath path = getPathFor(entry.deviceId(), entry.id()); |
| 458 | return retryAsyncUntilSuccess(() -> { |
| 459 | CompletableFuture<Boolean> future = new CompletableFuture<>(); |
| 460 | asyncFlows.get(path).whenComplete((value, getError) -> { |
| 461 | if (getError == null) { |
| 462 | if (value != null) { |
| 463 | Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value()); |
| 464 | entries.put(entry, entry); |
| 465 | asyncFlows.replace(path, entries, value.version()).whenComplete((succeeded, replaceError) -> { |
| 466 | if (replaceError == null) { |
| 467 | if (succeeded) { |
| 468 | log.trace("Stored new flow rule: {}", entry); |
| 469 | future.complete(true); |
| 470 | } else { |
| 471 | log.trace("Failed to store new flow rule: {}", entry); |
| 472 | future.completeExceptionally(RETRY); |
| 473 | } |
| 474 | } else { |
| 475 | future.completeExceptionally(replaceError); |
| 476 | } |
| 477 | }); |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 478 | } else { |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 479 | // If there are no entries stored for the device, initialize the device's flows. |
| 480 | Map<StoredFlowEntry, StoredFlowEntry> map = Maps.newHashMap(); |
| 481 | map.put(entry, entry); |
| 482 | asyncFlows.createRecursive(path, map).whenComplete((succeeded, createError) -> { |
| 483 | if (createError == null) { |
| 484 | if (succeeded) { |
| 485 | log.trace("Stored new flow rule: {}", entry); |
| 486 | future.complete(true); |
| 487 | } else { |
| 488 | log.trace("Failed to store new flow rule: {}", entry); |
| 489 | future.completeExceptionally(RETRY); |
| 490 | } |
| 491 | } else { |
| 492 | future.completeExceptionally(createError); |
| 493 | } |
| 494 | }); |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 495 | } |
| 496 | } else { |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 497 | future.completeExceptionally(getError); |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 498 | } |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 499 | }); |
| 500 | return future; |
| 501 | }); |
| 502 | } |
| 503 | |
| 504 | @SuppressWarnings("unchecked") |
| 505 | private CompletableFuture<Boolean> removeBatchEntry(FlowRuleBatchEntry batchEntry) { |
| 506 | FlowRule rule = batchEntry.target(); |
| 507 | DocumentPath path = getPathFor(rule.deviceId(), rule.id()); |
| 508 | return retryAsyncUntilSuccess(() -> { |
| 509 | CompletableFuture<Boolean> future = new CompletableFuture<>(); |
| 510 | asyncFlows.get(path).whenComplete((value, getError) -> { |
| 511 | if (getError == null) { |
| 512 | if (value != null) { |
| 513 | Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value()); |
| 514 | StoredFlowEntry entry = entries.get(rule); |
| 515 | if (entry != null) { |
| 516 | entry.setState(FlowEntryState.PENDING_REMOVE); |
| 517 | asyncFlows.replace(path, entries, value.version()).whenComplete((succeeded, error) -> { |
| 518 | if (error == null) { |
| 519 | if (succeeded) { |
| 520 | log.trace("Updated flow rule state to PENDING_REMOVE: {}", entry); |
| 521 | future.complete(true); |
| 522 | } else { |
| 523 | log.trace("Failed to update flow rule state to PENDING_REMOVE: {}", entry); |
| 524 | future.completeExceptionally(RETRY); |
| 525 | } |
| 526 | } else { |
| 527 | future.completeExceptionally(error); |
| 528 | } |
| 529 | }); |
| 530 | } else { |
| 531 | future.complete(false); |
| 532 | } |
| 533 | } else { |
| 534 | future.complete(false); |
| 535 | } |
| 536 | } else { |
| 537 | future.completeExceptionally(getError); |
| 538 | } |
| 539 | }); |
| 540 | return future; |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 541 | }); |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
| 546 | if (pendingBatches.remove(event.subject().batchId())) { |
| 547 | notifyDelegate(event); |
| 548 | } else { |
| 549 | clusterCommunicator.broadcast(event, COMPLETE_BATCH, serializer::encode); |
| 550 | } |
| 551 | } |
| 552 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 553 | @Override |
| 554 | public void deleteFlowRule(FlowRule rule) { |
| 555 | storeBatch( |
| 556 | new FlowRuleBatchOperation( |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 557 | Collections.singletonList( |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 558 | new FlowRuleBatchEntry( |
| 559 | FlowRuleOperation.REMOVE, |
| 560 | rule)), rule.deviceId(), idGenerator.getNewId())); |
| 561 | } |
| 562 | |
| 563 | @Override |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 564 | public FlowRuleEvent pendingFlowRule(FlowEntry rule) { |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 565 | DocumentPath path = getPathFor(rule.deviceId(), rule.id()); |
| 566 | return retryUntilSuccess(() -> { |
| 567 | Versioned<Map<StoredFlowEntry, StoredFlowEntry>> value = flows.get(path); |
| 568 | if (value != null) { |
| 569 | Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value()); |
| 570 | StoredFlowEntry entry = entries.get(rule); |
| 571 | if (entry != null && entry.state() != FlowEntryState.PENDING_ADD) { |
| 572 | entry.setState(FlowEntryState.PENDING_ADD); |
| 573 | if (flows.replace(path, entries, value.version())) { |
| 574 | log.trace("Updated flow rule state to PENDING_ADD: {}", entry); |
| 575 | return new FlowRuleEvent(RULE_UPDATED, rule); |
| 576 | } else { |
| 577 | log.trace("Failed to update flow rule state to PENDING_ADD: {}", entry); |
| 578 | return retry(); |
| 579 | } |
| 580 | } else { |
| 581 | return null; |
| 582 | } |
| 583 | } else { |
| 584 | return null; |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 585 | } |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 586 | }); |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | @Override |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 590 | @SuppressWarnings("unchecked") |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 591 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 592 | DocumentPath path = getPathFor(rule.deviceId(), rule.id()); |
| 593 | return retryUntilSuccess(() -> { |
| 594 | Versioned<Map<StoredFlowEntry, StoredFlowEntry>> value = flows.get(path); |
| 595 | if (value != null) { |
| 596 | Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value()); |
| 597 | StoredFlowEntry entry = entries.get(rule); |
| 598 | if (entry != null) { |
| 599 | FlowRuleEvent event; |
| 600 | String message; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 601 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 602 | entry.setBytes(rule.bytes()); |
| 603 | entry.setLife(rule.life(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS); |
| 604 | entry.setLiveType(rule.liveType()); |
| 605 | entry.setPackets(rule.packets()); |
| 606 | entry.setLastSeen(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 607 | |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 608 | // If the entry state is PENDING_ADD, set it to ADDED. Otherwise, just update the rule. |
| 609 | if (entry.state() == FlowEntryState.PENDING_ADD) { |
| 610 | entry.setState(FlowEntryState.ADDED); |
| 611 | event = new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 612 | message = "Updated flow rule state to ADDED: {}"; |
| 613 | } else { |
| 614 | event = new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 615 | message = "Updated flow rule: {}"; |
| 616 | } |
| 617 | |
| 618 | if (flows.replace(path, entries, value.version())) { |
| 619 | log.trace(message, entry); |
| 620 | return event; |
| 621 | } else { |
| 622 | log.trace("Failed to update flow rule: {}", entry); |
| 623 | return retry(); |
| 624 | } |
| 625 | } else { |
| 626 | // If the rule does not exist, return null. Inserting the rule risks race conditions |
| 627 | // that can result in removed rules being retained. |
| 628 | return null; |
| 629 | } |
| 630 | } else { |
Jordan Halterman | b8925dc | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 631 | return null; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 632 | } |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 633 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | @Override |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 637 | @SuppressWarnings("unchecked") |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 638 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 639 | DocumentPath path = getPathFor(rule.deviceId(), rule.id()); |
| 640 | return retryUntilSuccess(() -> { |
| 641 | Versioned<Map<StoredFlowEntry, StoredFlowEntry>> value = flows.get(path); |
| 642 | if (value != null) { |
| 643 | Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value()); |
| 644 | StoredFlowEntry entry = entries.remove(rule); |
| 645 | if (entry != null) { |
| 646 | if (flows.replace(path, entries, value.version())) { |
| 647 | log.trace("Removed flow rule: {}", entry); |
| 648 | return new FlowRuleEvent(RULE_REMOVED, entry); |
| 649 | } else { |
| 650 | log.trace("Failed to remove flow rule: {}", entry); |
| 651 | return retry(); |
| 652 | } |
| 653 | } else { |
| 654 | return null; |
| 655 | } |
| 656 | } else { |
| 657 | return null; |
| 658 | } |
| 659 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | @Override |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 663 | public void purgeFlowRule(DeviceId deviceId) { |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 664 | DocumentPath path = getPathFor(deviceId); |
Jordan Halterman | 6fd3291 | 2017-09-12 15:07:18 -0700 | [diff] [blame] | 665 | retryUntilSuccess(() -> { |
| 666 | try { |
| 667 | for (String flowId : flows.getChildren(path).keySet()) { |
| 668 | flows.removeNode(DocumentPath.from("root", deviceId.toString(), flowId)); |
| 669 | } |
| 670 | } catch (NoSuchDocumentPathException e) { |
| 671 | // Do nothing. There are no flows for the device. |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 672 | } |
Jordan Halterman | 6fd3291 | 2017-09-12 15:07:18 -0700 | [diff] [blame] | 673 | |
| 674 | // New children may have been created since they were removed above. Catch |
| 675 | // IllegalDocumentModificationException and retry if necessary. |
| 676 | try { |
| 677 | flows.removeNode(path); |
| 678 | } catch (NoSuchDocumentPathException e) { |
| 679 | return null; |
| 680 | } catch (IllegalDocumentModificationException e) { |
| 681 | return retry(); |
| 682 | } |
| 683 | return null; |
| 684 | }); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | @Override |
Victor Silva | 139bca4 | 2016-08-18 11:46:35 -0300 | [diff] [blame] | 688 | public void purgeFlowRules() { |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 689 | try { |
| 690 | for (String deviceId : flows.getChildren(flows.root()).keySet()) { |
| 691 | purgeFlowRule(DeviceId.deviceId(deviceId)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 692 | } |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 693 | } catch (NoSuchDocumentPathException e) { |
Jordan Halterman | 6fd3291 | 2017-09-12 15:07:18 -0700 | [diff] [blame] | 694 | // Do nothing if no children exist. |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 697 | |
| 698 | @Override |
| 699 | public FlowRuleEvent updateTableStatistics(DeviceId deviceId, |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 700 | List<TableStatisticsEntry> tableStats) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 701 | deviceTableStats.put(deviceId, tableStats); |
| 702 | return null; |
| 703 | } |
| 704 | |
| 705 | @Override |
| 706 | public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 707 | List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId); |
| 708 | if (tableStats == null) { |
| 709 | return Collections.emptyList(); |
| 710 | } |
| 711 | return ImmutableList.copyOf(tableStats); |
| 712 | } |
| 713 | |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 714 | @Override |
| 715 | public long getActiveFlowRuleCount(DeviceId deviceId) { |
| 716 | return Streams.stream(getTableStatistics(deviceId)) |
| 717 | .mapToLong(TableStatisticsEntry::activeFlowEntries) |
| 718 | .sum(); |
| 719 | } |
| 720 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 721 | private class InternalTableStatsListener |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 722 | implements EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 723 | @Override |
| 724 | public void event(EventuallyConsistentMapEvent<DeviceId, |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 725 | List<TableStatisticsEntry>> event) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 726 | //TODO: Generate an event to listeners (do we need?) |
| 727 | } |
| 728 | } |
Jordan Halterman | e0355ff | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 729 | } |