2 * Copyright (c) 2014 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.mdsal.dom.broker;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Throwables;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.util.Collection;
17 import java.util.concurrent.Callable;
18 import java.util.concurrent.ExecutionException;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.yangtools.util.DurationStatisticsTracker;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Implementation of blocking three-phase commit-coordination tasks without support of cancellation.
30 final class CommitCoordinationTask implements Callable<CommitInfo> {
37 private static final Logger LOG = LoggerFactory.getLogger(CommitCoordinationTask.class);
38 private final Collection<DOMStoreThreePhaseCommitCohort> cohorts;
39 private final DurationStatisticsTracker commitStatTracker;
40 private final DOMDataTreeWriteTransaction tx;
42 CommitCoordinationTask(final DOMDataTreeWriteTransaction transaction,
43 final Collection<DOMStoreThreePhaseCommitCohort> cohorts,
44 final DurationStatisticsTracker commitStatTracker) {
45 this.tx = requireNonNull(transaction, "transaction must not be null");
46 this.cohorts = requireNonNull(cohorts, "cohorts must not be null");
47 this.commitStatTracker = commitStatTracker;
51 public CommitInfo call() throws TransactionCommitFailedException {
52 final long startTime = commitStatTracker != null ? System.nanoTime() : 0;
54 Phase phase = Phase.CAN_COMMIT;
57 LOG.debug("Transaction {}: canCommit Started", tx.getIdentifier());
60 phase = Phase.PRE_COMMIT;
61 LOG.debug("Transaction {}: preCommit Started", tx.getIdentifier());
64 phase = Phase.DO_COMMIT;
65 LOG.debug("Transaction {}: doCommit Started", tx.getIdentifier());
68 LOG.debug("Transaction {}: doCommit completed", tx.getIdentifier());
69 return CommitInfo.empty();
70 } catch (final TransactionCommitFailedException e) {
71 LOG.warn("Tx: {} Error during phase {}, starting Abort", tx.getIdentifier(), phase, e);
75 if (commitStatTracker != null) {
76 commitStatTracker.addDuration(System.nanoTime() - startTime);
82 * Invokes canCommit on underlying cohorts and blocks till all results are returned.
85 * Valid state transition is from SUBMITTED to CAN_COMMIT, if currentPhase is not SUBMITTED throws
86 * IllegalStateException.
88 * @throws TransactionCommitFailedException If one of cohorts failed can Commit
90 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
91 private void canCommitBlocking() throws TransactionCommitFailedException {
92 for (final ListenableFuture<?> canCommit : canCommitAll()) {
94 final Boolean result = (Boolean)canCommit.get();
95 if (result == null || !result) {
96 throw new TransactionCommitFailedException("Can Commit failed, no detailed cause available.");
98 } catch (InterruptedException | ExecutionException e) {
99 throw TransactionCommitFailedExceptionMapper.CAN_COMMIT_ERROR_MAPPER.apply(e);
105 * Invokes canCommit on underlying cohorts and returns composite future which will contain {@link Boolean#TRUE} only
106 * and only if all cohorts returned true.
109 * Valid state transition is from SUBMITTED to CAN_COMMIT, if currentPhase is not SUBMITTED throws
110 * IllegalStateException.
112 * @return List of all cohorts futures from can commit phase.
114 private ListenableFuture<?>[] canCommitAll() {
115 final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
117 for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
118 ops[index++] = cohort.canCommit();
124 * Invokes preCommit on underlying cohorts and blocks until all results are returned.
127 * Valid state transition is from CAN_COMMIT to PRE_COMMIT, if current state is not CAN_COMMIT throws
128 * IllegalStateException.
130 * @throws TransactionCommitFailedException If one of cohorts failed preCommit
132 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
133 private void preCommitBlocking() throws TransactionCommitFailedException {
134 final ListenableFuture<?>[] preCommitFutures = preCommitAll();
136 for (final ListenableFuture<?> future : preCommitFutures) {
139 } catch (InterruptedException | ExecutionException e) {
140 throw TransactionCommitFailedExceptionMapper.PRE_COMMIT_MAPPER.apply(e);
145 * Invokes preCommit on underlying cohorts and returns future which will complete once all preCommit on cohorts
146 * completed or failed.
149 * Valid state transition is from CAN_COMMIT to PRE_COMMIT, if current state is not CAN_COMMIT throws
150 * IllegalStateException.
152 * @return List of all cohorts futures from can commit phase.
154 private ListenableFuture<?>[] preCommitAll() {
155 final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
157 for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
158 ops[index++] = cohort.preCommit();
164 * Invokes commit on underlying cohorts and blocks until all results are returned.
167 * Valid state transition is from PRE_COMMIT to COMMIT, if not throws IllegalStateException.
169 * @throws TransactionCommitFailedException If one of cohorts failed preCommit
171 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
172 private void commitBlocking() throws TransactionCommitFailedException {
173 final ListenableFuture<?>[] commitFutures = commitAll();
175 for (final ListenableFuture<?> future : commitFutures) {
178 } catch (InterruptedException | ExecutionException e) {
179 throw TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e);
184 * Invokes commit on underlying cohorts and returns future which
186 * once all commits on cohorts are completed.
189 * Valid state transition is from PRE_COMMIT to COMMIT, if not throws
190 * IllegalStateException
192 * @return List of all cohorts futures from can commit phase.
194 private ListenableFuture<?>[] commitAll() {
195 final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
197 for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
198 ops[index++] = cohort.commit();
204 * Aborts transaction.
207 * Invokes {@link DOMStoreThreePhaseCommitCohort#abort()} on all cohorts, blocks for all results. If any
208 * of the abort failed throws IllegalStateException, which will contains originalCause as suppressed Exception.
211 * If aborts we're successful throws supplied exception
213 * @param originalCause Exception which should be used to fail transaction for consumers of transaction future
214 * and listeners of transaction failure.
215 * @param phase phase in which the problem ensued
216 * @throws TransactionCommitFailedException on invocation of this method.
217 * @throws IllegalStateException if abort failed.
219 private void abortBlocking(final TransactionCommitFailedException originalCause)
220 throws TransactionCommitFailedException {
221 Exception cause = originalCause;
223 abortAsyncAll().get();
224 } catch (InterruptedException | ExecutionException e) {
225 LOG.error("Tx: {} Error during Abort.", tx.getIdentifier(), e);
226 cause = new IllegalStateException("Abort failed.", e);
227 cause.addSuppressed(e);
229 Throwables.propagateIfPossible(cause, TransactionCommitFailedException.class);
233 * Invokes abort on underlying cohorts and returns future which completes once all abort on cohorts are completed.
235 * @return Future which will complete once all cohorts completed abort.
237 @SuppressWarnings({"unchecked", "rawtypes"})
238 private ListenableFuture<Void> abortAsyncAll() {
240 final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
242 for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
243 ops[index++] = cohort.abort();
247 * We are returning all futures as list, not only succeeded ones in
248 * order to fail composite future if any of them failed.
249 * See Futures.allAsList for this description.
251 return (ListenableFuture) Futures.allAsList(ops);