IMDS: trim down commit overhead
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardWriteTransaction.java
1 /*
2  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *  Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore;
12
13 import akka.actor.ActorRef;
14 import akka.actor.PoisonPill;
15 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
16 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
17 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
18 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
19 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
20 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
21 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
23 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
24 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
26 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
27 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
28 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
29 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
30 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
31 import org.opendaylight.controller.cluster.datastore.modification.Modification;
32 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
33 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
35
36 /**
37  * @author: syedbahm
38  * Date: 8/6/14
39  */
40 public class ShardWriteTransaction extends ShardTransaction {
41
42     private final MutableCompositeModification compositeModification = new MutableCompositeModification();
43     private int totalBatchedModificationsReceived;
44     private Exception lastBatchedModificationsException;
45     private final ReadWriteShardDataTreeTransaction transaction;
46
47     public ShardWriteTransaction(ReadWriteShardDataTreeTransaction transaction, ActorRef shardActor,
48             ShardStats shardStats, String transactionID, short clientTxVersion) {
49         super(shardActor, shardStats, transactionID, clientTxVersion);
50         this.transaction = transaction;
51     }
52
53     @Override
54     protected ReadWriteShardDataTreeTransaction getDOMStoreTransaction() {
55         return transaction;
56     }
57
58     @Override
59     public void handleReceive(Object message) throws Exception {
60
61         if (message instanceof BatchedModifications) {
62             batchedModifications((BatchedModifications)message);
63         } else if (message instanceof ReadyTransaction) {
64             readyTransaction(!SERIALIZED_REPLY, false);
65         } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
66             readyTransaction(SERIALIZED_REPLY, false);
67         } else if(WriteData.isSerializedType(message)) {
68             writeData(WriteData.fromSerializable(message), SERIALIZED_REPLY);
69
70         } else if(MergeData.isSerializedType(message)) {
71             mergeData(MergeData.fromSerializable(message), SERIALIZED_REPLY);
72
73         } else if(DeleteData.isSerializedType(message)) {
74             deleteData(DeleteData.fromSerializable(message), SERIALIZED_REPLY);
75
76         } else if (message instanceof GetCompositedModification) {
77             // This is here for testing only
78             getSender().tell(new GetCompositeModificationReply(compositeModification), getSelf());
79         } else {
80             super.handleReceive(message);
81         }
82     }
83
84     private void batchedModifications(BatchedModifications batched) {
85         if (checkClosed()) {
86             if (batched.isReady()) {
87                 getSelf().tell(PoisonPill.getInstance(), getSelf());
88             }
89             return;
90         }
91
92         try {
93             for(Modification modification: batched.getModifications()) {
94                 compositeModification.addModification(modification);
95                 modification.apply(transaction.getSnapshot());
96             }
97
98             totalBatchedModificationsReceived++;
99             if(batched.isReady()) {
100                 if(lastBatchedModificationsException != null) {
101                     throw lastBatchedModificationsException;
102                 }
103
104                 if(totalBatchedModificationsReceived != batched.getTotalMessagesSent()) {
105                     throw new IllegalStateException(String.format(
106                             "The total number of batched messages received %d does not match the number sent %d",
107                             totalBatchedModificationsReceived, batched.getTotalMessagesSent()));
108                 }
109
110                 readyTransaction(false, batched.isDoCommitOnReady());
111             } else {
112                 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
113             }
114         } catch (Exception e) {
115             lastBatchedModificationsException = e;
116             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
117
118             if(batched.isReady()) {
119                 getSelf().tell(PoisonPill.getInstance(), getSelf());
120             }
121         }
122     }
123
124     protected final void dataExists(DataExists message, final boolean returnSerialized) {
125         super.dataExists(transaction, message, returnSerialized);
126     }
127
128     protected final void readData(ReadData message, final boolean returnSerialized) {
129         super.readData(transaction, message, returnSerialized);
130     }
131
132     private boolean checkClosed() {
133         if (transaction.isClosed()) {
134             getSender().tell(new akka.actor.Status.Failure(new IllegalStateException("Transaction is closed, no modifications allowed")), getSelf());
135             return true;
136         } else {
137             return false;
138         }
139     }
140
141     private void writeData(WriteData message, boolean returnSerialized) {
142         LOG.debug("writeData at path : {}", message.getPath());
143         if (checkClosed()) {
144             return;
145         }
146
147         compositeModification.addModification(
148                 new WriteModification(message.getPath(), message.getData()));
149         try {
150             transaction.getSnapshot().write(message.getPath(), message.getData());
151             WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
152             getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
153                 writeDataReply, getSelf());
154         }catch(Exception e){
155             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
156         }
157     }
158
159     private void mergeData(MergeData message, boolean returnSerialized) {
160         LOG.debug("mergeData at path : {}", message.getPath());
161         if (checkClosed()) {
162             return;
163         }
164
165         compositeModification.addModification(
166                 new MergeModification(message.getPath(), message.getData()));
167
168         try {
169             transaction.getSnapshot().merge(message.getPath(), message.getData());
170             MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
171             getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
172                 mergeDataReply, getSelf());
173         }catch(Exception e){
174             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
175         }
176     }
177
178     private void deleteData(DeleteData message, boolean returnSerialized) {
179         LOG.debug("deleteData at path : {}", message.getPath());
180         if (checkClosed()) {
181             return;
182         }
183
184         compositeModification.addModification(new DeleteModification(message.getPath()));
185         try {
186             transaction.getSnapshot().delete(message.getPath());
187             DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
188             getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
189                 deleteDataReply, getSelf());
190         } catch(Exception e) {
191             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
192         }
193     }
194
195     private void readyTransaction(boolean returnSerialized, boolean doImmediateCommit) {
196         String transactionID = getTransactionID();
197
198         LOG.debug("readyTransaction : {}", transactionID);
199
200         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
201
202         getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
203                 cohort, compositeModification, returnSerialized, doImmediateCommit), getContext());
204
205         // The shard will handle the commit from here so we're no longer needed - self-destruct.
206         getSelf().tell(PoisonPill.getInstance(), getSelf());
207     }
208
209     // These classes are in here for test purposes only
210
211     static class GetCompositedModification {
212     }
213
214     static class GetCompositeModificationReply {
215         private final CompositeModification modification;
216
217
218         GetCompositeModificationReply(CompositeModification modification) {
219             this.modification = modification;
220         }
221
222         public CompositeModification getModification() {
223             return modification;
224         }
225     }
226 }