Delay snapshot backed transaction ready error
[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.access.concepts.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 final Exception operationError;
41
42     protected LocalThreePhaseCommitCohort(final ActorContext actorContext, final ActorSelection leader,
43             final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
44             final DataTreeModification modification,
45             final Exception operationError) {
46         this.actorContext = Preconditions.checkNotNull(actorContext);
47         this.leader = Preconditions.checkNotNull(leader);
48         this.transaction = Preconditions.checkNotNull(transaction);
49         this.modification = Preconditions.checkNotNull(modification);
50         this.operationError = operationError;
51     }
52
53     protected LocalThreePhaseCommitCohort(final ActorContext actorContext, final ActorSelection leader,
54             final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final Exception operationError) {
55         this.actorContext = Preconditions.checkNotNull(actorContext);
56         this.leader = Preconditions.checkNotNull(leader);
57         this.transaction = Preconditions.checkNotNull(transaction);
58         this.operationError = Preconditions.checkNotNull(operationError);
59         this.modification = null;
60     }
61
62     private Future<Object> initiateCommit(final boolean immediate) {
63         if (operationError != null) {
64             return Futures.failed(operationError);
65         }
66
67         final ReadyLocalTransaction message = new ReadyLocalTransaction(transaction.getIdentifier(),
68                 modification, immediate);
69         return actorContext.executeOperationAsync(leader, message, actorContext.getTransactionCommitOperationTimeout());
70     }
71
72     Future<ActorSelection> initiateCoordinatedCommit() {
73         final Future<Object> messageFuture = initiateCommit(false);
74         final Future<ActorSelection> ret = TransactionReadyReplyMapper.transform(messageFuture, actorContext,
75                 transaction.getIdentifier());
76         ret.onComplete(new OnComplete<ActorSelection>() {
77             @Override
78             public void onComplete(final Throwable failure, final ActorSelection success) throws Throwable {
79                 if (failure != null) {
80                     LOG.info("Failed to prepare transaction {} on backend", transaction.getIdentifier(), failure);
81                     transactionAborted(transaction);
82                     return;
83                 }
84
85                 LOG.debug("Transaction {} resolved to actor {}", transaction.getIdentifier(), success);
86             }
87         }, actorContext.getClientDispatcher());
88
89         return ret;
90     }
91
92     Future<Object> initiateDirectCommit() {
93         final Future<Object> messageFuture = initiateCommit(true);
94         messageFuture.onComplete(new OnComplete<Object>() {
95             @Override
96             public void onComplete(final Throwable failure, final Object message) throws Throwable {
97                 if (failure != null) {
98                     LOG.error("Failed to prepare transaction {} on backend", transaction.getIdentifier(), failure);
99                     transactionAborted(transaction);
100                 } else if (CommitTransactionReply.isSerializedType(message)) {
101                     LOG.debug("Transaction {} committed successfully", transaction.getIdentifier());
102                     transactionCommitted(transaction);
103                 } else {
104                     LOG.error("Transaction {} resulted in unhandled message type {}, aborting", message.getClass());
105                     transactionAborted(transaction);
106                 }
107             }
108         }, actorContext.getClientDispatcher());
109
110         return messageFuture;
111     }
112
113     @Override
114     public final ListenableFuture<Boolean> canCommit() {
115         // Intended no-op
116         throw new UnsupportedOperationException();
117     }
118
119     @Override
120     public final ListenableFuture<Void> preCommit() {
121         // Intended no-op
122         throw new UnsupportedOperationException();
123     }
124
125     @Override
126     public final ListenableFuture<Void> abort() {
127         // Intended no-op
128         throw new UnsupportedOperationException();
129     }
130
131     @Override
132     public final ListenableFuture<Void> commit() {
133         // Intended no-op
134         throw new UnsupportedOperationException();
135     }
136
137     protected void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> aborted) {
138     }
139
140     protected void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> comitted) {
141     }
142 }