c2a05a6d6130a3d3b28dd9879eebef70dcaf9e65
[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.jmx.mbeans.shard.ShardStats;
20 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
22 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
23 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
24 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
26 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29
30 /**
31  * The ShardTransaction Actor represents a remote transaction
32  * <p>
33  * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
34  * </p>
35  * <p>
36  * Handles Messages <br/>
37  * ---------------- <br/>
38  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadData}
39  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
40  * </p>
41  */
42 public abstract class ShardTransaction extends AbstractUntypedActorWithMetering {
43     private final ActorRef shardActor;
44     private final ShardStats shardStats;
45     private final String transactionID;
46
47     protected ShardTransaction(ActorRef shardActor, ShardStats shardStats, String transactionID) {
48         super("shard-tx"); //actor name override used for metering. This does not change the "real" actor name
49         this.shardActor = shardActor;
50         this.shardStats = shardStats;
51         this.transactionID = Preconditions.checkNotNull(transactionID);
52     }
53
54     public static Props props(TransactionType type, AbstractShardDataTreeTransaction<?> transaction, ActorRef shardActor,
55             DatastoreContext datastoreContext, ShardStats shardStats, String transactionID) {
56         return Props.create(new ShardTransactionCreator(type, transaction, shardActor,
57            datastoreContext, shardStats, transactionID));
58     }
59
60     protected abstract AbstractShardDataTreeTransaction<?> getDOMStoreTransaction();
61
62     protected ActorRef getShardActor() {
63         return shardActor;
64     }
65
66     protected String getTransactionID() {
67         return transactionID;
68     }
69
70     @Override
71     public void handleReceive(Object message) {
72         if (CloseTransaction.isSerializedType(message)) {
73             closeTransaction(true);
74         } else if (message instanceof ReceiveTimeout) {
75             LOG.debug("Got ReceiveTimeout for inactivity - closing transaction {}", transactionID);
76             closeTransaction(false);
77         } else {
78             unknownMessage(message);
79         }
80     }
81
82     protected boolean returnCloseTransactionReply() {
83         return true;
84     }
85
86     private void closeTransaction(boolean sendReply) {
87         getDOMStoreTransaction().abort();
88
89         if(sendReply && returnCloseTransactionReply()) {
90             getSender().tell(new CloseTransactionReply(), getSelf());
91         }
92
93         getSelf().tell(PoisonPill.getInstance(), getSelf());
94     }
95
96     private boolean checkClosed(AbstractShardDataTreeTransaction<?> transaction) {
97         final boolean ret = transaction.isClosed();
98         if (ret) {
99             shardStats.incrementFailedReadTransactionsCount();
100             getSender().tell(new akka.actor.Status.Failure(new ReadFailedException("Transaction is closed")), getSelf());
101         }
102         return ret;
103     }
104
105     protected void readData(AbstractShardDataTreeTransaction<?> transaction, ReadData message) {
106         if (checkClosed(transaction)) {
107             return;
108         }
109
110         final YangInstanceIdentifier path = message.getPath();
111         Optional<NormalizedNode<?, ?>> optional = transaction.getSnapshot().readNode(path);
112         ReadDataReply readDataReply = new ReadDataReply(optional.orNull(), message.getVersion());
113         sender().tell(readDataReply.toSerializable(), self());
114     }
115
116     protected void dataExists(AbstractShardDataTreeTransaction<?> transaction, DataExists message) {
117         if (checkClosed(transaction)) {
118             return;
119         }
120
121         final YangInstanceIdentifier path = message.getPath();
122         boolean exists = transaction.getSnapshot().readNode(path).isPresent();
123         getSender().tell(new DataExistsReply(exists, message.getVersion()).toSerializable(), getSelf());
124     }
125
126     private static class ShardTransactionCreator implements Creator<ShardTransaction> {
127
128         private static final long serialVersionUID = 1L;
129
130         final AbstractShardDataTreeTransaction<?> transaction;
131         final ActorRef shardActor;
132         final DatastoreContext datastoreContext;
133         final ShardStats shardStats;
134         final String transactionID;
135         final TransactionType type;
136
137         ShardTransactionCreator(TransactionType type, AbstractShardDataTreeTransaction<?> transaction, ActorRef shardActor,
138                 DatastoreContext datastoreContext, ShardStats shardStats, String transactionID) {
139             this.transaction = Preconditions.checkNotNull(transaction);
140             this.shardActor = shardActor;
141             this.shardStats = shardStats;
142             this.datastoreContext = datastoreContext;
143             this.transactionID = Preconditions.checkNotNull(transactionID);
144             this.type = type;
145         }
146
147         @Override
148         public ShardTransaction create() throws Exception {
149             final ShardTransaction tx;
150             switch (type) {
151             case READ_ONLY:
152                 tx = new ShardReadTransaction(transaction, shardActor,
153                     shardStats, transactionID);
154                 break;
155             case READ_WRITE:
156                 tx = new ShardReadWriteTransaction((ReadWriteShardDataTreeTransaction)transaction,
157                     shardActor, shardStats, transactionID);
158                 break;
159             case WRITE_ONLY:
160                 tx = new ShardWriteTransaction((ReadWriteShardDataTreeTransaction)transaction,
161                     shardActor, shardStats, transactionID);
162                 break;
163             default:
164                 throw new IllegalArgumentException("Unhandled transaction type " + type);
165             }
166
167             tx.getContext().setReceiveTimeout(datastoreContext.getShardTransactionIdleTimeout());
168             return tx;
169         }
170     }
171 }