Metrics and Configuration
[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         log.debug("Forward commit transaction to Shard {} ", shardActor);
105         shardActor.forward(new ForwardedCommitTransaction(cohort, modification),
106             getContext());
107
108         getContext().parent().tell(PoisonPill.getInstance(), getSelf());
109
110     }
111
112     private void preCommit(PreCommitTransaction message) {
113         final ListenableFuture<Void> future = cohort.preCommit();
114         final ActorRef sender = getSender();
115         final ActorRef self = getSelf();
116         Futures.addCallback(future, new FutureCallback<Void>() {
117             @Override
118             public void onSuccess(Void v) {
119                 sender
120                     .tell(new PreCommitTransactionReply().toSerializable(),
121                         self);
122             }
123
124             @Override
125             public void onFailure(Throwable t) {
126                 LOG.error(t, "An exception happened during pre-commit");
127                 sender
128                     .tell(new akka.actor.Status.Failure(t), self);
129             }
130         });
131
132     }
133
134     private void canCommit(CanCommitTransaction message) {
135         final ListenableFuture<Boolean> future = cohort.canCommit();
136         final ActorRef sender = getSender();
137         final ActorRef self = getSelf();
138         Futures.addCallback(future, new FutureCallback<Boolean>() {
139             @Override
140             public void onSuccess(Boolean canCommit) {
141                 sender.tell(new CanCommitTransactionReply(canCommit)
142                     .toSerializable(), self);
143             }
144
145             @Override
146             public void onFailure(Throwable t) {
147                 LOG.error(t, "An exception happened during canCommit");
148                 sender
149                     .tell(new akka.actor.Status.Failure(t), self);
150             }
151         });
152     }
153
154     private static class ThreePhaseCommitCohortCreator implements Creator<ThreePhaseCommitCohort> {
155         final DOMStoreThreePhaseCommitCohort cohort;
156         final ActorRef shardActor;
157         final CompositeModification modification;
158         final ShardStats shardStats;
159
160         ThreePhaseCommitCohortCreator(DOMStoreThreePhaseCommitCohort cohort,
161             ActorRef shardActor, CompositeModification modification, ShardStats shardStats) {
162             this.cohort = cohort;
163             this.shardActor = shardActor;
164             this.modification = modification;
165             this.shardStats = shardStats;
166         }
167
168         @Override
169         public ThreePhaseCommitCohort create() throws Exception {
170             return new ThreePhaseCommitCohort(cohort, shardActor, modification, shardStats);
171         }
172     }
173 }