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;
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;
31 public class ThreePhaseCommitCohort extends AbstractUntypedActor {
32 private final DOMStoreThreePhaseCommitCohort cohort;
33 private final ActorRef shardActor;
34 private final CompositeModification modification;
36 public ThreePhaseCommitCohort(DOMStoreThreePhaseCommitCohort cohort,
37 ActorRef shardActor, CompositeModification modification) {
40 this.shardActor = shardActor;
41 this.modification = modification;
44 private final LoggingAdapter log =
45 Logging.getLogger(getContext().system(), this);
47 public static Props props(final DOMStoreThreePhaseCommitCohort cohort,
48 final ActorRef shardActor, final CompositeModification modification) {
49 return Props.create(new Creator<ThreePhaseCommitCohort>() {
51 public ThreePhaseCommitCohort create() throws Exception {
52 return new ThreePhaseCommitCohort(cohort, shardActor,
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());
74 unknownMessage(message);
78 private void abort(AbortTransaction message) {
79 final ListenableFuture<Void> future = cohort.abort();
80 final ActorRef sender = getSender();
81 final ActorRef self = getSelf();
83 Futures.addCallback(future, new FutureCallback<Void>() {
84 public void onSuccess(Void v) {
86 .tell(new AbortTransactionReply().toSerializable(),
90 public void onFailure(Throwable t) {
91 LOG.error(t, "An exception happened during abort");
93 .tell(new akka.actor.Status.Failure(t), self);
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),
104 getContext().parent().tell(PoisonPill.getInstance(), getSelf());
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) {
115 .tell(new PreCommitTransactionReply().toSerializable(),
119 public void onFailure(Throwable t) {
120 LOG.error(t, "An exception happened during pre-commit");
122 .tell(new akka.actor.Status.Failure(t), self);
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);
138 public void onFailure(Throwable t) {
139 LOG.error(t, "An exception happened during canCommit");
141 .tell(new akka.actor.Status.Failure(t), self);