db879c07730e99ddfb78aaca3af96790f6077c61
[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(final 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) {
82                 if (failure != null) {
83                     LOG.warn("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) {
100                 if (failure != null) {
101                     LOG.warn("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",
108                         transaction.getIdentifier(), message.getClass());
109                     transactionAborted(transaction);
110                 }
111             }
112         }, actorContext.getClientDispatcher());
113
114         return messageFuture;
115     }
116
117     @Override
118     public final ListenableFuture<Boolean> canCommit() {
119         // Intended no-op
120         throw new UnsupportedOperationException();
121     }
122
123     @Override
124     public final ListenableFuture<Void> preCommit() {
125         // Intended no-op
126         throw new UnsupportedOperationException();
127     }
128
129     @Override
130     public final ListenableFuture<Void> abort() {
131         // Intended no-op
132         throw new UnsupportedOperationException();
133     }
134
135     @Override
136     public final ListenableFuture<Void> commit() {
137         // Intended no-op
138         throw new UnsupportedOperationException();
139     }
140
141     protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> aborted) {
142     }
143
144     protected void transactionCommitted(final SnapshotBackedWriteTransaction<TransactionIdentifier> comitted) {
145     }
146 }