Do not overwrite operation failure
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalThreePhaseCommitCohort.java
1 /*
2  * Copyright (c) 2015 Cisco 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.base.Preconditions;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
18 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
19 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
21 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import scala.concurrent.Future;
26
27 /**
28  * Fake {@link DOMStoreThreePhaseCommitCohort} instantiated for local transactions to conform with the DOM
29  * transaction APIs. It is only used to hold the data from a local DOM transaction ready operation and to
30  * initiate direct or coordinated commits from the front-end by sending the ReadyLocalTransaction message.
31  * It is not actually called by the front-end to perform 3PC thus the canCommit/preCommit/commit methods
32  * are no-ops.
33  */
34 class LocalThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
35     private static final Logger LOG = LoggerFactory.getLogger(LocalThreePhaseCommitCohort.class);
36
37     private final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction;
38     private final DataTreeModification modification;
39     private final ActorContext actorContext;
40     private final ActorSelection leader;
41     private Exception operationError;
42
43     protected LocalThreePhaseCommitCohort(final ActorContext actorContext, final ActorSelection leader,
44             final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final DataTreeModification modification) {
45         this.actorContext = Preconditions.checkNotNull(actorContext);
46         this.leader = Preconditions.checkNotNull(leader);
47         this.transaction = Preconditions.checkNotNull(transaction);
48         this.modification = Preconditions.checkNotNull(modification);
49     }
50
51     protected LocalThreePhaseCommitCohort(final ActorContext actorContext, final ActorSelection leader,
52             final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final Exception operationError) {
53         this.actorContext = Preconditions.checkNotNull(actorContext);
54         this.leader = Preconditions.checkNotNull(leader);
55         this.transaction = Preconditions.checkNotNull(transaction);
56         this.operationError = Preconditions.checkNotNull(operationError);
57         this.modification = null;
58     }
59
60     private Future<Object> initiateCommit(final boolean immediate) {
61         if(operationError != null) {
62             return Futures.failed(operationError);
63         }
64
65         final ReadyLocalTransaction message = new ReadyLocalTransaction(transaction.getIdentifier().toString(),
66                 modification, immediate);
67         return actorContext.executeOperationAsync(leader, message, actorContext.getTransactionCommitOperationTimeout());
68     }
69
70     void setOperationError(@Nonnull Exception operationError) {
71         if (this.operationError != null) {
72             LOG.info("Cohort {} already had operation error", this, this.operationError);
73         }
74
75         this.operationError = Preconditions.checkNotNull(operationError);
76     }
77
78     Future<ActorSelection> initiateCoordinatedCommit() {
79         final Future<Object> messageFuture = initiateCommit(false);
80         final Future<ActorSelection> ret = TransactionReadyReplyMapper.transform(messageFuture, actorContext,
81                 transaction.getIdentifier());
82         ret.onComplete(new OnComplete<ActorSelection>() {
83             @Override
84             public void onComplete(final Throwable failure, final ActorSelection success) throws Throwable {
85                 if (failure != null) {
86                     LOG.info("Failed to prepare transaction {} on backend", transaction.getIdentifier(), failure);
87                     transactionAborted(transaction);
88                     return;
89                 }
90
91                 LOG.debug("Transaction {} resolved to actor {}", transaction.getIdentifier(), success);
92             }
93         }, actorContext.getClientDispatcher());
94
95         return ret;
96     }
97
98     Future<Object> initiateDirectCommit() {
99         final Future<Object> messageFuture = initiateCommit(true);
100         messageFuture.onComplete(new OnComplete<Object>() {
101             @Override
102             public void onComplete(final Throwable failure, final Object message) throws Throwable {
103                 if (failure != null) {
104                     LOG.error("Failed to prepare transaction {} on backend", transaction.getIdentifier(), failure);
105                     transactionAborted(transaction);
106                 } else if (CommitTransactionReply.SERIALIZABLE_CLASS.isInstance(message)) {
107                     LOG.debug("Transaction {} committed successfully", transaction.getIdentifier());
108                     transactionCommitted(transaction);
109                 } else {
110                     LOG.error("Transaction {} resulted in unhandled message type {}, aborting", message.getClass());
111                     transactionAborted(transaction);
112                 }
113             }
114         }, actorContext.getClientDispatcher());
115
116         return messageFuture;
117     }
118
119     @Override
120     public final ListenableFuture<Boolean> canCommit() {
121         // Intended no-op
122         throw new UnsupportedOperationException();
123     }
124
125     @Override
126     public final ListenableFuture<Void> preCommit() {
127         // Intended no-op
128         throw new UnsupportedOperationException();
129     }
130
131     @Override
132     public final ListenableFuture<Void> abort() {
133         // Intended no-op
134         throw new UnsupportedOperationException();
135     }
136
137     @Override
138     public final ListenableFuture<Void> commit() {
139         // Intended no-op
140         throw new UnsupportedOperationException();
141     }
142
143     protected void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
144     }
145
146     protected void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
147     }
148 }