Merge "Fix for possible NPE if Bundle is stopped."
[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 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
22 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
23 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.ForwardedCommitTransaction;
26 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
27 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
28 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
30
31 public class ThreePhaseCommitCohort extends AbstractUntypedActor {
32     private final DOMStoreThreePhaseCommitCohort cohort;
33     private final ActorRef shardActor;
34     private final CompositeModification modification;
35
36     public ThreePhaseCommitCohort(DOMStoreThreePhaseCommitCohort cohort,
37         ActorRef shardActor, CompositeModification modification) {
38
39         this.cohort = cohort;
40         this.shardActor = shardActor;
41         this.modification = modification;
42     }
43
44     private final LoggingAdapter log =
45         Logging.getLogger(getContext().system(), this);
46
47     public static Props props(final DOMStoreThreePhaseCommitCohort cohort,
48         final ActorRef shardActor, final CompositeModification modification) {
49         return Props.create(new Creator<ThreePhaseCommitCohort>() {
50             @Override
51             public ThreePhaseCommitCohort create() throws Exception {
52                 return new ThreePhaseCommitCohort(cohort, shardActor,
53                     modification);
54             }
55         });
56     }
57
58
59     @Override
60     public void handleReceive(Object message) throws Exception {
61         if (message.getClass()
62             .equals(CanCommitTransaction.SERIALIZABLE_CLASS)) {
63             canCommit(new CanCommitTransaction());
64         } else if (message.getClass()
65             .equals(PreCommitTransaction.SERIALIZABLE_CLASS)) {
66             preCommit(new PreCommitTransaction());
67         } else if (message.getClass()
68             .equals(CommitTransaction.SERIALIZABLE_CLASS)) {
69             commit(new CommitTransaction());
70         } else if (message.getClass()
71             .equals(AbortTransaction.SERIALIZABLE_CLASS)) {
72             abort(new AbortTransaction());
73         } else {
74             unknownMessage(message);
75         }
76     }
77
78     private void abort(AbortTransaction message) {
79         final ListenableFuture<Void> future = cohort.abort();
80         final ActorRef sender = getSender();
81         final ActorRef self = getSelf();
82
83         Futures.addCallback(future, new FutureCallback<Void>() {
84             public void onSuccess(Void v) {
85                 sender
86                     .tell(new AbortTransactionReply().toSerializable(),
87                         self);
88             }
89
90             public void onFailure(Throwable t) {
91                 LOG.error(t, "An exception happened during abort");
92                 sender
93                     .tell(new akka.actor.Status.Failure(t), self);
94             }
95         });
96     }
97
98     private void commit(CommitTransaction message) {
99         // Forward the commit to the shard
100         log.debug("Forward commit transaction to Shard {} ", shardActor);
101         shardActor.forward(new ForwardedCommitTransaction(cohort, modification),
102             getContext());
103
104         getContext().parent().tell(PoisonPill.getInstance(), getSelf());
105
106     }
107
108     private void preCommit(PreCommitTransaction message) {
109         final ListenableFuture<Void> future = cohort.preCommit();
110         final ActorRef sender = getSender();
111         final ActorRef self = getSelf();
112         Futures.addCallback(future, new FutureCallback<Void>() {
113             public void onSuccess(Void v) {
114                 sender
115                     .tell(new PreCommitTransactionReply().toSerializable(),
116                         self);
117             }
118
119             public void onFailure(Throwable t) {
120                 LOG.error(t, "An exception happened during pre-commit");
121                 sender
122                     .tell(new akka.actor.Status.Failure(t), self);
123             }
124         });
125
126     }
127
128     private void canCommit(CanCommitTransaction message) {
129         final ListenableFuture<Boolean> future = cohort.canCommit();
130         final ActorRef sender = getSender();
131         final ActorRef self = getSelf();
132         Futures.addCallback(future, new FutureCallback<Boolean>() {
133             public void onSuccess(Boolean canCommit) {
134                 sender.tell(new CanCommitTransactionReply(canCommit)
135                     .toSerializable(), self);
136             }
137
138             public void onFailure(Throwable t) {
139                 LOG.error(t, "An exception happened during canCommit");
140                 sender
141                     .tell(new akka.actor.Status.Failure(t), self);
142             }
143         });
144
145
146     }
147 }