Merge "Introduce a benchmark for databroker"
[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             int txnClientVersion) {
47         super(shardActor, schemaContext, shardStats, transactionID, txnClientVersion);
48         this.transaction = transaction;
49     }
50
51     @Override
52     protected DOMStoreTransaction getDOMStoreTransaction() {
53         return transaction;
54     }
55
56     @Override
57     public void handleReceive(Object message) throws Exception {
58
59         if (message instanceof WriteData) {
60             writeData(transaction, (WriteData) message, !SERIALIZED_REPLY);
61
62         } else if (message instanceof MergeData) {
63             mergeData(transaction, (MergeData) message, !SERIALIZED_REPLY);
64
65         } else if (message instanceof DeleteData) {
66             deleteData(transaction, (DeleteData) message, !SERIALIZED_REPLY);
67
68         } else if (message instanceof ReadyTransaction) {
69             readyTransaction(transaction, new ReadyTransaction(), !SERIALIZED_REPLY);
70
71         } else if(WriteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
72             writeData(transaction, WriteData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
73
74         } else if(MergeData.SERIALIZABLE_CLASS.equals(message.getClass())) {
75             mergeData(transaction, MergeData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
76
77         } else if(DeleteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
78             deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
79
80         } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
81             readyTransaction(transaction, new ReadyTransaction(), SERIALIZED_REPLY);
82
83         } else if (message instanceof GetCompositedModification) {
84             // This is here for testing only
85             getSender().tell(new GetCompositeModificationReply(
86                     new ImmutableCompositeModification(modification)), getSelf());
87         } else {
88             super.handleReceive(message);
89         }
90     }
91
92     private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
93             boolean returnSerialized) {
94         LOG.debug("writeData at path : {}", message.getPath());
95
96         modification.addModification(
97                 new WriteModification(message.getPath(), message.getData(), getSchemaContext()));
98         try {
99             transaction.write(message.getPath(), message.getData());
100             WriteDataReply writeDataReply = new WriteDataReply();
101             getSender().tell(returnSerialized ? writeDataReply.toSerializable() : writeDataReply,
102                 getSelf());
103         }catch(Exception e){
104             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
105         }
106     }
107
108     private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
109             boolean returnSerialized) {
110         LOG.debug("mergeData at path : {}", message.getPath());
111
112         modification.addModification(
113                 new MergeModification(message.getPath(), message.getData(), getSchemaContext()));
114
115         try {
116             transaction.merge(message.getPath(), message.getData());
117             MergeDataReply mergeDataReply = new MergeDataReply();
118             getSender().tell(returnSerialized ? mergeDataReply.toSerializable() : mergeDataReply ,
119                 getSelf());
120         }catch(Exception e){
121             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
122         }
123     }
124
125     private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
126             boolean returnSerialized) {
127         LOG.debug("deleteData at path : {}", message.getPath());
128
129         modification.addModification(new DeleteModification(message.getPath()));
130         try {
131             transaction.delete(message.getPath());
132             DeleteDataReply deleteDataReply = new DeleteDataReply();
133             getSender().tell(returnSerialized ? deleteDataReply.toSerializable() : deleteDataReply,
134                 getSelf());
135         }catch(Exception e){
136             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
137         }
138     }
139
140     private void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message,
141             boolean returnSerialized) {
142         String transactionID = getTransactionID();
143
144         LOG.debug("readyTransaction : {}", transactionID);
145
146         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
147
148         getShardActor().forward(new ForwardedReadyTransaction(transactionID, getTxnClientVersion(),
149                 cohort, modification, returnSerialized), getContext());
150
151         // The shard will handle the commit from here so we're no longer needed - self-destruct.
152         getSelf().tell(PoisonPill.getInstance(), getSelf());
153     }
154
155     // These classes are in here for test purposes only
156
157     static class GetCompositedModification {
158     }
159
160     static class GetCompositeModificationReply {
161         private final CompositeModification modification;
162
163
164         GetCompositeModificationReply(CompositeModification modification) {
165             this.modification = modification;
166         }
167
168         public CompositeModification getModification() {
169             return modification;
170         }
171     }
172 }