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.ActorSelection;
12 import akka.dispatch.Futures;
13 import akka.dispatch.OnComplete;
14 import com.google.common.collect.Lists;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.Collections;
18 import java.util.Iterator;
19 import java.util.List;
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.CommitTransactionReply;
26 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.Future;
30 import scala.runtime.AbstractFunction1;
33 * ThreePhaseCommitCohortProxy represents a set of remote cohort proxies
35 public class ThreePhaseCommitCohortProxy extends AbstractThreePhaseCommitCohort<ActorSelection> {
37 private static final Logger LOG = LoggerFactory.getLogger(ThreePhaseCommitCohortProxy.class);
39 private final ActorContext actorContext;
40 private final List<Future<ActorSelection>> cohortFutures;
41 private volatile List<ActorSelection> cohorts;
42 private final String transactionId;
44 public ThreePhaseCommitCohortProxy(ActorContext actorContext,
45 List<Future<ActorSelection>> cohortFutures, String transactionId) {
46 this.actorContext = actorContext;
47 this.cohortFutures = cohortFutures;
48 this.transactionId = transactionId;
51 private Future<Void> buildCohortList() {
53 Future<Iterable<ActorSelection>> combinedFutures = Futures.sequence(cohortFutures,
54 actorContext.getClientDispatcher());
56 return combinedFutures.transform(new AbstractFunction1<Iterable<ActorSelection>, Void>() {
58 public Void apply(Iterable<ActorSelection> actorSelections) {
59 cohorts = Lists.newArrayList(actorSelections);
60 if(LOG.isDebugEnabled()) {
61 LOG.debug("Tx {} successfully built cohort path list: {}",
62 transactionId, cohorts);
66 }, TransactionReadyReplyMapper.SAME_FAILURE_TRANSFORMER, actorContext.getClientDispatcher());
70 public ListenableFuture<Boolean> canCommit() {
71 if(LOG.isDebugEnabled()) {
72 LOG.debug("Tx {} canCommit", transactionId);
74 final SettableFuture<Boolean> returnFuture = SettableFuture.create();
76 // The first phase of canCommit is to gather the list of cohort actor paths that will
77 // participate in the commit. buildCohortPathsList combines the cohort path Futures into
78 // one Future which we wait on asynchronously here. The cohort actor paths are
79 // extracted from ReadyTransactionReply messages by the Futures that were obtained earlier
80 // and passed to us from upstream processing. If any one fails then we'll fail canCommit.
82 buildCohortList().onComplete(new OnComplete<Void>() {
84 public void onComplete(Throwable failure, Void notUsed) throws Throwable {
86 if(LOG.isDebugEnabled()) {
87 LOG.debug("Tx {}: a cohort Future failed: {}", transactionId, failure);
89 returnFuture.setException(failure);
91 finishCanCommit(returnFuture);
94 }, actorContext.getClientDispatcher());
99 private void finishCanCommit(final SettableFuture<Boolean> returnFuture) {
100 if(LOG.isDebugEnabled()) {
101 LOG.debug("Tx {} finishCanCommit", transactionId);
104 // For empty transactions return immediately
105 if(cohorts.size() == 0){
106 if(LOG.isDebugEnabled()) {
107 LOG.debug("Tx {}: canCommit returning result: {}", transactionId, true);
109 returnFuture.set(Boolean.TRUE);
113 final Object message = new CanCommitTransaction(transactionId).toSerializable();
115 final Iterator<ActorSelection> iterator = cohorts.iterator();
117 final OnComplete<Object> onComplete = new OnComplete<Object>() {
119 public void onComplete(Throwable failure, Object response) throws Throwable {
120 if (failure != null) {
121 if (LOG.isDebugEnabled()) {
122 LOG.debug("Tx {}: a canCommit cohort Future failed: {}", transactionId, failure);
124 returnFuture.setException(failure);
128 boolean result = true;
129 if (response.getClass().equals(CanCommitTransactionReply.SERIALIZABLE_CLASS)) {
130 CanCommitTransactionReply reply =
131 CanCommitTransactionReply.fromSerializable(response);
132 if (!reply.getCanCommit()) {
136 LOG.error("Unexpected response type {}", response.getClass());
137 returnFuture.setException(new IllegalArgumentException(
138 String.format("Unexpected response type %s", response.getClass())));
142 if(iterator.hasNext() && result){
143 Future<Object> future = actorContext.executeOperationAsync(iterator.next(), message,
144 actorContext.getTransactionCommitOperationTimeout());
145 future.onComplete(this, actorContext.getClientDispatcher());
147 if(LOG.isDebugEnabled()) {
148 LOG.debug("Tx {}: canCommit returning result: {}", transactionId, result);
150 returnFuture.set(Boolean.valueOf(result));
156 Future<Object> future = actorContext.executeOperationAsync(iterator.next(), message,
157 actorContext.getTransactionCommitOperationTimeout());
158 future.onComplete(onComplete, actorContext.getClientDispatcher());
161 private Future<Iterable<Object>> invokeCohorts(Object message) {
162 List<Future<Object>> futureList = Lists.newArrayListWithCapacity(cohorts.size());
163 for(ActorSelection cohort : cohorts) {
164 if(LOG.isDebugEnabled()) {
165 LOG.debug("Tx {}: Sending {} to cohort {}", transactionId, message, cohort);
167 futureList.add(actorContext.executeOperationAsync(cohort, message, actorContext.getTransactionCommitOperationTimeout()));
170 return Futures.sequence(futureList, actorContext.getClientDispatcher());
174 public ListenableFuture<Void> preCommit() {
175 // We don't need to do anything here - preCommit is done atomically with the commit phase
177 return IMMEDIATE_VOID_SUCCESS;
181 public ListenableFuture<Void> abort() {
182 // Note - we pass false for propagateException. In the front-end data broker, this method
183 // is called when one of the 3 phases fails with an exception. We'd rather have that
184 // original exception propagated to the client. If our abort fails and we propagate the
185 // exception then that exception will supersede and suppress the original exception. But
186 // it's the original exception that is the root cause and of more interest to the client.
188 return voidOperation("abort", new AbortTransaction(transactionId).toSerializable(),
189 AbortTransactionReply.SERIALIZABLE_CLASS, false);
193 public ListenableFuture<Void> commit() {
194 OperationCallback operationCallback = cohortFutures.isEmpty() ? OperationCallback.NO_OP_CALLBACK :
195 new TransactionRateLimitingCallback(actorContext);
197 return voidOperation("commit", new CommitTransaction(transactionId).toSerializable(),
198 CommitTransactionReply.SERIALIZABLE_CLASS, true, operationCallback);
201 private ListenableFuture<Void> voidOperation(final String operationName, final Object message,
202 final Class<?> expectedResponseClass, final boolean propagateException) {
203 return voidOperation(operationName, message, expectedResponseClass, propagateException,
204 OperationCallback.NO_OP_CALLBACK);
207 private ListenableFuture<Void> voidOperation(final String operationName, final Object message,
208 final Class<?> expectedResponseClass, final boolean propagateException, final OperationCallback callback) {
210 if(LOG.isDebugEnabled()) {
211 LOG.debug("Tx {} {}", transactionId, operationName);
213 final SettableFuture<Void> returnFuture = SettableFuture.create();
215 // The cohort actor list should already be built at this point by the canCommit phase but,
216 // if not for some reason, we'll try to build it here.
218 if(cohorts != null) {
219 finishVoidOperation(operationName, message, expectedResponseClass, propagateException,
220 returnFuture, callback);
222 buildCohortList().onComplete(new OnComplete<Void>() {
224 public void onComplete(Throwable failure, Void notUsed) throws Throwable {
225 if(failure != null) {
226 if(LOG.isDebugEnabled()) {
227 LOG.debug("Tx {}: a {} cohort path Future failed: {}", transactionId,
228 operationName, failure);
230 if(propagateException) {
231 returnFuture.setException(failure);
233 returnFuture.set(null);
236 finishVoidOperation(operationName, message, expectedResponseClass,
237 propagateException, returnFuture, callback);
240 }, actorContext.getClientDispatcher());
246 private void finishVoidOperation(final String operationName, final Object message,
247 final Class<?> expectedResponseClass, final boolean propagateException,
248 final SettableFuture<Void> returnFuture, final OperationCallback callback) {
249 if(LOG.isDebugEnabled()) {
250 LOG.debug("Tx {} finish {}", transactionId, operationName);
255 Future<Iterable<Object>> combinedFuture = invokeCohorts(message);
257 combinedFuture.onComplete(new OnComplete<Iterable<Object>>() {
259 public void onComplete(Throwable failure, Iterable<Object> responses) throws Throwable {
261 Throwable exceptionToPropagate = failure;
262 if(exceptionToPropagate == null) {
263 for(Object response: responses) {
264 if(!response.getClass().equals(expectedResponseClass)) {
265 exceptionToPropagate = new IllegalArgumentException(
266 String.format("Unexpected response type %s",
267 response.getClass()));
273 if(exceptionToPropagate != null) {
275 if(LOG.isDebugEnabled()) {
276 LOG.debug("Tx {}: a {} cohort Future failed: {}", transactionId,
277 operationName, exceptionToPropagate);
279 if(propagateException) {
280 // We don't log the exception here to avoid redundant logging since we're
281 // propagating to the caller in MD-SAL core who will log it.
282 returnFuture.setException(exceptionToPropagate);
284 // Since the caller doesn't want us to propagate the exception we'll also
285 // not log it normally. But it's usually not good to totally silence
286 // exceptions so we'll log it to debug level.
287 if(LOG.isDebugEnabled()) {
288 LOG.debug(String.format("%s failed", message.getClass().getSimpleName()),
289 exceptionToPropagate);
291 returnFuture.set(null);
297 if(LOG.isDebugEnabled()) {
298 LOG.debug("Tx {}: {} succeeded", transactionId, operationName);
300 returnFuture.set(null);
305 }, actorContext.getClientDispatcher());
309 List<Future<ActorSelection>> getCohortFutures() {
310 return Collections.unmodifiableList(cohortFutures);