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.cluster.datastore.modification.MutableCompositeModification;
34 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
35 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
36 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
37 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
38 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
39 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import java.util.HashMap;
45 import java.util.concurrent.ExecutionException;
46 import java.util.concurrent.Executors;
49 * A Shard represents a portion of the logical data tree <br/>
51 * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
54 public class Shard extends UntypedProcessor {
56 public static final String DEFAULT_NAME = "default";
58 private final ListeningExecutorService storeExecutor =
59 MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
61 private final InMemoryDOMDataStore store;
63 private final Map<Object, DOMStoreThreePhaseCommitCohort>
64 modificationToCohort = new HashMap<>();
66 private final LoggingAdapter log =
67 Logging.getLogger(getContext().system(), this);
69 // By default persistent will be true and can be turned off using the system
70 // property persistent
71 private final boolean persistent;
73 private SchemaContext schemaContext;
75 private Shard(String name) {
77 String setting = System.getProperty("shard.persistent");
78 this.persistent = !"false".equals(setting);
80 log.info("Creating shard : {} persistent : {}", name , persistent);
82 store = new InMemoryDOMDataStore(name, storeExecutor);
85 public static Props props(final String name) {
86 return Props.create(new Creator<Shard>() {
89 public Shard create() throws Exception {
90 return new Shard(name);
98 public void onReceive(Object message) throws Exception {
99 log.debug("Received message {}", message);
101 if(!recoveryFinished()){
102 // FIXME : Properly handle recovery
106 if (message instanceof CreateTransactionChain) {
107 createTransactionChain();
108 } else if (message instanceof RegisterChangeListener) {
109 registerChangeListener((RegisterChangeListener) message);
110 } else if (message instanceof UpdateSchemaContext) {
111 updateSchemaContext((UpdateSchemaContext) message);
112 } else if (message instanceof ForwardedCommitTransaction) {
113 handleForwardedCommit((ForwardedCommitTransaction) message);
114 } else if (message instanceof Persistent) {
115 commit(((Persistent)message).payload());
116 } else if (message instanceof CreateTransaction) {
117 createTransaction((CreateTransaction) message);
118 } else if(message instanceof NonPersistent){
119 commit(((NonPersistent)message).payload());
123 private void createTransaction(CreateTransaction createTransaction) {
124 DOMStoreReadWriteTransaction transaction =
125 store.newReadWriteTransaction();
126 ActorRef transactionActor = getContext().actorOf(
127 ShardTransaction.props(transaction, getSelf(), schemaContext), "shard-" + createTransaction.getTransactionId());
129 .tell(new CreateTransactionReply(transactionActor.path().toString(), createTransaction.getTransactionId()).toSerializable(),
133 private void commit(Object serialized) {
134 Modification modification = MutableCompositeModification.fromSerializable(serialized, schemaContext);
135 DOMStoreThreePhaseCommitCohort cohort =
136 modificationToCohort.remove(serialized);
137 if (cohort == null) {
139 "Could not find cohort for modification : " + modification);
142 final ListenableFuture<Void> future = cohort.commit();
143 final ActorRef sender = getSender();
144 final ActorRef self = getSelf();
145 future.addListener(new Runnable() {
150 sender.tell(new CommitTransactionReply(), self);
151 } catch (InterruptedException | ExecutionException e) {
152 // FIXME : Handle this properly
153 log.error(e, "An exception happened when committing");
156 }, getContext().dispatcher());
159 private void handleForwardedCommit(ForwardedCommitTransaction message) {
160 Object serializedModification = message.getModification().toSerializable();
163 .put(serializedModification , message.getCohort());
165 getSelf().forward(Persistent.create(serializedModification),
168 getSelf().forward(NonPersistent.create(serializedModification),
173 private void updateSchemaContext(UpdateSchemaContext message) {
174 this.schemaContext = message.getSchemaContext();
175 store.onGlobalContextUpdated(message.getSchemaContext());
178 private void registerChangeListener(
179 RegisterChangeListener registerChangeListener) {
181 ActorSelection dataChangeListenerPath = getContext()
182 .system().actorSelection(registerChangeListener.getDataChangeListenerPath());
184 AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>
185 listener = new DataChangeListenerProxy(dataChangeListenerPath);
187 org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>>
189 store.registerChangeListener(registerChangeListener.getPath(),
190 listener, registerChangeListener.getScope());
191 ActorRef listenerRegistration =
192 getContext().actorOf(
193 DataChangeListenerRegistration.props(registration));
195 .tell(new RegisterChangeListenerReply(listenerRegistration.path()),
199 private void createTransactionChain() {
200 DOMStoreTransactionChain chain = store.createTransactionChain();
201 ActorRef transactionChain =
202 getContext().actorOf(ShardTransactionChain.props(chain, schemaContext));
204 .tell(new CreateTransactionChainReply(transactionChain.path()),