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.PoisonPill;
13 import akka.actor.Props;
14 import akka.actor.ReceiveTimeout;
15 import akka.japi.Creator;
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
20 import org.opendaylight.controller.cluster.datastore.exceptions.UnknownMessageException;
21 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
23 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
24 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
25 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
26 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
27 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
28 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
29 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
30 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
31 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
32 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
33 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
34 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
35 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
36 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
37 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
38 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
39 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
40 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
41 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
42 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
43 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
44 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
45 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
46 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
52 * The ShardTransaction Actor represents a remote transaction
54 * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
57 * Even though the DOMStore and the DOMStoreTransactionChain implement multiple types of transactions
58 * the ShardTransaction Actor only works with read-write transactions. This is just to keep the logic simple. At this
59 * time there are no known advantages for creating a read-only or write-only transaction which may change over time
60 * at which point we can optimize things in the distributed store as well.
63 * Handles Messages <br/>
64 * ---------------- <br/>
65 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadData}
66 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.WriteData}
67 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.MergeData}
68 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.DeleteData}
69 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction}
70 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
73 public abstract class ShardTransaction extends AbstractUntypedActor {
75 private final ActorRef shardActor;
76 protected final SchemaContext schemaContext;
78 private final MutableCompositeModification modification = new MutableCompositeModification();
80 protected ShardTransaction(ActorRef shardActor, SchemaContext schemaContext) {
81 this.shardActor = shardActor;
82 this.schemaContext = schemaContext;
85 public static Props props(DOMStoreTransaction transaction, ActorRef shardActor,
86 SchemaContext schemaContext, ShardContext shardContext) {
87 return Props.create(new ShardTransactionCreator(transaction, shardActor, schemaContext,
91 protected abstract DOMStoreTransaction getDOMStoreTransaction();
94 public void handleReceive(Object message) throws Exception {
95 if (message.getClass().equals(CloseTransaction.SERIALIZABLE_CLASS)) {
96 closeTransaction(true);
97 } else if (message instanceof GetCompositedModification) {
98 // This is here for testing only
99 getSender().tell(new GetCompositeModificationReply(
100 new ImmutableCompositeModification(modification)), getSelf());
101 } else if (message instanceof ReceiveTimeout) {
102 LOG.debug("Got ReceiveTimeout for inactivity - closing Tx");
103 closeTransaction(false);
105 throw new UnknownMessageException(message);
109 private void closeTransaction(boolean sendReply) {
110 getDOMStoreTransaction().close();
113 getSender().tell(new CloseTransactionReply().toSerializable(), getSelf());
116 getSelf().tell(PoisonPill.getInstance(), getSelf());
119 protected void readData(DOMStoreReadTransaction transaction,ReadData message) {
120 final ActorRef sender = getSender();
121 final ActorRef self = getSelf();
122 final YangInstanceIdentifier path = message.getPath();
123 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> future =
124 transaction.read(path);
126 future.addListener(new Runnable() {
130 Optional<NormalizedNode<?, ?>> optional = future.checkedGet();
131 if (optional.isPresent()) {
132 sender.tell(new ReadDataReply(schemaContext,optional.get()).toSerializable(), self);
134 sender.tell(new ReadDataReply(schemaContext,null).toSerializable(), self);
136 } catch (Exception e) {
137 sender.tell(new akka.actor.Status.Failure(e),self);
141 }, getContext().dispatcher());
144 protected void dataExists(DOMStoreReadTransaction transaction, DataExists message) {
145 final YangInstanceIdentifier path = message.getPath();
148 Boolean exists = transaction.exists(path).checkedGet();
149 getSender().tell(new DataExistsReply(exists).toSerializable(), getSelf());
150 } catch (ReadFailedException e) {
151 getSender().tell(new akka.actor.Status.Failure(e),getSelf());
156 protected void writeData(DOMStoreWriteTransaction transaction, WriteData message) {
157 modification.addModification(
158 new WriteModification(message.getPath(), message.getData(),schemaContext));
159 LOG.debug("writeData at path : " + message.getPath().toString());
162 transaction.write(message.getPath(), message.getData());
163 getSender().tell(new WriteDataReply().toSerializable(), getSelf());
165 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
169 protected void mergeData(DOMStoreWriteTransaction transaction, MergeData message) {
170 modification.addModification(
171 new MergeModification(message.getPath(), message.getData(), schemaContext));
172 LOG.debug("mergeData at path : " + message.getPath().toString());
174 transaction.merge(message.getPath(), message.getData());
175 getSender().tell(new MergeDataReply().toSerializable(), getSelf());
177 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
181 protected void deleteData(DOMStoreWriteTransaction transaction, DeleteData message) {
182 LOG.debug("deleteData at path : " + message.getPath().toString());
183 modification.addModification(new DeleteModification(message.getPath()));
185 transaction.delete(message.getPath());
186 getSender().tell(new DeleteDataReply().toSerializable(), getSelf());
188 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
192 protected void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message) {
193 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
194 ActorRef cohortActor = getContext().actorOf(
195 ThreePhaseCommitCohort.props(cohort, shardActor, modification), "cohort");
197 .tell(new ReadyTransactionReply(cohortActor.path()).toSerializable(), getSelf());
201 private static class ShardTransactionCreator implements Creator<ShardTransaction> {
203 private static final long serialVersionUID = 1L;
205 final DOMStoreTransaction transaction;
206 final ActorRef shardActor;
207 final SchemaContext schemaContext;
208 final ShardContext shardContext;
210 ShardTransactionCreator(DOMStoreTransaction transaction, ActorRef shardActor,
211 SchemaContext schemaContext, ShardContext actorContext) {
212 this.transaction = transaction;
213 this.shardActor = shardActor;
214 this.shardContext = actorContext;
215 this.schemaContext = schemaContext;
219 public ShardTransaction create() throws Exception {
221 if(transaction instanceof DOMStoreReadWriteTransaction) {
222 tx = new ShardReadWriteTransaction((DOMStoreReadWriteTransaction)transaction,
223 shardActor, schemaContext);
224 } else if(transaction instanceof DOMStoreReadTransaction) {
225 tx = new ShardReadTransaction((DOMStoreReadTransaction)transaction, shardActor,
228 tx = new ShardWriteTransaction((DOMStoreWriteTransaction)transaction,
229 shardActor, schemaContext);
232 tx.getContext().setReceiveTimeout(shardContext.getShardTransactionIdleTimeout());
237 // These classes are in here for test purposes only
239 static class GetCompositedModification {
243 static class GetCompositeModificationReply {
244 private final CompositeModification modification;
247 GetCompositeModificationReply(CompositeModification modification) {
248 this.modification = modification;
252 public CompositeModification getModification() {