Convert DatastoreSnapshotRestore to OSGi DS
[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 package org.opendaylight.controller.cluster.datastore;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.actor.PoisonPill;
14 import akka.actor.Props;
15 import akka.actor.ReceiveTimeout;
16 import akka.japi.Creator;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActorWithMetering;
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.mdsal.common.api.ReadFailedException;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29
30 /**
31  * The ShardTransaction Actor represents a remote transaction that delegates all actions to DOMDataReadWriteTransaction.
32  */
33 public abstract class ShardTransaction extends AbstractUntypedActorWithMetering {
34     private final ActorRef shardActor;
35     private final ShardStats shardStats;
36     private final TransactionIdentifier transactionId;
37
38     protected ShardTransaction(final ActorRef shardActor, final ShardStats shardStats,
39             final TransactionIdentifier transactionId) {
40         // actor name override used for metering. This does not change the "real" actor name
41         super("shard-tx");
42         this.shardActor = shardActor;
43         this.shardStats = shardStats;
44         this.transactionId = requireNonNull(transactionId);
45     }
46
47     public static Props props(final TransactionType type, final AbstractShardDataTreeTransaction<?> transaction,
48             final ActorRef shardActor, final DatastoreContext datastoreContext, final ShardStats shardStats) {
49         return Props.create(ShardTransaction.class,
50             new ShardTransactionCreator(type, transaction, shardActor, datastoreContext, shardStats));
51     }
52
53     protected abstract AbstractShardDataTreeTransaction<?> getDOMStoreTransaction();
54
55     protected ActorRef getShardActor() {
56         return shardActor;
57     }
58
59     protected final TransactionIdentifier getTransactionId() {
60         return transactionId;
61     }
62
63     @Override
64     public void handleReceive(final Object message) {
65         if (CloseTransaction.isSerializedType(message)) {
66             closeTransaction(true);
67         } else if (message instanceof ReceiveTimeout) {
68             LOG.debug("Got ReceiveTimeout for inactivity - closing transaction {}", transactionId);
69             closeTransaction(false);
70         } else {
71             unknownMessage(message);
72         }
73     }
74
75     protected boolean returnCloseTransactionReply() {
76         return true;
77     }
78
79     private void closeTransaction(final boolean sendReply) {
80         getDOMStoreTransaction().abortFromTransactionActor();
81
82         if (sendReply && returnCloseTransactionReply()) {
83             getSender().tell(new CloseTransactionReply(), getSelf());
84         }
85
86         getSelf().tell(PoisonPill.getInstance(), getSelf());
87     }
88
89     private boolean checkClosed(final AbstractShardDataTreeTransaction<?> transaction) {
90         final boolean ret = transaction.isClosed();
91         if (ret) {
92             shardStats.incrementFailedReadTransactionsCount();
93             getSender().tell(new akka.actor.Status.Failure(new ReadFailedException("Transaction is closed")),
94                     getSelf());
95         }
96         return ret;
97     }
98
99     protected void readData(final AbstractShardDataTreeTransaction<?> transaction, final ReadData message) {
100         if (checkClosed(transaction)) {
101             return;
102         }
103
104         final YangInstanceIdentifier path = message.getPath();
105         ReadDataReply readDataReply = new ReadDataReply(transaction.getSnapshot().readNode(path).orElse(null),
106             message.getVersion());
107         sender().tell(readDataReply.toSerializable(), self());
108     }
109
110     protected void dataExists(final AbstractShardDataTreeTransaction<?> transaction, final DataExists message) {
111         if (checkClosed(transaction)) {
112             return;
113         }
114
115         final YangInstanceIdentifier path = message.getPath();
116         boolean exists = transaction.getSnapshot().readNode(path).isPresent();
117         getSender().tell(new DataExistsReply(exists, message.getVersion()).toSerializable(), getSelf());
118     }
119
120     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Some fields are not Serializable but we don't "
121             + "create remote instances of this actor and thus don't need it to be Serializable.")
122     private static class ShardTransactionCreator implements Creator<ShardTransaction> {
123
124         private static final long serialVersionUID = 1L;
125
126         final AbstractShardDataTreeTransaction<?> transaction;
127         final ActorRef shardActor;
128         final DatastoreContext datastoreContext;
129         final ShardStats shardStats;
130         final TransactionType type;
131
132         ShardTransactionCreator(final TransactionType type, final AbstractShardDataTreeTransaction<?> transaction,
133                 final ActorRef shardActor, final DatastoreContext datastoreContext, final ShardStats shardStats) {
134             this.transaction = requireNonNull(transaction);
135             this.shardActor = shardActor;
136             this.shardStats = shardStats;
137             this.datastoreContext = datastoreContext;
138             this.type = type;
139         }
140
141         @Override
142         public ShardTransaction create() {
143             final ShardTransaction tx;
144             switch (type) {
145                 case READ_ONLY:
146                     tx = new ShardReadTransaction(transaction, shardActor, shardStats);
147                     break;
148                 case READ_WRITE:
149                     tx = new ShardReadWriteTransaction((ReadWriteShardDataTreeTransaction)transaction, shardActor,
150                             shardStats);
151                     break;
152                 case WRITE_ONLY:
153                     tx = new ShardWriteTransaction((ReadWriteShardDataTreeTransaction)transaction, shardActor,
154                             shardStats);
155                     break;
156                 default:
157                     throw new IllegalArgumentException("Unhandled transaction type " + type);
158             }
159
160             tx.getContext().setReceiveTimeout(datastoreContext.getShardTransactionIdleTimeout());
161             return tx;
162         }
163     }
164 }