alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.flow.impl; |
| 2 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 4 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 5 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 6 | import static org.onlab.onos.store.flow.impl.FlowStoreMessageSubjects.*; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 7 | import static org.onlab.util.Tools.namedThreads; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 8 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 9 | import java.io.IOException; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 10 | import java.util.ArrayList; |
| 11 | import java.util.Arrays; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 12 | import java.util.Collection; |
| 13 | import java.util.Collections; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 14 | import java.util.Map.Entry; |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 15 | import java.util.Set; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 16 | import java.util.concurrent.ExecutionException; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 17 | import java.util.concurrent.ExecutorService; |
| 18 | import java.util.concurrent.Executors; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 19 | import java.util.concurrent.Future; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 20 | import java.util.concurrent.TimeUnit; |
| 21 | import java.util.concurrent.TimeoutException; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 22 | import java.util.concurrent.atomic.AtomicInteger; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 23 | import java.util.List; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 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; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 31 | import org.onlab.onos.cluster.ClusterService; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 32 | import org.onlab.onos.cluster.NodeId; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.Device; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.DeviceId; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.device.DeviceService; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
| 38 | import org.onlab.onos.net.flow.FlowEntry; |
| 39 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.flow.FlowId; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.flow.FlowRule; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 43 | import org.onlab.onos.net.flow.FlowRuleBatchEvent; |
| 44 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 45 | import org.onlab.onos.net.flow.FlowRuleBatchRequest; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 46 | import org.onlab.onos.net.flow.FlowRuleEvent; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 47 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 48 | import org.onlab.onos.net.flow.FlowRuleEvent.Type; |
| 49 | import org.onlab.onos.net.flow.FlowRuleStore; |
| 50 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 51 | import org.onlab.onos.net.flow.StoredFlowEntry; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 52 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 53 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 54 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 55 | import org.onlab.onos.store.flow.ReplicaInfo; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 56 | import org.onlab.onos.store.flow.ReplicaInfoEvent; |
| 57 | import org.onlab.onos.store.flow.ReplicaInfoEventListener; |
Yuta HIGUCHI | fe280eb | 2014-10-17 12:10:43 -0700 | [diff] [blame] | 58 | import org.onlab.onos.store.flow.ReplicaInfoService; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 59 | import org.onlab.onos.store.hz.AbstractHazelcastStore; |
| 60 | import org.onlab.onos.store.hz.SMap; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 61 | import org.onlab.onos.store.serializers.DistributedStoreSerializers; |
| 62 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 63 | import org.onlab.util.KryoNamespace; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 64 | import org.slf4j.Logger; |
| 65 | |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 66 | import com.google.common.base.Function; |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 67 | import com.google.common.cache.Cache; |
| 68 | import com.google.common.cache.CacheBuilder; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 69 | import com.google.common.cache.CacheLoader; |
| 70 | import com.google.common.cache.LoadingCache; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 71 | import com.google.common.collect.ArrayListMultimap; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 72 | import com.google.common.collect.ImmutableList; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 73 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 74 | import com.google.common.collect.Iterables; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 75 | import com.google.common.collect.Multimap; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 76 | import com.google.common.util.concurrent.Futures; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 77 | import com.google.common.util.concurrent.ListenableFuture; |
| 78 | import com.google.common.util.concurrent.SettableFuture; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 79 | import com.hazelcast.core.IMap; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 82 | * Manages inventory of flow rules using a distributed state management protocol. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 83 | */ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 84 | @Component(immediate = true) |
| 85 | @Service |
| 86 | public class DistributedFlowRuleStore |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 87 | extends AbstractHazelcastStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 88 | implements FlowRuleStore { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 89 | |
| 90 | private final Logger log = getLogger(getClass()); |
| 91 | |
| 92 | // store entries as a pile of rules, no info about device tables |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 93 | private final Multimap<DeviceId, StoredFlowEntry> flowEntries = |
| 94 | ArrayListMultimap.<DeviceId, StoredFlowEntry>create(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 95 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 96 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 98 | protected ReplicaInfoService replicaInfoManager; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 101 | protected ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 104 | protected ClusterService clusterService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected DeviceService deviceService; |
| 108 | |
| 109 | private final AtomicInteger localBatchIdGen = new AtomicInteger(); |
| 110 | |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 111 | // TODO: make this configurable |
| 112 | private int pendingFutureTimeoutMinutes = 5; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 113 | |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 114 | private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures = |
| 115 | CacheBuilder.newBuilder() |
| 116 | .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES) |
| 117 | // TODO Explicitly fail the future if expired? |
| 118 | //.removalListener(listener) |
| 119 | .build(); |
| 120 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 121 | private LoadingCache<DeviceId, SMap<FlowId, ImmutableList<StoredFlowEntry>>> smaps; |
| 122 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 123 | |
| 124 | private final ExecutorService futureListeners = |
| 125 | Executors.newCachedThreadPool(namedThreads("flowstore-peer-responders")); |
| 126 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 127 | private final ExecutorService backupExecutors = |
| 128 | Executors.newSingleThreadExecutor(namedThreads("async-backups")); |
| 129 | |
| 130 | // TODO make this configurable |
| 131 | private boolean syncBackup = false; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 132 | |
| 133 | protected static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 134 | @Override |
| 135 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 136 | serializerPool = KryoNamespace.newBuilder() |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 137 | .register(DistributedStoreSerializers.COMMON) |
| 138 | .build() |
| 139 | .populate(1); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | // TODO: make this configurable |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 144 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 145 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 146 | private ReplicaInfoEventListener replicaInfoEventListener; |
| 147 | |
| 148 | @Override |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 149 | @Activate |
| 150 | public void activate() { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 151 | |
| 152 | super.serializer = SERIALIZER; |
| 153 | super.theInstance = storeService.getHazelcastInstance(); |
| 154 | |
| 155 | // Cache to create SMap on demand |
| 156 | smaps = CacheBuilder.newBuilder() |
| 157 | .softValues() |
| 158 | .build(new SMapLoader()); |
| 159 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 160 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new ClusterMessageHandler() { |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 161 | |
| 162 | @Override |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 163 | public void handle(final ClusterMessage message) { |
| 164 | FlowRuleBatchOperation operation = SERIALIZER.decode(message.payload()); |
| 165 | log.info("received batch request {}", operation); |
| 166 | final ListenableFuture<CompletedBatchOperation> f = storeBatchInternal(operation); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 167 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 168 | f.addListener(new Runnable() { |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 169 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 170 | @Override |
| 171 | public void run() { |
| 172 | CompletedBatchOperation result = Futures.getUnchecked(f); |
| 173 | try { |
| 174 | message.respond(SERIALIZER.encode(result)); |
| 175 | } catch (IOException e) { |
| 176 | log.error("Failed to respond back", e); |
| 177 | } |
| 178 | } |
| 179 | }, futureListeners); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 180 | } |
| 181 | }); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 182 | |
| 183 | clusterCommunicator.addSubscriber(GET_FLOW_ENTRY, new ClusterMessageHandler() { |
| 184 | |
| 185 | @Override |
| 186 | public void handle(ClusterMessage message) { |
| 187 | FlowRule rule = SERIALIZER.decode(message.payload()); |
| 188 | log.info("received get flow entry request for {}", rule); |
| 189 | FlowEntry flowEntry = getFlowEntryInternal(rule); |
| 190 | try { |
| 191 | message.respond(SERIALIZER.encode(flowEntry)); |
| 192 | } catch (IOException e) { |
| 193 | log.error("Failed to respond back", e); |
| 194 | } |
| 195 | } |
| 196 | }); |
| 197 | |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 198 | clusterCommunicator.addSubscriber(GET_DEVICE_FLOW_ENTRIES, new ClusterMessageHandler() { |
| 199 | |
| 200 | @Override |
| 201 | public void handle(ClusterMessage message) { |
| 202 | DeviceId deviceId = SERIALIZER.decode(message.payload()); |
| 203 | log.info("Received get flow entries request for {} from {}", deviceId, message.sender()); |
| 204 | Set<FlowEntry> flowEntries = getFlowEntriesInternal(deviceId); |
| 205 | try { |
| 206 | message.respond(SERIALIZER.encode(flowEntries)); |
| 207 | } catch (IOException e) { |
| 208 | log.error("Failed to respond to peer's getFlowEntries request", e); |
| 209 | } |
| 210 | } |
| 211 | }); |
| 212 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 213 | replicaInfoEventListener = new InternalReplicaInfoEventListener(); |
| 214 | |
| 215 | replicaInfoManager.addListener(replicaInfoEventListener); |
| 216 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 217 | log.info("Started"); |
| 218 | } |
| 219 | |
| 220 | @Deactivate |
| 221 | public void deactivate() { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 222 | replicaInfoManager.removeListener(replicaInfoEventListener); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 223 | log.info("Stopped"); |
| 224 | } |
| 225 | |
| 226 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 227 | // TODO: This is not a efficient operation on a distributed sharded |
| 228 | // flow store. We need to revisit the need for this operation or at least |
| 229 | // make it device specific. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 230 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 231 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 232 | // implementing in-efficient operation for debugging purpose. |
| 233 | int sum = 0; |
| 234 | for (Device device : deviceService.getDevices()) { |
| 235 | final DeviceId did = device.id(); |
| 236 | sum += Iterables.size(getFlowEntries(did)); |
| 237 | } |
| 238 | return sum; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 242 | public synchronized FlowEntry getFlowEntry(FlowRule rule) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 243 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
| 244 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 245 | return getFlowEntryInternal(rule); |
| 246 | } |
| 247 | |
| 248 | log.info("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
| 249 | replicaInfo.master().orNull(), rule.deviceId()); |
| 250 | |
| 251 | ClusterMessage message = new ClusterMessage( |
| 252 | clusterService.getLocalNode().id(), |
| 253 | FlowStoreMessageSubjects.GET_FLOW_ENTRY, |
| 254 | SERIALIZER.encode(rule)); |
| 255 | |
| 256 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 257 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 258 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 259 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 260 | // FIXME: throw a FlowStoreException |
| 261 | throw new RuntimeException(e); |
| 262 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | private synchronized StoredFlowEntry getFlowEntryInternal(FlowRule rule) { |
| 266 | for (StoredFlowEntry f : flowEntries.get(rule.deviceId())) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 267 | if (f.equals(rule)) { |
| 268 | return f; |
| 269 | } |
| 270 | } |
| 271 | return null; |
| 272 | } |
| 273 | |
| 274 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 275 | public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 276 | |
| 277 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 278 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 279 | return getFlowEntriesInternal(deviceId); |
| 280 | } |
| 281 | |
| 282 | log.info("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", |
| 283 | replicaInfo.master().orNull(), deviceId); |
| 284 | |
| 285 | ClusterMessage message = new ClusterMessage( |
| 286 | clusterService.getLocalNode().id(), |
| 287 | GET_DEVICE_FLOW_ENTRIES, |
| 288 | SERIALIZER.encode(deviceId)); |
| 289 | |
| 290 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 291 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 292 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 293 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 294 | // FIXME: throw a FlowStoreException |
| 295 | throw new RuntimeException(e); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 300 | Collection<? extends FlowEntry> rules = flowEntries.get(deviceId); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 301 | if (rules == null) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 302 | return Collections.emptySet(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 303 | } |
| 304 | return ImmutableSet.copyOf(rules); |
| 305 | } |
| 306 | |
| 307 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 308 | public void storeFlowRule(FlowRule rule) { |
| 309 | storeBatch(new FlowRuleBatchOperation(Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)))); |
| 310 | } |
| 311 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 312 | // FIXME document that all of the FlowEntries must be about same device |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 313 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 314 | public Future<CompletedBatchOperation> storeBatch(FlowRuleBatchOperation operation) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 315 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 316 | if (operation.getOperations().isEmpty()) { |
| 317 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 318 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 319 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 320 | DeviceId deviceId = operation.getOperations().get(0).getTarget().deviceId(); |
| 321 | |
| 322 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 323 | |
| 324 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 325 | return storeBatchInternal(operation); |
| 326 | } |
| 327 | |
| 328 | log.info("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
| 329 | replicaInfo.master().orNull(), deviceId); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 330 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 331 | ClusterMessage message = new ClusterMessage( |
| 332 | clusterService.getLocalNode().id(), |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 333 | APPLY_BATCH_FLOWS, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 334 | SERIALIZER.encode(operation)); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 335 | |
| 336 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 337 | ListenableFuture<byte[]> responseFuture = |
| 338 | clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 339 | return Futures.transform(responseFuture, new Function<byte[], CompletedBatchOperation>() { |
| 340 | @Override |
| 341 | public CompletedBatchOperation apply(byte[] input) { |
| 342 | return SERIALIZER.decode(input); |
| 343 | } |
| 344 | }); |
| 345 | } catch (IOException e) { |
| 346 | return Futures.immediateFailedFuture(e); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 350 | private ListenableFuture<CompletedBatchOperation> storeBatchInternal(FlowRuleBatchOperation operation) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 351 | final List<StoredFlowEntry> toRemove = new ArrayList<>(); |
| 352 | final List<StoredFlowEntry> toAdd = new ArrayList<>(); |
| 353 | DeviceId did = null; |
| 354 | |
| 355 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 356 | for (FlowRuleBatchEntry batchEntry : operation.getOperations()) { |
| 357 | FlowRule flowRule = batchEntry.getTarget(); |
| 358 | FlowRuleOperation op = batchEntry.getOperator(); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 359 | if (did == null) { |
| 360 | did = flowRule.deviceId(); |
| 361 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 362 | if (op.equals(FlowRuleOperation.REMOVE)) { |
| 363 | StoredFlowEntry entry = getFlowEntryInternal(flowRule); |
| 364 | if (entry != null) { |
| 365 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 366 | toRemove.add(entry); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 367 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 368 | } else if (op.equals(FlowRuleOperation.ADD)) { |
| 369 | StoredFlowEntry flowEntry = new DefaultFlowEntry(flowRule); |
| 370 | DeviceId deviceId = flowRule.deviceId(); |
| 371 | if (!flowEntries.containsEntry(deviceId, flowEntry)) { |
| 372 | flowEntries.put(deviceId, flowEntry); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 373 | toAdd.add(flowEntry); |
| 374 | } |
| 375 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 376 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 377 | if (toAdd.isEmpty() && toRemove.isEmpty()) { |
| 378 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet())); |
| 379 | } |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 380 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 381 | // create remote backup copies |
| 382 | final DeviceId deviceId = did; |
| 383 | updateBackup(deviceId, toAdd, toRemove); |
| 384 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 385 | SettableFuture<CompletedBatchOperation> r = SettableFuture.create(); |
| 386 | final int batchId = localBatchIdGen.incrementAndGet(); |
| 387 | |
| 388 | pendingFutures.put(batchId, r); |
| 389 | notifyDelegate(FlowRuleBatchEvent.requested(new FlowRuleBatchRequest(batchId, toAdd, toRemove))); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 390 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 391 | return r; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 394 | private void updateBackup(final DeviceId deviceId, |
| 395 | final List<StoredFlowEntry> toAdd, |
| 396 | final List<? extends FlowRule> list) { |
| 397 | |
| 398 | Future<?> submit = backupExecutors.submit(new UpdateBackup(deviceId, toAdd, list)); |
| 399 | |
| 400 | if (syncBackup) { |
| 401 | // wait for backup to complete |
| 402 | try { |
| 403 | submit.get(); |
| 404 | } catch (InterruptedException | ExecutionException e) { |
| 405 | log.error("Failed to create backups", e); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | private void updateBackup(DeviceId deviceId, List<StoredFlowEntry> toAdd) { |
| 411 | updateBackup(deviceId, toAdd, Collections.<FlowEntry>emptyList()); |
| 412 | } |
| 413 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 414 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 415 | public void deleteFlowRule(FlowRule rule) { |
| 416 | storeBatch(new FlowRuleBatchOperation(Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule)))); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 420 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
| 421 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 0858b35 | 2014-10-17 11:56:06 -0700 | [diff] [blame] | 422 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 423 | return addOrUpdateFlowRuleInternal(rule); |
| 424 | } |
| 425 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 426 | log.error("Tried to update FlowRule {} state," |
| 427 | + " while the Node was not the master.", rule); |
| 428 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | private synchronized FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 432 | final DeviceId did = rule.deviceId(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 433 | |
| 434 | // check if this new rule is an update to an existing entry |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 435 | StoredFlowEntry stored = getFlowEntryInternal(rule); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 436 | if (stored != null) { |
| 437 | stored.setBytes(rule.bytes()); |
| 438 | stored.setLife(rule.life()); |
| 439 | stored.setPackets(rule.packets()); |
| 440 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 441 | stored.setState(FlowEntryState.ADDED); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 442 | // update backup. |
| 443 | updateBackup(did, Arrays.asList(stored)); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 444 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 445 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 446 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 447 | } |
| 448 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 449 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 450 | // TODO: also update backup. |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 451 | flowEntries.put(did, new DefaultFlowEntry(rule)); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 452 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 453 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 457 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 458 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 0858b35 | 2014-10-17 11:56:06 -0700 | [diff] [blame] | 459 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 460 | // bypass and handle it locally |
| 461 | return removeFlowRuleInternal(rule); |
| 462 | } |
| 463 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 464 | log.error("Tried to remove FlowRule {}," |
| 465 | + " while the Node was not the master.", rule); |
| 466 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | private synchronized FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 470 | final DeviceId deviceId = rule.deviceId(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 471 | // This is where one could mark a rule as removed and still keep it in the store. |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 472 | final boolean removed = flowEntries.remove(deviceId, rule); |
| 473 | updateBackup(deviceId, Collections.<StoredFlowEntry>emptyList(), Arrays.asList(rule)); |
| 474 | if (removed) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 475 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 476 | } else { |
| 477 | return null; |
| 478 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 479 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 480 | |
| 481 | @Override |
| 482 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 483 | final Integer batchId = event.subject().batchId(); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 484 | SettableFuture<CompletedBatchOperation> future |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 485 | = pendingFutures.getIfPresent(batchId); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 486 | if (future != null) { |
| 487 | future.set(event.result()); |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 488 | pendingFutures.invalidate(batchId); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 489 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 490 | notifyDelegate(event); |
| 491 | } |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 492 | |
| 493 | private synchronized void loadFromBackup(final DeviceId did) { |
| 494 | // should relax synchronized condition |
| 495 | |
| 496 | try { |
| 497 | log.info("Loading FlowRules for {} from backups", did); |
| 498 | SMap<FlowId, ImmutableList<StoredFlowEntry>> backupFlowTable = smaps.get(did); |
| 499 | for (Entry<FlowId, ImmutableList<StoredFlowEntry>> e |
| 500 | : backupFlowTable.entrySet()) { |
| 501 | |
| 502 | // TODO: should we be directly updating internal structure or |
| 503 | // should we be triggering event? |
| 504 | log.debug("loading {}", e.getValue()); |
| 505 | for (StoredFlowEntry entry : e.getValue()) { |
| 506 | flowEntries.remove(did, entry); |
| 507 | flowEntries.put(did, entry); |
| 508 | } |
| 509 | } |
| 510 | } catch (ExecutionException e) { |
| 511 | log.error("Failed to load backup flowtable for {}", did, e); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | private synchronized void removeFromPrimary(final DeviceId did) { |
| 516 | Collection<StoredFlowEntry> removed = flowEntries.removeAll(did); |
| 517 | log.debug("removedFromPrimary {}", removed); |
| 518 | } |
| 519 | |
| 520 | private final class SMapLoader |
| 521 | extends CacheLoader<DeviceId, SMap<FlowId, ImmutableList<StoredFlowEntry>>> { |
| 522 | |
| 523 | @Override |
| 524 | public SMap<FlowId, ImmutableList<StoredFlowEntry>> load(DeviceId id) |
| 525 | throws Exception { |
| 526 | IMap<byte[], byte[]> map = theInstance.getMap("flowtable_" + id.toString()); |
| 527 | return new SMap<FlowId, ImmutableList<StoredFlowEntry>>(map, SERIALIZER); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | private final class InternalReplicaInfoEventListener |
| 532 | implements ReplicaInfoEventListener { |
| 533 | |
| 534 | @Override |
| 535 | public void event(ReplicaInfoEvent event) { |
| 536 | final NodeId local = clusterService.getLocalNode().id(); |
| 537 | final DeviceId did = event.subject(); |
| 538 | final ReplicaInfo rInfo = event.replicaInfo(); |
| 539 | |
| 540 | switch (event.type()) { |
| 541 | case MASTER_CHANGED: |
| 542 | if (local.equals(rInfo.master().orNull())) { |
| 543 | // This node is the new master, populate local structure |
| 544 | // from backup |
| 545 | loadFromBackup(did); |
| 546 | } else { |
| 547 | // This node is no longer the master holder, |
| 548 | // clean local structure |
| 549 | removeFromPrimary(did); |
| 550 | // FIXME: probably should stop pending backup activities in |
| 551 | // executors to avoid overwriting with old value |
| 552 | } |
| 553 | break; |
| 554 | default: |
| 555 | break; |
| 556 | |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | // Task to update FlowEntries in backup HZ store |
| 562 | private final class UpdateBackup implements Runnable { |
| 563 | |
| 564 | private final DeviceId deviceId; |
| 565 | private final List<StoredFlowEntry> toAdd; |
| 566 | private final List<? extends FlowRule> toRemove; |
| 567 | |
| 568 | public UpdateBackup(DeviceId deviceId, |
| 569 | List<StoredFlowEntry> toAdd, |
| 570 | List<? extends FlowRule> list) { |
| 571 | this.deviceId = checkNotNull(deviceId); |
| 572 | this.toAdd = checkNotNull(toAdd); |
| 573 | this.toRemove = checkNotNull(list); |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public void run() { |
| 578 | try { |
| 579 | log.debug("update backup {} +{} -{}", deviceId, toAdd, toRemove); |
| 580 | final SMap<FlowId, ImmutableList<StoredFlowEntry>> backupFlowTable = smaps.get(deviceId); |
| 581 | // Following should be rewritten using async APIs |
| 582 | for (StoredFlowEntry entry : toAdd) { |
| 583 | final FlowId id = entry.id(); |
| 584 | ImmutableList<StoredFlowEntry> original = backupFlowTable.get(id); |
| 585 | List<StoredFlowEntry> list = new ArrayList<>(); |
| 586 | if (original != null) { |
| 587 | list.addAll(original); |
| 588 | } |
| 589 | |
| 590 | list.remove(entry); |
| 591 | list.add(entry); |
| 592 | |
| 593 | ImmutableList<StoredFlowEntry> newValue = ImmutableList.copyOf(list); |
| 594 | boolean success; |
| 595 | if (original == null) { |
| 596 | success = (backupFlowTable.putIfAbsent(id, newValue) == null); |
| 597 | } else { |
| 598 | success = backupFlowTable.replace(id, original, newValue); |
| 599 | } |
| 600 | // TODO retry? |
| 601 | if (!success) { |
| 602 | log.error("Updating backup failed."); |
| 603 | } |
| 604 | } |
| 605 | for (FlowRule entry : toRemove) { |
| 606 | final FlowId id = entry.id(); |
| 607 | ImmutableList<StoredFlowEntry> original = backupFlowTable.get(id); |
| 608 | List<StoredFlowEntry> list = new ArrayList<>(); |
| 609 | if (original != null) { |
| 610 | list.addAll(original); |
| 611 | } |
| 612 | |
| 613 | list.remove(entry); |
| 614 | |
| 615 | ImmutableList<StoredFlowEntry> newValue = ImmutableList.copyOf(list); |
| 616 | boolean success; |
| 617 | if (original == null) { |
| 618 | success = (backupFlowTable.putIfAbsent(id, newValue) == null); |
| 619 | } else { |
| 620 | success = backupFlowTable.replace(id, original, newValue); |
| 621 | } |
| 622 | // TODO retry? |
| 623 | if (!success) { |
| 624 | log.error("Updating backup failed."); |
| 625 | } |
| 626 | } |
| 627 | } catch (ExecutionException e) { |
| 628 | log.error("Failed to write to backups", e); |
| 629 | } |
| 630 | |
| 631 | } |
| 632 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 633 | } |