Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [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.intent.impl; |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 17 | |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static com.google.common.base.Preconditions.checkState; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.util.Map; |
Praseed Balakrishnan | 1516f48 | 2014-12-03 16:33:44 -0800 | [diff] [blame] | 23 | import java.util.Objects; |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 24 | import java.util.Set; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 25 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.cluster.ClusterService; |
| 33 | import org.onosproject.cluster.LeadershipEvent; |
| 34 | import org.onosproject.cluster.LeadershipEventListener; |
| 35 | import org.onosproject.cluster.LeadershipService; |
| 36 | import org.onosproject.cluster.NodeId; |
| 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.core.CoreService; |
| 39 | import org.onosproject.event.AbstractListenerRegistry; |
| 40 | import org.onosproject.event.EventDeliveryService; |
| 41 | import org.onosproject.net.intent.IntentBatchDelegate; |
| 42 | import org.onosproject.net.intent.IntentBatchLeaderEvent; |
| 43 | import org.onosproject.net.intent.IntentBatchListener; |
| 44 | import org.onosproject.net.intent.IntentBatchService; |
| 45 | import org.onosproject.net.intent.IntentOperations; |
| 46 | import org.onosproject.store.hz.SQueue; |
| 47 | import org.onosproject.store.hz.StoreService; |
| 48 | import org.onosproject.store.serializers.KryoNamespaces; |
| 49 | import org.onosproject.store.serializers.KryoSerializer; |
| 50 | import org.onosproject.store.serializers.StoreSerializer; |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 51 | import org.onlab.util.KryoNamespace; |
| 52 | import org.slf4j.Logger; |
| 53 | |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 54 | import com.google.common.collect.Maps; |
| 55 | import com.google.common.collect.Sets; |
| 56 | import com.hazelcast.core.HazelcastInstance; |
| 57 | import com.hazelcast.core.IQueue; |
| 58 | import com.hazelcast.core.ItemEvent; |
| 59 | import com.hazelcast.core.ItemListener; |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 60 | |
| 61 | @Component(immediate = true) |
| 62 | @Service |
| 63 | public class HazelcastIntentBatchQueue |
| 64 | implements IntentBatchService { |
| 65 | |
| 66 | private final Logger log = getLogger(getClass()); |
| 67 | private static final String TOPIC_BASE = "intent-batch-"; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected CoreService coreService; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected ClusterService clusterService; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected LeadershipService leadershipService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected StoreService storeService; |
| 80 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected EventDeliveryService eventDispatcher; |
| 83 | |
| 84 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 85 | private HazelcastInstance theInstance; |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 86 | private NodeId localControllerNodeId; |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 87 | protected StoreSerializer serializer; |
| 88 | private IntentBatchDelegate delegate; |
| 89 | private InternalLeaderListener leaderListener = new InternalLeaderListener(); |
| 90 | private final Map<ApplicationId, SQueue<IntentOperations>> batchQueues |
| 91 | = Maps.newHashMap(); // FIXME make distributed? |
| 92 | private final Set<ApplicationId> myTopics = Sets.newHashSet(); |
| 93 | private final Map<ApplicationId, IntentOperations> outstandingOps |
| 94 | = Maps.newHashMap(); |
| 95 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 96 | private final AbstractListenerRegistry<IntentBatchLeaderEvent, IntentBatchListener> |
| 97 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 98 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 99 | @Activate |
| 100 | public void activate() { |
| 101 | theInstance = storeService.getHazelcastInstance(); |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 102 | localControllerNodeId = clusterService.getLocalNode().id(); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 103 | leadershipService.addListener(leaderListener); |
| 104 | |
| 105 | serializer = new KryoSerializer() { |
| 106 | |
| 107 | @Override |
| 108 | protected void setupKryoPool() { |
| 109 | serializerPool = KryoNamespace.newBuilder() |
| 110 | .setRegistrationRequired(false) |
| 111 | .register(KryoNamespaces.API) |
| 112 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 113 | .build(); |
| 114 | } |
| 115 | |
| 116 | }; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 117 | eventDispatcher.addSink(IntentBatchLeaderEvent.class, listenerRegistry); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 118 | log.info("Started"); |
| 119 | } |
| 120 | |
| 121 | @Deactivate |
| 122 | public void deactivate() { |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 123 | eventDispatcher.removeSink(IntentBatchLeaderEvent.class); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 124 | leadershipService.removeListener(leaderListener); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 125 | for (ApplicationId appId: batchQueues.keySet()) { |
| 126 | leadershipService.withdraw(getTopic(appId)); |
| 127 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 128 | log.info("Stopped"); |
| 129 | } |
| 130 | |
| 131 | public static String getTopic(ApplicationId appId) { |
| 132 | return TOPIC_BASE + checkNotNull(appId.id()); |
| 133 | } |
| 134 | |
| 135 | public ApplicationId getAppId(String topic) { |
| 136 | checkState(topic.startsWith(TOPIC_BASE), |
| 137 | "Trying to get app id for invalid topic: {}", topic); |
| 138 | Short id = Short.parseShort(topic.substring(TOPIC_BASE.length())); |
| 139 | return coreService.getAppId(id); |
| 140 | } |
| 141 | |
| 142 | private SQueue<IntentOperations> getQueue(ApplicationId appId) { |
| 143 | SQueue<IntentOperations> queue = batchQueues.get(appId); |
| 144 | if (queue == null) { |
| 145 | synchronized (this) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 146 | String topic = getTopic(appId); |
| 147 | IQueue<byte[]> rawQueue = theInstance.getQueue(topic); |
| 148 | queue = new SQueue<>(rawQueue, serializer); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 149 | queue.addItemListener(new InternalItemListener(appId), false); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 150 | batchQueues.putIfAbsent(appId, queue); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 151 | leadershipService.runForLeadership(topic); |
| 152 | } |
| 153 | } |
| 154 | return queue; |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void addIntentOperations(IntentOperations ops) { |
| 159 | checkNotNull(ops, "Intent operations cannot be null."); |
| 160 | ApplicationId appId = ops.appId(); |
| 161 | getQueue(appId).add(ops); // TODO consider using put here |
| 162 | dispatchNextOperation(appId); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void removeIntentOperations(IntentOperations ops) { |
| 167 | ApplicationId appId = ops.appId(); |
| 168 | synchronized (this) { |
Praseed Balakrishnan | 1516f48 | 2014-12-03 16:33:44 -0800 | [diff] [blame] | 169 | IntentOperations outstanding = outstandingOps.get(appId); |
| 170 | if (outstanding != null) { |
| 171 | checkState(Objects.equals(ops, outstanding), |
| 172 | "Operation {} does not match outstanding operation {}", |
| 173 | ops, outstanding); |
| 174 | } else { |
| 175 | log.warn("Operation {} not found", ops); |
| 176 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 177 | SQueue<IntentOperations> queue = batchQueues.get(appId); |
| 178 | // TODO consider alternatives to remove |
| 179 | checkState(queue.remove().equals(ops), |
| 180 | "Operations are wrong."); |
| 181 | outstandingOps.remove(appId); |
| 182 | dispatchNextOperation(appId); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Dispatches the next available operations to the delegate, unless |
| 188 | * we are not the leader for this application id or there is an |
| 189 | * outstanding operations for this application id. |
| 190 | * |
| 191 | * @param appId application id |
| 192 | */ |
| 193 | private void dispatchNextOperation(ApplicationId appId) { |
| 194 | synchronized (this) { |
| 195 | if (!myTopics.contains(appId) || |
| 196 | outstandingOps.containsKey(appId)) { |
| 197 | return; |
| 198 | } |
| 199 | IntentOperations ops = batchQueues.get(appId).peek(); |
| 200 | if (ops != null) { |
| 201 | outstandingOps.put(appId, ops); |
| 202 | delegate.execute(ops); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Record the leadership change for the given topic. If we have become the |
| 209 | * leader, then dispatch the next operations. If we have lost leadership, |
| 210 | * then cancel the last operations. |
| 211 | * |
| 212 | * @param topic topic based on application id |
| 213 | * @param leader true if we have become the leader, false otherwise |
| 214 | */ |
| 215 | private void leaderChanged(String topic, boolean leader) { |
| 216 | ApplicationId appId = getAppId(topic); |
| 217 | //TODO we are using the event caller's thread, should we use our own? |
| 218 | synchronized (this) { |
| 219 | if (leader) { |
| 220 | myTopics.add(appId); |
| 221 | checkState(!outstandingOps.containsKey(appId), |
| 222 | "Existing intent ops for app id: {}", appId); |
| 223 | dispatchNextOperation(appId); |
| 224 | } else { |
| 225 | myTopics.remove(appId); |
| 226 | IntentOperations ops = outstandingOps.get(appId); |
| 227 | if (ops != null) { |
| 228 | delegate.cancel(ops); |
| 229 | } |
| 230 | outstandingOps.remove(appId); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 235 | private class InternalItemListener implements ItemListener<IntentOperations> { |
| 236 | |
| 237 | private final ApplicationId appId; |
| 238 | |
| 239 | public InternalItemListener(ApplicationId appId) { |
| 240 | this.appId = appId; |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void itemAdded(ItemEvent<IntentOperations> item) { |
| 245 | dispatchNextOperation(appId); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void itemRemoved(ItemEvent<IntentOperations> item) { |
| 250 | // no-op |
| 251 | } |
| 252 | } |
| 253 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 254 | private class InternalLeaderListener implements LeadershipEventListener { |
| 255 | @Override |
| 256 | public void event(LeadershipEvent event) { |
Yuta HIGUCHI | 47b2f55 | 2014-11-28 20:13:15 -0800 | [diff] [blame] | 257 | log.trace("Leadership Event: time = {} type = {} event = {}", |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 258 | event.time(), event.type(), event); |
| 259 | |
| 260 | String topic = event.subject().topic(); |
| 261 | if (!topic.startsWith(TOPIC_BASE)) { |
| 262 | return; // Not our topic: ignore |
| 263 | } |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 264 | if (!event.subject().leader().equals(localControllerNodeId)) { |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 265 | // run for leadership |
| 266 | getQueue(getAppId(topic)); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 267 | return; // The event is not about this instance: ignore |
| 268 | } |
| 269 | |
| 270 | switch (event.type()) { |
| 271 | case LEADER_ELECTED: |
| 272 | log.info("Elected leader for app {}", getAppId(topic)); |
| 273 | leaderChanged(topic, true); |
| 274 | break; |
| 275 | case LEADER_BOOTED: |
| 276 | log.info("Lost leader election for app {}", getAppId(topic)); |
| 277 | leaderChanged(topic, false); |
| 278 | break; |
| 279 | case LEADER_REELECTED: |
| 280 | break; |
| 281 | default: |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public Set<IntentOperations> getPendingOperations() { |
| 289 | Set<IntentOperations> ops = Sets.newHashSet(); |
| 290 | synchronized (this) { |
| 291 | for (SQueue<IntentOperations> queue : batchQueues.values()) { |
| 292 | ops.addAll(queue); |
| 293 | } |
| 294 | return ops; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | @Override |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 299 | public boolean isLocalLeader(ApplicationId applicationId) { |
| 300 | return myTopics.contains(applicationId); |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void setDelegate(IntentBatchDelegate delegate) { |
| 305 | this.delegate = checkNotNull(delegate, "Delegate cannot be null"); |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public void unsetDelegate(IntentBatchDelegate delegate) { |
| 310 | if (this.delegate != null && this.delegate.equals(delegate)) { |
| 311 | this.delegate = null; |
| 312 | } |
| 313 | } |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 314 | |
| 315 | @Override |
| 316 | public void addListener(IntentBatchListener listener) { |
| 317 | listenerRegistry.addListener(listener); |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public void removeListener(IntentBatchListener listener) { |
| 322 | listenerRegistry.removeListener(listener); |
| 323 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 324 | } |