Deprecate CloseTransaction protobuff message
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorRef;
12 import akka.actor.PoisonPill;
13 import akka.actor.Props;
14 import akka.actor.ReceiveTimeout;
15 import akka.japi.Creator;
16 import com.google.common.base.Optional;
17 import com.google.common.base.Preconditions;
18 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActorWithMetering;
19 import org.opendaylight.controller.cluster.datastore.exceptions.UnknownMessageException;
20 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
21 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
23 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
24 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30
31 /**
32  * The ShardTransaction Actor represents a remote transaction
33  * <p>
34  * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
35  * </p>
36  * <p>
37  * Handles Messages <br/>
38  * ---------------- <br/>
39  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadData}
40  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
41  * </p>
42  */
43 public abstract class ShardTransaction extends AbstractUntypedActorWithMetering {
44
45     protected static final boolean SERIALIZED_REPLY = true;
46
47     private final ActorRef shardActor;
48     private final ShardStats shardStats;
49     private final String transactionID;
50     private final short clientTxVersion;
51
52     protected ShardTransaction(ActorRef shardActor, ShardStats shardStats, String transactionID,
53             short clientTxVersion) {
54         super("shard-tx"); //actor name override used for metering. This does not change the "real" actor name
55         this.shardActor = shardActor;
56         this.shardStats = shardStats;
57         this.transactionID = Preconditions.checkNotNull(transactionID);
58         this.clientTxVersion = clientTxVersion;
59     }
60
61     public static Props props(TransactionType type, AbstractShardDataTreeTransaction<?> transaction, ActorRef shardActor,
62             DatastoreContext datastoreContext, ShardStats shardStats, String transactionID, short txnClientVersion) {
63         return Props.create(new ShardTransactionCreator(type, transaction, shardActor,
64            datastoreContext, shardStats, transactionID, txnClientVersion));
65     }
66
67     protected abstract AbstractShardDataTreeTransaction<?> getDOMStoreTransaction();
68
69     protected ActorRef getShardActor() {
70         return shardActor;
71     }
72
73     protected String getTransactionID() {
74         return transactionID;
75     }
76
77     protected short getClientTxVersion() {
78         return clientTxVersion;
79     }
80
81     @Override
82     public void handleReceive(Object message) throws Exception {
83         if (CloseTransaction.isSerializedType(message)) {
84             closeTransaction(true);
85         } else if (message instanceof ReceiveTimeout) {
86             if(LOG.isDebugEnabled()) {
87                 LOG.debug("Got ReceiveTimeout for inactivity - closing Tx");
88             }
89             closeTransaction(false);
90         } else {
91             throw new UnknownMessageException(message);
92         }
93     }
94
95     protected boolean returnCloseTransactionReply() {
96         return true;
97     }
98
99     private void closeTransaction(boolean sendReply) {
100         getDOMStoreTransaction().abort();
101
102         if(sendReply && returnCloseTransactionReply()) {
103             getSender().tell(new CloseTransactionReply(), getSelf());
104         }
105
106         getSelf().tell(PoisonPill.getInstance(), getSelf());
107     }
108
109     private boolean checkClosed(AbstractShardDataTreeTransaction<?> transaction) {
110         final boolean ret = transaction.isClosed();
111         if (ret) {
112             shardStats.incrementFailedReadTransactionsCount();
113             getSender().tell(new akka.actor.Status.Failure(new ReadFailedException("Transaction is closed")), getSelf());
114         }
115         return ret;
116     }
117
118     protected void readData(AbstractShardDataTreeTransaction<?> transaction, ReadData message,
119             final boolean returnSerialized) {
120
121         if (checkClosed(transaction)) {
122             return;
123         }
124
125         final YangInstanceIdentifier path = message.getPath();
126         Optional<NormalizedNode<?, ?>> optional = transaction.getSnapshot().readNode(path);
127         ReadDataReply readDataReply = new ReadDataReply(optional.orNull(), clientTxVersion);
128         sender().tell((returnSerialized ? readDataReply.toSerializable(): readDataReply), self());
129     }
130
131     protected void dataExists(AbstractShardDataTreeTransaction<?> transaction, DataExists message,
132         final boolean returnSerialized) {
133
134         if (checkClosed(transaction)) {
135             return;
136         }
137
138         final YangInstanceIdentifier path = message.getPath();
139         boolean exists = transaction.getSnapshot().readNode(path).isPresent();
140         DataExistsReply dataExistsReply = DataExistsReply.create(exists);
141         getSender().tell(returnSerialized ? dataExistsReply.toSerializable() :
142             dataExistsReply, getSelf());
143     }
144
145     private static class ShardTransactionCreator implements Creator<ShardTransaction> {
146
147         private static final long serialVersionUID = 1L;
148
149         final AbstractShardDataTreeTransaction<?> transaction;
150         final ActorRef shardActor;
151         final DatastoreContext datastoreContext;
152         final ShardStats shardStats;
153         final String transactionID;
154         final short txnClientVersion;
155         final TransactionType type;
156
157         ShardTransactionCreator(TransactionType type, AbstractShardDataTreeTransaction<?> transaction, ActorRef shardActor,
158                 DatastoreContext datastoreContext, ShardStats shardStats, String transactionID, short txnClientVersion) {
159             this.transaction = Preconditions.checkNotNull(transaction);
160             this.shardActor = shardActor;
161             this.shardStats = shardStats;
162             this.datastoreContext = datastoreContext;
163             this.transactionID = Preconditions.checkNotNull(transactionID);
164             this.txnClientVersion = txnClientVersion;
165             this.type = type;
166         }
167
168         @Override
169         public ShardTransaction create() throws Exception {
170             final ShardTransaction tx;
171             switch (type) {
172             case READ_ONLY:
173                 tx = new ShardReadTransaction(transaction, shardActor,
174                     shardStats, transactionID, txnClientVersion);
175                 break;
176             case READ_WRITE:
177                 tx = new ShardReadWriteTransaction((ReadWriteShardDataTreeTransaction)transaction,
178                     shardActor, shardStats, transactionID, txnClientVersion);
179                 break;
180             case WRITE_ONLY:
181                 tx = new ShardWriteTransaction((ReadWriteShardDataTreeTransaction)transaction,
182                     shardActor, shardStats, transactionID, txnClientVersion);
183                 break;
184             default:
185                 throw new IllegalArgumentException("Unhandled transaction type " + type);
186             }
187
188             tx.getContext().setReceiveTimeout(datastoreContext.getShardTransactionIdleTimeout());
189             return tx;
190         }
191     }
192 }