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; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 19 | import static org.onlab.util.Tools.namedThreads; |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 20 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 21 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.APPLY_BATCH_FLOWS; |
| 22 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.GET_DEVICE_FLOW_ENTRIES; |
| 23 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.GET_FLOW_ENTRY; |
| 24 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.REMOVE_FLOW_ENTRY; |
| 25 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 26 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 27 | import java.io.IOException; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | import java.util.Arrays; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 30 | import java.util.Collection; |
| 31 | import java.util.Collections; |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 32 | import java.util.HashSet; |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 33 | import java.util.List; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 34 | import java.util.Map.Entry; |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 35 | import java.util.Set; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 36 | import java.util.concurrent.ExecutionException; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 37 | import java.util.concurrent.ExecutorService; |
| 38 | import java.util.concurrent.Executors; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 39 | import java.util.concurrent.Future; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 40 | import java.util.concurrent.TimeUnit; |
| 41 | import java.util.concurrent.TimeoutException; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 42 | import java.util.concurrent.atomic.AtomicInteger; |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 43 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 44 | |
| 45 | import org.apache.felix.scr.annotations.Activate; |
| 46 | import org.apache.felix.scr.annotations.Component; |
| 47 | import org.apache.felix.scr.annotations.Deactivate; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 48 | import org.apache.felix.scr.annotations.Reference; |
| 49 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 50 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 51 | import org.onlab.util.KryoNamespace; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.cluster.ClusterService; |
| 53 | import org.onosproject.cluster.NodeId; |
| 54 | import org.onosproject.net.Device; |
| 55 | import org.onosproject.net.DeviceId; |
| 56 | import org.onosproject.net.device.DeviceService; |
| 57 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 58 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 59 | import org.onosproject.net.flow.FlowEntry; |
| 60 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 61 | import org.onosproject.net.flow.FlowId; |
| 62 | import org.onosproject.net.flow.FlowRule; |
| 63 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 64 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 65 | import org.onosproject.net.flow.FlowRuleBatchEvent; |
| 66 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 67 | import org.onosproject.net.flow.FlowRuleBatchRequest; |
| 68 | import org.onosproject.net.flow.FlowRuleEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 69 | import org.onosproject.net.flow.FlowRuleEvent.Type; |
| 70 | import org.onosproject.net.flow.FlowRuleStore; |
| 71 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 72 | import org.onosproject.net.flow.StoredFlowEntry; |
| 73 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 74 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 75 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 76 | import org.onosproject.store.flow.ReplicaInfo; |
| 77 | import org.onosproject.store.flow.ReplicaInfoEvent; |
| 78 | import org.onosproject.store.flow.ReplicaInfoEventListener; |
| 79 | import org.onosproject.store.flow.ReplicaInfoService; |
| 80 | import org.onosproject.store.hz.AbstractHazelcastStore; |
| 81 | import org.onosproject.store.hz.SMap; |
| 82 | import org.onosproject.store.serializers.DecodeTo; |
| 83 | import org.onosproject.store.serializers.KryoSerializer; |
| 84 | import org.onosproject.store.serializers.StoreSerializer; |
| 85 | import org.onosproject.store.serializers.impl.DistributedStoreSerializers; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 86 | import org.slf4j.Logger; |
| 87 | |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 88 | import com.google.common.cache.Cache; |
| 89 | import com.google.common.cache.CacheBuilder; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 90 | import com.google.common.cache.CacheLoader; |
| 91 | import com.google.common.cache.LoadingCache; |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 92 | import com.google.common.cache.RemovalListener; |
| 93 | import com.google.common.cache.RemovalNotification; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 94 | import com.google.common.collect.ArrayListMultimap; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 95 | import com.google.common.collect.ImmutableList; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 96 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 97 | import com.google.common.collect.Iterables; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 98 | import com.google.common.collect.Multimap; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 99 | import com.google.common.util.concurrent.Futures; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 100 | import com.google.common.util.concurrent.ListenableFuture; |
| 101 | import com.google.common.util.concurrent.SettableFuture; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 102 | import com.hazelcast.core.IMap; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 105 | * Manages inventory of flow rules using a distributed state management protocol. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 106 | */ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 107 | @Component(immediate = true) |
| 108 | @Service |
| 109 | public class DistributedFlowRuleStore |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 110 | extends AbstractHazelcastStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 111 | implements FlowRuleStore { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 112 | |
| 113 | private final Logger log = getLogger(getClass()); |
| 114 | |
Yuta HIGUCHI | e9b2b00 | 2014-11-04 12:25:47 -0800 | [diff] [blame] | 115 | // primary data: |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 116 | // read/write needs to be locked |
| 117 | private final ReentrantReadWriteLock flowEntriesLock = new ReentrantReadWriteLock(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 118 | // store entries as a pile of rules, no info about device tables |
Yuta HIGUCHI | e9b2b00 | 2014-11-04 12:25:47 -0800 | [diff] [blame] | 119 | private final Multimap<DeviceId, StoredFlowEntry> flowEntries |
| 120 | = ArrayListMultimap.<DeviceId, StoredFlowEntry>create(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 121 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 123 | protected ReplicaInfoService replicaInfoManager; |
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 ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 129 | protected ClusterService clusterService; |
| 130 | |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 132 | protected DeviceService deviceService; |
| 133 | |
| 134 | private final AtomicInteger localBatchIdGen = new AtomicInteger(); |
| 135 | |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 136 | private int pendingFutureTimeoutMinutes = 5; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 137 | |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 138 | private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures = |
| 139 | CacheBuilder.newBuilder() |
| 140 | .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES) |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 141 | .removalListener(new TimeoutFuture()) |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 142 | .build(); |
| 143 | |
Yuta HIGUCHI | e9b2b00 | 2014-11-04 12:25:47 -0800 | [diff] [blame] | 144 | // 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] | 145 | private LoadingCache<DeviceId, SMap<FlowId, ImmutableList<StoredFlowEntry>>> smaps; |
| 146 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 147 | |
| 148 | private final ExecutorService futureListeners = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 149 | Executors.newCachedThreadPool(namedThreads("onos-flowstore-peer-responders")); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 150 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 151 | private final ExecutorService backupExecutors = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 152 | Executors.newSingleThreadExecutor(namedThreads("onos-async-backups")); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 153 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 154 | private boolean syncBackup = false; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 155 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 156 | protected static final StoreSerializer SERIALIZER = new KryoSerializer() { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 157 | @Override |
| 158 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 159 | serializerPool = KryoNamespace.newBuilder() |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 160 | .register(DistributedStoreSerializers.STORE_COMMON) |
| 161 | .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) |
Yuta HIGUCHI | 2f15833 | 2014-11-25 13:32:06 -0800 | [diff] [blame] | 162 | .register(FlowRuleEvent.class) |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 163 | .register(FlowRuleEvent.Type.class) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 164 | .build(); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 165 | } |
| 166 | }; |
| 167 | |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 168 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 169 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 170 | private ReplicaInfoEventListener replicaInfoEventListener; |
| 171 | |
| 172 | @Override |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 173 | @Activate |
| 174 | public void activate() { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 175 | |
| 176 | super.serializer = SERIALIZER; |
| 177 | super.theInstance = storeService.getHazelcastInstance(); |
| 178 | |
| 179 | // Cache to create SMap on demand |
| 180 | smaps = CacheBuilder.newBuilder() |
| 181 | .softValues() |
| 182 | .build(new SMapLoader()); |
| 183 | |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 184 | final NodeId local = clusterService.getLocalNode().id(); |
| 185 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 186 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new OnStoreBatch(local)); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 187 | |
| 188 | clusterCommunicator.addSubscriber(GET_FLOW_ENTRY, new ClusterMessageHandler() { |
| 189 | |
| 190 | @Override |
| 191 | public void handle(ClusterMessage message) { |
| 192 | FlowRule rule = SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 193 | log.trace("received get flow entry request for {}", rule); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 194 | FlowEntry flowEntry = getFlowEntryInternal(rule); |
| 195 | try { |
| 196 | message.respond(SERIALIZER.encode(flowEntry)); |
| 197 | } catch (IOException e) { |
| 198 | log.error("Failed to respond back", e); |
| 199 | } |
| 200 | } |
| 201 | }); |
| 202 | |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 203 | clusterCommunicator.addSubscriber(GET_DEVICE_FLOW_ENTRIES, new ClusterMessageHandler() { |
| 204 | |
| 205 | @Override |
| 206 | public void handle(ClusterMessage message) { |
| 207 | DeviceId deviceId = SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 208 | log.trace("Received get flow entries request for {} from {}", deviceId, message.sender()); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 209 | Set<FlowEntry> flowEntries = getFlowEntriesInternal(deviceId); |
| 210 | try { |
| 211 | message.respond(SERIALIZER.encode(flowEntries)); |
| 212 | } catch (IOException e) { |
| 213 | log.error("Failed to respond to peer's getFlowEntries request", e); |
| 214 | } |
| 215 | } |
| 216 | }); |
| 217 | |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 218 | clusterCommunicator.addSubscriber(REMOVE_FLOW_ENTRY, new ClusterMessageHandler() { |
| 219 | |
| 220 | @Override |
| 221 | public void handle(ClusterMessage message) { |
| 222 | FlowEntry rule = SERIALIZER.decode(message.payload()); |
| 223 | log.trace("received get flow entry request for {}", rule); |
| 224 | FlowRuleEvent event = removeFlowRuleInternal(rule); |
| 225 | try { |
| 226 | message.respond(SERIALIZER.encode(event)); |
| 227 | } catch (IOException e) { |
| 228 | log.error("Failed to respond back", e); |
| 229 | } |
| 230 | } |
| 231 | }); |
| 232 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 233 | replicaInfoEventListener = new InternalReplicaInfoEventListener(); |
| 234 | |
| 235 | replicaInfoManager.addListener(replicaInfoEventListener); |
| 236 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 237 | log.info("Started"); |
| 238 | } |
| 239 | |
| 240 | @Deactivate |
| 241 | public void deactivate() { |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 242 | clusterCommunicator.removeSubscriber(REMOVE_FLOW_ENTRY); |
| 243 | clusterCommunicator.removeSubscriber(GET_DEVICE_FLOW_ENTRIES); |
| 244 | clusterCommunicator.removeSubscriber(GET_FLOW_ENTRY); |
| 245 | clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 246 | replicaInfoManager.removeListener(replicaInfoEventListener); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 247 | log.info("Stopped"); |
| 248 | } |
| 249 | |
| 250 | |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 251 | // This is not a efficient operation on a distributed sharded |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 252 | // flow store. We need to revisit the need for this operation or at least |
| 253 | // make it device specific. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 254 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 255 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 256 | // implementing in-efficient operation for debugging purpose. |
| 257 | int sum = 0; |
| 258 | for (Device device : deviceService.getDevices()) { |
| 259 | final DeviceId did = device.id(); |
| 260 | sum += Iterables.size(getFlowEntries(did)); |
| 261 | } |
| 262 | return sum; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 266 | public FlowEntry getFlowEntry(FlowRule rule) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 267 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 268 | |
| 269 | if (!replicaInfo.master().isPresent()) { |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 270 | log.warn("Failed to getFlowEntry: No master for {}", rule.deviceId()); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 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) { |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 290 | log.warn("Unable to fetch flow store contents from {}", replicaInfo.master().get()); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 291 | } |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 292 | return null; |
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()) { |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 315 | log.warn("Failed to getFlowEntries: No master for {}", deviceId); |
Yuta HIGUCHI | 2c1d847 | 2014-10-31 14:13:38 -0700 | [diff] [blame] | 316 | return Collections.emptyList(); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 319 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 320 | return getFlowEntriesInternal(deviceId); |
| 321 | } |
| 322 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 323 | log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 324 | replicaInfo.master().orNull(), deviceId); |
| 325 | |
| 326 | ClusterMessage message = new ClusterMessage( |
| 327 | clusterService.getLocalNode().id(), |
| 328 | GET_DEVICE_FLOW_ENTRIES, |
| 329 | SERIALIZER.encode(deviceId)); |
| 330 | |
| 331 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 332 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 333 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 334 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 335 | log.warn("Unable to fetch flow store contents from {}", replicaInfo.master().get()); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 336 | } |
Yuta HIGUCHI | 24f79eb | 2014-12-12 15:46:43 -0800 | [diff] [blame] | 337 | return Collections.emptyList(); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 340 | private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) { |
| 341 | flowEntriesLock.readLock().lock(); |
| 342 | try { |
| 343 | Collection<? extends FlowEntry> rules = flowEntries.get(deviceId); |
| 344 | if (rules == null) { |
| 345 | return Collections.emptySet(); |
| 346 | } |
| 347 | return ImmutableSet.copyOf(rules); |
| 348 | } finally { |
| 349 | flowEntriesLock.readLock().unlock(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 350 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 354 | public void storeFlowRule(FlowRule rule) { |
| 355 | storeBatch(new FlowRuleBatchOperation(Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)))); |
| 356 | } |
| 357 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 358 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 359 | public Future<CompletedBatchOperation> storeBatch(FlowRuleBatchOperation operation) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 360 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 361 | if (operation.getOperations().isEmpty()) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 362 | return Futures.immediateFuture(new CompletedBatchOperation(true, |
| 363 | Collections.<FlowRule>emptySet())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 364 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 365 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 366 | DeviceId deviceId = operation.getOperations().get(0).getTarget().deviceId(); |
| 367 | |
| 368 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 369 | |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 370 | if (!replicaInfo.master().isPresent()) { |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 371 | log.warn("Failed to storeBatch: No master for {}", deviceId); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 372 | // TODO: revisit if this should be "success" from Future point of view |
| 373 | // with every FlowEntry failed |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 374 | return Futures.immediateFailedFuture(new IOException("Failed to storeBatch: No master for " + deviceId)); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | final NodeId local = clusterService.getLocalNode().id(); |
| 378 | if (replicaInfo.master().get().equals(local)) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 379 | return storeBatchInternal(operation); |
| 380 | } |
| 381 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 382 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 383 | replicaInfo.master().orNull(), deviceId); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 384 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 385 | ClusterMessage message = new ClusterMessage( |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 386 | local, |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 387 | APPLY_BATCH_FLOWS, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 388 | SERIALIZER.encode(operation)); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 389 | |
| 390 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 391 | ListenableFuture<byte[]> responseFuture = |
| 392 | clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 393 | return Futures.transform(responseFuture, new DecodeTo<CompletedBatchOperation>(SERIALIZER)); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 394 | } catch (IOException e) { |
| 395 | return Futures.immediateFailedFuture(e); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 399 | private ListenableFuture<CompletedBatchOperation> |
Yuta HIGUCHI | e9b2b00 | 2014-11-04 12:25:47 -0800 | [diff] [blame] | 400 | storeBatchInternal(FlowRuleBatchOperation operation) { |
| 401 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 402 | final List<FlowRuleBatchEntry> toRemove = new ArrayList<>(); |
| 403 | final List<FlowRuleBatchEntry> toAdd = new ArrayList<>(); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 404 | DeviceId did = null; |
| 405 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 406 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 407 | flowEntriesLock.writeLock().lock(); |
| 408 | try { |
| 409 | for (FlowRuleBatchEntry batchEntry : operation.getOperations()) { |
| 410 | FlowRule flowRule = batchEntry.getTarget(); |
| 411 | FlowRuleOperation op = batchEntry.getOperator(); |
| 412 | if (did == null) { |
| 413 | did = flowRule.deviceId(); |
| 414 | } |
| 415 | if (op.equals(FlowRuleOperation.REMOVE)) { |
| 416 | StoredFlowEntry entry = getFlowEntryInternal(flowRule); |
| 417 | if (entry != null) { |
| 418 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 419 | toRemove.add(batchEntry); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 420 | } |
| 421 | } else if (op.equals(FlowRuleOperation.ADD)) { |
| 422 | StoredFlowEntry flowEntry = new DefaultFlowEntry(flowRule); |
| 423 | DeviceId deviceId = flowRule.deviceId(); |
Yuta HIGUCHI | 08f156b | 2014-12-12 13:34:05 -0800 | [diff] [blame] | 424 | Collection<StoredFlowEntry> ft = flowEntries.get(deviceId); |
| 425 | |
| 426 | // always add requested FlowRule |
| 427 | // Note: 2 equal FlowEntry may have different treatment |
| 428 | ft.remove(flowEntry); |
| 429 | ft.add(flowEntry); |
| 430 | toAdd.add(batchEntry); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | if (toAdd.isEmpty() && toRemove.isEmpty()) { |
| 434 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowRule>emptySet())); |
| 435 | } |
| 436 | |
| 437 | // create remote backup copies |
| 438 | updateBackup(did, toAdd, toRemove); |
| 439 | } finally { |
| 440 | flowEntriesLock.writeLock().unlock(); |
| 441 | } |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 442 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 443 | SettableFuture<CompletedBatchOperation> r = SettableFuture.create(); |
| 444 | final int batchId = localBatchIdGen.incrementAndGet(); |
| 445 | |
| 446 | pendingFutures.put(batchId, r); |
| 447 | notifyDelegate(FlowRuleBatchEvent.requested(new FlowRuleBatchRequest(batchId, toAdd, toRemove))); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 448 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 449 | return r; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 452 | private void updateBackup(final DeviceId deviceId, |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 453 | final List<FlowRuleBatchEntry> toAdd, |
| 454 | final List<FlowRuleBatchEntry> list) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 455 | |
| 456 | Future<?> submit = backupExecutors.submit(new UpdateBackup(deviceId, toAdd, list)); |
| 457 | |
| 458 | if (syncBackup) { |
| 459 | // wait for backup to complete |
| 460 | try { |
| 461 | submit.get(); |
| 462 | } catch (InterruptedException | ExecutionException e) { |
| 463 | log.error("Failed to create backups", e); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 468 | private void updateBackup(DeviceId deviceId, List<FlowRuleBatchEntry> toAdd) { |
| 469 | |
| 470 | updateBackup(deviceId, toAdd, Collections.<FlowRuleBatchEntry>emptyList()); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 471 | } |
| 472 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 473 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 474 | public void deleteFlowRule(FlowRule rule) { |
| 475 | storeBatch(new FlowRuleBatchOperation(Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule)))); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 479 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
| 480 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 481 | final NodeId localId = clusterService.getLocalNode().id(); |
| 482 | if (localId.equals(replicaInfo.master().orNull())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 483 | return addOrUpdateFlowRuleInternal(rule); |
| 484 | } |
| 485 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 486 | log.warn("Tried to update FlowRule {} state," |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 487 | + " while the Node was not the master.", rule); |
| 488 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 491 | private FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 492 | final DeviceId did = rule.deviceId(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 493 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 494 | flowEntriesLock.writeLock().lock(); |
| 495 | try { |
| 496 | // check if this new rule is an update to an existing entry |
| 497 | StoredFlowEntry stored = getFlowEntryInternal(rule); |
| 498 | if (stored != null) { |
| 499 | stored.setBytes(rule.bytes()); |
| 500 | stored.setLife(rule.life()); |
| 501 | stored.setPackets(rule.packets()); |
| 502 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 503 | stored.setState(FlowEntryState.ADDED); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 504 | FlowRuleBatchEntry entry = |
| 505 | new FlowRuleBatchEntry(FlowRuleOperation.ADD, stored); |
| 506 | updateBackup(did, Arrays.asList(entry)); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 507 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 508 | } |
| 509 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 510 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 511 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 512 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame] | 513 | // TODO: also update backup if the behavior is correct. |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 514 | flowEntries.put(did, new DefaultFlowEntry(rule)); |
| 515 | } finally { |
| 516 | flowEntriesLock.writeLock().unlock(); |
| 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( |
| 543 | clusterService.getLocalNode().id(), |
| 544 | REMOVE_FLOW_ENTRY, |
| 545 | SERIALIZER.encode(rule)); |
| 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(); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 558 | flowEntriesLock.writeLock().lock(); |
| 559 | try { |
| 560 | // This is where one could mark a rule as removed and still keep it in the store. |
| 561 | final boolean removed = flowEntries.remove(deviceId, rule); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 562 | FlowRuleBatchEntry entry = |
| 563 | new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule); |
| 564 | updateBackup(deviceId, Collections.<FlowRuleBatchEntry>emptyList(), Arrays.asList(entry)); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 565 | if (removed) { |
| 566 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 567 | } else { |
| 568 | return null; |
| 569 | } |
| 570 | } finally { |
| 571 | flowEntriesLock.writeLock().unlock(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 572 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 573 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 574 | |
| 575 | @Override |
| 576 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 577 | final Integer batchId = event.subject().batchId(); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 578 | SettableFuture<CompletedBatchOperation> future |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 579 | = pendingFutures.getIfPresent(batchId); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 580 | if (future != null) { |
| 581 | future.set(event.result()); |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 582 | pendingFutures.invalidate(batchId); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 583 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 584 | notifyDelegate(event); |
| 585 | } |
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 | private void loadFromBackup(final DeviceId did) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 588 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 589 | flowEntriesLock.writeLock().lock(); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 590 | try { |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 591 | log.debug("Loading FlowRules for {} from backups", did); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 592 | SMap<FlowId, ImmutableList<StoredFlowEntry>> backupFlowTable = smaps.get(did); |
| 593 | for (Entry<FlowId, ImmutableList<StoredFlowEntry>> e |
| 594 | : backupFlowTable.entrySet()) { |
| 595 | |
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); |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame] | 722 | // TODO: probably should stop pending backup activities in |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 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 | } |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 775 | if (!success) { |
| 776 | log.error("Updating backup failed."); |
| 777 | } |
| 778 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 779 | for (FlowRuleBatchEntry bEntry : toRemove) { |
| 780 | final FlowRule entry = bEntry.getTarget(); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 781 | final FlowId id = entry.id(); |
| 782 | ImmutableList<StoredFlowEntry> original = backupFlowTable.get(id); |
| 783 | List<StoredFlowEntry> list = new ArrayList<>(); |
| 784 | if (original != null) { |
| 785 | list.addAll(original); |
| 786 | } |
| 787 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 788 | list.remove(bEntry.getTarget()); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 789 | |
| 790 | ImmutableList<StoredFlowEntry> newValue = ImmutableList.copyOf(list); |
| 791 | boolean success; |
| 792 | if (original == null) { |
| 793 | success = (backupFlowTable.putIfAbsent(id, newValue) == null); |
| 794 | } else { |
| 795 | success = backupFlowTable.replace(id, original, newValue); |
| 796 | } |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 797 | if (!success) { |
| 798 | log.error("Updating backup failed."); |
| 799 | } |
| 800 | } |
| 801 | } catch (ExecutionException e) { |
| 802 | log.error("Failed to write to backups", e); |
| 803 | } |
| 804 | |
| 805 | } |
| 806 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 807 | } |