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