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 akka.actor.ActorSelection;
11 import akka.dispatch.Futures;
12 import akka.dispatch.OnComplete;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Optional;
16 import java.util.SortedSet;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
20 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.concurrent.Future;
29 * Fake {@link DOMStoreThreePhaseCommitCohort} instantiated for local transactions to conform with the DOM
30 * transaction APIs. It is only used to hold the data from a local DOM transaction ready operation and to
31 * initiate direct or coordinated commits from the front-end by sending the ReadyLocalTransaction message.
32 * It is not actually called by the front-end to perform 3PC thus the canCommit/preCommit/commit methods
35 class LocalThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
36 private static final Logger LOG = LoggerFactory.getLogger(LocalThreePhaseCommitCohort.class);
38 private final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction;
39 private final DataTreeModification modification;
40 private final ActorContext actorContext;
41 private final ActorSelection leader;
42 private final Exception operationError;
44 protected LocalThreePhaseCommitCohort(final ActorContext actorContext, final ActorSelection leader,
45 final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
46 final DataTreeModification modification,
47 final Exception operationError) {
48 this.actorContext = Preconditions.checkNotNull(actorContext);
49 this.leader = Preconditions.checkNotNull(leader);
50 this.transaction = Preconditions.checkNotNull(transaction);
51 this.modification = Preconditions.checkNotNull(modification);
52 this.operationError = operationError;
55 protected LocalThreePhaseCommitCohort(final ActorContext actorContext, final ActorSelection leader,
56 final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final Exception operationError) {
57 this.actorContext = Preconditions.checkNotNull(actorContext);
58 this.leader = Preconditions.checkNotNull(leader);
59 this.transaction = Preconditions.checkNotNull(transaction);
60 this.operationError = Preconditions.checkNotNull(operationError);
61 this.modification = null;
64 private Future<Object> initiateCommit(final boolean immediate,
65 final Optional<SortedSet<String>> participatingShardNames) {
66 if (operationError != null) {
67 return Futures.failed(operationError);
70 final ReadyLocalTransaction message = new ReadyLocalTransaction(transaction.getIdentifier(),
71 modification, immediate, participatingShardNames);
72 return actorContext.executeOperationAsync(leader, message, actorContext.getTransactionCommitOperationTimeout());
75 Future<ActorSelection> initiateCoordinatedCommit(Optional<SortedSet<String>> participatingShardNames) {
76 final Future<Object> messageFuture = initiateCommit(false, participatingShardNames);
77 final Future<ActorSelection> ret = TransactionReadyReplyMapper.transform(messageFuture, actorContext,
78 transaction.getIdentifier());
79 ret.onComplete(new OnComplete<ActorSelection>() {
81 public void onComplete(final Throwable failure, final ActorSelection success) throws Throwable {
82 if (failure != null) {
83 LOG.info("Failed to prepare transaction {} on backend", transaction.getIdentifier(), failure);
84 transactionAborted(transaction);
88 LOG.debug("Transaction {} resolved to actor {}", transaction.getIdentifier(), success);
90 }, actorContext.getClientDispatcher());
95 Future<Object> initiateDirectCommit() {
96 final Future<Object> messageFuture = initiateCommit(true, Optional.empty());
97 messageFuture.onComplete(new OnComplete<Object>() {
99 public void onComplete(final Throwable failure, final Object message) throws Throwable {
100 if (failure != null) {
101 LOG.error("Failed to prepare transaction {} on backend", transaction.getIdentifier(), failure);
102 transactionAborted(transaction);
103 } else if (CommitTransactionReply.isSerializedType(message)) {
104 LOG.debug("Transaction {} committed successfully", transaction.getIdentifier());
105 transactionCommitted(transaction);
107 LOG.error("Transaction {} resulted in unhandled message type {}, aborting", message.getClass());
108 transactionAborted(transaction);
111 }, actorContext.getClientDispatcher());
113 return messageFuture;
117 public final ListenableFuture<Boolean> canCommit() {
119 throw new UnsupportedOperationException();
123 public final ListenableFuture<Void> preCommit() {
125 throw new UnsupportedOperationException();
129 public final ListenableFuture<Void> abort() {
131 throw new UnsupportedOperationException();
135 public final ListenableFuture<Void> commit() {
137 throw new UnsupportedOperationException();
140 protected void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> aborted) {
143 protected void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> comitted) {