Merge "Bug 1965: Fixed DataChangedReply sent to deadletters"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ThreePhaseCommitCohort.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorRef;
12 import akka.actor.PoisonPill;
13 import akka.actor.Props;
14 import akka.event.Logging;
15 import akka.event.LoggingAdapter;
16 import akka.japi.Creator;
17
18 import com.google.common.util.concurrent.FutureCallback;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
22
23 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
24 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
26 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
27 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
28 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
29 import org.opendaylight.controller.cluster.datastore.messages.ForwardedCommitTransaction;
30 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
31 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
32 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
34
35 public class ThreePhaseCommitCohort extends AbstractUntypedActor {
36     private final DOMStoreThreePhaseCommitCohort cohort;
37     private final ActorRef shardActor;
38     private final CompositeModification modification;
39     private final ShardStats shardStats;
40
41     public ThreePhaseCommitCohort(DOMStoreThreePhaseCommitCohort cohort,
42         ActorRef shardActor, CompositeModification modification, ShardStats shardStats) {
43
44         this.cohort = cohort;
45         this.shardActor = shardActor;
46         this.modification = modification;
47         this.shardStats = shardStats;
48     }
49
50     private final LoggingAdapter log =
51         Logging.getLogger(getContext().system(), this);
52
53     public static Props props(final DOMStoreThreePhaseCommitCohort cohort,
54             final ActorRef shardActor, final CompositeModification modification,
55             ShardStats shardStats) {
56         return Props.create(new ThreePhaseCommitCohortCreator(cohort, shardActor, modification,
57                 shardStats));
58     }
59
60     @Override
61     public void handleReceive(Object message) throws Exception {
62         if (message.getClass()
63             .equals(CanCommitTransaction.SERIALIZABLE_CLASS)) {
64             canCommit(new CanCommitTransaction());
65         } else if (message.getClass()
66             .equals(PreCommitTransaction.SERIALIZABLE_CLASS)) {
67             preCommit(new PreCommitTransaction());
68         } else if (message.getClass()
69             .equals(CommitTransaction.SERIALIZABLE_CLASS)) {
70             commit(new CommitTransaction());
71         } else if (message.getClass()
72             .equals(AbortTransaction.SERIALIZABLE_CLASS)) {
73             abort(new AbortTransaction());
74         } else {
75             unknownMessage(message);
76         }
77     }
78
79     private void abort(AbortTransaction message) {
80         final ListenableFuture<Void> future = cohort.abort();
81         final ActorRef sender = getSender();
82         final ActorRef self = getSelf();
83
84         Futures.addCallback(future, new FutureCallback<Void>() {
85             @Override
86             public void onSuccess(Void v) {
87                 shardStats.incrementAbortTransactionsCount();
88                 sender
89                     .tell(new AbortTransactionReply().toSerializable(),
90                     self);
91             }
92
93             @Override
94             public void onFailure(Throwable t) {
95                 LOG.error(t, "An exception happened during abort");
96                 sender
97                     .tell(new akka.actor.Status.Failure(t), self);
98             }
99         });
100     }
101
102     private void commit(CommitTransaction message) {
103         // Forward the commit to the shard
104         if(log.isDebugEnabled()) {
105             log.debug("Forward commit transaction to Shard {} ", shardActor);
106         }
107         shardActor.forward(new ForwardedCommitTransaction(cohort, modification),
108             getContext());
109
110         getContext().parent().tell(PoisonPill.getInstance(), getSelf());
111
112     }
113
114     private void preCommit(PreCommitTransaction message) {
115         final ListenableFuture<Void> future = cohort.preCommit();
116         final ActorRef sender = getSender();
117         final ActorRef self = getSelf();
118         Futures.addCallback(future, new FutureCallback<Void>() {
119             @Override
120             public void onSuccess(Void v) {
121                 sender
122                     .tell(new PreCommitTransactionReply().toSerializable(),
123                         self);
124             }
125
126             @Override
127             public void onFailure(Throwable t) {
128                 LOG.error(t, "An exception happened during pre-commit");
129                 sender
130                     .tell(new akka.actor.Status.Failure(t), self);
131             }
132         });
133
134     }
135
136     private void canCommit(CanCommitTransaction message) {
137         final ListenableFuture<Boolean> future = cohort.canCommit();
138         final ActorRef sender = getSender();
139         final ActorRef self = getSelf();
140         Futures.addCallback(future, new FutureCallback<Boolean>() {
141             @Override
142             public void onSuccess(Boolean canCommit) {
143                 sender.tell(new CanCommitTransactionReply(canCommit)
144                     .toSerializable(), self);
145             }
146
147             @Override
148             public void onFailure(Throwable t) {
149                 LOG.error(t, "An exception happened during canCommit");
150                 sender
151                     .tell(new akka.actor.Status.Failure(t), self);
152             }
153         });
154     }
155
156     private static class ThreePhaseCommitCohortCreator implements Creator<ThreePhaseCommitCohort> {
157         final DOMStoreThreePhaseCommitCohort cohort;
158         final ActorRef shardActor;
159         final CompositeModification modification;
160         final ShardStats shardStats;
161
162         ThreePhaseCommitCohortCreator(DOMStoreThreePhaseCommitCohort cohort,
163             ActorRef shardActor, CompositeModification modification, ShardStats shardStats) {
164             this.cohort = cohort;
165             this.shardActor = shardActor;
166             this.modification = modification;
167             this.shardStats = shardStats;
168         }
169
170         @Override
171         public ThreePhaseCommitCohort create() throws Exception {
172             return new ThreePhaseCommitCohort(cohort, shardActor, modification, shardStats);
173         }
174     }
175 }