Bug 2252: Terminate ShardWriteTransaction actor on ready
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardWriteTransaction.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco 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.DeleteData;
17 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
18 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
20 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
21 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
23 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
24 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
25 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
26 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
27 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
28 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
29 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34
35 /**
36  * @author: syedbahm
37  * Date: 8/6/14
38  */
39 public class ShardWriteTransaction extends ShardTransaction {
40
41     private final MutableCompositeModification modification = new MutableCompositeModification();
42     private final DOMStoreWriteTransaction transaction;
43
44     public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor,
45             SchemaContext schemaContext, ShardStats shardStats, String transactionID) {
46         super(shardActor, schemaContext, shardStats, transactionID);
47         this.transaction = transaction;
48     }
49
50     @Override
51     protected DOMStoreTransaction getDOMStoreTransaction() {
52         return transaction;
53     }
54
55     @Override
56     public void handleReceive(Object message) throws Exception {
57
58         if (message instanceof WriteData) {
59             writeData(transaction, (WriteData) message, !SERIALIZED_REPLY);
60
61         } else if (message instanceof MergeData) {
62             mergeData(transaction, (MergeData) message, !SERIALIZED_REPLY);
63
64         } else if (message instanceof DeleteData) {
65             deleteData(transaction, (DeleteData) message, !SERIALIZED_REPLY);
66
67         } else if (message instanceof ReadyTransaction) {
68             readyTransaction(transaction, new ReadyTransaction(), !SERIALIZED_REPLY);
69
70         } else if(WriteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
71             writeData(transaction, WriteData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
72
73         } else if(MergeData.SERIALIZABLE_CLASS.equals(message.getClass())) {
74             mergeData(transaction, MergeData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
75
76         } else if(DeleteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
77             deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
78
79         } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
80             readyTransaction(transaction, new ReadyTransaction(), SERIALIZED_REPLY);
81
82         } else if (message instanceof GetCompositedModification) {
83             // This is here for testing only
84             getSender().tell(new GetCompositeModificationReply(
85                     new ImmutableCompositeModification(modification)), getSelf());
86         } else {
87             super.handleReceive(message);
88         }
89     }
90
91     private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
92             boolean returnSerialized) {
93         LOG.debug("writeData at path : {}", message.getPath());
94
95         modification.addModification(
96                 new WriteModification(message.getPath(), message.getData(), getSchemaContext()));
97         try {
98             transaction.write(message.getPath(), message.getData());
99             WriteDataReply writeDataReply = new WriteDataReply();
100             getSender().tell(returnSerialized ? writeDataReply.toSerializable() : writeDataReply,
101                 getSelf());
102         }catch(Exception e){
103             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
104         }
105     }
106
107     private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
108             boolean returnSerialized) {
109         LOG.debug("mergeData at path : {}", message.getPath());
110
111         modification.addModification(
112                 new MergeModification(message.getPath(), message.getData(), getSchemaContext()));
113
114         try {
115             transaction.merge(message.getPath(), message.getData());
116             MergeDataReply mergeDataReply = new MergeDataReply();
117             getSender().tell(returnSerialized ? mergeDataReply.toSerializable() : mergeDataReply ,
118                 getSelf());
119         }catch(Exception e){
120             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
121         }
122     }
123
124     private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
125             boolean returnSerialized) {
126         LOG.debug("deleteData at path : {}", message.getPath());
127
128         modification.addModification(new DeleteModification(message.getPath()));
129         try {
130             transaction.delete(message.getPath());
131             DeleteDataReply deleteDataReply = new DeleteDataReply();
132             getSender().tell(returnSerialized ? deleteDataReply.toSerializable() : deleteDataReply,
133                 getSelf());
134         }catch(Exception e){
135             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
136         }
137     }
138
139     private void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message,
140             boolean returnSerialized) {
141         String transactionID = getTransactionID();
142
143         LOG.debug("readyTransaction : {}", transactionID);
144
145         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
146
147         getShardActor().forward(new ForwardedReadyTransaction(transactionID, cohort, modification,
148                 returnSerialized), getContext());
149
150         // The shard will handle the commit from here so we're no longer needed - self-destruct.
151         getSelf().tell(PoisonPill.getInstance(), getSelf());
152     }
153
154     // These classes are in here for test purposes only
155
156     static class GetCompositedModification {
157     }
158
159     static class GetCompositeModificationReply {
160         private final CompositeModification modification;
161
162
163         GetCompositeModificationReply(CompositeModification modification) {
164             this.modification = modification;
165         }
166
167         public CompositeModification getModification() {
168             return modification;
169         }
170     }
171 }