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