2 * Copyright (c) 2015 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
8 package org.opendaylight.controller.cluster.datastore;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.ActorSelection;
13 import akka.dispatch.Futures;
14 import akka.dispatch.OnComplete;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.Optional;
17 import java.util.SortedSet;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
20 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
21 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import scala.concurrent.Future;
30 * Fake {@link DOMStoreThreePhaseCommitCohort} instantiated for local transactions to conform with the DOM
31 * transaction APIs. It is only used to hold the data from a local DOM transaction ready operation and to
32 * initiate direct or coordinated commits from the front-end by sending the ReadyLocalTransaction message.
33 * It is not actually called by the front-end to perform 3PC thus the canCommit/preCommit/commit methods
36 class LocalThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
37 private static final Logger LOG = LoggerFactory.getLogger(LocalThreePhaseCommitCohort.class);
39 private final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction;
40 private final DataTreeModification modification;
41 private final ActorUtils actorUtils;
42 private final ActorSelection leader;
43 private final Exception operationError;
45 protected LocalThreePhaseCommitCohort(final ActorUtils actorUtils, final ActorSelection leader,
46 final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
47 final DataTreeModification modification,
48 final Exception operationError) {
49 this.actorUtils = requireNonNull(actorUtils);
50 this.leader = requireNonNull(leader);
51 this.transaction = requireNonNull(transaction);
52 this.modification = requireNonNull(modification);
53 this.operationError = operationError;
56 protected LocalThreePhaseCommitCohort(final ActorUtils actorUtils, final ActorSelection leader,
57 final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final Exception operationError) {
58 this.actorUtils = requireNonNull(actorUtils);
59 this.leader = requireNonNull(leader);
60 this.transaction = requireNonNull(transaction);
61 this.operationError = requireNonNull(operationError);
62 this.modification = null;
65 private Future<Object> initiateCommit(final boolean immediate,
66 final Optional<SortedSet<String>> participatingShardNames) {
67 if (operationError != null) {
68 return Futures.failed(operationError);
71 final ReadyLocalTransaction message = new ReadyLocalTransaction(transaction.getIdentifier(),
72 modification, immediate, participatingShardNames);
73 return actorUtils.executeOperationAsync(leader, message, actorUtils.getTransactionCommitOperationTimeout());
76 Future<ActorSelection> initiateCoordinatedCommit(final Optional<SortedSet<String>> participatingShardNames) {
77 final Future<Object> messageFuture = initiateCommit(false, participatingShardNames);
78 final Future<ActorSelection> ret = TransactionReadyReplyMapper.transform(messageFuture, actorUtils,
79 transaction.getIdentifier());
80 ret.onComplete(new OnComplete<ActorSelection>() {
82 public void onComplete(final Throwable failure, final ActorSelection success) {
83 if (failure != null) {
84 LOG.warn("Failed to prepare transaction {} on backend", transaction.getIdentifier(), failure);
85 transactionAborted(transaction);
89 LOG.debug("Transaction {} resolved to actor {}", transaction.getIdentifier(), success);
91 }, actorUtils.getClientDispatcher());
96 Future<Object> initiateDirectCommit() {
97 final Future<Object> messageFuture = initiateCommit(true, Optional.empty());
98 messageFuture.onComplete(new OnComplete<Object>() {
100 public void onComplete(final Throwable failure, final Object message) {
101 if (failure != null) {
102 LOG.warn("Failed to prepare transaction {} on backend", transaction.getIdentifier(), failure);
103 transactionAborted(transaction);
104 } else if (CommitTransactionReply.isSerializedType(message)) {
105 LOG.debug("Transaction {} committed successfully", transaction.getIdentifier());
106 transactionCommitted(transaction);
108 LOG.error("Transaction {} resulted in unhandled message type {}, aborting",
109 transaction.getIdentifier(), message.getClass());
110 transactionAborted(transaction);
113 }, actorUtils.getClientDispatcher());
115 return messageFuture;
119 public final ListenableFuture<Boolean> canCommit() {
121 throw new UnsupportedOperationException();
125 public final ListenableFuture<Void> preCommit() {
127 throw new UnsupportedOperationException();
131 public final ListenableFuture<Void> abort() {
133 throw new UnsupportedOperationException();
137 public final ListenableFuture<Void> commit() {
139 throw new UnsupportedOperationException();
142 protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> aborted) {
145 protected void transactionCommitted(final SnapshotBackedWriteTransaction<TransactionIdentifier> comitted) {