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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActorWithMetering;
21 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
22 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
23 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
25 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
27 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 * The ShardTransaction Actor represents a remote transaction that delegates all actions to DOMDataReadWriteTransaction.
35 public abstract class ShardTransaction extends AbstractUntypedActorWithMetering {
36 private final ActorRef shardActor;
37 private final ShardStats shardStats;
38 private final TransactionIdentifier transactionId;
40 protected ShardTransaction(ActorRef shardActor, ShardStats shardStats, TransactionIdentifier transactionId) {
41 super("shard-tx"); //actor name override used for metering. This does not change the "real" actor name
42 this.shardActor = shardActor;
43 this.shardStats = shardStats;
44 this.transactionId = Preconditions.checkNotNull(transactionId);
47 public static Props props(TransactionType type, AbstractShardDataTreeTransaction<?> transaction,
48 ActorRef shardActor, DatastoreContext datastoreContext, ShardStats shardStats) {
49 return Props.create(new ShardTransactionCreator(type, transaction, shardActor, datastoreContext, shardStats));
52 protected abstract AbstractShardDataTreeTransaction<?> getDOMStoreTransaction();
54 protected ActorRef getShardActor() {
58 protected final TransactionIdentifier getTransactionId() {
63 public void handleReceive(Object message) {
64 if (CloseTransaction.isSerializedType(message)) {
65 closeTransaction(true);
66 } else if (message instanceof ReceiveTimeout) {
67 LOG.debug("Got ReceiveTimeout for inactivity - closing transaction {}", transactionId);
68 closeTransaction(false);
70 unknownMessage(message);
74 protected boolean returnCloseTransactionReply() {
78 private void closeTransaction(boolean sendReply) {
79 getDOMStoreTransaction().abort();
81 if (sendReply && returnCloseTransactionReply()) {
82 getSender().tell(new CloseTransactionReply(), getSelf());
85 getSelf().tell(PoisonPill.getInstance(), getSelf());
88 private boolean checkClosed(AbstractShardDataTreeTransaction<?> transaction) {
89 final boolean ret = transaction.isClosed();
91 shardStats.incrementFailedReadTransactionsCount();
92 getSender().tell(new akka.actor.Status.Failure(new ReadFailedException("Transaction is closed")),
98 protected void readData(AbstractShardDataTreeTransaction<?> transaction, ReadData message) {
99 if (checkClosed(transaction)) {
103 final YangInstanceIdentifier path = message.getPath();
104 Optional<NormalizedNode<?, ?>> optional = transaction.getSnapshot().readNode(path);
105 ReadDataReply readDataReply = new ReadDataReply(optional.orNull(), message.getVersion());
106 sender().tell(readDataReply.toSerializable(), self());
109 protected void dataExists(AbstractShardDataTreeTransaction<?> transaction, DataExists message) {
110 if (checkClosed(transaction)) {
114 final YangInstanceIdentifier path = message.getPath();
115 boolean exists = transaction.getSnapshot().readNode(path).isPresent();
116 getSender().tell(new DataExistsReply(exists, message.getVersion()).toSerializable(), getSelf());
119 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Some fields are not Serializable but we don't "
120 + "create remote instances of this actor and thus don't need it to be Serializable.")
121 private static class ShardTransactionCreator implements Creator<ShardTransaction> {
123 private static final long serialVersionUID = 1L;
125 final AbstractShardDataTreeTransaction<?> transaction;
126 final ActorRef shardActor;
127 final DatastoreContext datastoreContext;
128 final ShardStats shardStats;
129 final TransactionType type;
131 ShardTransactionCreator(TransactionType type, AbstractShardDataTreeTransaction<?> transaction,
132 ActorRef shardActor, DatastoreContext datastoreContext, ShardStats shardStats) {
133 this.transaction = Preconditions.checkNotNull(transaction);
134 this.shardActor = shardActor;
135 this.shardStats = shardStats;
136 this.datastoreContext = datastoreContext;
141 public ShardTransaction create() throws Exception {
142 final ShardTransaction tx;
145 tx = new ShardReadTransaction(transaction, shardActor, shardStats);
148 tx = new ShardReadWriteTransaction((ReadWriteShardDataTreeTransaction)transaction, shardActor,
152 tx = new ShardWriteTransaction((ReadWriteShardDataTreeTransaction)transaction, shardActor,
156 throw new IllegalArgumentException("Unhandled transaction type " + type);
159 tx.getContext().setReceiveTimeout(datastoreContext.getShardTransactionIdleTimeout());