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.ActorSelection;
13 import akka.actor.Props;
14 import akka.event.Logging;
15 import akka.event.LoggingAdapter;
16 import akka.japi.Creator;
17 import akka.persistence.Persistent;
18 import akka.persistence.UntypedProcessor;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.ListeningExecutorService;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
23 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
24 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
25 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
26 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
27 import org.opendaylight.controller.cluster.datastore.messages.ForwardedCommitTransaction;
28 import org.opendaylight.controller.cluster.datastore.messages.NonPersistent;
29 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
30 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
31 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
32 import org.opendaylight.controller.cluster.datastore.modification.Modification;
33 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
34 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
35 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
36 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
37 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
38 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import java.util.HashMap;
44 import java.util.concurrent.ExecutionException;
45 import java.util.concurrent.Executors;
48 * A Shard represents a portion of the logical data tree <br/>
50 * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
53 public class Shard extends UntypedProcessor {
55 public static final String DEFAULT_NAME = "default";
57 private final ListeningExecutorService storeExecutor =
58 MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
60 private final InMemoryDOMDataStore store;
62 private final Map<Modification, DOMStoreThreePhaseCommitCohort>
63 modificationToCohort = new HashMap<>();
65 private final LoggingAdapter log =
66 Logging.getLogger(getContext().system(), this);
68 // By default persistent will be true and can be turned off using the system
69 // property persistent
70 private final boolean persistent;
72 private SchemaContext schemaContext;
74 private Shard(String name) {
76 String setting = System.getProperty("shard.persistent");
77 this.persistent = !"false".equals(setting);
79 log.info("Creating shard : {} persistent : {}", name , persistent);
81 store = new InMemoryDOMDataStore(name, storeExecutor);
84 public static Props props(final String name) {
85 return Props.create(new Creator<Shard>() {
88 public Shard create() throws Exception {
89 return new Shard(name);
97 public void onReceive(Object message) throws Exception {
98 log.debug("Received message {}", message);
100 if (message instanceof CreateTransactionChain) {
101 createTransactionChain();
102 } else if (message instanceof RegisterChangeListener) {
103 registerChangeListener((RegisterChangeListener) message);
104 } else if (message instanceof UpdateSchemaContext) {
105 updateSchemaContext((UpdateSchemaContext) message);
106 } else if (message instanceof ForwardedCommitTransaction) {
107 handleForwardedCommit((ForwardedCommitTransaction) message);
108 } else if (message instanceof Persistent) {
109 commit((Modification) ((Persistent) message).payload());
110 } else if (message instanceof CreateTransaction) {
111 createTransaction((CreateTransaction) message);
112 } else if(message instanceof NonPersistent){
113 commit((Modification) ((NonPersistent) message).payload());
117 private void createTransaction(CreateTransaction createTransaction) {
118 DOMStoreReadWriteTransaction transaction =
119 store.newReadWriteTransaction();
120 ActorRef transactionActor = getContext().actorOf(
121 ShardTransaction.props(transaction, getSelf(), schemaContext), "shard-" + createTransaction.getTransactionId());
123 .tell(new CreateTransactionReply(transactionActor.path().toString(), createTransaction.getTransactionId()).toSerializable(),
127 private void commit(Modification modification) {
128 DOMStoreThreePhaseCommitCohort cohort =
129 modificationToCohort.remove(modification);
130 if (cohort == null) {
132 "Could not find cohort for modification : " + modification);
135 final ListenableFuture<Void> future = cohort.commit();
136 final ActorRef sender = getSender();
137 final ActorRef self = getSelf();
138 future.addListener(new Runnable() {
143 sender.tell(new CommitTransactionReply(), self);
144 } catch (InterruptedException | ExecutionException e) {
145 // FIXME : Handle this properly
146 log.error(e, "An exception happened when committing");
149 }, getContext().dispatcher());
152 private void handleForwardedCommit(ForwardedCommitTransaction message) {
154 .put(message.getModification(), message.getCohort());
156 getSelf().forward(Persistent.create(message.getModification()),
159 getSelf().forward(NonPersistent.create(message.getModification()),
164 private void updateSchemaContext(UpdateSchemaContext message) {
165 this.schemaContext = message.getSchemaContext();
166 store.onGlobalContextUpdated(message.getSchemaContext());
169 private void registerChangeListener(
170 RegisterChangeListener registerChangeListener) {
172 ActorSelection dataChangeListenerPath = getContext()
173 .system().actorSelection(registerChangeListener.getDataChangeListenerPath());
175 AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>
176 listener = new DataChangeListenerProxy(dataChangeListenerPath);
178 org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>>
180 store.registerChangeListener(registerChangeListener.getPath(),
181 listener, registerChangeListener.getScope());
182 ActorRef listenerRegistration =
183 getContext().actorOf(
184 DataChangeListenerRegistration.props(registration));
186 .tell(new RegisterChangeListenerReply(listenerRegistration.path()),
190 private void createTransactionChain() {
191 DOMStoreTransactionChain chain = store.createTransactionChain();
192 ActorRef transactionChain =
193 getContext().actorOf(ShardTransactionChain.props(chain, schemaContext));
195 .tell(new CreateTransactionChainReply(transactionChain.path()),