Merge "Bug 1686 - Rework TopologyManager to use transaction chaining in order to...
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
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;
16
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
19
20 import org.opendaylight.controller.cluster.datastore.exceptions.UnknownMessageException;
21 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
22 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
23 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
25 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
26 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
27 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
28 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
29 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
30 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
31 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
32 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
33 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
34 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
35 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
36 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
37 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
38 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
39 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
40 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
41 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
42 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
43 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
44 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
45 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
46 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
47 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51
52 /**
53  * The ShardTransaction Actor represents a remote transaction
54  * <p>
55  * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
56  * </p>
57  * <p>
58  * Even though the DOMStore and the DOMStoreTransactionChain implement multiple types of transactions
59  * the ShardTransaction Actor only works with read-write transactions. This is just to keep the logic simple. At this
60  * time there are no known advantages for creating a read-only or write-only transaction which may change over time
61  * at which point we can optimize things in the distributed store as well.
62  * </p>
63  * <p>
64  * Handles Messages <br/>
65  * ---------------- <br/>
66  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadData}
67  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.WriteData}
68  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.MergeData}
69  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.DeleteData}
70  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction}
71  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
72  * </p>
73  */
74 public abstract class ShardTransaction extends AbstractUntypedActor {
75
76     private final ActorRef shardActor;
77     protected final SchemaContext schemaContext;
78     private final ShardStats shardStats;
79
80     private final MutableCompositeModification modification = new MutableCompositeModification();
81
82     protected ShardTransaction(ActorRef shardActor, SchemaContext schemaContext,
83             ShardStats shardStats) {
84         this.shardActor = shardActor;
85         this.schemaContext = schemaContext;
86         this.shardStats = shardStats;
87     }
88
89     public static Props props(DOMStoreTransaction transaction, ActorRef shardActor,
90             SchemaContext schemaContext,DatastoreContext datastoreContext, ShardStats shardStats) {
91         return Props.create(new ShardTransactionCreator(transaction, shardActor, schemaContext,
92            datastoreContext, shardStats));
93     }
94
95     protected abstract DOMStoreTransaction getDOMStoreTransaction();
96
97     @Override
98     public void handleReceive(Object message) throws Exception {
99         if (message.getClass().equals(CloseTransaction.SERIALIZABLE_CLASS)) {
100             closeTransaction(true);
101         } else if (message instanceof GetCompositedModification) {
102             // This is here for testing only
103             getSender().tell(new GetCompositeModificationReply(
104                     new ImmutableCompositeModification(modification)), getSelf());
105         } else if (message instanceof ReceiveTimeout) {
106             LOG.debug("Got ReceiveTimeout for inactivity - closing Tx");
107             closeTransaction(false);
108         } else {
109             throw new UnknownMessageException(message);
110         }
111     }
112
113     private void closeTransaction(boolean sendReply) {
114         getDOMStoreTransaction().close();
115
116         if(sendReply) {
117             getSender().tell(new CloseTransactionReply().toSerializable(), getSelf());
118         }
119
120         getSelf().tell(PoisonPill.getInstance(), getSelf());
121     }
122
123     protected void readData(DOMStoreReadTransaction transaction,ReadData message) {
124         final ActorRef sender = getSender();
125         final ActorRef self = getSelf();
126         final YangInstanceIdentifier path = message.getPath();
127         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> future =
128                 transaction.read(path);
129
130         future.addListener(new Runnable() {
131             @Override
132             public void run() {
133                 try {
134                     Optional<NormalizedNode<?, ?>> optional = future.checkedGet();
135                     if (optional.isPresent()) {
136                         sender.tell(new ReadDataReply(schemaContext,optional.get()).toSerializable(), self);
137                     } else {
138                         sender.tell(new ReadDataReply(schemaContext,null).toSerializable(), self);
139                     }
140                 } catch (Exception e) {
141                     shardStats.incrementFailedReadTransactionsCount();
142                     sender.tell(new akka.actor.Status.Failure(e), self);
143                 }
144
145             }
146         }, getContext().dispatcher());
147     }
148
149     protected void dataExists(DOMStoreReadTransaction transaction, DataExists message) {
150         final YangInstanceIdentifier path = message.getPath();
151
152         try {
153             Boolean exists = transaction.exists(path).checkedGet();
154             getSender().tell(new DataExistsReply(exists).toSerializable(), getSelf());
155         } catch (ReadFailedException e) {
156             getSender().tell(new akka.actor.Status.Failure(e),getSelf());
157         }
158
159     }
160
161     protected void writeData(DOMStoreWriteTransaction transaction, WriteData message) {
162         modification.addModification(
163                 new WriteModification(message.getPath(), message.getData(),schemaContext));
164         LOG.debug("writeData at path : " + message.getPath().toString());
165
166         try {
167             transaction.write(message.getPath(), message.getData());
168             getSender().tell(new WriteDataReply().toSerializable(), getSelf());
169         }catch(Exception e){
170             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
171         }
172     }
173
174     protected void mergeData(DOMStoreWriteTransaction transaction, MergeData message) {
175         modification.addModification(
176                 new MergeModification(message.getPath(), message.getData(), schemaContext));
177         LOG.debug("mergeData at path : " + message.getPath().toString());
178         try {
179             transaction.merge(message.getPath(), message.getData());
180             getSender().tell(new MergeDataReply().toSerializable(), getSelf());
181         }catch(Exception e){
182             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
183         }
184     }
185
186     protected void deleteData(DOMStoreWriteTransaction transaction, DeleteData message) {
187         LOG.debug("deleteData at path : " + message.getPath().toString());
188         modification.addModification(new DeleteModification(message.getPath()));
189         try {
190             transaction.delete(message.getPath());
191             getSender().tell(new DeleteDataReply().toSerializable(), getSelf());
192         }catch(Exception e){
193             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
194         }
195     }
196
197     protected void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message) {
198         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
199         ActorRef cohortActor = getContext().actorOf(
200             ThreePhaseCommitCohort.props(cohort, shardActor, modification, shardStats), "cohort");
201         getSender()
202         .tell(new ReadyTransactionReply(cohortActor.path()).toSerializable(), getSelf());
203
204     }
205
206     private static class ShardTransactionCreator implements Creator<ShardTransaction> {
207
208         private static final long serialVersionUID = 1L;
209
210         final DOMStoreTransaction transaction;
211         final ActorRef shardActor;
212         final SchemaContext schemaContext;
213         final DatastoreContext datastoreContext;
214         final ShardStats shardStats;
215
216         ShardTransactionCreator(DOMStoreTransaction transaction, ActorRef shardActor,
217                 SchemaContext schemaContext, DatastoreContext datastoreContext,
218                 ShardStats shardStats) {
219             this.transaction = transaction;
220             this.shardActor = shardActor;
221             this.shardStats = shardStats;
222             this.schemaContext = schemaContext;
223             this.datastoreContext = datastoreContext;
224         }
225
226         @Override
227         public ShardTransaction create() throws Exception {
228             ShardTransaction tx;
229             if(transaction instanceof DOMStoreReadWriteTransaction) {
230                 tx = new ShardReadWriteTransaction((DOMStoreReadWriteTransaction)transaction,
231                         shardActor, schemaContext, shardStats);
232             } else if(transaction instanceof DOMStoreReadTransaction) {
233                 tx = new ShardReadTransaction((DOMStoreReadTransaction)transaction, shardActor,
234                         schemaContext, shardStats);
235             } else {
236                 tx = new ShardWriteTransaction((DOMStoreWriteTransaction)transaction,
237                         shardActor, schemaContext, shardStats);
238             }
239
240             tx.getContext().setReceiveTimeout(datastoreContext.getShardTransactionIdleTimeout());
241             return tx;
242         }
243     }
244
245     // These classes are in here for test purposes only
246
247     static class GetCompositedModification {
248     }
249
250
251     static class GetCompositeModificationReply {
252         private final CompositeModification modification;
253
254
255         GetCompositeModificationReply(CompositeModification modification) {
256             this.modification = modification;
257         }
258
259
260         public CompositeModification getModification() {
261             return modification;
262         }
263     }
264 }