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 final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
31 private static final Logger LOG = LoggerFactory.getLogger(SimpleShardDataTreeCohort.class);
33 private final DataTreeModification transaction;
34 private final ShardDataTree dataTree;
35 private final TransactionIdentifier transactionId;
36 private final CompositeDataTreeCohort userCohorts;
38 private State state = State.READY;
39 private DataTreeCandidateTip candidate;
40 private FutureCallback<?> callback;
41 private Exception nextFailure;
43 SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction,
44 final TransactionIdentifier transactionId, final CompositeDataTreeCohort userCohorts) {
45 this.dataTree = Preconditions.checkNotNull(dataTree);
46 this.transaction = Preconditions.checkNotNull(transaction);
47 this.transactionId = Preconditions.checkNotNull(transactionId);
48 this.userCohorts = Preconditions.checkNotNull(userCohorts);
51 SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction,
52 final TransactionIdentifier transactionId, final Exception nextFailure) {
53 this.dataTree = Preconditions.checkNotNull(dataTree);
54 this.transaction = Preconditions.checkNotNull(transaction);
55 this.transactionId = Preconditions.checkNotNull(transactionId);
56 this.userCohorts = null;
57 this.nextFailure = Preconditions.checkNotNull(nextFailure);
61 public TransactionIdentifier getIdentifier() {
66 DataTreeCandidateTip getCandidate() {
71 DataTreeModification getDataTreeModification() {
75 private void checkState(final State expected) {
76 Preconditions.checkState(state == expected, "State %s does not match expected state %s", state, expected);
80 public void canCommit(final FutureCallback<Void> newCallback) {
81 if (state == State.CAN_COMMIT_PENDING) {
85 checkState(State.READY);
86 this.callback = Preconditions.checkNotNull(newCallback);
87 state = State.CAN_COMMIT_PENDING;
89 if (nextFailure == null) {
90 dataTree.startCanCommit(this);
92 failedCanCommit(nextFailure);
97 public void preCommit(final FutureCallback<DataTreeCandidate> newCallback) {
98 checkState(State.CAN_COMMIT_COMPLETE);
99 this.callback = Preconditions.checkNotNull(newCallback);
100 state = State.PRE_COMMIT_PENDING;
102 if (nextFailure == null) {
103 dataTree.startPreCommit(this);
105 failedPreCommit(nextFailure);
110 public void abort(final FutureCallback<Void> abortCallback) {
111 if (!dataTree.startAbort(this)) {
112 abortCallback.onSuccess(null);
117 state = State.ABORTED;
119 final Optional<List<Future<Object>>> maybeAborts = userCohorts.abort();
120 if (!maybeAborts.isPresent()) {
121 abortCallback.onSuccess(null);
125 final Future<Iterable<Object>> aborts = Futures.sequence(maybeAborts.get(), ExecutionContexts.global());
126 if (aborts.isCompleted()) {
127 abortCallback.onSuccess(null);
131 aborts.onComplete(new OnComplete<Iterable<Object>>() {
133 public void onComplete(final Throwable failure, final Iterable<Object> objs) {
134 if (failure != null) {
135 abortCallback.onFailure(failure);
137 abortCallback.onSuccess(null);
140 }, ExecutionContexts.global());
144 public void commit(final FutureCallback<UnsignedLong> newCallback) {
145 checkState(State.PRE_COMMIT_COMPLETE);
146 this.callback = Preconditions.checkNotNull(newCallback);
147 state = State.COMMIT_PENDING;
149 if (nextFailure == null) {
150 dataTree.startCommit(this, candidate);
152 failedCommit(nextFailure);
156 private <T> FutureCallback<T> switchState(final State newState) {
157 @SuppressWarnings("unchecked")
158 final FutureCallback<T> ret = (FutureCallback<T>) this.callback;
159 this.callback = null;
160 LOG.debug("Transaction {} changing state from {} to {}", transactionId, state, newState);
161 this.state = newState;
165 void setNewCandidate(final DataTreeCandidateTip dataTreeCandidate) {
166 checkState(State.PRE_COMMIT_COMPLETE);
167 this.candidate = Verify.verifyNotNull(dataTreeCandidate);
170 void successfulCanCommit() {
171 switchState(State.CAN_COMMIT_COMPLETE).onSuccess(null);
174 void failedCanCommit(final Exception cause) {
175 switchState(State.FAILED).onFailure(cause);
179 * Run user-defined canCommit and preCommit hooks. We want to run these before we initiate persistence so that
180 * any failure to validate is propagated before we record the transaction.
182 * @param dataTreeCandidate {@link DataTreeCandidate} under consideration
183 * @throws ExecutionException if the operation fails
184 * @throws TimeoutException if the operation times out
186 // FIXME: this should be asynchronous
187 void userPreCommit(final DataTreeCandidate dataTreeCandidate) throws ExecutionException, TimeoutException {
189 userCohorts.canCommit(dataTreeCandidate);
190 userCohorts.preCommit();
193 void successfulPreCommit(final DataTreeCandidateTip dataTreeCandidate) {
194 LOG.trace("Transaction {} prepared candidate {}", transaction, dataTreeCandidate);
195 this.candidate = Verify.verifyNotNull(dataTreeCandidate);
196 switchState(State.PRE_COMMIT_COMPLETE).onSuccess(dataTreeCandidate);
199 void failedPreCommit(final Exception cause) {
200 if (LOG.isTraceEnabled()) {
201 LOG.trace("Transaction {} failed to prepare", transaction, cause);
203 LOG.error("Transaction {} failed to prepare", transactionId, cause);
207 switchState(State.FAILED).onFailure(cause);
210 void successfulCommit(final UnsignedLong journalIndex) {
212 userCohorts.commit();
213 } catch (TimeoutException | ExecutionException e) {
214 // We are probably dead, depending on what the cohorts end up doing
215 LOG.error("User cohorts failed to commit", e);
218 switchState(State.COMMITTED).onSuccess(journalIndex);
221 void failedCommit(final Exception cause) {
222 if (LOG.isTraceEnabled()) {
223 LOG.trace("Transaction {} failed to commit", transaction, cause);
225 LOG.error("Transaction failed to commit", cause);
229 switchState(State.FAILED).onFailure(cause);
233 public State getState() {
237 void reportFailure(final Exception cause) {
238 if (nextFailure == null) {
239 this.nextFailure = Preconditions.checkNotNull(cause);
241 LOG.debug("Transaction {} already has a set failure, not updating it", transactionId, cause);
246 public boolean isFailed() {
247 return state == State.FAILED || nextFailure != null;
251 ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
252 return super.addToStringAttributes(toStringHelper).add("nextFailure", nextFailure);