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 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 18 | import com.google.common.cache.CacheBuilder; |
| 19 | import com.google.common.cache.CacheLoader; |
| 20 | import com.google.common.cache.LoadingCache; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.Iterables; |
| 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Sets; |
| 25 | import com.google.common.util.concurrent.ListenableFuture; |
| 26 | import com.hazelcast.core.IMap; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 33 | import org.onlab.util.KryoNamespace; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 34 | import org.onlab.util.NewConcurrentHashMap; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.cluster.ClusterService; |
| 36 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 37 | import org.onosproject.core.CoreService; |
| 38 | import org.onosproject.core.IdGenerator; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.Device; |
| 40 | import org.onosproject.net.DeviceId; |
| 41 | import org.onosproject.net.device.DeviceService; |
| 42 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 43 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 44 | import org.onosproject.net.flow.FlowEntry; |
| 45 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 46 | import org.onosproject.net.flow.FlowId; |
| 47 | import org.onosproject.net.flow.FlowRule; |
| 48 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.FlowRuleBatchEvent; |
| 51 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 52 | import org.onosproject.net.flow.FlowRuleBatchRequest; |
| 53 | import org.onosproject.net.flow.FlowRuleEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flow.FlowRuleEvent.Type; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 55 | import org.onosproject.net.flow.FlowRuleService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 56 | import org.onosproject.net.flow.FlowRuleStore; |
| 57 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 58 | import org.onosproject.net.flow.StoredFlowEntry; |
| 59 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 60 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 61 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 62 | import org.onosproject.store.flow.ReplicaInfo; |
| 63 | import org.onosproject.store.flow.ReplicaInfoEvent; |
| 64 | import org.onosproject.store.flow.ReplicaInfoEventListener; |
| 65 | import org.onosproject.store.flow.ReplicaInfoService; |
| 66 | import org.onosproject.store.hz.AbstractHazelcastStore; |
| 67 | import org.onosproject.store.hz.SMap; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 68 | import org.onosproject.store.serializers.KryoSerializer; |
| 69 | import org.onosproject.store.serializers.StoreSerializer; |
| 70 | import org.onosproject.store.serializers.impl.DistributedStoreSerializers; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 71 | import org.slf4j.Logger; |
| 72 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 73 | import java.io.IOException; |
| 74 | import java.util.ArrayList; |
| 75 | import java.util.Arrays; |
| 76 | import java.util.Collections; |
| 77 | import java.util.HashSet; |
| 78 | import java.util.List; |
| 79 | import java.util.Map; |
| 80 | import java.util.Map.Entry; |
| 81 | import java.util.Set; |
| 82 | import java.util.concurrent.ConcurrentHashMap; |
| 83 | import java.util.concurrent.ConcurrentMap; |
| 84 | import java.util.concurrent.CopyOnWriteArraySet; |
| 85 | import java.util.concurrent.ExecutionException; |
| 86 | import java.util.concurrent.ExecutorService; |
| 87 | import java.util.concurrent.Executors; |
| 88 | import java.util.concurrent.Future; |
| 89 | import java.util.concurrent.TimeUnit; |
| 90 | import java.util.concurrent.TimeoutException; |
| 91 | import java.util.stream.Collectors; |
| 92 | |
| 93 | import static com.google.common.base.Preconditions.checkNotNull; |
| 94 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
| 95 | import static org.onlab.util.Tools.namedThreads; |
| 96 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 97 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.*; |
| 98 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 101 | * Manages inventory of flow rules using a distributed state management protocol. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 102 | */ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 103 | @Component(immediate = true) |
| 104 | @Service |
| 105 | public class DistributedFlowRuleStore |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 106 | extends AbstractHazelcastStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 107 | implements FlowRuleStore { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 108 | |
| 109 | private final Logger log = getLogger(getClass()); |
| 110 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 111 | private InternalFlowTable flowTable = new InternalFlowTable(); |
| 112 | |
| 113 | /*private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, Set<StoredFlowEntry>>> |
| 114 | flowEntries = new ConcurrentHashMap<>();*/ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 115 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 117 | protected ReplicaInfoService replicaInfoManager; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 120 | protected ClusterCommunicationService clusterCommunicator; |
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 ClusterService clusterService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 126 | protected DeviceService deviceService; |
| 127 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | protected CoreService coreService; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 130 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 131 | private Map<Long, NodeId> pendingResponses = Maps.newConcurrentMap(); |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 132 | |
Yuta HIGUCHI | e9b2b00 | 2014-11-04 12:25:47 -0800 | [diff] [blame] | 133 | // 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] | 134 | private LoadingCache<DeviceId, SMap<FlowId, ImmutableList<StoredFlowEntry>>> smaps; |
| 135 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 136 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 137 | private final ExecutorService backupExecutors = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 138 | Executors.newSingleThreadExecutor(namedThreads("onos-async-backups")); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 139 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 140 | private boolean syncBackup = false; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 141 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 142 | protected static final StoreSerializer SERIALIZER = new KryoSerializer() { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 143 | @Override |
| 144 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 145 | serializerPool = KryoNamespace.newBuilder() |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 146 | .register(DistributedStoreSerializers.STORE_COMMON) |
| 147 | .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) |
Yuta HIGUCHI | 2f15833 | 2014-11-25 13:32:06 -0800 | [diff] [blame] | 148 | .register(FlowRuleEvent.class) |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 149 | .register(FlowRuleEvent.Type.class) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 150 | .build(); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 151 | } |
| 152 | }; |
| 153 | |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 154 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 155 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 156 | private ReplicaInfoEventListener replicaInfoEventListener; |
| 157 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 158 | private IdGenerator idGenerator; |
| 159 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 160 | @Override |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 161 | @Activate |
| 162 | public void activate() { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 163 | |
| 164 | super.serializer = SERIALIZER; |
| 165 | super.theInstance = storeService.getHazelcastInstance(); |
| 166 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 167 | idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC); |
| 168 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 169 | // Cache to create SMap on demand |
| 170 | smaps = CacheBuilder.newBuilder() |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 171 | .softValues() |
| 172 | .build(new SMapLoader()); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 173 | |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 174 | final NodeId local = clusterService.getLocalNode().id(); |
| 175 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 176 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new OnStoreBatch(local)); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 177 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 178 | clusterCommunicator.addSubscriber(REMOTE_APPLY_COMPLETED, new ClusterMessageHandler() { |
| 179 | @Override |
| 180 | public void handle(ClusterMessage message) { |
| 181 | FlowRuleBatchEvent event = SERIALIZER.decode(message.payload()); |
| 182 | log.trace("received completed notification for {}", event); |
| 183 | notifyDelegate(event); |
| 184 | } |
| 185 | }); |
| 186 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 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); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 193 | FlowEntry flowEntry = flowTable.getFlowEntry(rule); //getFlowEntryInternal(rule); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 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()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 208 | Set<FlowEntry> flowEntries = flowTable.getFlowEntries(deviceId); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 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 | |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 250 | // This is not a efficient operation on a distributed sharded |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 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()) { |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 269 | log.warn("Failed to getFlowEntry: No master for {}", rule.deviceId()); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 270 | return null; |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 273 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 274 | return flowTable.getFlowEntry(rule); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 277 | log.trace("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 278 | replicaInfo.master().orNull(), rule.deviceId()); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 279 | |
| 280 | ClusterMessage message = new ClusterMessage( |
| 281 | clusterService.getLocalNode().id(), |
| 282 | FlowStoreMessageSubjects.GET_FLOW_ENTRY, |
| 283 | SERIALIZER.encode(rule)); |
| 284 | |
| 285 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 286 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 287 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 288 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 289 | log.warn("Unable to fetch flow store contents from {}", replicaInfo.master().get()); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 290 | } |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 291 | return null; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 294 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 295 | |
| 296 | @Override |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 297 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 298 | |
| 299 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 300 | |
| 301 | if (!replicaInfo.master().isPresent()) { |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 302 | log.warn("Failed to getFlowEntries: No master for {}", deviceId); |
Yuta HIGUCHI | 2c1d847 | 2014-10-31 14:13:38 -0700 | [diff] [blame] | 303 | return Collections.emptyList(); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 306 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 307 | return flowTable.getFlowEntries(deviceId); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 310 | log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 311 | replicaInfo.master().orNull(), deviceId); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 312 | |
| 313 | ClusterMessage message = new ClusterMessage( |
| 314 | clusterService.getLocalNode().id(), |
| 315 | GET_DEVICE_FLOW_ENTRIES, |
| 316 | SERIALIZER.encode(deviceId)); |
| 317 | |
| 318 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 319 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 320 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 321 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 322 | log.warn("Unable to fetch flow store contents from {}", replicaInfo.master().get()); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 323 | } |
Yuta HIGUCHI | 24f79eb | 2014-12-12 15:46:43 -0800 | [diff] [blame] | 324 | return Collections.emptyList(); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 327 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 328 | |
| 329 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 330 | public void storeFlowRule(FlowRule rule) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 331 | storeBatch(new FlowRuleBatchOperation( |
| 332 | Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)), |
| 333 | rule.deviceId(), idGenerator.getNewId())); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 336 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 337 | public void storeBatch(FlowRuleBatchOperation operation) { |
| 338 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 339 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 340 | if (operation.getOperations().isEmpty()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 341 | |
| 342 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 343 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 344 | new CompletedBatchOperation(true, Collections.emptySet(), |
| 345 | operation.deviceId()))); |
| 346 | return; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 347 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 348 | |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 349 | DeviceId deviceId = operation.getOperations().get(0).target().deviceId(); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 350 | |
| 351 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 352 | |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 353 | if (!replicaInfo.master().isPresent()) { |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 354 | log.warn("Failed to storeBatch: No master for {}", deviceId); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 355 | |
| 356 | Set<FlowRule> allFailures = operation.getOperations().stream() |
| 357 | .map(op -> op.getTarget()) |
| 358 | .collect(Collectors.toSet()); |
| 359 | |
| 360 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 361 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 362 | new CompletedBatchOperation(false, allFailures, operation.deviceId()))); |
| 363 | return; |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | final NodeId local = clusterService.getLocalNode().id(); |
| 367 | if (replicaInfo.master().get().equals(local)) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 368 | storeBatchInternal(operation); |
| 369 | return; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 372 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 373 | replicaInfo.master().orNull(), deviceId); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 374 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 375 | ClusterMessage message = new ClusterMessage( |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 376 | local, |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 377 | APPLY_BATCH_FLOWS, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 378 | SERIALIZER.encode(operation)); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 379 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 380 | //CompletedBatchOperation response; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 381 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 382 | ListenableFuture<byte[]> responseFuture = |
| 383 | clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 384 | /*response = |
| 385 | Futures.transform(responseFuture, |
| 386 | new DecodeTo<CompletedBatchOperation>(SERIALIZER)) |
| 387 | .get(500 * operation.size(), TimeUnit.MILLISECONDS); |
| 388 | |
| 389 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 390 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), response));*/ |
| 391 | |
| 392 | } catch (IOException /*| InterruptedException | ExecutionException | TimeoutException*/ e) { |
| 393 | log.warn("Failed to storeBatch: {}", e.getMessage()); |
| 394 | |
| 395 | Set<FlowRule> allFailures = operation.getOperations().stream() |
| 396 | .map(op -> op.getTarget()) |
| 397 | .collect(Collectors.toSet()); |
| 398 | |
| 399 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 400 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 401 | new CompletedBatchOperation(false, allFailures, deviceId))); |
| 402 | return; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 403 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 404 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 407 | private void storeBatchInternal(FlowRuleBatchOperation operation) { |
Yuta HIGUCHI | e9b2b00 | 2014-11-04 12:25:47 -0800 | [diff] [blame] | 408 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 409 | final DeviceId did = operation.deviceId(); |
| 410 | //final Collection<FlowEntry> ft = flowTable.getFlowEntries(did); |
| 411 | Set<FlowRuleBatchEntry> currentOps; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 412 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 413 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 414 | currentOps = operation.getOperations().stream().map( |
| 415 | op -> { |
| 416 | StoredFlowEntry entry; |
| 417 | switch (op.getOperator()) { |
| 418 | case ADD: |
| 419 | entry = new DefaultFlowEntry(op.getTarget()); |
| 420 | // always add requested FlowRule
 |
| 421 | // Note: 2 equal FlowEntry may have different treatment |
| 422 | flowTable.remove(entry.deviceId(), entry); |
| 423 | flowTable.add(entry); |
| 424 | |
| 425 | return op; |
| 426 | case REMOVE: |
| 427 | entry = flowTable.getFlowEntry(op.target()); |
| 428 | if (entry != null) { |
| 429 | entry.setState(FlowEntryState.PENDING_REMOVE); |
| 430 | return op; |
| 431 | } |
| 432 | break; |
| 433 | case MODIFY: |
| 434 | //TODO: figure this out at some point |
| 435 | break; |
| 436 | default: |
| 437 | log.warn("Unknown flow operation operator: {}", op.getOperator()); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 438 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 439 | return null; |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 440 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 441 | ).filter(op -> op != null).collect(Collectors.toSet()); |
| 442 | if (currentOps.isEmpty()) { |
| 443 | batchOperationComplete(FlowRuleBatchEvent.completed( |
| 444 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 445 | new CompletedBatchOperation(true, Collections.emptySet(), did))); |
| 446 | return; |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 447 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 448 | updateBackup(did, currentOps); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 449 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 450 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 451 | notifyDelegate(FlowRuleBatchEvent.requested(new |
| 452 | FlowRuleBatchRequest(operation.id(), currentOps), operation.deviceId())); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 453 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 454 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 457 | private void updateBackup(DeviceId deviceId, final Set<FlowRuleBatchEntry> entries) { |
| 458 | Future<?> backup = backupExecutors.submit(new UpdateBackup(deviceId, entries)); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 459 | |
| 460 | if (syncBackup) { |
| 461 | // wait for backup to complete |
| 462 | try { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 463 | backup.get(); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 464 | } catch (InterruptedException | ExecutionException e) { |
| 465 | log.error("Failed to create backups", e); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 470 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 471 | public void deleteFlowRule(FlowRule rule) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 472 | storeBatch( |
| 473 | new FlowRuleBatchOperation( |
| 474 | Arrays.asList( |
| 475 | new FlowRuleBatchEntry( |
| 476 | FlowRuleOperation.REMOVE, |
| 477 | rule)), rule.deviceId(), idGenerator.getNewId())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 481 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
| 482 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 483 | final NodeId localId = clusterService.getLocalNode().id(); |
| 484 | if (localId.equals(replicaInfo.master().orNull())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 485 | return addOrUpdateFlowRuleInternal(rule); |
| 486 | } |
| 487 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 488 | log.warn("Tried to update FlowRule {} state," |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 489 | + " while the Node was not the master.", rule); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 490 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 493 | private FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 494 | final DeviceId did = rule.deviceId(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 495 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 496 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 497 | // check if this new rule is an update to an existing entry |
| 498 | StoredFlowEntry stored = flowTable.getFlowEntry(rule); |
| 499 | if (stored != null) { |
| 500 | stored.setBytes(rule.bytes()); |
| 501 | stored.setLife(rule.life()); |
| 502 | stored.setPackets(rule.packets()); |
| 503 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 504 | stored.setState(FlowEntryState.ADDED); |
| 505 | FlowRuleBatchEntry entry = |
| 506 | new FlowRuleBatchEntry(FlowRuleOperation.ADD, stored); |
| 507 | updateBackup(did, Sets.newHashSet(entry)); |
| 508 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 509 | } |
| 510 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 511 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 512 | |
| 513 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
| 514 | // TODO: also update backup if the behavior is correct. |
| 515 | flowTable.add(rule); |
| 516 | |
| 517 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 518 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 519 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 523 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
Yuta HIGUCHI | b6eb914 | 2014-11-26 16:18:13 -0800 | [diff] [blame] | 524 | final DeviceId deviceId = rule.deviceId(); |
| 525 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 526 | |
| 527 | final NodeId localId = clusterService.getLocalNode().id(); |
| 528 | if (localId.equals(replicaInfo.master().orNull())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 529 | // bypass and handle it locally |
| 530 | return removeFlowRuleInternal(rule); |
| 531 | } |
| 532 | |
Yuta HIGUCHI | b6eb914 | 2014-11-26 16:18:13 -0800 | [diff] [blame] | 533 | if (!replicaInfo.master().isPresent()) { |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 534 | log.warn("Failed to removeFlowRule: No master for {}", deviceId); |
Yuta HIGUCHI | b6eb914 | 2014-11-26 16:18:13 -0800 | [diff] [blame] | 535 | // TODO: revisit if this should be null (="no-op") or Exception |
| 536 | return null; |
| 537 | } |
| 538 | |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 539 | log.trace("Forwarding removeFlowRule to {}, which is the primary (master) for device {}", |
Yuta HIGUCHI | b6eb914 | 2014-11-26 16:18:13 -0800 | [diff] [blame] | 540 | replicaInfo.master().orNull(), deviceId); |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 541 | |
| 542 | ClusterMessage message = new ClusterMessage( |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 543 | clusterService.getLocalNode().id(), |
| 544 | REMOVE_FLOW_ENTRY, |
| 545 | SERIALIZER.encode(rule)); |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 546 | |
| 547 | try { |
| 548 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 549 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 550 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame] | 551 | // TODO: Retry against latest master or throw a FlowStoreException |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 552 | throw new RuntimeException(e); |
| 553 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 556 | private FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 557 | final DeviceId deviceId = rule.deviceId(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 558 | // This is where one could mark a rule as removed and still keep it in the store. |
| 559 | final boolean removed = flowTable.remove(deviceId, rule); //flowEntries.remove(deviceId, rule); |
| 560 | FlowRuleBatchEntry entry = |
| 561 | new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule); |
| 562 | updateBackup(deviceId, Sets.newHashSet(entry)); |
| 563 | if (removed) { |
| 564 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 565 | } else { |
| 566 | return null; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 567 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 568 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 569 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 570 | |
| 571 | @Override |
| 572 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 573 | //FIXME: need a per device pending response |
| 574 | |
| 575 | NodeId nodeId = pendingResponses.remove(event.subject().batchId()); |
| 576 | if (nodeId == null) { |
| 577 | notifyDelegate(event); |
| 578 | } else { |
| 579 | try { |
| 580 | ClusterMessage message = new ClusterMessage( |
| 581 | clusterService.getLocalNode().id(), |
| 582 | REMOTE_APPLY_COMPLETED, |
| 583 | SERIALIZER.encode(event)); |
| 584 | clusterCommunicator.sendAndReceive(message, nodeId); |
| 585 | } catch (IOException e) { |
| 586 | log.warn("Failed to respond to peer for batch operation result"); |
| 587 | } |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 588 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 589 | } |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 590 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 591 | private void loadFromBackup(final DeviceId did) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 592 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 593 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 594 | try { |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 595 | log.debug("Loading FlowRules for {} from backups", did); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 596 | SMap<FlowId, ImmutableList<StoredFlowEntry>> backupFlowTable = smaps.get(did); |
| 597 | for (Entry<FlowId, ImmutableList<StoredFlowEntry>> e |
| 598 | : backupFlowTable.entrySet()) { |
| 599 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 600 | log.trace("loading {}", e.getValue()); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 601 | for (StoredFlowEntry entry : e.getValue()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 602 | flowTable.getFlowEntriesById(entry).remove(entry); |
| 603 | flowTable.getFlowEntriesById(entry).add(entry); |
| 604 | |
| 605 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | } catch (ExecutionException e) { |
| 609 | log.error("Failed to load backup flowtable for {}", did, e); |
| 610 | } |
| 611 | } |
| 612 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 613 | private void removeFromPrimary(final DeviceId did) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 614 | flowTable.clearDevice(did); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 617 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 618 | private final class OnStoreBatch implements ClusterMessageHandler { |
| 619 | private final NodeId local; |
| 620 | |
| 621 | private OnStoreBatch(NodeId local) { |
| 622 | this.local = local; |
| 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public void handle(final ClusterMessage message) { |
| 627 | FlowRuleBatchOperation operation = SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 628 | log.debug("received batch request {}", operation); |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 629 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 630 | final DeviceId deviceId = operation.deviceId(); |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 631 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 632 | if (!local.equals(replicaInfo.master().orNull())) { |
| 633 | |
| 634 | Set<FlowRule> failures = new HashSet<>(operation.size()); |
| 635 | for (FlowRuleBatchEntry op : operation.getOperations()) { |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 636 | failures.add(op.target()); |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 637 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 638 | CompletedBatchOperation allFailed = new CompletedBatchOperation(false, failures, deviceId); |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 639 | // This node is no longer the master, respond as all failed. |
| 640 | // TODO: we might want to wrap response in envelope |
| 641 | // to distinguish sw programming failure and hand over |
| 642 | // it make sense in the latter case to retry immediately. |
| 643 | try { |
| 644 | message.respond(SERIALIZER.encode(allFailed)); |
| 645 | } catch (IOException e) { |
| 646 | log.error("Failed to respond back", e); |
| 647 | } |
| 648 | return; |
| 649 | } |
| 650 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 651 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 652 | pendingResponses.put(operation.id(), message.sender()); |
| 653 | storeBatchInternal(operation); |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 654 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 658 | private final class SMapLoader |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 659 | extends CacheLoader<DeviceId, SMap<FlowId, ImmutableList<StoredFlowEntry>>> { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 660 | |
| 661 | @Override |
| 662 | public SMap<FlowId, ImmutableList<StoredFlowEntry>> load(DeviceId id) |
| 663 | throws Exception { |
| 664 | IMap<byte[], byte[]> map = theInstance.getMap("flowtable_" + id.toString()); |
| 665 | return new SMap<FlowId, ImmutableList<StoredFlowEntry>>(map, SERIALIZER); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | private final class InternalReplicaInfoEventListener |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 670 | implements ReplicaInfoEventListener { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 671 | |
| 672 | @Override |
| 673 | public void event(ReplicaInfoEvent event) { |
| 674 | final NodeId local = clusterService.getLocalNode().id(); |
| 675 | final DeviceId did = event.subject(); |
| 676 | final ReplicaInfo rInfo = event.replicaInfo(); |
| 677 | |
| 678 | switch (event.type()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 679 | case MASTER_CHANGED: |
| 680 | if (local.equals(rInfo.master().orNull())) { |
| 681 | // This node is the new master, populate local structure |
| 682 | // from backup |
| 683 | loadFromBackup(did); |
| 684 | } else { |
| 685 | // This node is no longer the master holder, |
| 686 | // clean local structure |
| 687 | removeFromPrimary(did); |
| 688 | // TODO: probably should stop pending backup activities in |
| 689 | // executors to avoid overwriting with old value |
| 690 | } |
| 691 | break; |
| 692 | default: |
| 693 | break; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 694 | |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | // Task to update FlowEntries in backup HZ store |
| 700 | private final class UpdateBackup implements Runnable { |
| 701 | |
| 702 | private final DeviceId deviceId; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 703 | private final Set<FlowRuleBatchEntry> ops; |
| 704 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 705 | |
| 706 | public UpdateBackup(DeviceId deviceId, |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 707 | Set<FlowRuleBatchEntry> ops) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 708 | this.deviceId = checkNotNull(deviceId); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 709 | this.ops = checkNotNull(ops); |
| 710 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | @Override |
| 714 | public void run() { |
| 715 | try { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 716 | log.trace("update backup {} {}", deviceId, ops |
| 717 | ); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 718 | final SMap<FlowId, ImmutableList<StoredFlowEntry>> backupFlowTable = smaps.get(deviceId); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 719 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 720 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 721 | ops.stream().forEach( |
| 722 | op -> { |
| 723 | final FlowRule entry = op.getTarget(); |
| 724 | final FlowId id = entry.id(); |
| 725 | ImmutableList<StoredFlowEntry> original = backupFlowTable.get(id); |
| 726 | List<StoredFlowEntry> list = new ArrayList<>(); |
| 727 | if (original != null) { |
| 728 | list.addAll(original); |
| 729 | } |
| 730 | list.remove(op.getTarget()); |
| 731 | if (op.getOperator() == FlowRuleOperation.ADD) { |
| 732 | list.add((StoredFlowEntry) entry); |
| 733 | } |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 734 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 735 | ImmutableList<StoredFlowEntry> newValue = ImmutableList.copyOf(list); |
| 736 | boolean success; |
| 737 | if (original == null) { |
| 738 | success = (backupFlowTable.putIfAbsent(id, newValue) == null); |
| 739 | } else { |
| 740 | success = backupFlowTable.replace(id, original, newValue); |
| 741 | } |
| 742 | if (!success) { |
| 743 | log.error("Updating backup failed."); |
| 744 | } |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 745 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 746 | } |
| 747 | ); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 748 | } catch (ExecutionException e) { |
| 749 | log.error("Failed to write to backups", e); |
| 750 | } |
| 751 | |
| 752 | } |
| 753 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 754 | |
| 755 | private class InternalFlowTable { |
| 756 | |
| 757 | /* |
| 758 | TODO: This needs to be cleaned up. Perhaps using the eventually consistent |
| 759 | map when it supports distributed to a sequence of instances. |
| 760 | */ |
| 761 | |
| 762 | |
| 763 | private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, Set<StoredFlowEntry>>> |
| 764 | flowEntries = new ConcurrentHashMap<>(); |
| 765 | |
| 766 | |
| 767 | private NewConcurrentHashMap<FlowId, Set<StoredFlowEntry>> lazyEmptyFlowTable() { |
| 768 | return NewConcurrentHashMap.<FlowId, Set<StoredFlowEntry>>ifNeeded(); |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * Returns the flow table for specified device. |
| 773 | * |
| 774 | * @param deviceId identifier of the device |
| 775 | * @return Map representing Flow Table of given device. |
| 776 | */ |
| 777 | private ConcurrentMap<FlowId, Set<StoredFlowEntry>> getFlowTable(DeviceId deviceId) { |
| 778 | return createIfAbsentUnchecked(flowEntries, |
| 779 | deviceId, lazyEmptyFlowTable()); |
| 780 | } |
| 781 | |
| 782 | private Set<StoredFlowEntry> getFlowEntriesInternal(DeviceId deviceId, FlowId flowId) { |
| 783 | final ConcurrentMap<FlowId, Set<StoredFlowEntry>> flowTable = getFlowTable(deviceId); |
| 784 | Set<StoredFlowEntry> r = flowTable.get(flowId); |
| 785 | if (r == null) { |
| 786 | final Set<StoredFlowEntry> concurrentlyAdded; |
| 787 | r = new CopyOnWriteArraySet<>(); |
| 788 | concurrentlyAdded = flowTable.putIfAbsent(flowId, r); |
| 789 | if (concurrentlyAdded != null) { |
| 790 | return concurrentlyAdded; |
| 791 | } |
| 792 | } |
| 793 | return r; |
| 794 | } |
| 795 | |
| 796 | private StoredFlowEntry getFlowEntryInternal(FlowRule rule) { |
| 797 | for (StoredFlowEntry f : getFlowEntriesInternal(rule.deviceId(), rule.id())) { |
| 798 | if (f.equals(rule)) { |
| 799 | return f; |
| 800 | } |
| 801 | } |
| 802 | return null; |
| 803 | } |
| 804 | |
| 805 | private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) { |
| 806 | return getFlowTable(deviceId).values().stream() |
| 807 | .flatMap((list -> list.stream())).collect(Collectors.toSet()); |
| 808 | |
| 809 | } |
| 810 | |
| 811 | |
| 812 | public StoredFlowEntry getFlowEntry(FlowRule rule) { |
| 813 | return getFlowEntryInternal(rule); |
| 814 | } |
| 815 | |
| 816 | public Set<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 817 | return getFlowEntriesInternal(deviceId); |
| 818 | } |
| 819 | |
| 820 | public Set<StoredFlowEntry> getFlowEntriesById(FlowEntry entry) { |
| 821 | return getFlowEntriesInternal(entry.deviceId(), entry.id()); |
| 822 | } |
| 823 | |
| 824 | public void add(FlowEntry rule) { |
| 825 | ((CopyOnWriteArraySet) |
| 826 | getFlowEntriesInternal(rule.deviceId(), rule.id())).add(rule); |
| 827 | } |
| 828 | |
| 829 | public boolean remove(DeviceId deviceId, FlowEntry rule) { |
| 830 | return ((CopyOnWriteArraySet) |
| 831 | getFlowEntriesInternal(deviceId, rule.id())).remove(rule); |
| 832 | //return flowEntries.remove(deviceId, rule); |
| 833 | } |
| 834 | |
| 835 | public void clearDevice(DeviceId did) { |
| 836 | flowEntries.remove(did); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 841 | } |