Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.store.intent.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.util.KryoNamespace; |
| 28 | import org.onosproject.cluster.ClusterService; |
| 29 | import org.onosproject.cluster.NodeId; |
| 30 | import org.onosproject.net.intent.BatchWrite; |
| 31 | import org.onosproject.net.intent.Intent; |
| 32 | import org.onosproject.net.intent.IntentClockService; |
| 33 | import org.onosproject.net.intent.IntentEvent; |
| 34 | import org.onosproject.net.intent.IntentId; |
| 35 | import org.onosproject.net.intent.IntentState; |
| 36 | import org.onosproject.net.intent.IntentStore; |
| 37 | import org.onosproject.net.intent.IntentStoreDelegate; |
| 38 | import org.onosproject.store.AbstractStore; |
| 39 | import org.onosproject.store.Timestamp; |
| 40 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 41 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 42 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 43 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 44 | import org.onosproject.store.impl.Timestamped; |
| 45 | import org.onosproject.store.serializers.KryoSerializer; |
| 46 | import org.onosproject.store.serializers.impl.DistributedStoreSerializers; |
| 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import java.io.IOException; |
| 50 | import java.util.ArrayList; |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 51 | import java.util.Collections; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 52 | import java.util.List; |
| 53 | import java.util.Set; |
| 54 | import java.util.concurrent.ConcurrentHashMap; |
| 55 | import java.util.concurrent.ConcurrentMap; |
| 56 | import java.util.concurrent.ExecutorService; |
| 57 | import java.util.concurrent.Executors; |
| 58 | import java.util.concurrent.ScheduledExecutorService; |
| 59 | import java.util.concurrent.TimeUnit; |
| 60 | |
| 61 | import static com.google.common.base.Preconditions.checkArgument; |
| 62 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 63 | import static org.onlab.util.Tools.minPriority; |
| 64 | import static org.onlab.util.Tools.namedThreads; |
| 65 | import static org.onosproject.net.intent.IntentState.INSTALL_REQ; |
| 66 | import static org.onosproject.store.intent.impl.GossipIntentStoreMessageSubjects.INTENT_ANTI_ENTROPY_ADVERTISEMENT; |
| 67 | import static org.onosproject.store.intent.impl.GossipIntentStoreMessageSubjects.INTENT_SET_INSTALLABLES_MSG; |
| 68 | import static org.onosproject.store.intent.impl.GossipIntentStoreMessageSubjects.INTENT_UPDATED_MSG; |
| 69 | import static org.slf4j.LoggerFactory.getLogger; |
| 70 | |
| 71 | /** |
| 72 | * Manages inventory of Intents in a distributed data store that uses optimistic |
| 73 | * replication and gossip based techniques. |
| 74 | */ |
| 75 | @Component(immediate = true, enabled = false) |
| 76 | @Service |
| 77 | public class GossipIntentStore |
| 78 | extends AbstractStore<IntentEvent, IntentStoreDelegate> |
| 79 | implements IntentStore { |
| 80 | |
| 81 | private final Logger log = getLogger(getClass()); |
| 82 | |
| 83 | private final ConcurrentMap<IntentId, Intent> intents = |
| 84 | new ConcurrentHashMap<>(); |
| 85 | |
| 86 | private final ConcurrentMap<IntentId, Timestamped<IntentState>> intentStates |
| 87 | = new ConcurrentHashMap<>(); |
| 88 | |
| 89 | private final Set<IntentId> withdrawRequestedIntents |
| 90 | = Sets.newConcurrentHashSet(); |
| 91 | |
| 92 | private ConcurrentMap<IntentId, Timestamped<List<Intent>>> installables |
| 93 | = new ConcurrentHashMap<>(); |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected IntentClockService intentClockService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected ClusterCommunicationService clusterCommunicator; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected ClusterService clusterService; |
| 103 | |
| 104 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 105 | @Override |
| 106 | protected void setupKryoPool() { |
| 107 | serializerPool = KryoNamespace.newBuilder() |
| 108 | .register(DistributedStoreSerializers.STORE_COMMON) |
| 109 | .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) |
| 110 | .register(InternalIntentEvent.class) |
| 111 | .register(InternalSetInstallablesEvent.class) |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 112 | .register(Collections.emptyList().getClass()) |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 113 | //.register(InternalIntentAntiEntropyEvent.class) |
| 114 | //.register(IntentAntiEntropyAdvertisement.class) |
| 115 | .build(); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | private ExecutorService executor; |
| 120 | |
| 121 | private ScheduledExecutorService backgroundExecutor; |
| 122 | |
| 123 | // TODO: Make these anti-entropy params configurable |
| 124 | private long initialDelaySec = 5; |
| 125 | private long periodSec = 5; |
| 126 | |
| 127 | @Activate |
| 128 | public void activate() { |
| 129 | clusterCommunicator.addSubscriber(INTENT_UPDATED_MSG, |
| 130 | new InternalIntentCreateOrUpdateEventListener()); |
| 131 | clusterCommunicator.addSubscriber(INTENT_SET_INSTALLABLES_MSG, |
| 132 | new InternalIntentSetInstallablesListener()); |
| 133 | clusterCommunicator.addSubscriber( |
| 134 | INTENT_ANTI_ENTROPY_ADVERTISEMENT, |
| 135 | new InternalIntentAntiEntropyAdvertisementListener()); |
| 136 | |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 137 | executor = Executors.newCachedThreadPool(namedThreads("onos-intent-fg-%d")); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 138 | |
| 139 | backgroundExecutor = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 140 | newSingleThreadScheduledExecutor(minPriority(namedThreads("onos-intent-bg-%d"))); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 141 | |
| 142 | // start anti-entropy thread |
| 143 | //backgroundExecutor.scheduleAtFixedRate(new SendAdvertisementTask(), |
| 144 | //initialDelaySec, periodSec, TimeUnit.SECONDS); |
| 145 | |
| 146 | log.info("Started"); |
| 147 | } |
| 148 | |
| 149 | @Deactivate |
| 150 | public void deactivate() { |
| 151 | executor.shutdownNow(); |
| 152 | backgroundExecutor.shutdownNow(); |
| 153 | try { |
| 154 | if (!backgroundExecutor.awaitTermination(5, TimeUnit.SECONDS)) { |
| 155 | log.error("Timeout during executor shutdown"); |
| 156 | } |
| 157 | } catch (InterruptedException e) { |
| 158 | log.error("Error during executor shutdown", e); |
| 159 | } |
| 160 | |
| 161 | intents.clear(); |
| 162 | |
| 163 | log.info("Stopped"); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public long getIntentCount() { |
| 168 | return intents.size(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public Iterable<Intent> getIntents() { |
| 173 | // TODO don't actually need to copy intents, they are immutable |
| 174 | return ImmutableList.copyOf(intents.values()); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public Intent getIntent(IntentId intentId) { |
| 179 | return intents.get(intentId); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public IntentState getIntentState(IntentId intentId) { |
| 184 | Timestamped<IntentState> state = intentStates.get(intentId); |
| 185 | if (state != null) { |
| 186 | return state.value(); |
| 187 | } |
| 188 | return null; |
| 189 | } |
| 190 | |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 191 | private IntentEvent setStateInternal(IntentId intentId, IntentState newState, Timestamp timestamp) { |
| 192 | switch (newState) { |
| 193 | case WITHDRAW_REQ: |
| 194 | withdrawRequestedIntents.add(intentId); |
| 195 | break; |
| 196 | case INSTALL_REQ: |
| 197 | case COMPILING: |
| 198 | case INSTALLING: |
| 199 | case INSTALLED: |
| 200 | case RECOMPILING: |
| 201 | case WITHDRAWING: |
| 202 | case WITHDRAWN: |
| 203 | case FAILED: |
| 204 | synchronized (intentStates) { |
| 205 | Timestamped<IntentState> existing = intentStates.get(intentId); |
| 206 | if (existing == null || !existing.isNewer(timestamp)) { |
| 207 | intentStates.put(intentId, new Timestamped<>(newState, timestamp)); |
| 208 | } |
| 209 | } |
| 210 | break; |
| 211 | default: |
| 212 | log.warn("Unknown intent state {}", newState); |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | try { |
| 217 | // TODO make sure it's OK if the intent is null |
| 218 | return IntentEvent.getEvent(newState, intents.get(intentId)); |
| 219 | } catch (IllegalArgumentException e) { |
| 220 | // Transient states can't be used for events, so don't send one |
| 221 | return null; |
| 222 | } |
| 223 | } |
| 224 | |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 225 | private void setInstallableIntentsInternal(IntentId intentId, |
| 226 | List<Intent> installableIntents, |
| 227 | Timestamp timestamp) { |
| 228 | synchronized (installables) { |
| 229 | Timestamped<List<Intent>> existing = installables.get(intentId); |
| 230 | if (existing == null || !existing.isNewer(timestamp)) { |
| 231 | installables.put(intentId, |
| 232 | new Timestamped<>(installableIntents, timestamp)); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public List<Intent> getInstallableIntents(IntentId intentId) { |
| 239 | Timestamped<List<Intent>> tInstallables = installables.get(intentId); |
| 240 | if (tInstallables != null) { |
| 241 | return tInstallables.value(); |
| 242 | } |
| 243 | return null; |
| 244 | } |
| 245 | |
| 246 | @Override |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 247 | public List<BatchWrite.Operation> batchWrite(BatchWrite batch) { |
| 248 | |
| 249 | List<IntentEvent> events = Lists.newArrayList(); |
| 250 | List<BatchWrite.Operation> failed = new ArrayList<>(); |
| 251 | |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 252 | Timestamp timestamp = null; |
| 253 | |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 254 | for (BatchWrite.Operation op : batch.operations()) { |
| 255 | switch (op.type()) { |
| 256 | case CREATE_INTENT: |
| 257 | checkArgument(op.args().size() == 1, |
| 258 | "CREATE_INTENT takes 1 argument. %s", op); |
| 259 | Intent intent = op.arg(0); |
| 260 | |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 261 | timestamp = intentClockService.getTimestamp(intent.id()); |
| 262 | if (createIntentInternal(intent)) { |
| 263 | events.add(setStateInternal(intent.id(), INSTALL_REQ, timestamp)); |
| 264 | notifyPeers(new InternalIntentEvent(intent.id(), intent, |
| 265 | INSTALL_REQ, timestamp)); |
| 266 | } |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 267 | |
| 268 | break; |
| 269 | case REMOVE_INTENT: |
| 270 | checkArgument(op.args().size() == 1, |
| 271 | "REMOVE_INTENT takes 1 argument. %s", op); |
| 272 | IntentId intentId = (IntentId) op.arg(0); |
| 273 | // TODO implement |
| 274 | |
| 275 | break; |
| 276 | case SET_STATE: |
| 277 | checkArgument(op.args().size() == 2, |
| 278 | "SET_STATE takes 2 arguments. %s", op); |
| 279 | intent = op.arg(0); |
| 280 | IntentState newState = op.arg(1); |
| 281 | |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 282 | timestamp = intentClockService.getTimestamp(intent.id()); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 283 | IntentEvent externalEvent = setStateInternal(intent.id(), newState, timestamp); |
| 284 | events.add(externalEvent); |
| 285 | notifyPeers(new InternalIntentEvent(intent.id(), null, newState, timestamp)); |
| 286 | |
| 287 | break; |
| 288 | case SET_INSTALLABLE: |
| 289 | checkArgument(op.args().size() == 2, |
| 290 | "SET_INSTALLABLE takes 2 arguments. %s", op); |
| 291 | intentId = op.arg(0); |
| 292 | List<Intent> installableIntents = op.arg(1); |
| 293 | |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 294 | timestamp = intentClockService.getTimestamp(intentId); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 295 | setInstallableIntentsInternal( |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 296 | intentId, installableIntents, timestamp); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 297 | |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 298 | notifyPeers(new InternalSetInstallablesEvent(intentId, installableIntents, timestamp)); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 299 | |
| 300 | break; |
| 301 | case REMOVE_INSTALLED: |
| 302 | checkArgument(op.args().size() == 1, |
| 303 | "REMOVE_INSTALLED takes 1 argument. %s", op); |
| 304 | intentId = op.arg(0); |
| 305 | // TODO implement |
| 306 | break; |
| 307 | default: |
| 308 | log.warn("Unknown Operation encountered: {}", op); |
| 309 | failed.add(op); |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | notifyDelegate(events); |
| 315 | return failed; |
| 316 | } |
| 317 | |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 318 | private boolean createIntentInternal(Intent intent) { |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 319 | Intent oldValue = intents.putIfAbsent(intent.id(), intent); |
| 320 | if (oldValue == null) { |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 321 | return true; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | log.warn("Intent ID {} already in store, throwing new update away", |
| 325 | intent.id()); |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 326 | return false; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | private void notifyPeers(InternalIntentEvent event) { |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame^] | 330 | broadcastMessage(INTENT_UPDATED_MSG, event); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | private void notifyPeers(InternalSetInstallablesEvent event) { |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame^] | 334 | broadcastMessage(INTENT_SET_INSTALLABLES_MSG, event); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame^] | 337 | private void broadcastMessage(MessageSubject subject, Object event) { |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 338 | ClusterMessage message = new ClusterMessage( |
| 339 | clusterService.getLocalNode().id(), |
| 340 | subject, |
| 341 | SERIALIZER.encode(event)); |
| 342 | clusterCommunicator.broadcast(message); |
| 343 | } |
| 344 | |
| 345 | private void unicastMessage(NodeId peer, |
| 346 | MessageSubject subject, |
| 347 | Object event) throws IOException { |
| 348 | ClusterMessage message = new ClusterMessage( |
| 349 | clusterService.getLocalNode().id(), |
| 350 | subject, |
| 351 | SERIALIZER.encode(event)); |
| 352 | clusterCommunicator.unicast(message, peer); |
| 353 | } |
| 354 | |
| 355 | private void notifyDelegateIfNotNull(IntentEvent event) { |
| 356 | if (event != null) { |
| 357 | notifyDelegate(event); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | private final class InternalIntentCreateOrUpdateEventListener |
| 362 | implements ClusterMessageHandler { |
| 363 | @Override |
| 364 | public void handle(ClusterMessage message) { |
| 365 | |
| 366 | log.debug("Received intent update event from peer: {}", message.sender()); |
| 367 | InternalIntentEvent event = SERIALIZER.decode(message.payload()); |
| 368 | |
| 369 | IntentId intentId = event.intentId(); |
| 370 | Intent intent = event.intent(); |
| 371 | IntentState state = event.state(); |
| 372 | Timestamp timestamp = event.timestamp(); |
| 373 | |
| 374 | executor.submit(() -> { |
| 375 | try { |
| 376 | switch (state) { |
| 377 | case INSTALL_REQ: |
Jonathan Hart | 229794b | 2015-01-23 16:15:07 -0800 | [diff] [blame] | 378 | createIntentInternal(intent); |
| 379 | // Fallthrough to setStateInternal for INSTALL_REQ |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 380 | default: |
| 381 | notifyDelegateIfNotNull(setStateInternal(intentId, state, timestamp)); |
| 382 | break; |
| 383 | } |
| 384 | } catch (Exception e) { |
| 385 | log.warn("Exception thrown handling intent create or update", e); |
| 386 | } |
| 387 | }); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | private final class InternalIntentSetInstallablesListener |
| 392 | implements ClusterMessageHandler { |
| 393 | @Override |
| 394 | public void handle(ClusterMessage message) { |
| 395 | log.debug("Received intent set installables event from peer: {}", message.sender()); |
| 396 | InternalSetInstallablesEvent event = SERIALIZER.decode(message.payload()); |
| 397 | |
| 398 | IntentId intentId = event.intentId(); |
| 399 | List<Intent> installables = event.installables(); |
| 400 | Timestamp timestamp = event.timestamp(); |
| 401 | |
| 402 | executor.submit(() -> { |
| 403 | try { |
| 404 | setInstallableIntentsInternal(intentId, installables, timestamp); |
| 405 | } catch (Exception e) { |
| 406 | log.warn("Exception thrown handling intent set installables", e); |
| 407 | } |
| 408 | }); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | private final class InternalIntentAntiEntropyAdvertisementListener |
| 413 | implements ClusterMessageHandler { |
| 414 | |
| 415 | @Override |
| 416 | public void handle(ClusterMessage message) { |
| 417 | log.trace("Received intent Anti-Entropy advertisement from peer: {}", message.sender()); |
| 418 | // TODO implement |
| 419 | //IntentAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload()); |
| 420 | backgroundExecutor.submit(() -> { |
| 421 | try { |
| 422 | log.debug("something"); |
| 423 | //handleAntiEntropyAdvertisement(advertisement); |
| 424 | } catch (Exception e) { |
| 425 | log.warn("Exception thrown handling intent advertisements", e); |
| 426 | } |
| 427 | }); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |