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;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 public class ShardWriteTransaction extends ShardTransaction {
43 private final MutableCompositeModification compositeModification = new MutableCompositeModification();
44 private int totalBatchedModificationsReceived;
45 private Exception lastBatchedModificationsException;
46 private final DOMStoreWriteTransaction transaction;
48 public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor,
49 SchemaContext schemaContext, ShardStats shardStats, String transactionID,
50 short clientTxVersion) {
51 super(shardActor, schemaContext, shardStats, transactionID, clientTxVersion);
52 this.transaction = transaction;
56 protected DOMStoreTransaction getDOMStoreTransaction() {
61 public void handleReceive(Object message) throws Exception {
63 if (message instanceof BatchedModifications) {
64 batchedModifications((BatchedModifications)message);
65 } else if (message instanceof ReadyTransaction) {
66 readyTransaction(transaction, !SERIALIZED_REPLY);
67 } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
68 readyTransaction(transaction, SERIALIZED_REPLY);
69 } else if(WriteData.isSerializedType(message)) {
70 writeData(transaction, WriteData.fromSerializable(message), SERIALIZED_REPLY);
72 } else if(MergeData.isSerializedType(message)) {
73 mergeData(transaction, MergeData.fromSerializable(message), SERIALIZED_REPLY);
75 } else if(DeleteData.isSerializedType(message)) {
76 deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
78 } else if (message instanceof GetCompositedModification) {
79 // This is here for testing only
80 getSender().tell(new GetCompositeModificationReply(compositeModification), getSelf());
82 super.handleReceive(message);
86 private void batchedModifications(BatchedModifications batched) {
88 for(Modification modification: batched.getModifications()) {
89 compositeModification.addModification(modification);
90 modification.apply(transaction);
93 totalBatchedModificationsReceived++;
94 if(batched.isReady()) {
95 if(lastBatchedModificationsException != null) {
96 throw lastBatchedModificationsException;
99 if(totalBatchedModificationsReceived != batched.getTotalMessagesSent()) {
100 throw new IllegalStateException(String.format(
101 "The total number of batched messages received %d does not match the number sent %d",
102 totalBatchedModificationsReceived, batched.getTotalMessagesSent()));
105 readyTransaction(transaction, false);
107 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
109 } catch (Exception e) {
110 lastBatchedModificationsException = e;
111 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
113 if(batched.isReady()) {
114 getSelf().tell(PoisonPill.getInstance(), getSelf());
119 private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
120 boolean returnSerialized) {
121 LOG.debug("writeData at path : {}", message.getPath());
123 compositeModification.addModification(
124 new WriteModification(message.getPath(), message.getData()));
126 transaction.write(message.getPath(), message.getData());
127 WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
128 getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
129 writeDataReply, getSelf());
131 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
135 private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
136 boolean returnSerialized) {
137 LOG.debug("mergeData at path : {}", message.getPath());
139 compositeModification.addModification(
140 new MergeModification(message.getPath(), message.getData()));
143 transaction.merge(message.getPath(), message.getData());
144 MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
145 getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
146 mergeDataReply, getSelf());
148 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
152 private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
153 boolean returnSerialized) {
154 LOG.debug("deleteData at path : {}", message.getPath());
156 compositeModification.addModification(new DeleteModification(message.getPath()));
158 transaction.delete(message.getPath());
159 DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
160 getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
161 deleteDataReply, getSelf());
163 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
167 private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
168 String transactionID = getTransactionID();
170 LOG.debug("readyTransaction : {}", transactionID);
172 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
174 getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
175 cohort, compositeModification, returnSerialized), getContext());
177 // The shard will handle the commit from here so we're no longer needed - self-destruct.
178 getSelf().tell(PoisonPill.getInstance(), getSelf());
181 // These classes are in here for test purposes only
183 static class GetCompositedModification {
186 static class GetCompositeModificationReply {
187 private final CompositeModification modification;
190 GetCompositeModificationReply(CompositeModification modification) {
191 this.modification = modification;
194 public CompositeModification getModification() {