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 static java.util.Objects.requireNonNull;
12 import akka.dispatch.OnComplete;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.List;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
18 import org.opendaylight.mdsal.common.api.CommitInfo;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
20 import org.opendaylight.yangtools.yang.common.Empty;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import scala.concurrent.Future;
26 * A cohort proxy implementation for a single-shard transaction commit. If the transaction was a direct commit
27 * to the shard, this implementation elides the CanCommitTransaction and CommitTransaction messages to the
28 * shard as an optimization.
30 * @author Thomas Pantelis
32 class SingleCommitCohortProxy extends AbstractThreePhaseCommitCohort<Object> {
33 private static final Logger LOG = LoggerFactory.getLogger(SingleCommitCohortProxy.class);
35 private final ActorUtils actorUtils;
36 private final Future<Object> cohortFuture;
37 private final TransactionIdentifier transactionId;
38 private volatile DOMStoreThreePhaseCommitCohort delegateCohort = NoOpDOMStoreThreePhaseCommitCohort.INSTANCE;
39 private final OperationCallback.Reference operationCallbackRef;
41 SingleCommitCohortProxy(final ActorUtils actorUtils, final Future<Object> cohortFuture,
42 final TransactionIdentifier transactionId, final OperationCallback.Reference operationCallbackRef) {
43 this.actorUtils = actorUtils;
44 this.cohortFuture = cohortFuture;
45 this.transactionId = requireNonNull(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<>() {
57 public void onComplete(final Throwable failure, final Object cohortResponse) {
58 if (failure != null) {
59 operationCallbackRef.get().failure();
60 returnFuture.setException(failure);
64 operationCallbackRef.get().success();
66 LOG.debug("Tx {} successfully completed direct commit", transactionId);
68 // The Future was the result of a direct commit to the shard, essentially eliding the
69 // front-end 3PC coordination. We don't really care about the specific Future
70 // response object, only that it completed successfully. At this point the Tx is complete
71 // so return true. The subsequent preCommit and commit phases will be no-ops, ie return
72 // immediate success, to complete the 3PC for the front-end.
73 returnFuture.set(Boolean.TRUE);
75 }, actorUtils.getClientDispatcher());
81 public ListenableFuture<Empty> preCommit() {
82 return delegateCohort.preCommit();
86 public ListenableFuture<Empty> abort() {
87 return delegateCohort.abort();
91 public ListenableFuture<? extends CommitInfo> commit() {
92 return delegateCohort.commit();
96 List<Future<Object>> getCohortFutures() {
97 return List.of(cohortFuture);