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.util.concurrent.CheckedFuture;
18 import org.opendaylight.controller.cluster.datastore.exceptions.UnknownMessageException;
19 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardMBeanFactory;
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.DeleteData;
25 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
26 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
27 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
28 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
29 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
30 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
31 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
32 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
33 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
34 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
35 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
36 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
37 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
38 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
39 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
40 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
41 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
42 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
43 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
44 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
45 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 * The ShardTransaction Actor represents a remote transaction
53 * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
56 * Even though the DOMStore and the DOMStoreTransactionChain implement multiple types of transactions
57 * the ShardTransaction Actor only works with read-write transactions. This is just to keep the logic simple. At this
58 * time there are no known advantages for creating a read-only or write-only transaction which may change over time
59 * at which point we can optimize things in the distributed store as well.
62 * Handles Messages <br/>
63 * ---------------- <br/>
64 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadData}
65 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.WriteData}
66 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.MergeData}
67 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.DeleteData}
68 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction}
69 * <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
72 public abstract class ShardTransaction extends AbstractUntypedActor {
74 private final ActorRef shardActor;
75 protected final SchemaContext schemaContext;
76 private final String shardName;
79 private final MutableCompositeModification modification = new MutableCompositeModification();
81 protected ShardTransaction(ActorRef shardActor, SchemaContext schemaContext,
83 this.shardActor = shardActor;
84 this.schemaContext = schemaContext;
85 this.shardName = shardName;
88 public static Props props(DOMStoreTransaction transaction, ActorRef shardActor,
89 SchemaContext schemaContext,DatastoreContext datastoreContext, String shardName) {
90 return Props.create(new ShardTransactionCreator(transaction, shardActor, schemaContext,
91 datastoreContext, shardName));
94 protected abstract DOMStoreTransaction getDOMStoreTransaction();
97 public void handleReceive(Object message) throws Exception {
98 if (message.getClass().equals(CloseTransaction.SERIALIZABLE_CLASS)) {
99 closeTransaction(true);
100 } else if (message instanceof GetCompositedModification) {
101 // This is here for testing only
102 getSender().tell(new GetCompositeModificationReply(
103 new ImmutableCompositeModification(modification)), getSelf());
104 } else if (message instanceof ReceiveTimeout) {
105 LOG.debug("Got ReceiveTimeout for inactivity - closing Tx");
106 closeTransaction(false);
108 throw new UnknownMessageException(message);
112 private void closeTransaction(boolean sendReply) {
113 getDOMStoreTransaction().close();
116 getSender().tell(new CloseTransactionReply().toSerializable(), getSelf());
119 getSelf().tell(PoisonPill.getInstance(), getSelf());
122 protected void readData(DOMStoreReadTransaction transaction,ReadData message) {
123 final ActorRef sender = getSender();
124 final ActorRef self = getSelf();
125 final YangInstanceIdentifier path = message.getPath();
126 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> future =
127 transaction.read(path);
129 future.addListener(new Runnable() {
133 Optional<NormalizedNode<?, ?>> optional = future.checkedGet();
134 if (optional.isPresent()) {
135 sender.tell(new ReadDataReply(schemaContext,optional.get()).toSerializable(), self);
137 sender.tell(new ReadDataReply(schemaContext,null).toSerializable(), self);
139 } catch (Exception e) {
140 ShardMBeanFactory.getShardStatsMBean(shardName).incrementFailedReadTransactionsCount();
141 sender.tell(new akka.actor.Status.Failure(e), self);
145 }, getContext().dispatcher());
148 protected void dataExists(DOMStoreReadTransaction transaction, DataExists message) {
149 final YangInstanceIdentifier path = message.getPath();
152 Boolean exists = transaction.exists(path).checkedGet();
153 getSender().tell(new DataExistsReply(exists).toSerializable(), getSelf());
154 } catch (ReadFailedException e) {
155 getSender().tell(new akka.actor.Status.Failure(e),getSelf());
160 protected void writeData(DOMStoreWriteTransaction transaction, WriteData message) {
161 modification.addModification(
162 new WriteModification(message.getPath(), message.getData(),schemaContext));
163 LOG.debug("writeData at path : " + message.getPath().toString());
166 transaction.write(message.getPath(), message.getData());
167 getSender().tell(new WriteDataReply().toSerializable(), getSelf());
169 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
173 protected void mergeData(DOMStoreWriteTransaction transaction, MergeData message) {
174 modification.addModification(
175 new MergeModification(message.getPath(), message.getData(), schemaContext));
176 LOG.debug("mergeData at path : " + message.getPath().toString());
178 transaction.merge(message.getPath(), message.getData());
179 getSender().tell(new MergeDataReply().toSerializable(), getSelf());
181 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
185 protected void deleteData(DOMStoreWriteTransaction transaction, DeleteData message) {
186 LOG.debug("deleteData at path : " + message.getPath().toString());
187 modification.addModification(new DeleteModification(message.getPath()));
189 transaction.delete(message.getPath());
190 getSender().tell(new DeleteDataReply().toSerializable(), getSelf());
192 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
196 protected void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message) {
197 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
198 ActorRef cohortActor = getContext().actorOf(
199 ThreePhaseCommitCohort.props(cohort, shardActor, modification, shardName), "cohort");
201 .tell(new ReadyTransactionReply(cohortActor.path()).toSerializable(), getSelf());
205 private static class ShardTransactionCreator implements Creator<ShardTransaction> {
207 private static final long serialVersionUID = 1L;
209 final DOMStoreTransaction transaction;
210 final ActorRef shardActor;
211 final SchemaContext schemaContext;
212 final DatastoreContext datastoreContext;
213 final String shardName;
215 ShardTransactionCreator(DOMStoreTransaction transaction, ActorRef shardActor,
216 SchemaContext schemaContext, DatastoreContext datastoreContext, String shardName) {
217 this.transaction = transaction;
218 this.shardActor = shardActor;
219 this.shardName = shardName;
220 this.schemaContext = schemaContext;
221 this.datastoreContext = datastoreContext;
225 public ShardTransaction create() throws Exception {
227 if(transaction instanceof DOMStoreReadWriteTransaction) {
228 tx = new ShardReadWriteTransaction((DOMStoreReadWriteTransaction)transaction,
229 shardActor, schemaContext, shardName);
230 } else if(transaction instanceof DOMStoreReadTransaction) {
231 tx = new ShardReadTransaction((DOMStoreReadTransaction)transaction, shardActor,
232 schemaContext, shardName);
234 tx = new ShardWriteTransaction((DOMStoreWriteTransaction)transaction,
235 shardActor, schemaContext, shardName);
238 tx.getContext().setReceiveTimeout(datastoreContext.getShardTransactionIdleTimeout());
243 // These classes are in here for test purposes only
245 static class GetCompositedModification {
249 static class GetCompositeModificationReply {
250 private final CompositeModification modification;
253 GetCompositeModificationReply(CompositeModification modification) {
254 this.modification = modification;
258 public CompositeModification getModification() {