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.DeleteData;
19 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
20 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
22 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
23 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
24 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
25 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
26 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
27 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
28 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
29 import org.opendaylight.controller.cluster.datastore.modification.Modification;
30 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
31 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
40 public class ShardWriteTransaction extends ShardTransaction {
42 private final MutableCompositeModification compositeModification = new MutableCompositeModification();
43 private int totalBatchedModificationsReceived;
44 private Exception lastBatchedModificationsException;
45 private final DOMStoreWriteTransaction transaction;
47 public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor,
48 ShardStats shardStats, String transactionID, short clientTxVersion) {
49 super(shardActor, shardStats, transactionID, clientTxVersion);
50 this.transaction = transaction;
54 protected DOMStoreTransaction getDOMStoreTransaction() {
59 public void handleReceive(Object message) throws Exception {
61 if (message instanceof BatchedModifications) {
62 batchedModifications((BatchedModifications)message);
63 } else if (message instanceof ReadyTransaction) {
64 readyTransaction(transaction, !SERIALIZED_REPLY);
65 } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
66 readyTransaction(transaction, SERIALIZED_REPLY);
67 } else if(WriteData.isSerializedType(message)) {
68 writeData(transaction, WriteData.fromSerializable(message), SERIALIZED_REPLY);
70 } else if(MergeData.isSerializedType(message)) {
71 mergeData(transaction, MergeData.fromSerializable(message), SERIALIZED_REPLY);
73 } else if(DeleteData.isSerializedType(message)) {
74 deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
76 } else if (message instanceof GetCompositedModification) {
77 // This is here for testing only
78 getSender().tell(new GetCompositeModificationReply(compositeModification), getSelf());
80 super.handleReceive(message);
84 private void batchedModifications(BatchedModifications batched) {
86 for(Modification modification: batched.getModifications()) {
87 compositeModification.addModification(modification);
88 modification.apply(transaction);
91 totalBatchedModificationsReceived++;
92 if(batched.isReady()) {
93 if(lastBatchedModificationsException != null) {
94 throw lastBatchedModificationsException;
97 if(totalBatchedModificationsReceived != batched.getTotalMessagesSent()) {
98 throw new IllegalStateException(String.format(
99 "The total number of batched messages received %d does not match the number sent %d",
100 totalBatchedModificationsReceived, batched.getTotalMessagesSent()));
103 readyTransaction(transaction, false);
105 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
107 } catch (Exception e) {
108 lastBatchedModificationsException = e;
109 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
111 if(batched.isReady()) {
112 getSelf().tell(PoisonPill.getInstance(), getSelf());
117 private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
118 boolean returnSerialized) {
119 LOG.debug("writeData at path : {}", message.getPath());
121 compositeModification.addModification(
122 new WriteModification(message.getPath(), message.getData()));
124 transaction.write(message.getPath(), message.getData());
125 WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
126 getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
127 writeDataReply, getSelf());
129 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
133 private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
134 boolean returnSerialized) {
135 LOG.debug("mergeData at path : {}", message.getPath());
137 compositeModification.addModification(
138 new MergeModification(message.getPath(), message.getData()));
141 transaction.merge(message.getPath(), message.getData());
142 MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
143 getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
144 mergeDataReply, getSelf());
146 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
150 private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
151 boolean returnSerialized) {
152 LOG.debug("deleteData at path : {}", message.getPath());
154 compositeModification.addModification(new DeleteModification(message.getPath()));
156 transaction.delete(message.getPath());
157 DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
158 getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
159 deleteDataReply, getSelf());
161 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
165 private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
166 String transactionID = getTransactionID();
168 LOG.debug("readyTransaction : {}", transactionID);
170 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
172 getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
173 cohort, compositeModification, returnSerialized), getContext());
175 // The shard will handle the commit from here so we're no longer needed - self-destruct.
176 getSelf().tell(PoisonPill.getInstance(), getSelf());
179 // These classes are in here for test purposes only
181 static class GetCompositedModification {
184 static class GetCompositeModificationReply {
185 private final CompositeModification modification;
188 GetCompositeModificationReply(CompositeModification modification) {
189 this.modification = modification;
192 public CompositeModification getModification() {