Fix warnings/javadocs in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / SimpleShardDataTreeCohort.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.dispatch.ExecutionContexts;
11 import akka.dispatch.OnComplete;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Verify;
14 import com.google.common.primitives.UnsignedLong;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.SettableFuture;
19 import java.util.Optional;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeoutException;
22 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
23 import org.opendaylight.yangtools.concepts.Identifiable;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.Future;
30
31 final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Identifiable<TransactionIdentifier> {
32     private static final Logger LOG = LoggerFactory.getLogger(SimpleShardDataTreeCohort.class);
33     private static final ListenableFuture<Void> VOID_FUTURE = Futures.immediateFuture(null);
34     private final DataTreeModification transaction;
35     private final ShardDataTree dataTree;
36     private final TransactionIdentifier transactionId;
37     private final CompositeDataTreeCohort userCohorts;
38
39     private State state = State.READY;
40     private DataTreeCandidateTip candidate;
41     private FutureCallback<?> callback;
42     private Exception nextFailure;
43
44     SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction,
45             final TransactionIdentifier transactionId, final CompositeDataTreeCohort userCohorts) {
46         this.dataTree = Preconditions.checkNotNull(dataTree);
47         this.transaction = Preconditions.checkNotNull(transaction);
48         this.transactionId = Preconditions.checkNotNull(transactionId);
49         this.userCohorts = Preconditions.checkNotNull(userCohorts);
50     }
51
52     @Override
53     public TransactionIdentifier getIdentifier() {
54         return transactionId;
55     }
56
57     @Override
58     DataTreeCandidateTip getCandidate() {
59         return candidate;
60     }
61
62     @Override
63
64     DataTreeModification getDataTreeModification() {
65         return transaction;
66     }
67
68     private void checkState(final State expected) {
69         Preconditions.checkState(state == expected, "State %s does not match expected state %s", state, expected);
70     }
71
72     @Override
73     public void canCommit(final FutureCallback<Void> newCallback) {
74         if (state == State.CAN_COMMIT_PENDING) {
75             return;
76         }
77
78         checkState(State.READY);
79         this.callback = Preconditions.checkNotNull(newCallback);
80         state = State.CAN_COMMIT_PENDING;
81         dataTree.startCanCommit(this);
82     }
83
84     @Override
85     public void preCommit(final FutureCallback<DataTreeCandidate> newCallback) {
86         checkState(State.CAN_COMMIT_COMPLETE);
87         this.callback = Preconditions.checkNotNull(newCallback);
88         state = State.PRE_COMMIT_PENDING;
89
90         if (nextFailure == null) {
91             dataTree.startPreCommit(this);
92         } else {
93             failedPreCommit(nextFailure);
94         }
95     }
96
97     @Override
98     public ListenableFuture<Void> abort() {
99         dataTree.startAbort(this);
100         state = State.ABORTED;
101
102         final Optional<Future<Iterable<Object>>> maybeAborts = userCohorts.abort();
103         if (!maybeAborts.isPresent()) {
104             return VOID_FUTURE;
105         }
106
107         final Future<Iterable<Object>> aborts = maybeAborts.get();
108         if (aborts.isCompleted()) {
109             return VOID_FUTURE;
110         }
111
112         final SettableFuture<Void> ret = SettableFuture.create();
113         aborts.onComplete(new OnComplete<Iterable<Object>>() {
114             @Override
115             public void onComplete(final Throwable failure, final Iterable<Object> objs) {
116                 if (failure != null) {
117                     ret.setException(failure);
118                 } else {
119                     ret.set(null);
120                 }
121             }
122         }, ExecutionContexts.global());
123
124         return ret;
125     }
126
127     @Override
128     public void commit(final FutureCallback<UnsignedLong> newCallback) {
129         checkState(State.PRE_COMMIT_COMPLETE);
130         this.callback = Preconditions.checkNotNull(newCallback);
131         state = State.COMMIT_PENDING;
132         dataTree.startCommit(this, candidate);
133     }
134
135     private <T> FutureCallback<T> switchState(final State newState) {
136         @SuppressWarnings("unchecked")
137         final FutureCallback<T> ret = (FutureCallback<T>) this.callback;
138         this.callback = null;
139         LOG.debug("Transaction {} changing state from {} to {}", transactionId, state, newState);
140         this.state = newState;
141         return ret;
142     }
143
144     void successfulCanCommit() {
145         switchState(State.CAN_COMMIT_COMPLETE).onSuccess(null);
146     }
147
148     void failedCanCommit(final Exception cause) {
149         switchState(State.FAILED).onFailure(cause);
150     }
151
152     /**
153      * Run user-defined canCommit and preCommit hooks. We want to run these before we initiate persistence so that
154      * any failure to validate is propagated before we record the transaction.
155      *
156      * @param dataTreeCandidate {@link DataTreeCandidate} under consideration
157      * @throws ExecutionException if the operation fails
158      * @throws TimeoutException if the operation times out
159      */
160     // FIXME: this should be asynchronous
161     void userPreCommit(final DataTreeCandidate dataTreeCandidate) throws ExecutionException, TimeoutException {
162         userCohorts.canCommit(dataTreeCandidate);
163         userCohorts.preCommit();
164     }
165
166     void successfulPreCommit(final DataTreeCandidateTip dataTreeCandidate) {
167         LOG.trace("Transaction {} prepared candidate {}", transaction, dataTreeCandidate);
168         this.candidate = Verify.verifyNotNull(dataTreeCandidate);
169         switchState(State.PRE_COMMIT_COMPLETE).onSuccess(dataTreeCandidate);
170     }
171
172     void failedPreCommit(final Exception cause) {
173         if (LOG.isTraceEnabled()) {
174             LOG.trace("Transaction {} failed to prepare", transaction, cause);
175         } else {
176             LOG.error("Transaction {} failed to prepare", transactionId, cause);
177         }
178
179         userCohorts.abort();
180         switchState(State.FAILED).onFailure(cause);
181     }
182
183     void successfulCommit(final UnsignedLong journalIndex) {
184         try {
185             userCohorts.commit();
186         } catch (TimeoutException | ExecutionException e) {
187             // We are probably dead, depending on what the cohorts end up doing
188             LOG.error("User cohorts failed to commit", e);
189         }
190
191         switchState(State.COMMITTED).onSuccess(journalIndex);
192     }
193
194     void failedCommit(final Exception cause) {
195         if (LOG.isTraceEnabled()) {
196             LOG.trace("Transaction {} failed to commit", transaction, cause);
197         } else {
198             LOG.error("Transaction failed to commit", cause);
199         }
200
201         userCohorts.abort();
202         switchState(State.FAILED).onFailure(cause);
203     }
204
205     @Override
206     public State getState() {
207         return state;
208     }
209
210     void reportFailure(final Exception cause) {
211         this.nextFailure = Preconditions.checkNotNull(cause);
212     }
213
214     @Override
215     public boolean isFailed() {
216         return state == State.FAILED || nextFailure != null;
217     }
218 }