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.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;
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;
39 private State state = State.READY;
40 private DataTreeCandidateTip candidate;
41 private FutureCallback<?> callback;
42 private Exception nextFailure;
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);
53 public TransactionIdentifier getIdentifier() {
58 DataTreeCandidateTip getCandidate() {
64 DataTreeModification getDataTreeModification() {
68 private void checkState(final State expected) {
69 Preconditions.checkState(state == expected, "State %s does not match expected state %s", state, expected);
73 public void canCommit(final FutureCallback<Void> newCallback) {
74 if (state == State.CAN_COMMIT_PENDING) {
78 checkState(State.READY);
79 this.callback = Preconditions.checkNotNull(newCallback);
80 state = State.CAN_COMMIT_PENDING;
81 dataTree.startCanCommit(this);
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;
90 if (nextFailure == null) {
91 dataTree.startPreCommit(this);
93 failedPreCommit(nextFailure);
98 public ListenableFuture<Void> abort() {
99 dataTree.startAbort(this);
100 state = State.ABORTED;
102 final Optional<Future<Iterable<Object>>> maybeAborts = userCohorts.abort();
103 if (!maybeAborts.isPresent()) {
107 final Future<Iterable<Object>> aborts = maybeAborts.get();
108 if (aborts.isCompleted()) {
112 final SettableFuture<Void> ret = SettableFuture.create();
113 aborts.onComplete(new OnComplete<Iterable<Object>>() {
115 public void onComplete(final Throwable failure, final Iterable<Object> objs) {
116 if (failure != null) {
117 ret.setException(failure);
122 }, ExecutionContexts.global());
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);
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;
144 void successfulCanCommit() {
145 switchState(State.CAN_COMMIT_COMPLETE).onSuccess(null);
148 void failedCanCommit(final Exception cause) {
149 switchState(State.FAILED).onFailure(cause);
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.
156 * @param dataTreeCandidate {@link DataTreeCandidate} under consideration
157 * @throws ExecutionException if the operation fails
158 * @throws TimeoutException if the operation times out
160 // FIXME: this should be asynchronous
161 void userPreCommit(final DataTreeCandidate dataTreeCandidate) throws ExecutionException, TimeoutException {
162 userCohorts.canCommit(dataTreeCandidate);
163 userCohorts.preCommit();
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);
172 void failedPreCommit(final Exception cause) {
173 if (LOG.isTraceEnabled()) {
174 LOG.trace("Transaction {} failed to prepare", transaction, cause);
176 LOG.error("Transaction {} failed to prepare", transactionId, cause);
180 switchState(State.FAILED).onFailure(cause);
183 void successfulCommit(final UnsignedLong journalIndex) {
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);
191 switchState(State.COMMITTED).onSuccess(journalIndex);
194 void failedCommit(final Exception cause) {
195 if (LOG.isTraceEnabled()) {
196 LOG.trace("Transaction {} failed to commit", transaction, cause);
198 LOG.error("Transaction failed to commit", cause);
202 switchState(State.FAILED).onFailure(cause);
206 public State getState() {
210 void reportFailure(final Exception cause) {
211 this.nextFailure = Preconditions.checkNotNull(cause);
215 public boolean isFailed() {
216 return state == State.FAILED || nextFailure != null;