2 * Copyright (c) 2015 Brocade Communications 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
8 package org.opendaylight.controller.cluster.datastore;
10 import akka.actor.ActorSelection;
11 import akka.dispatch.Futures;
12 import akka.dispatch.OnComplete;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.Arrays;
17 import java.util.List;
18 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import scala.concurrent.Future;
25 * A cohort proxy implementation for a single-shard transaction commit. If the transaction was a direct commit
26 * to the shard, this implementation elides the CanCommitTransaction and CommitTransaction messages to the
27 * shard as an optimization. Otherwise the 3-phase commit to the shard is delegated to a
28 * ThreePhaseCommitCohortProxy instance (this is for backwards compatibility with pre-Lithium versions).
30 * @author Thomas Pantelis
32 class SingleCommitCohortProxy extends AbstractThreePhaseCommitCohort<Object> {
33 private static final Logger LOG = LoggerFactory.getLogger(SingleCommitCohortProxy.class);
35 private final ActorContext actorContext;
36 private final Future<Object> cohortFuture;
37 private final String transactionId;
38 private volatile DOMStoreThreePhaseCommitCohort delegateCohort = NoOpDOMStoreThreePhaseCommitCohort.INSTANCE;
39 private final OperationCallback.Reference operationCallbackRef;
41 SingleCommitCohortProxy(ActorContext actorContext, Future<Object> cohortFuture, String transactionId,
42 OperationCallback.Reference operationCallbackRef) {
43 this.actorContext = actorContext;
44 this.cohortFuture = cohortFuture;
45 this.transactionId = transactionId;
46 this.operationCallbackRef = operationCallbackRef;
50 public ListenableFuture<Boolean> canCommit() {
51 LOG.debug("Tx {} canCommit", transactionId);
53 final SettableFuture<Boolean> returnFuture = SettableFuture.create();
55 cohortFuture.onComplete(new OnComplete<Object>() {
57 public void onComplete(Throwable failure, Object cohortResponse) {
59 operationCallbackRef.get().failure();
60 returnFuture.setException(failure);
64 operationCallbackRef.get().success();
66 if(cohortResponse instanceof ActorSelection) {
67 handlePreLithiumActorCohort((ActorSelection)cohortResponse, returnFuture);
71 LOG.debug("Tx {} successfully completed direct commit", transactionId);
73 // The Future was the result of a direct commit to the shard, essentially eliding the
74 // front-end 3PC coordination. We don't really care about the specific Future
75 // response object, only that it completed successfully. At this point the Tx is complete
76 // so return true. The subsequent preCommit and commit phases will be no-ops, ie return
77 // immediate success, to complete the 3PC for the front-end.
78 returnFuture.set(Boolean.TRUE);
80 }, actorContext.getClientDispatcher());
86 public ListenableFuture<Void> preCommit() {
87 return delegateCohort.preCommit();
91 public ListenableFuture<Void> abort() {
92 return delegateCohort.abort();
96 public ListenableFuture<Void> commit() {
97 return delegateCohort.commit();
101 List<Future<Object>> getCohortFutures() {
102 return Arrays.asList(cohortFuture);
105 private void handlePreLithiumActorCohort(ActorSelection actorSelection, final SettableFuture<Boolean> returnFuture) {
106 // Handle backwards compatibility. An ActorSelection response would be returned from a
107 // pre-Lithium version. In this case delegate to a ThreePhaseCommitCohortProxy.
108 delegateCohort = new ThreePhaseCommitCohortProxy(actorContext,
109 Arrays.asList(Futures.successful(actorSelection)), transactionId);
110 com.google.common.util.concurrent.Futures.addCallback(delegateCohort.canCommit(), new FutureCallback<Boolean>() {
112 public void onSuccess(Boolean canCommit) {
113 returnFuture.set(canCommit);
117 public void onFailure(Throwable t) {
118 returnFuture.setException(t);