+public class ThreePhaseCommitCohort extends AbstractUntypedActor {
+ private final DOMStoreThreePhaseCommitCohort cohort;
+ private final ActorRef shardActor;
+ private final CompositeModification modification;
+ private final String shardName;
+
+ public ThreePhaseCommitCohort(DOMStoreThreePhaseCommitCohort cohort,
+ ActorRef shardActor, CompositeModification modification,String shardName) {
+
+ this.cohort = cohort;
+ this.shardActor = shardActor;
+ this.modification = modification;
+ this.shardName = shardName;
+ }
+
+ private final LoggingAdapter log =
+ Logging.getLogger(getContext().system(), this);
+
+ public static Props props(final DOMStoreThreePhaseCommitCohort cohort,
+ final ActorRef shardActor, final CompositeModification modification,
+ String shardName) {
+ return Props.create(new ThreePhaseCommitCohortCreator(cohort, shardActor, modification,
+ shardName));
+ }
+
+ @Override
+ public void handleReceive(Object message) throws Exception {
+ if (message.getClass()
+ .equals(CanCommitTransaction.SERIALIZABLE_CLASS)) {
+ canCommit(new CanCommitTransaction());
+ } else if (message.getClass()
+ .equals(PreCommitTransaction.SERIALIZABLE_CLASS)) {
+ preCommit(new PreCommitTransaction());
+ } else if (message.getClass()
+ .equals(CommitTransaction.SERIALIZABLE_CLASS)) {
+ commit(new CommitTransaction());
+ } else if (message.getClass()
+ .equals(AbortTransaction.SERIALIZABLE_CLASS)) {
+ abort(new AbortTransaction());
+ } else {
+ unknownMessage(message);
+ }
+ }
+
+ private void abort(AbortTransaction message) {
+ final ListenableFuture<Void> future = cohort.abort();
+ final ActorRef sender = getSender();
+ final ActorRef self = getSelf();
+
+ Futures.addCallback(future, new FutureCallback<Void>() {
+ @Override
+ public void onSuccess(Void v) {
+ ShardMBeanFactory.getShardStatsMBean(shardName).incrementAbortTransactionsCount();
+ sender
+ .tell(new AbortTransactionReply().toSerializable(),
+ self);
+ }
+
+ @Override
+ public void onFailure(Throwable t) {
+ LOG.error(t, "An exception happened during abort");
+ sender
+ .tell(new akka.actor.Status.Failure(t), self);
+ }
+ });
+ }
+
+ private void commit(CommitTransaction message) {
+ // Forward the commit to the shard
+ log.debug("Forward commit transaction to Shard {} ", shardActor);
+ shardActor.forward(new ForwardedCommitTransaction(cohort, modification),
+ getContext());
+
+ getContext().parent().tell(PoisonPill.getInstance(), getSelf());
+
+ }
+
+ private void preCommit(PreCommitTransaction message) {
+ final ListenableFuture<Void> future = cohort.preCommit();
+ final ActorRef sender = getSender();
+ final ActorRef self = getSelf();
+ Futures.addCallback(future, new FutureCallback<Void>() {
+ @Override
+ public void onSuccess(Void v) {
+ sender
+ .tell(new PreCommitTransactionReply().toSerializable(),
+ self);
+ }
+
+ @Override
+ public void onFailure(Throwable t) {
+ LOG.error(t, "An exception happened during pre-commit");
+ sender
+ .tell(new akka.actor.Status.Failure(t), self);
+ }
+ });
+
+ }
+
+ private void canCommit(CanCommitTransaction message) {
+ final ListenableFuture<Boolean> future = cohort.canCommit();
+ final ActorRef sender = getSender();
+ final ActorRef self = getSelf();
+ Futures.addCallback(future, new FutureCallback<Boolean>() {
+ @Override
+ public void onSuccess(Boolean canCommit) {
+ sender.tell(new CanCommitTransactionReply(canCommit)
+ .toSerializable(), self);
+ }