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 akka.serialization.Serialization;
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
20 import org.opendaylight.controller.cluster.datastore.exceptions.UnknownMessageException;
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.DeleteData;
27 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
28 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
29 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
30 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
31 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
32 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
33 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
34 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
35 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
36 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
37 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
38 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
39 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
40 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
41 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
42 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
43 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
44 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
45 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
46 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
47 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
53 * The ShardTransaction Actor represents a remote transaction
55 * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
58 * Even though the DOMStore and the DOMStoreTransactionChain implement multiple types of transactions
59 * the ShardTransaction Actor only works with read-write transactions. This is just to keep the logic simple. At this
60 * time there are no known advantages for creating a read-only or write-only transaction which may change over time
61 * at which point we can optimize things in the distributed store as well.
64 * Handles Messages <br/>
65 * ---------------- <br/>
66 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadData}
67 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.WriteData}
68 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.MergeData}
69 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.DeleteData}
70 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction}
71 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
74 public abstract class ShardTransaction extends AbstractUntypedActor {
76 private final ActorRef shardActor;
77 protected final SchemaContext schemaContext;
78 private final ShardStats shardStats;
80 private final MutableCompositeModification modification = new MutableCompositeModification();
82 protected ShardTransaction(ActorRef shardActor, SchemaContext schemaContext,
83 ShardStats shardStats) {
84 this.shardActor = shardActor;
85 this.schemaContext = schemaContext;
86 this.shardStats = shardStats;
89 public static Props props(DOMStoreTransaction transaction, ActorRef shardActor,
90 SchemaContext schemaContext,DatastoreContext datastoreContext, ShardStats shardStats) {
91 return Props.create(new ShardTransactionCreator(transaction, shardActor, schemaContext,
92 datastoreContext, shardStats));
95 protected abstract DOMStoreTransaction getDOMStoreTransaction();
98 public void handleReceive(Object message) throws Exception {
99 if (message.getClass().equals(CloseTransaction.SERIALIZABLE_CLASS)) {
100 closeTransaction(true);
101 } else if (message instanceof GetCompositedModification) {
102 // This is here for testing only
103 getSender().tell(new GetCompositeModificationReply(
104 new ImmutableCompositeModification(modification)), getSelf());
105 } else if (message instanceof ReceiveTimeout) {
106 if(LOG.isDebugEnabled()) {
107 LOG.debug("Got ReceiveTimeout for inactivity - closing Tx");
109 closeTransaction(false);
111 throw new UnknownMessageException(message);
115 private void closeTransaction(boolean sendReply) {
116 getDOMStoreTransaction().close();
119 getSender().tell(new CloseTransactionReply().toSerializable(), getSelf());
122 getSelf().tell(PoisonPill.getInstance(), getSelf());
125 protected void readData(DOMStoreReadTransaction transaction,ReadData message) {
126 final ActorRef sender = getSender();
127 final ActorRef self = getSelf();
128 final YangInstanceIdentifier path = message.getPath();
129 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> future =
130 transaction.read(path);
132 future.addListener(new Runnable() {
136 Optional<NormalizedNode<?, ?>> optional = future.checkedGet();
137 if (optional.isPresent()) {
138 sender.tell(new ReadDataReply(schemaContext,optional.get()).toSerializable(), self);
140 sender.tell(new ReadDataReply(schemaContext,null).toSerializable(), self);
142 } catch (Exception e) {
143 shardStats.incrementFailedReadTransactionsCount();
144 sender.tell(new akka.actor.Status.Failure(e), self);
148 }, getContext().dispatcher());
151 protected void dataExists(DOMStoreReadTransaction transaction, DataExists message) {
152 final YangInstanceIdentifier path = message.getPath();
155 Boolean exists = transaction.exists(path).checkedGet();
156 getSender().tell(new DataExistsReply(exists).toSerializable(), getSelf());
157 } catch (ReadFailedException e) {
158 getSender().tell(new akka.actor.Status.Failure(e),getSelf());
163 protected void writeData(DOMStoreWriteTransaction transaction, WriteData message) {
164 modification.addModification(
165 new WriteModification(message.getPath(), message.getData(),schemaContext));
166 if(LOG.isDebugEnabled()) {
167 LOG.debug("writeData at path : " + message.getPath().toString());
170 transaction.write(message.getPath(), message.getData());
171 getSender().tell(new WriteDataReply().toSerializable(), getSelf());
173 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
177 protected void mergeData(DOMStoreWriteTransaction transaction, MergeData message) {
178 modification.addModification(
179 new MergeModification(message.getPath(), message.getData(), schemaContext));
180 if(LOG.isDebugEnabled()) {
181 LOG.debug("mergeData at path : " + message.getPath().toString());
184 transaction.merge(message.getPath(), message.getData());
185 getSender().tell(new MergeDataReply().toSerializable(), getSelf());
187 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
191 protected void deleteData(DOMStoreWriteTransaction transaction, DeleteData message) {
192 if(LOG.isDebugEnabled()) {
193 LOG.debug("deleteData at path : " + message.getPath().toString());
195 modification.addModification(new DeleteModification(message.getPath()));
197 transaction.delete(message.getPath());
198 getSender().tell(new DeleteDataReply().toSerializable(), getSelf());
200 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
204 protected void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message) {
205 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
206 ActorRef cohortActor = getContext().actorOf(
207 ThreePhaseCommitCohort.props(cohort, shardActor, modification, shardStats), "cohort");
208 getSender().tell(new ReadyTransactionReply(
209 Serialization.serializedActorPath(cohortActor)).toSerializable(), getSelf());
213 private static class ShardTransactionCreator implements Creator<ShardTransaction> {
215 private static final long serialVersionUID = 1L;
217 final DOMStoreTransaction transaction;
218 final ActorRef shardActor;
219 final SchemaContext schemaContext;
220 final DatastoreContext datastoreContext;
221 final ShardStats shardStats;
223 ShardTransactionCreator(DOMStoreTransaction transaction, ActorRef shardActor,
224 SchemaContext schemaContext, DatastoreContext datastoreContext,
225 ShardStats shardStats) {
226 this.transaction = transaction;
227 this.shardActor = shardActor;
228 this.shardStats = shardStats;
229 this.schemaContext = schemaContext;
230 this.datastoreContext = datastoreContext;
234 public ShardTransaction create() throws Exception {
236 if(transaction instanceof DOMStoreReadWriteTransaction) {
237 tx = new ShardReadWriteTransaction((DOMStoreReadWriteTransaction)transaction,
238 shardActor, schemaContext, shardStats);
239 } else if(transaction instanceof DOMStoreReadTransaction) {
240 tx = new ShardReadTransaction((DOMStoreReadTransaction)transaction, shardActor,
241 schemaContext, shardStats);
243 tx = new ShardWriteTransaction((DOMStoreWriteTransaction)transaction,
244 shardActor, schemaContext, shardStats);
247 tx.getContext().setReceiveTimeout(datastoreContext.getShardTransactionIdleTimeout());
252 // These classes are in here for test purposes only
254 static class GetCompositedModification {
258 static class GetCompositeModificationReply {
259 private final CompositeModification modification;
262 GetCompositeModificationReply(CompositeModification modification) {
263 this.modification = modification;
267 public CompositeModification getModification() {