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