Merge "Improve performance of XmlElement.getName"
[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.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;
23
24 /**
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).
29  *
30  * @author Thomas Pantelis
31  */
32 class SingleCommitCohortProxy extends AbstractThreePhaseCommitCohort<Object> {
33     private static final Logger LOG = LoggerFactory.getLogger(SingleCommitCohortProxy.class);
34
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;
40
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;
47     }
48
49     @Override
50     public ListenableFuture<Boolean> canCommit() {
51         LOG.debug("Tx {} canCommit", transactionId);
52
53         final SettableFuture<Boolean> returnFuture = SettableFuture.create();
54
55         cohortFuture.onComplete(new OnComplete<Object>() {
56             @Override
57             public void onComplete(Throwable failure, Object cohortResponse) {
58                 if(failure != null) {
59                     operationCallbackRef.get().failure();
60                     returnFuture.setException(failure);
61                     return;
62                 }
63
64                 operationCallbackRef.get().success();
65
66                 if(cohortResponse instanceof ActorSelection) {
67                     handlePreLithiumActorCohort((ActorSelection)cohortResponse, returnFuture);
68                     return;
69                 }
70
71                 LOG.debug("Tx {} successfully completed direct commit", transactionId);
72
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);
79             }
80         }, actorContext.getClientDispatcher());
81
82         return returnFuture;
83     }
84
85     @Override
86     public ListenableFuture<Void> preCommit() {
87         return delegateCohort.preCommit();
88     }
89
90     @Override
91     public ListenableFuture<Void> abort() {
92         return delegateCohort.abort();
93     }
94
95     @Override
96     public ListenableFuture<Void> commit() {
97         return delegateCohort.commit();
98     }
99
100     @Override
101     List<Future<Object>> getCohortFutures() {
102         return Arrays.asList(cohortFuture);
103     }
104
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>() {
111             @Override
112             public void onSuccess(Boolean canCommit) {
113                 returnFuture.set(canCommit);
114             }
115
116             @Override
117             public void onFailure(Throwable t) {
118                 returnFuture.setException(t);
119             }
120         });
121     }
122 }