2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
18 import com.google.common.util.concurrent.FutureCallback;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
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;
33 public class ThreePhaseCommitCohort extends AbstractUntypedActor {
34 private final DOMStoreThreePhaseCommitCohort cohort;
35 private final ActorRef shardActor;
36 private final CompositeModification modification;
38 public ThreePhaseCommitCohort(DOMStoreThreePhaseCommitCohort cohort,
39 ActorRef shardActor, CompositeModification modification) {
42 this.shardActor = shardActor;
43 this.modification = modification;
46 private final LoggingAdapter log =
47 Logging.getLogger(getContext().system(), this);
49 public static Props props(final DOMStoreThreePhaseCommitCohort cohort,
50 final ActorRef shardActor, final CompositeModification modification) {
51 return Props.create(new ThreePhaseCommitCohortCreator(cohort, shardActor, modification));
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());
69 unknownMessage(message);
73 private void abort(AbortTransaction message) {
74 final ListenableFuture<Void> future = cohort.abort();
75 final ActorRef sender = getSender();
76 final ActorRef self = getSelf();
78 Futures.addCallback(future, new FutureCallback<Void>() {
80 public void onSuccess(Void v) {
82 .tell(new AbortTransactionReply().toSerializable(),
87 public void onFailure(Throwable t) {
88 LOG.error(t, "An exception happened during abort");
90 .tell(new akka.actor.Status.Failure(t), self);
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),
101 getContext().parent().tell(PoisonPill.getInstance(), getSelf());
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>() {
111 public void onSuccess(Void v) {
113 .tell(new PreCommitTransactionReply().toSerializable(),
118 public void onFailure(Throwable t) {
119 LOG.error(t, "An exception happened during pre-commit");
121 .tell(new akka.actor.Status.Failure(t), self);
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>() {
133 public void onSuccess(Boolean canCommit) {
134 sender.tell(new CanCommitTransactionReply(canCommit)
135 .toSerializable(), self);
139 public void onFailure(Throwable t) {
140 LOG.error(t, "An exception happened during canCommit");
142 .tell(new akka.actor.Status.Failure(t), self);
147 private static class ThreePhaseCommitCohortCreator implements Creator<ThreePhaseCommitCohort> {
148 final DOMStoreThreePhaseCommitCohort cohort;
149 final ActorRef shardActor;
150 final CompositeModification modification;
152 ThreePhaseCommitCohortCreator(DOMStoreThreePhaseCommitCohort cohort,
153 ActorRef shardActor, CompositeModification modification) {
154 this.cohort = cohort;
155 this.shardActor = shardActor;
156 this.modification = modification;
160 public ThreePhaseCommitCohort create() throws Exception {
161 return new ThreePhaseCommitCohort(cohort, shardActor, modification);