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