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