2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
11 package org.opendaylight.controller.cluster.datastore;
13 import akka.actor.ActorRef;
14 import akka.actor.PoisonPill;
15 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
16 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
17 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
18 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
19 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
20 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
21 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
23 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
24 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
26 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
27 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
28 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
29 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
30 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
31 import org.opendaylight.controller.cluster.datastore.modification.Modification;
32 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
33 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
39 public class ShardWriteTransaction extends ShardTransaction {
41 private final MutableCompositeModification compositeModification = new MutableCompositeModification();
42 private int totalBatchedModificationsReceived;
43 private Exception lastBatchedModificationsException;
44 private final ReadWriteShardDataTreeTransaction transaction;
46 public ShardWriteTransaction(ReadWriteShardDataTreeTransaction transaction, ActorRef shardActor,
47 ShardStats shardStats, String transactionID, short clientTxVersion) {
48 super(shardActor, shardStats, transactionID, clientTxVersion);
49 this.transaction = transaction;
53 protected ReadWriteShardDataTreeTransaction getDOMStoreTransaction() {
58 public void handleReceive(Object message) throws Exception {
60 if (message instanceof BatchedModifications) {
61 batchedModifications((BatchedModifications)message);
62 } else if (message instanceof ReadyTransaction) {
63 readyTransaction(!SERIALIZED_REPLY, false);
64 } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
65 readyTransaction(SERIALIZED_REPLY, false);
66 } else if(WriteData.isSerializedType(message)) {
67 writeData(WriteData.fromSerializable(message), SERIALIZED_REPLY);
69 } else if(MergeData.isSerializedType(message)) {
70 mergeData(MergeData.fromSerializable(message), SERIALIZED_REPLY);
72 } else if(DeleteData.isSerializedType(message)) {
73 deleteData(DeleteData.fromSerializable(message), SERIALIZED_REPLY);
75 } else if (message instanceof GetCompositedModification) {
76 // This is here for testing only
77 getSender().tell(new GetCompositeModificationReply(compositeModification), getSelf());
79 super.handleReceive(message);
83 private void batchedModifications(BatchedModifications batched) {
85 if (batched.isReady()) {
86 getSelf().tell(PoisonPill.getInstance(), getSelf());
92 for(Modification modification: batched.getModifications()) {
93 compositeModification.addModification(modification);
94 modification.apply(transaction.getSnapshot());
97 totalBatchedModificationsReceived++;
98 if(batched.isReady()) {
99 if(lastBatchedModificationsException != null) {
100 throw lastBatchedModificationsException;
103 if(totalBatchedModificationsReceived != batched.getTotalMessagesSent()) {
104 throw new IllegalStateException(String.format(
105 "The total number of batched messages received %d does not match the number sent %d",
106 totalBatchedModificationsReceived, batched.getTotalMessagesSent()));
109 readyTransaction(false, batched.isDoCommitOnReady());
111 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
113 } catch (Exception e) {
114 lastBatchedModificationsException = e;
115 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
117 if(batched.isReady()) {
118 getSelf().tell(PoisonPill.getInstance(), getSelf());
123 protected final void dataExists(DataExists message, final boolean returnSerialized) {
124 super.dataExists(transaction, message, returnSerialized);
127 protected final void readData(ReadData message, final boolean returnSerialized) {
128 super.readData(transaction, message, returnSerialized);
131 private boolean checkClosed() {
132 if (transaction.isClosed()) {
133 getSender().tell(new akka.actor.Status.Failure(new IllegalStateException("Transaction is closed, no modifications allowed")), getSelf());
140 private void writeData(WriteData message, boolean returnSerialized) {
141 LOG.debug("writeData at path : {}", message.getPath());
146 compositeModification.addModification(
147 new WriteModification(message.getPath(), message.getData()));
149 transaction.getSnapshot().write(message.getPath(), message.getData());
150 WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
151 getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
152 writeDataReply, getSelf());
154 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
158 private void mergeData(MergeData message, boolean returnSerialized) {
159 LOG.debug("mergeData at path : {}", message.getPath());
164 compositeModification.addModification(
165 new MergeModification(message.getPath(), message.getData()));
168 transaction.getSnapshot().merge(message.getPath(), message.getData());
169 MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
170 getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
171 mergeDataReply, getSelf());
173 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
177 private void deleteData(DeleteData message, boolean returnSerialized) {
178 LOG.debug("deleteData at path : {}", message.getPath());
183 compositeModification.addModification(new DeleteModification(message.getPath()));
185 transaction.getSnapshot().delete(message.getPath());
186 DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
187 getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
188 deleteDataReply, getSelf());
189 } catch(Exception e) {
190 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
194 private void readyTransaction(boolean returnSerialized, boolean doImmediateCommit) {
195 String transactionID = getTransactionID();
197 LOG.debug("readyTransaction : {}", transactionID);
199 ShardDataTreeCohort cohort = transaction.ready();
201 getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
202 cohort, compositeModification, returnSerialized, doImmediateCommit), getContext());
204 // The shard will handle the commit from here so we're no longer needed - self-destruct.
205 getSelf().tell(PoisonPill.getInstance(), getSelf());
208 // These classes are in here for test purposes only
210 static class GetCompositedModification {
213 static class GetCompositeModificationReply {
214 private final CompositeModification modification;
217 GetCompositeModificationReply(CompositeModification modification) {
218 this.modification = modification;
221 public CompositeModification getModification() {