2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import akka.event.Logging;
14 import akka.event.LoggingAdapter;
15 import akka.japi.Creator;
16 import akka.persistence.Persistent;
17 import akka.persistence.UntypedProcessor;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.ListeningExecutorService;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
23 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
24 import org.opendaylight.controller.cluster.datastore.messages.ForwardedCommitTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
26 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
27 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
28 import org.opendaylight.controller.cluster.datastore.modification.Modification;
29 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
30 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
33 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import java.util.HashMap;
38 import java.util.concurrent.ExecutionException;
39 import java.util.concurrent.Executors;
42 * A Shard represents a portion of the logical data tree <br/>
44 * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
47 public class Shard extends UntypedProcessor {
49 public static final String DEFAULT_NAME = "default";
51 private final ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
53 private final InMemoryDOMDataStore store;
55 private final Map<Modification, DOMStoreThreePhaseCommitCohort> modificationToCohort = new HashMap<>();
57 private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
59 private Shard(String name){
60 store = new InMemoryDOMDataStore(name, storeExecutor);
63 public static Props props(final String name) {
64 return Props.create(new Creator<Shard>() {
67 public Shard create() throws Exception {
68 return new Shard(name);
75 public void onReceive(Object message) throws Exception {
76 if (message instanceof CreateTransactionChain) {
77 createTransactionChain();
78 } else if (message instanceof RegisterChangeListener) {
79 registerChangeListener((RegisterChangeListener) message);
80 } else if (message instanceof UpdateSchemaContext) {
81 updateSchemaContext((UpdateSchemaContext) message);
82 } else if (message instanceof ForwardedCommitTransaction ) {
83 handleForwardedCommit((ForwardedCommitTransaction) message);
84 } else if (message instanceof Persistent){
85 commit((Persistent) message);
89 private void commit(Persistent message) {
90 Modification modification = (Modification) message.payload();
91 DOMStoreThreePhaseCommitCohort cohort = modificationToCohort.remove(modification);
93 log.error("Could not find cohort for modification : " + modification);
96 final ListenableFuture<Void> future = cohort.commit();
97 final ActorRef sender = getSender();
98 final ActorRef self = getSelf();
99 future.addListener(new Runnable() {
104 sender.tell(new CommitTransactionReply(), self);
105 } catch (InterruptedException | ExecutionException e) {
106 log.error(e, "An exception happened when committing");
109 }, getContext().dispatcher());
112 private void handleForwardedCommit(ForwardedCommitTransaction message) {
113 log.info("received forwarded transaction");
114 modificationToCohort.put(message.getModification(), message.getCohort());
115 getSelf().forward(Persistent.create(message.getModification()), getContext());
118 private void updateSchemaContext(UpdateSchemaContext message) {
119 store.onGlobalContextUpdated(message.getSchemaContext());
122 private void registerChangeListener(RegisterChangeListener registerChangeListener) {
123 org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> registration =
124 store.registerChangeListener(registerChangeListener.getPath(), registerChangeListener.getListener(), registerChangeListener.getScope());
125 ActorRef listenerRegistration = getContext().actorOf(ListenerRegistration.props(registration));
126 getSender().tell(new RegisterChangeListenerReply(listenerRegistration.path()), getSelf());
129 private void createTransactionChain() {
130 DOMStoreTransactionChain chain = store.createTransactionChain();
131 ActorRef transactionChain = getContext().actorOf(ShardTransactionChain.props(chain));
132 getSender().tell(new CreateTransactionChainReply(transactionChain.path()), getSelf());