Merge "Verify BatchedModifications messages sent vs received"
[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.DeleteData;
19 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
20 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
22 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
23 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
24 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
25 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
26 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
27 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
28 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
29 import org.opendaylight.controller.cluster.datastore.modification.Modification;
30 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
31 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36
37 /**
38  * @author: syedbahm
39  * Date: 8/6/14
40  */
41 public class ShardWriteTransaction extends ShardTransaction {
42
43     private final MutableCompositeModification compositeModification = new MutableCompositeModification();
44     private int totalBatchedModificationsReceived;
45     private Exception lastBatchedModificationsException;
46     private final DOMStoreWriteTransaction transaction;
47
48     public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor,
49             SchemaContext schemaContext, ShardStats shardStats, String transactionID,
50             short clientTxVersion) {
51         super(shardActor, schemaContext, shardStats, transactionID, clientTxVersion);
52         this.transaction = transaction;
53     }
54
55     @Override
56     protected DOMStoreTransaction getDOMStoreTransaction() {
57         return transaction;
58     }
59
60     @Override
61     public void handleReceive(Object message) throws Exception {
62
63         if (message instanceof BatchedModifications) {
64             batchedModifications((BatchedModifications)message);
65         } else if (message instanceof ReadyTransaction) {
66             readyTransaction(transaction, !SERIALIZED_REPLY);
67         } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
68             readyTransaction(transaction, SERIALIZED_REPLY);
69         } else if(WriteData.isSerializedType(message)) {
70             writeData(transaction, WriteData.fromSerializable(message), SERIALIZED_REPLY);
71
72         } else if(MergeData.isSerializedType(message)) {
73             mergeData(transaction, MergeData.fromSerializable(message), SERIALIZED_REPLY);
74
75         } else if(DeleteData.isSerializedType(message)) {
76             deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
77
78         } else if (message instanceof GetCompositedModification) {
79             // This is here for testing only
80             getSender().tell(new GetCompositeModificationReply(compositeModification), getSelf());
81         } else {
82             super.handleReceive(message);
83         }
84     }
85
86     private void batchedModifications(BatchedModifications batched) {
87         try {
88             for(Modification modification: batched.getModifications()) {
89                 compositeModification.addModification(modification);
90                 modification.apply(transaction);
91             }
92
93             totalBatchedModificationsReceived++;
94             if(batched.isReady()) {
95                 if(lastBatchedModificationsException != null) {
96                     throw lastBatchedModificationsException;
97                 }
98
99                 if(totalBatchedModificationsReceived != batched.getTotalMessagesSent()) {
100                     throw new IllegalStateException(String.format(
101                             "The total number of batched messages received %d does not match the number sent %d",
102                             totalBatchedModificationsReceived, batched.getTotalMessagesSent()));
103                 }
104
105                 readyTransaction(transaction, false);
106             } else {
107                 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
108             }
109         } catch (Exception e) {
110             lastBatchedModificationsException = e;
111             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
112
113             if(batched.isReady()) {
114                 getSelf().tell(PoisonPill.getInstance(), getSelf());
115             }
116         }
117     }
118
119     private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
120             boolean returnSerialized) {
121         LOG.debug("writeData at path : {}", message.getPath());
122
123         compositeModification.addModification(
124                 new WriteModification(message.getPath(), message.getData()));
125         try {
126             transaction.write(message.getPath(), message.getData());
127             WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
128             getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
129                 writeDataReply, getSelf());
130         }catch(Exception e){
131             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
132         }
133     }
134
135     private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
136             boolean returnSerialized) {
137         LOG.debug("mergeData at path : {}", message.getPath());
138
139         compositeModification.addModification(
140                 new MergeModification(message.getPath(), message.getData()));
141
142         try {
143             transaction.merge(message.getPath(), message.getData());
144             MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
145             getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
146                 mergeDataReply, getSelf());
147         }catch(Exception e){
148             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
149         }
150     }
151
152     private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
153             boolean returnSerialized) {
154         LOG.debug("deleteData at path : {}", message.getPath());
155
156         compositeModification.addModification(new DeleteModification(message.getPath()));
157         try {
158             transaction.delete(message.getPath());
159             DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
160             getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
161                 deleteDataReply, getSelf());
162         }catch(Exception e){
163             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
164         }
165     }
166
167     private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
168         String transactionID = getTransactionID();
169
170         LOG.debug("readyTransaction : {}", transactionID);
171
172         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
173
174         getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
175                 cohort, compositeModification, returnSerialized), getContext());
176
177         // The shard will handle the commit from here so we're no longer needed - self-destruct.
178         getSelf().tell(PoisonPill.getInstance(), getSelf());
179     }
180
181     // These classes are in here for test purposes only
182
183     static class GetCompositedModification {
184     }
185
186     static class GetCompositeModificationReply {
187         private final CompositeModification modification;
188
189
190         GetCompositeModificationReply(CompositeModification modification) {
191             this.modification = modification;
192         }
193
194         public CompositeModification getModification() {
195             return modification;
196         }
197     }
198 }