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.controller.md.sal.dom.broker.impl;
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.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
21 import org.opendaylight.yangtools.util.DurationStatisticsTracker;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Implementation of blocking three-phase commit-coordination tasks without
27 * support of cancellation.
29 final class CommitCoordinationTask implements Callable<Void> {
36 private static final Logger LOG = LoggerFactory.getLogger(CommitCoordinationTask.class);
37 private final Collection<DOMStoreThreePhaseCommitCohort> cohorts;
38 private final DurationStatisticsTracker commitStatTracker;
39 private final DOMDataWriteTransaction tx;
41 public CommitCoordinationTask(final DOMDataWriteTransaction transaction,
42 final Collection<DOMStoreThreePhaseCommitCohort> cohorts,
43 final DurationStatisticsTracker commitStatTracker) {
44 this.tx = Preconditions.checkNotNull(transaction, "transaction must not be null");
45 this.cohorts = Preconditions.checkNotNull(cohorts, "cohorts must not be null");
46 this.commitStatTracker = commitStatTracker;
50 public Void call() throws TransactionCommitFailedException {
51 final long startTime = commitStatTracker != null ? System.nanoTime() : 0;
53 Phase phase = Phase.canCommit;
56 LOG.debug("Transaction {}: canCommit Started", tx.getIdentifier());
59 phase = Phase.preCommit;
60 LOG.debug("Transaction {}: preCommit Started", tx.getIdentifier());
63 phase = Phase.doCommit;
64 LOG.debug("Transaction {}: doCommit Started", tx.getIdentifier());
67 LOG.debug("Transaction {}: doCommit completed", tx.getIdentifier());
69 } catch (final TransactionCommitFailedException e) {
70 LOG.warn("Tx: {} Error during phase {}, starting Abort", tx.getIdentifier(), phase, e);
74 if (commitStatTracker != null) {
75 commitStatTracker.addDuration(System.nanoTime() - startTime);
82 * Invokes canCommit on underlying cohorts and blocks till
83 * all results are returned.
85 * Valid state transition is from SUBMITTED to CAN_COMMIT,
86 * if currentPhase is not SUBMITTED throws IllegalStateException.
88 * @throws TransactionCommitFailedException
89 * If one of cohorts failed can Commit
92 private void canCommitBlocking() throws TransactionCommitFailedException {
93 for (final ListenableFuture<?> canCommit : canCommitAll()) {
95 final Boolean result = (Boolean)canCommit.get();
96 if (result == null || !result) {
97 throw new TransactionCommitFailedException("Can Commit failed, no detailed cause available.");
99 } catch (InterruptedException | ExecutionException e) {
100 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.
111 * Valid state transition is from SUBMITTED to CAN_COMMIT,
112 * if currentPhase is not SUBMITTED throws IllegalStateException.
114 * @return List of all cohorts futures from can commit phase.
117 private ListenableFuture<?>[] canCommitAll() {
118 final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
120 for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
121 ops[i++] = cohort.canCommit();
128 * Invokes preCommit on underlying cohorts and blocks till
129 * all results are returned.
131 * Valid state transition is from CAN_COMMIT to PRE_COMMIT, if current
132 * state is not CAN_COMMIT
133 * throws IllegalStateException.
135 * @throws TransactionCommitFailedException
136 * If one of cohorts failed preCommit
139 private void preCommitBlocking() throws TransactionCommitFailedException {
140 final ListenableFuture<?>[] preCommitFutures = preCommitAll();
142 for(final ListenableFuture<?> future : preCommitFutures) {
145 } catch (InterruptedException | ExecutionException e) {
146 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[i++] = cohort.preCommit();
175 * Invokes commit on underlying cohorts and blocks till
176 * 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);
198 * Invokes commit on underlying cohorts and returns future which
200 * 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[i++] = cohort.commit();
217 * Aborts transaction.
219 * Invokes {@link DOMStoreThreePhaseCommitCohort#abort()} on all
221 * for all results. If any of the abort failed throws
222 * IllegalStateException,
223 * which will contains originalCause as suppressed Exception.
225 * If aborts we're successful throws supplied exception
227 * @param originalCause
228 * Exception which should be used to fail transaction for
229 * consumers of transaction
230 * future and listeners of transaction failure.
231 * @param phase phase in which the problem ensued
232 * @throws TransactionCommitFailedException
233 * on invocation of this method.
235 * @throws IllegalStateException
238 private void abortBlocking(final TransactionCommitFailedException originalCause) throws TransactionCommitFailedException {
239 Exception cause = originalCause;
241 abortAsyncAll().get();
242 } catch (InterruptedException | ExecutionException e) {
243 LOG.error("Tx: {} Error during Abort.", tx.getIdentifier(), e);
244 cause = new IllegalStateException("Abort failed.", e);
245 cause.addSuppressed(e);
247 Throwables.propagateIfPossible(cause, TransactionCommitFailedException.class);
251 * Invokes abort on underlying cohorts and returns future which
252 * completes once all abort on cohorts are completed.
254 * @return Future which will complete once all cohorts completed
257 @SuppressWarnings({"unchecked", "rawtypes"})
258 private ListenableFuture<Void> abortAsyncAll() {
260 final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
262 for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
263 ops[i++] = cohort.abort();
267 * We are returning all futures as list, not only succeeded ones in
268 * order to fail composite future if any of them failed.
269 * See Futures.allAsList for this description.
271 return (ListenableFuture) Futures.allAsList(ops);