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 static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.base.Preconditions;
15 import com.google.common.primitives.UnsignedLong;
16 import com.google.common.util.concurrent.FutureCallback;
17 import java.util.Optional;
18 import java.util.SortedSet;
19 import java.util.concurrent.CompletionStage;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.yangtools.yang.common.Empty;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateTip;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
30 private static final Logger LOG = LoggerFactory.getLogger(SimpleShardDataTreeCohort.class);
32 private final DataTreeModification transaction;
33 private final ShardDataTree dataTree;
34 private final TransactionIdentifier transactionId;
35 private final CompositeDataTreeCohort userCohorts;
36 private final @Nullable SortedSet<String> participatingShardNames;
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 final Optional<SortedSet<String>> participatingShardNames) {
46 this.dataTree = requireNonNull(dataTree);
47 this.transaction = requireNonNull(transaction);
48 this.transactionId = requireNonNull(transactionId);
49 this.userCohorts = requireNonNull(userCohorts);
50 this.participatingShardNames = requireNonNull(participatingShardNames).orElse(null);
53 SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction,
54 final TransactionIdentifier transactionId, final Exception nextFailure) {
55 this.dataTree = requireNonNull(dataTree);
56 this.transaction = requireNonNull(transaction);
57 this.transactionId = requireNonNull(transactionId);
59 participatingShardNames = null;
60 this.nextFailure = requireNonNull(nextFailure);
64 public TransactionIdentifier getIdentifier() {
69 DataTreeCandidateTip getCandidate() {
74 DataTreeModification getDataTreeModification() {
79 Optional<SortedSet<String>> getParticipatingShardNames() {
80 return Optional.ofNullable(participatingShardNames);
83 private void checkState(final State expected) {
84 Preconditions.checkState(state == expected, "State %s does not match expected state %s for %s",
85 state, expected, getIdentifier());
89 public void canCommit(final FutureCallback<Empty> newCallback) {
90 if (state == State.CAN_COMMIT_PENDING) {
94 checkState(State.READY);
95 callback = requireNonNull(newCallback);
96 state = State.CAN_COMMIT_PENDING;
98 if (nextFailure == null) {
99 dataTree.startCanCommit(this);
101 failedCanCommit(nextFailure);
106 public void preCommit(final FutureCallback<DataTreeCandidate> newCallback) {
107 checkState(State.CAN_COMMIT_COMPLETE);
108 callback = requireNonNull(newCallback);
109 state = State.PRE_COMMIT_PENDING;
111 if (nextFailure == null) {
112 dataTree.startPreCommit(this);
114 failedPreCommit(nextFailure);
119 public void abort(final FutureCallback<Empty> abortCallback) {
120 if (!dataTree.startAbort(this)) {
121 abortCallback.onSuccess(Empty.value());
126 state = State.ABORTED;
128 final Optional<CompletionStage<?>> maybeAborts = userCohorts.abort();
129 if (!maybeAborts.isPresent()) {
130 abortCallback.onSuccess(Empty.value());
134 maybeAborts.get().whenComplete((noop, failure) -> {
135 if (failure != null) {
136 abortCallback.onFailure(failure);
138 abortCallback.onSuccess(Empty.value());
144 public void commit(final FutureCallback<UnsignedLong> newCallback) {
145 checkState(State.PRE_COMMIT_COMPLETE);
146 callback = requireNonNull(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>) callback;
160 LOG.debug("Transaction {} changing state from {} to {}", transactionId, state, newState);
165 void setNewCandidate(final DataTreeCandidateTip dataTreeCandidate) {
166 checkState(State.PRE_COMMIT_COMPLETE);
167 candidate = verifyNotNull(dataTreeCandidate);
170 void successfulCanCommit() {
171 switchState(State.CAN_COMMIT_COMPLETE).onSuccess(Empty.value());
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 * @param futureCallback the callback to invoke on completion, which may be immediate or async.
185 void userPreCommit(final DataTreeCandidate dataTreeCandidate, final FutureCallback<Empty> futureCallback) {
188 final Optional<CompletionStage<Empty>> maybeCanCommitFuture = userCohorts.canCommit(dataTreeCandidate);
189 if (!maybeCanCommitFuture.isPresent()) {
190 doUserPreCommit(futureCallback);
194 maybeCanCommitFuture.get().whenComplete((noop, failure) -> {
195 if (failure != null) {
196 futureCallback.onFailure(failure);
198 doUserPreCommit(futureCallback);
203 private void doUserPreCommit(final FutureCallback<Empty> futureCallback) {
204 final Optional<CompletionStage<Empty>> maybePreCommitFuture = userCohorts.preCommit();
205 if (!maybePreCommitFuture.isPresent()) {
206 futureCallback.onSuccess(Empty.value());
210 maybePreCommitFuture.get().whenComplete((noop, failure) -> {
211 if (failure != null) {
212 futureCallback.onFailure(failure);
214 futureCallback.onSuccess(Empty.value());
219 void successfulPreCommit(final DataTreeCandidateTip dataTreeCandidate) {
220 LOG.trace("Transaction {} prepared candidate {}", transaction, dataTreeCandidate);
221 candidate = verifyNotNull(dataTreeCandidate);
222 switchState(State.PRE_COMMIT_COMPLETE).onSuccess(dataTreeCandidate);
225 void failedPreCommit(final Throwable cause) {
226 if (LOG.isTraceEnabled()) {
227 LOG.trace("Transaction {} failed to prepare", transaction, cause);
229 LOG.error("Transaction {} failed to prepare", transactionId, cause);
233 switchState(State.FAILED).onFailure(cause);
236 void successfulCommit(final UnsignedLong journalIndex, final Runnable onComplete) {
237 final Optional<CompletionStage<Empty>> maybeCommitFuture = userCohorts.commit();
238 if (!maybeCommitFuture.isPresent()) {
239 finishSuccessfulCommit(journalIndex, onComplete);
243 maybeCommitFuture.get().whenComplete((noop, failure) -> {
244 if (failure != null) {
245 LOG.error("User cohorts failed to commit", failure);
248 finishSuccessfulCommit(journalIndex, onComplete);
252 private void finishSuccessfulCommit(final UnsignedLong journalIndex, final Runnable onComplete) {
253 switchState(State.COMMITTED).onSuccess(journalIndex);
257 void failedCommit(final Exception cause) {
258 if (LOG.isTraceEnabled()) {
259 LOG.trace("Transaction {} failed to commit", transaction, cause);
261 LOG.error("Transaction failed to commit", cause);
265 switchState(State.FAILED).onFailure(cause);
269 public State getState() {
273 void reportFailure(final Exception cause) {
274 if (nextFailure == null) {
275 nextFailure = requireNonNull(cause);
277 LOG.debug("Transaction {} already has a set failure, not updating it", transactionId, cause);
282 public boolean isFailed() {
283 return state == State.FAILED || nextFailure != null;
287 ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
288 return super.addToStringAttributes(toStringHelper).add("nextFailure", nextFailure);