2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import akka.dispatch.ExecutionContexts;
11 import akka.dispatch.Futures;
12 import akka.dispatch.OnComplete;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Verify;
16 import com.google.common.primitives.UnsignedLong;
17 import com.google.common.util.concurrent.FutureCallback;
18 import java.util.List;
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.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import scala.concurrent.Future;
30 abstract class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
31 static final class DeadOnArrival extends SimpleShardDataTreeCohort {
32 private final Exception failure;
34 DeadOnArrival(final ShardDataTree dataTree, final DataTreeModification transaction,
35 final TransactionIdentifier transactionId, final Exception failure) {
36 super(dataTree, transaction, transactionId, null);
37 this.failure = Preconditions.checkNotNull(failure);
41 void throwCanCommitFailure() throws Exception {
46 ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
47 return super.addToStringAttributes(toStringHelper).add("failure", failure);
51 static final class Normal extends SimpleShardDataTreeCohort {
52 Normal(final ShardDataTree dataTree, final DataTreeModification transaction,
53 final TransactionIdentifier transactionId, final CompositeDataTreeCohort userCohorts) {
54 super(dataTree, transaction, transactionId, Preconditions.checkNotNull(userCohorts));
58 void throwCanCommitFailure() {
63 private static final Logger LOG = LoggerFactory.getLogger(SimpleShardDataTreeCohort.class);
65 private final DataTreeModification transaction;
66 private final ShardDataTree dataTree;
67 private final TransactionIdentifier transactionId;
68 private final CompositeDataTreeCohort userCohorts;
70 private State state = State.READY;
71 private DataTreeCandidateTip candidate;
72 private FutureCallback<?> callback;
73 private Exception nextFailure;
75 SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction,
76 final TransactionIdentifier transactionId, final CompositeDataTreeCohort userCohorts) {
77 this.dataTree = Preconditions.checkNotNull(dataTree);
78 this.transaction = Preconditions.checkNotNull(transaction);
79 this.transactionId = Preconditions.checkNotNull(transactionId);
80 this.userCohorts = userCohorts;
84 public TransactionIdentifier getIdentifier() {
89 DataTreeCandidateTip getCandidate() {
94 DataTreeModification getDataTreeModification() {
98 private void checkState(final State expected) {
99 Preconditions.checkState(state == expected, "State %s does not match expected state %s", state, expected);
103 public void canCommit(final FutureCallback<Void> newCallback) {
104 if (state == State.CAN_COMMIT_PENDING) {
108 checkState(State.READY);
109 this.callback = Preconditions.checkNotNull(newCallback);
110 state = State.CAN_COMMIT_PENDING;
112 if (nextFailure == null) {
113 dataTree.startCanCommit(this);
115 failedCanCommit(nextFailure);
120 public void preCommit(final FutureCallback<DataTreeCandidate> newCallback) {
121 checkState(State.CAN_COMMIT_COMPLETE);
122 this.callback = Preconditions.checkNotNull(newCallback);
123 state = State.PRE_COMMIT_PENDING;
125 if (nextFailure == null) {
126 dataTree.startPreCommit(this);
128 failedPreCommit(nextFailure);
133 public void abort(final FutureCallback<Void> abortCallback) {
134 if (!dataTree.startAbort(this)) {
135 abortCallback.onSuccess(null);
140 state = State.ABORTED;
142 final Optional<List<Future<Object>>> maybeAborts = userCohorts.abort();
143 if (!maybeAborts.isPresent()) {
144 abortCallback.onSuccess(null);
148 final Future<Iterable<Object>> aborts = Futures.sequence(maybeAborts.get(), ExecutionContexts.global());
149 if (aborts.isCompleted()) {
150 abortCallback.onSuccess(null);
154 aborts.onComplete(new OnComplete<Iterable<Object>>() {
156 public void onComplete(final Throwable failure, final Iterable<Object> objs) {
157 if (failure != null) {
158 abortCallback.onFailure(failure);
160 abortCallback.onSuccess(null);
163 }, ExecutionContexts.global());
167 public void commit(final FutureCallback<UnsignedLong> newCallback) {
168 checkState(State.PRE_COMMIT_COMPLETE);
169 this.callback = Preconditions.checkNotNull(newCallback);
170 state = State.COMMIT_PENDING;
172 if (nextFailure == null) {
173 dataTree.startCommit(this, candidate);
175 failedCommit(nextFailure);
179 private <T> FutureCallback<T> switchState(final State newState) {
180 @SuppressWarnings("unchecked")
181 final FutureCallback<T> ret = (FutureCallback<T>) this.callback;
182 this.callback = null;
183 LOG.debug("Transaction {} changing state from {} to {}", transactionId, state, newState);
184 this.state = newState;
188 void setNewCandidate(final DataTreeCandidateTip dataTreeCandidate) {
189 checkState(State.PRE_COMMIT_COMPLETE);
190 this.candidate = Verify.verifyNotNull(dataTreeCandidate);
193 void successfulCanCommit() {
194 switchState(State.CAN_COMMIT_COMPLETE).onSuccess(null);
197 void failedCanCommit(final Exception cause) {
198 switchState(State.FAILED).onFailure(cause);
202 * Run user-defined canCommit and preCommit hooks. We want to run these before we initiate persistence so that
203 * any failure to validate is propagated before we record the transaction.
205 * @param dataTreeCandidate {@link DataTreeCandidate} under consideration
206 * @throws ExecutionException if the operation fails
207 * @throws TimeoutException if the operation times out
209 // FIXME: this should be asynchronous
210 void userPreCommit(final DataTreeCandidate dataTreeCandidate) throws ExecutionException, TimeoutException {
212 userCohorts.canCommit(dataTreeCandidate);
213 userCohorts.preCommit();
216 void successfulPreCommit(final DataTreeCandidateTip dataTreeCandidate) {
217 LOG.trace("Transaction {} prepared candidate {}", transaction, dataTreeCandidate);
218 this.candidate = Verify.verifyNotNull(dataTreeCandidate);
219 switchState(State.PRE_COMMIT_COMPLETE).onSuccess(dataTreeCandidate);
222 void failedPreCommit(final Exception cause) {
223 if (LOG.isTraceEnabled()) {
224 LOG.trace("Transaction {} failed to prepare", transaction, cause);
226 LOG.error("Transaction {} failed to prepare", transactionId, cause);
230 switchState(State.FAILED).onFailure(cause);
233 void successfulCommit(final UnsignedLong journalIndex) {
235 userCohorts.commit();
236 } catch (TimeoutException | ExecutionException e) {
237 // We are probably dead, depending on what the cohorts end up doing
238 LOG.error("User cohorts failed to commit", e);
241 switchState(State.COMMITTED).onSuccess(journalIndex);
244 void failedCommit(final Exception cause) {
245 if (LOG.isTraceEnabled()) {
246 LOG.trace("Transaction {} failed to commit", transaction, cause);
248 LOG.error("Transaction failed to commit", cause);
252 switchState(State.FAILED).onFailure(cause);
256 public State getState() {
260 void reportFailure(final Exception cause) {
261 this.nextFailure = Preconditions.checkNotNull(cause);
265 * If there is an initial failure, throw it so the caller can process it.
267 * @throws Exception reported failure.
269 abstract void throwCanCommitFailure() throws Exception;
272 public boolean isFailed() {
273 return state == State.FAILED || nextFailure != null;
277 ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
278 return super.addToStringAttributes(toStringHelper).add("nextFailure", nextFailure);