2ad2488d6d652263c07327983c97e6b429a9c625
[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 package org.opendaylight.controller.cluster.datastore;
11
12 import akka.actor.ActorRef;
13 import akka.actor.PoisonPill;
14 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
15 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
16 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
17 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
18 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
20 import org.opendaylight.controller.cluster.datastore.modification.Modification;
21
22 /**
23  * @author: syedbahm
24  * Date: 8/6/14
25  */
26 public class ShardWriteTransaction extends ShardTransaction {
27
28     private int totalBatchedModificationsReceived;
29     private Exception lastBatchedModificationsException;
30     private final ReadWriteShardDataTreeTransaction transaction;
31
32     public ShardWriteTransaction(ReadWriteShardDataTreeTransaction transaction, ActorRef shardActor,
33             ShardStats shardStats, String transactionID) {
34         super(shardActor, shardStats, transactionID);
35         this.transaction = transaction;
36     }
37
38     @Override
39     protected ReadWriteShardDataTreeTransaction getDOMStoreTransaction() {
40         return transaction;
41     }
42
43     @Override
44     public void handleReceive(Object message) {
45         if (message instanceof BatchedModifications) {
46             batchedModifications((BatchedModifications)message);
47         } else {
48             super.handleReceive(message);
49         }
50     }
51
52     private void batchedModifications(BatchedModifications batched) {
53         if (checkClosed()) {
54             if (batched.isReady()) {
55                 getSelf().tell(PoisonPill.getInstance(), getSelf());
56             }
57             return;
58         }
59
60         try {
61             for(Modification modification: batched.getModifications()) {
62                 modification.apply(transaction.getSnapshot());
63             }
64
65             totalBatchedModificationsReceived++;
66             if(batched.isReady()) {
67                 if(lastBatchedModificationsException != null) {
68                     throw lastBatchedModificationsException;
69                 }
70
71                 if(totalBatchedModificationsReceived != batched.getTotalMessagesSent()) {
72                     throw new IllegalStateException(String.format(
73                             "The total number of batched messages received %d does not match the number sent %d",
74                             totalBatchedModificationsReceived, batched.getTotalMessagesSent()));
75                 }
76
77                 readyTransaction(false, batched.isDoCommitOnReady(), batched.getVersion());
78             } else {
79                 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
80             }
81         } catch (Exception e) {
82             lastBatchedModificationsException = e;
83             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
84
85             if(batched.isReady()) {
86                 getSelf().tell(PoisonPill.getInstance(), getSelf());
87             }
88         }
89     }
90
91     protected final void dataExists(DataExists message) {
92         super.dataExists(transaction, message);
93     }
94
95     protected final void readData(ReadData message) {
96         super.readData(transaction, message);
97     }
98
99     private boolean checkClosed() {
100         if (transaction.isClosed()) {
101             getSender().tell(new akka.actor.Status.Failure(new IllegalStateException("Transaction is closed, no modifications allowed")), getSelf());
102             return true;
103         } else {
104             return false;
105         }
106     }
107
108     private void readyTransaction(boolean returnSerialized, boolean doImmediateCommit, short clientTxVersion) {
109         String transactionID = getTransactionID();
110
111         LOG.debug("readyTransaction : {}", transactionID);
112
113         getShardActor().forward(new ForwardedReadyTransaction(transactionID, clientTxVersion,
114                 transaction, doImmediateCommit), getContext());
115
116         // The shard will handle the commit from here so we're no longer needed - self-destruct.
117         getSelf().tell(PoisonPill.getInstance(), getSelf());
118     }
119 }