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
10 package org.opendaylight.controller.cluster.datastore;
12 import akka.actor.ActorRef;
13 import akka.actor.PoisonPill;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
16 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
17 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
18 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
20 import org.opendaylight.controller.cluster.datastore.modification.Modification;
23 * Actor for a shard write-only transaction.
27 public class ShardWriteTransaction extends ShardTransaction {
29 private int totalBatchedModificationsReceived;
30 private Exception lastBatchedModificationsException;
31 private final ReadWriteShardDataTreeTransaction transaction;
33 public ShardWriteTransaction(final ReadWriteShardDataTreeTransaction transaction, final ActorRef shardActor,
34 final ShardStats shardStats) {
35 super(shardActor, shardStats, transaction.getIdentifier());
36 this.transaction = transaction;
40 protected ReadWriteShardDataTreeTransaction getDOMStoreTransaction() {
45 public void handleReceive(final Object message) {
46 if (message instanceof BatchedModifications) {
47 batchedModifications((BatchedModifications)message);
49 super.handleReceive(message);
53 @SuppressWarnings("checkstyle:IllegalCatch")
54 private void batchedModifications(final BatchedModifications batched) {
56 if (batched.isReady()) {
57 getSelf().tell(PoisonPill.getInstance(), getSelf());
63 for (Modification modification: batched.getModifications()) {
64 modification.apply(transaction.getSnapshot());
67 totalBatchedModificationsReceived++;
68 if (batched.isReady()) {
69 if (lastBatchedModificationsException != null) {
70 throw lastBatchedModificationsException;
73 if (totalBatchedModificationsReceived != batched.getTotalMessagesSent()) {
74 throw new IllegalStateException(String.format(
75 "The total number of batched messages received %d does not match the number sent %d",
76 totalBatchedModificationsReceived, batched.getTotalMessagesSent()));
79 readyTransaction(batched);
81 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
83 } catch (Exception e) {
84 lastBatchedModificationsException = e;
85 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
87 if (batched.isReady()) {
88 getSelf().tell(PoisonPill.getInstance(), getSelf());
93 protected final void dataExists(final DataExists message) {
94 super.dataExists(transaction, message);
97 protected final void readData(final ReadData message) {
98 super.readData(transaction, message);
101 private boolean checkClosed() {
102 final boolean ret = transaction.isClosed();
104 getSender().tell(new akka.actor.Status.Failure(new IllegalStateException(
105 "Transaction is closed, no modifications allowed")), getSelf());
110 private void readyTransaction(final BatchedModifications batched) {
111 TransactionIdentifier transactionID = getTransactionId();
113 LOG.debug("readyTransaction : {}", transactionID);
115 getShardActor().forward(new ForwardedReadyTransaction(transactionID, batched.getVersion(),
116 transaction, batched.isDoCommitOnReady(), batched.getParticipatingShardNames()), getContext());
118 // The shard will handle the commit from here so we're no longer needed - self-destruct.
119 getSelf().tell(PoisonPill.getInstance(), getSelf());