2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
32 * The ShardTransaction Actor represents a remote transaction
34 * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
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}
43 public abstract class ShardTransaction extends AbstractUntypedActorWithMetering {
45 protected static final boolean SERIALIZED_REPLY = true;
47 private final ActorRef shardActor;
48 private final ShardStats shardStats;
49 private final String transactionID;
50 private final short clientTxVersion;
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;
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));
67 protected abstract AbstractShardDataTreeTransaction<?> getDOMStoreTransaction();
69 protected ActorRef getShardActor() {
73 protected String getTransactionID() {
77 protected short getClientTxVersion() {
78 return clientTxVersion;
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");
89 closeTransaction(false);
91 throw new UnknownMessageException(message);
95 protected boolean returnCloseTransactionReply() {
99 private void closeTransaction(boolean sendReply) {
100 getDOMStoreTransaction().abort();
102 if(sendReply && returnCloseTransactionReply()) {
103 getSender().tell(new CloseTransactionReply(), getSelf());
106 getSelf().tell(PoisonPill.getInstance(), getSelf());
109 private boolean checkClosed(AbstractShardDataTreeTransaction<?> transaction) {
110 final boolean ret = transaction.isClosed();
112 shardStats.incrementFailedReadTransactionsCount();
113 getSender().tell(new akka.actor.Status.Failure(new ReadFailedException("Transaction is closed")), getSelf());
118 protected void readData(AbstractShardDataTreeTransaction<?> transaction, ReadData message,
119 final boolean returnSerialized) {
121 if (checkClosed(transaction)) {
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());
131 protected void dataExists(AbstractShardDataTreeTransaction<?> transaction, DataExists message,
132 final boolean returnSerialized) {
134 if (checkClosed(transaction)) {
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());
145 private static class ShardTransactionCreator implements Creator<ShardTransaction> {
147 private static final long serialVersionUID = 1L;
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;
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;
169 public ShardTransaction create() throws Exception {
170 final ShardTransaction tx;
173 tx = new ShardReadTransaction(transaction, shardActor,
174 shardStats, transactionID, txnClientVersion);
177 tx = new ShardReadWriteTransaction((ReadWriteShardDataTreeTransaction)transaction,
178 shardActor, shardStats, transactionID, txnClientVersion);
181 tx = new ShardWriteTransaction((ReadWriteShardDataTreeTransaction)transaction,
182 shardActor, shardStats, transactionID, txnClientVersion);
185 throw new IllegalArgumentException("Unhandled transaction type " + type);
188 tx.getContext().setReceiveTimeout(datastoreContext.getShardTransactionIdleTimeout());