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