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