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