Merge changes I114cbac1,I45c2e7cd
[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
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 DOMStoreWriteTransaction transaction;
46
47     public ShardWriteTransaction(DOMStoreWriteTransaction 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 DOMStoreTransaction 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(transaction, !SERIALIZED_REPLY);
65         } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
66             readyTransaction(transaction, SERIALIZED_REPLY);
67         } else if(WriteData.isSerializedType(message)) {
68             writeData(transaction, WriteData.fromSerializable(message), SERIALIZED_REPLY);
69
70         } else if(MergeData.isSerializedType(message)) {
71             mergeData(transaction, MergeData.fromSerializable(message), SERIALIZED_REPLY);
72
73         } else if(DeleteData.isSerializedType(message)) {
74             deleteData(transaction, 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         try {
86             for(Modification modification: batched.getModifications()) {
87                 compositeModification.addModification(modification);
88                 modification.apply(transaction);
89             }
90
91             totalBatchedModificationsReceived++;
92             if(batched.isReady()) {
93                 if(lastBatchedModificationsException != null) {
94                     throw lastBatchedModificationsException;
95                 }
96
97                 if(totalBatchedModificationsReceived != batched.getTotalMessagesSent()) {
98                     throw new IllegalStateException(String.format(
99                             "The total number of batched messages received %d does not match the number sent %d",
100                             totalBatchedModificationsReceived, batched.getTotalMessagesSent()));
101                 }
102
103                 readyTransaction(transaction, false);
104             } else {
105                 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
106             }
107         } catch (Exception e) {
108             lastBatchedModificationsException = e;
109             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
110
111             if(batched.isReady()) {
112                 getSelf().tell(PoisonPill.getInstance(), getSelf());
113             }
114         }
115     }
116
117     private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
118             boolean returnSerialized) {
119         LOG.debug("writeData at path : {}", message.getPath());
120
121         compositeModification.addModification(
122                 new WriteModification(message.getPath(), message.getData()));
123         try {
124             transaction.write(message.getPath(), message.getData());
125             WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
126             getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
127                 writeDataReply, getSelf());
128         }catch(Exception e){
129             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
130         }
131     }
132
133     private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
134             boolean returnSerialized) {
135         LOG.debug("mergeData at path : {}", message.getPath());
136
137         compositeModification.addModification(
138                 new MergeModification(message.getPath(), message.getData()));
139
140         try {
141             transaction.merge(message.getPath(), message.getData());
142             MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
143             getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
144                 mergeDataReply, getSelf());
145         }catch(Exception e){
146             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
147         }
148     }
149
150     private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
151             boolean returnSerialized) {
152         LOG.debug("deleteData at path : {}", message.getPath());
153
154         compositeModification.addModification(new DeleteModification(message.getPath()));
155         try {
156             transaction.delete(message.getPath());
157             DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
158             getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
159                 deleteDataReply, getSelf());
160         }catch(Exception e){
161             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
162         }
163     }
164
165     private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
166         String transactionID = getTransactionID();
167
168         LOG.debug("readyTransaction : {}", transactionID);
169
170         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
171
172         getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
173                 cohort, compositeModification, returnSerialized), getContext());
174
175         // The shard will handle the commit from here so we're no longer needed - self-destruct.
176         getSelf().tell(PoisonPill.getInstance(), getSelf());
177     }
178
179     // These classes are in here for test purposes only
180
181     static class GetCompositedModification {
182     }
183
184     static class GetCompositeModificationReply {
185         private final CompositeModification modification;
186
187
188         GetCompositeModificationReply(CompositeModification modification) {
189             this.modification = modification;
190         }
191
192         public CompositeModification getModification() {
193             return modification;
194         }
195     }
196 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.