Fix shard deadlock in 3 nodes
[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 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;
27
28 /**
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
33  * are no-ops.
34  */
35 class LocalThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
36     private static final Logger LOG = LoggerFactory.getLogger(LocalThreePhaseCommitCohort.class);
37
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;
43
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;
53     }
54
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;
62     }
63
64     private Future<Object> initiateCommit(final boolean immediate,
65             final Optional<SortedSet<String>> participatingShardNames) {
66         if (operationError != null) {
67             return Futures.failed(operationError);
68         }
69
70         final ReadyLocalTransaction message = new ReadyLocalTransaction(transaction.getIdentifier(),
71                 modification, immediate, participatingShardNames);
72         return actorContext.executeOperationAsync(leader, message, actorContext.getTransactionCommitOperationTimeout());
73     }
74
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>() {
80             @Override
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);
85                     return;
86                 }
87
88                 LOG.debug("Transaction {} resolved to actor {}", transaction.getIdentifier(), success);
89             }
90         }, actorContext.getClientDispatcher());
91
92         return ret;
93     }
94
95     Future<Object> initiateDirectCommit() {
96         final Future<Object> messageFuture = initiateCommit(true, Optional.empty());
97         messageFuture.onComplete(new OnComplete<Object>() {
98             @Override
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);
106                 } else {
107                     LOG.error("Transaction {} resulted in unhandled message type {}, aborting", message.getClass());
108                     transactionAborted(transaction);
109                 }
110             }
111         }, actorContext.getClientDispatcher());
112
113         return messageFuture;
114     }
115
116     @Override
117     public final ListenableFuture<Boolean> canCommit() {
118         // Intended no-op
119         throw new UnsupportedOperationException();
120     }
121
122     @Override
123     public final ListenableFuture<Void> preCommit() {
124         // Intended no-op
125         throw new UnsupportedOperationException();
126     }
127
128     @Override
129     public final ListenableFuture<Void> abort() {
130         // Intended no-op
131         throw new UnsupportedOperationException();
132     }
133
134     @Override
135     public final ListenableFuture<Void> commit() {
136         // Intended no-op
137         throw new UnsupportedOperationException();
138     }
139
140     protected void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> aborted) {
141     }
142
143     protected void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> comitted) {
144     }
145 }