Merge "Bug 2038: Ensure only one concurrent 3-phase commit in Shard"
[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 org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
15 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
16 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
17 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
19 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
20 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
22 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
23 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
24 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
25 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
26 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
27 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
28 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33
34 /**
35  * @author: syedbahm
36  * Date: 8/6/14
37  */
38 public class ShardWriteTransaction extends ShardTransaction {
39
40     private final MutableCompositeModification modification = new MutableCompositeModification();
41     private final DOMStoreWriteTransaction transaction;
42
43     public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor,
44             SchemaContext schemaContext, ShardStats shardStats, String transactionID) {
45         super(shardActor, schemaContext, shardStats, transactionID);
46         this.transaction = transaction;
47     }
48
49     @Override
50     protected DOMStoreTransaction getDOMStoreTransaction() {
51         return transaction;
52     }
53
54     @Override
55     public void handleReceive(Object message) throws Exception {
56         if(WriteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
57             writeData(transaction, WriteData.fromSerializable(message, getSchemaContext()));
58         } else if(MergeData.SERIALIZABLE_CLASS.equals(message.getClass())) {
59             mergeData(transaction, MergeData.fromSerializable(message, getSchemaContext()));
60         } else if(DeleteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
61             deleteData(transaction, DeleteData.fromSerializable(message));
62         } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
63             readyTransaction(transaction, new ReadyTransaction());
64         } else if (message instanceof GetCompositedModification) {
65             // This is here for testing only
66             getSender().tell(new GetCompositeModificationReply(
67                     new ImmutableCompositeModification(modification)), getSelf());
68         } else {
69             super.handleReceive(message);
70         }
71     }
72
73     private void writeData(DOMStoreWriteTransaction transaction, WriteData message) {
74         modification.addModification(
75                 new WriteModification(message.getPath(), message.getData(), getSchemaContext()));
76         if(LOG.isDebugEnabled()) {
77             LOG.debug("writeData at path : " + message.getPath().toString());
78         }
79         try {
80             transaction.write(message.getPath(), message.getData());
81             getSender().tell(new WriteDataReply().toSerializable(), getSelf());
82         }catch(Exception e){
83             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
84         }
85     }
86
87     private void mergeData(DOMStoreWriteTransaction transaction, MergeData message) {
88         modification.addModification(
89                 new MergeModification(message.getPath(), message.getData(), getSchemaContext()));
90         if(LOG.isDebugEnabled()) {
91             LOG.debug("mergeData at path : " + message.getPath().toString());
92         }
93         try {
94             transaction.merge(message.getPath(), message.getData());
95             getSender().tell(new MergeDataReply().toSerializable(), getSelf());
96         }catch(Exception e){
97             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
98         }
99     }
100
101     private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message) {
102         if(LOG.isDebugEnabled()) {
103             LOG.debug("deleteData at path : " + message.getPath().toString());
104         }
105         modification.addModification(new DeleteModification(message.getPath()));
106         try {
107             transaction.delete(message.getPath());
108             getSender().tell(new DeleteDataReply().toSerializable(), getSelf());
109         }catch(Exception e){
110             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
111         }
112     }
113
114     private void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message) {
115         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
116
117         getShardActor().forward(new ForwardedReadyTransaction(getTransactionID(), cohort, modification),
118                 getContext());
119     }
120
121     // These classes are in here for test purposes only
122
123     static class GetCompositedModification {
124     }
125
126     static class GetCompositeModificationReply {
127         private final CompositeModification modification;
128
129
130         GetCompositeModificationReply(CompositeModification modification) {
131             this.modification = modification;
132         }
133
134         public CompositeModification getModification() {
135             return modification;
136         }
137     }
138 }