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
9 package org.opendaylight.mdsal.dom.broker;
11 import com.google.common.base.Preconditions;
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 java.util.Collection;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.mdsal.common.api.CommitInfo;
19 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.yangtools.util.DurationStatisticsTracker;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Implementation of blocking three-phase commit-coordination tasks without
28 * 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 = Preconditions.checkNotNull(transaction, "transaction must not be null");
46 this.cohorts = Preconditions.checkNotNull(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
83 * all results are returned.
86 * Valid state transition is from SUBMITTED to CAN_COMMIT,
87 * if currentPhase is not SUBMITTED throws IllegalStateException.
89 * @throws TransactionCommitFailedException
90 * If one of cohorts failed can Commit
93 private void canCommitBlocking() throws TransactionCommitFailedException {
94 for (final ListenableFuture<?> canCommit : canCommitAll()) {
96 final Boolean result = (Boolean)canCommit.get();
97 if (result == null || !result) {
98 throw new TransactionCommitFailedException("Can Commit failed, no detailed cause available.");
100 } catch (InterruptedException | ExecutionException e) {
101 throw TransactionCommitFailedExceptionMapper.CAN_COMMIT_ERROR_MAPPER.apply(e);
107 * Invokes canCommit on underlying cohorts and returns composite future
108 * which will contains {@link Boolean#TRUE} only and only if
109 * all cohorts returned true.
112 * Valid state transition is from SUBMITTED to CAN_COMMIT,
113 * if currentPhase is not SUBMITTED throws IllegalStateException.
115 * @return List of all cohorts futures from can commit phase.
118 private ListenableFuture<?>[] canCommitAll() {
119 final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
121 for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
122 ops[index++] = cohort.canCommit();
128 * Invokes preCommit on underlying cohorts and blocks till
129 * all results are returned.
132 * Valid state transition is from CAN_COMMIT to PRE_COMMIT, if current
133 * state is not CAN_COMMIT
134 * throws IllegalStateException.
136 * @throws TransactionCommitFailedException
137 * If one of cohorts failed preCommit
140 private void preCommitBlocking() throws TransactionCommitFailedException {
141 final ListenableFuture<?>[] preCommitFutures = preCommitAll();
143 for (final ListenableFuture<?> future : preCommitFutures) {
146 } catch (InterruptedException | ExecutionException e) {
147 throw TransactionCommitFailedExceptionMapper.PRE_COMMIT_MAPPER.apply(e);
152 * Invokes preCommit on underlying cohorts and returns future
153 * which will complete once all preCommit on cohorts completed or
157 * Valid state transition is from CAN_COMMIT to PRE_COMMIT, if current
158 * state is not CAN_COMMIT
159 * throws IllegalStateException.
161 * @return List of all cohorts futures from can commit phase.
164 private ListenableFuture<?>[] preCommitAll() {
165 final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
167 for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
168 ops[index++] = cohort.preCommit();
174 * Invokes commit on underlying cohorts and blocks till
175 * all results are returned.
178 * Valid state transition is from PRE_COMMIT to COMMIT, if not throws
179 * IllegalStateException.
181 * @throws TransactionCommitFailedException
182 * If one of cohorts failed preCommit
185 private void commitBlocking() throws TransactionCommitFailedException {
186 final ListenableFuture<?>[] commitFutures = commitAll();
188 for (final ListenableFuture<?> future : commitFutures) {
191 } catch (InterruptedException | ExecutionException e) {
192 throw TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e);
197 * Invokes commit on underlying cohorts and returns future which
199 * once all commits on cohorts are completed.
202 * Valid state transition is from PRE_COMMIT to COMMIT, if not throws
203 * IllegalStateException
205 * @return List of all cohorts futures from can commit phase.
207 private ListenableFuture<?>[] commitAll() {
208 final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
210 for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
211 ops[index++] = cohort.commit();
217 * Aborts transaction.
220 * Invokes {@link DOMStoreThreePhaseCommitCohort#abort()} on all
222 * for all results. If any of the abort failed throws
223 * IllegalStateException,
224 * which will contains originalCause as suppressed Exception.
227 * If aborts we're successful throws supplied exception
229 * @param originalCause
230 * Exception which should be used to fail transaction for
231 * consumers of transaction
232 * future and listeners of transaction failure.
233 * @param phase phase in which the problem ensued
234 * @throws TransactionCommitFailedException
235 * on invocation of this method.
237 * @throws IllegalStateException
240 private void abortBlocking(final TransactionCommitFailedException originalCause)
241 throws TransactionCommitFailedException {
242 Exception cause = originalCause;
244 abortAsyncAll().get();
245 } catch (InterruptedException | ExecutionException e) {
246 LOG.error("Tx: {} Error during Abort.", tx.getIdentifier(), e);
247 cause = new IllegalStateException("Abort failed.", e);
248 cause.addSuppressed(e);
250 Throwables.propagateIfPossible(cause, TransactionCommitFailedException.class);
254 * Invokes abort on underlying cohorts and returns future which
255 * completes once all abort on cohorts are completed.
257 * @return Future which will complete once all cohorts completed
260 @SuppressWarnings({"unchecked", "rawtypes"})
261 private ListenableFuture<Void> abortAsyncAll() {
263 final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
265 for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
266 ops[index++] = cohort.abort();
270 * We are returning all futures as list, not only succeeded ones in
271 * order to fail composite future if any of them failed.
272 * See Futures.allAsList for this description.
274 return (ListenableFuture) Futures.allAsList(ops);