0823c902ae4947ede13b3807a70a04cc46546704
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / SingleCommitCohortProxy.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.dispatch.OnComplete;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.util.Arrays;
14 import java.util.List;
15 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import scala.concurrent.Future;
20
21 /**
22  * A cohort proxy implementation for a single-shard transaction commit. If the transaction was a direct commit
23  * to the shard, this implementation elides the CanCommitTransaction and CommitTransaction messages to the
24  * shard as an optimization. Otherwise the 3-phase commit to the shard is delegated to a
25  * ThreePhaseCommitCohortProxy instance (this is for backwards compatibility with pre-Lithium versions).
26  *
27  * @author Thomas Pantelis
28  */
29 class SingleCommitCohortProxy extends AbstractThreePhaseCommitCohort<Object> {
30     private static final Logger LOG = LoggerFactory.getLogger(SingleCommitCohortProxy.class);
31
32     private final ActorContext actorContext;
33     private final Future<Object> cohortFuture;
34     private final String transactionId;
35     private volatile DOMStoreThreePhaseCommitCohort delegateCohort = NoOpDOMStoreThreePhaseCommitCohort.INSTANCE;
36     private final OperationCallback.Reference operationCallbackRef;
37
38     SingleCommitCohortProxy(ActorContext actorContext, Future<Object> cohortFuture, String transactionId,
39             OperationCallback.Reference operationCallbackRef) {
40         this.actorContext = actorContext;
41         this.cohortFuture = cohortFuture;
42         this.transactionId = transactionId;
43         this.operationCallbackRef = operationCallbackRef;
44     }
45
46     @Override
47     public ListenableFuture<Boolean> canCommit() {
48         LOG.debug("Tx {} canCommit", transactionId);
49
50         final SettableFuture<Boolean> returnFuture = SettableFuture.create();
51
52         cohortFuture.onComplete(new OnComplete<Object>() {
53             @Override
54             public void onComplete(Throwable failure, Object cohortResponse) {
55                 if(failure != null) {
56                     operationCallbackRef.get().failure();
57                     returnFuture.setException(failure);
58                     return;
59                 }
60
61                 operationCallbackRef.get().success();
62
63                 LOG.debug("Tx {} successfully completed direct commit", transactionId);
64
65                 // The Future was the result of a direct commit to the shard, essentially eliding the
66                 // front-end 3PC coordination. We don't really care about the specific Future
67                 // response object, only that it completed successfully. At this point the Tx is complete
68                 // so return true. The subsequent preCommit and commit phases will be no-ops, ie return
69                 // immediate success, to complete the 3PC for the front-end.
70                 returnFuture.set(Boolean.TRUE);
71             }
72         }, actorContext.getClientDispatcher());
73
74         return returnFuture;
75     }
76
77     @Override
78     public ListenableFuture<Void> preCommit() {
79         return delegateCohort.preCommit();
80     }
81
82     @Override
83     public ListenableFuture<Void> abort() {
84         return delegateCohort.abort();
85     }
86
87     @Override
88     public ListenableFuture<Void> commit() {
89         return delegateCohort.commit();
90     }
91
92     @Override
93     List<Future<Object>> getCohortFutures() {
94         return Arrays.asList(cohortFuture);
95     }
96 }