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.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.controller.cluster.datastore.utils.PruningDataTreeModification;
24 import org.opendaylight.yangtools.concepts.Identifiable;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import scala.concurrent.Future;
32 final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Identifiable<TransactionIdentifier> {
33 private static final Logger LOG = LoggerFactory.getLogger(SimpleShardDataTreeCohort.class);
34 private static final ListenableFuture<Void> VOID_FUTURE = Futures.immediateFuture(null);
35 private final DataTreeModification transaction;
36 private final ShardDataTree dataTree;
37 private final TransactionIdentifier transactionId;
38 private final CompositeDataTreeCohort userCohorts;
40 private State state = State.READY;
41 private DataTreeCandidateTip candidate;
42 private FutureCallback<?> callback;
43 private Exception nextFailure;
45 SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction,
46 final TransactionIdentifier transactionId, final CompositeDataTreeCohort userCohorts) {
47 this.dataTree = Preconditions.checkNotNull(dataTree);
48 this.transaction = Preconditions.checkNotNull(transaction);
49 this.transactionId = Preconditions.checkNotNull(transactionId);
50 this.userCohorts = Preconditions.checkNotNull(userCohorts);
54 public TransactionIdentifier getIdentifier() {
59 DataTreeCandidateTip getCandidate() {
64 public DataTreeModification getDataTreeModification() {
65 DataTreeModification dataTreeModification = transaction;
66 if (transaction instanceof PruningDataTreeModification){
67 dataTreeModification = ((PruningDataTreeModification) transaction).getResultingModification();
69 return dataTreeModification;
72 private void checkState(State expected) {
73 Preconditions.checkState(state == expected, "State %s does not match expected state %s", state, expected);
77 public void canCommit(final FutureCallback<Void> callback) {
78 if(state == State.CAN_COMMIT_PENDING) {
82 checkState(State.READY);
83 this.callback = Preconditions.checkNotNull(callback);
84 state = State.CAN_COMMIT_PENDING;
85 dataTree.startCanCommit(this);
89 public void preCommit(final FutureCallback<DataTreeCandidate> callback) {
90 checkState(State.CAN_COMMIT_COMPLETE);
91 this.callback = Preconditions.checkNotNull(callback);
92 state = State.PRE_COMMIT_PENDING;
94 if (nextFailure == null) {
95 dataTree.startPreCommit(this);
97 failedPreCommit(nextFailure);
102 public ListenableFuture<Void> abort() {
103 dataTree.startAbort(this);
104 state = State.ABORTED;
106 final Optional<Future<Iterable<Object>>> maybeAborts = userCohorts.abort();
107 if (!maybeAborts.isPresent()) {
111 final Future<Iterable<Object>> aborts = maybeAborts.get();
112 if (aborts.isCompleted()) {
116 final SettableFuture<Void> ret = SettableFuture.create();
117 aborts.onComplete(new OnComplete<Iterable<Object>>() {
119 public void onComplete(final Throwable failure, final Iterable<Object> objs) {
120 if (failure != null) {
121 ret.setException(failure);
126 }, ExecutionContexts.global());
132 public void commit(final FutureCallback<UnsignedLong> callback) {
133 checkState(State.PRE_COMMIT_COMPLETE);
134 this.callback = Preconditions.checkNotNull(callback);
135 state = State.COMMIT_PENDING;
136 dataTree.startCommit(this, candidate);
139 private <T> FutureCallback<T> switchState(final State newState) {
140 @SuppressWarnings("unchecked")
141 final FutureCallback<T> ret = (FutureCallback<T>) this.callback;
142 this.callback = null;
143 LOG.debug("Transaction {} changing state from {} to {}", transactionId, state, newState);
144 this.state = newState;
148 void successfulCanCommit() {
149 switchState(State.CAN_COMMIT_COMPLETE).onSuccess(null);
152 void failedCanCommit(final Exception cause) {
153 switchState(State.FAILED).onFailure(cause);
157 * Run user-defined canCommit and preCommit hooks. We want to run these before we initiate persistence so that
158 * any failure to validate is propagated before we record the transaction.
160 * @param candidate {@link DataTreeCandidate} under consideration
161 * @throws ExecutionException
162 * @throws TimeoutException
164 // FIXME: this should be asynchronous
165 void userPreCommit(final DataTreeCandidate candidate) throws ExecutionException, TimeoutException {
166 userCohorts.canCommit(candidate);
167 userCohorts.preCommit();
170 void successfulPreCommit(final DataTreeCandidateTip candidate) {
171 LOG.trace("Transaction {} prepared candidate {}", transaction, candidate);
172 this.candidate = Verify.verifyNotNull(candidate);
173 switchState(State.PRE_COMMIT_COMPLETE).onSuccess(candidate);
176 void failedPreCommit(final Exception cause) {
177 if (LOG.isTraceEnabled()) {
178 LOG.trace("Transaction {} failed to prepare", transaction, cause);
180 LOG.error("Transaction {} failed to prepare", transactionId, cause);
184 switchState(State.FAILED).onFailure(cause);
187 void successfulCommit(final UnsignedLong journalIndex) {
189 userCohorts.commit();
190 } catch (TimeoutException | ExecutionException e) {
191 // We are probably dead, depending on what the cohorts end up doing
192 LOG.error("User cohorts failed to commit", e);
195 switchState(State.COMMITTED).onSuccess(journalIndex);
198 void failedCommit(final Exception cause) {
199 if (LOG.isTraceEnabled()) {
200 LOG.trace("Transaction {} failed to commit", transaction, cause);
202 LOG.error("Transaction failed to commit", cause);
206 switchState(State.FAILED).onFailure(cause);
210 public State getState() {
214 void reportFailure(final Exception cause) {
215 this.nextFailure = Preconditions.checkNotNull(cause);
219 public boolean isFailed() {
220 return state == State.FAILED || nextFailure != null;