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