2 * Copyright (c) 2015 Brocade Communications 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 static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18 import java.util.List;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.mdsal.common.api.CommitInfo;
21 import org.opendaylight.yangtools.yang.common.Empty;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import scala.concurrent.Future;
27 * An AbstractThreePhaseCommitCohort implementation used for debugging. If a failure occurs, the transaction
28 * call site is printed.
30 * @author Thomas Pantelis
32 class DebugThreePhaseCommitCohort extends AbstractThreePhaseCommitCohort<Object> {
33 private static final Logger LOG = LoggerFactory.getLogger(DebugThreePhaseCommitCohort.class);
35 private final AbstractThreePhaseCommitCohort<?> delegate;
36 private final Throwable debugContext;
37 private final TransactionIdentifier transactionId;
39 @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_FINAL")
40 private Logger log = LOG;
42 DebugThreePhaseCommitCohort(final TransactionIdentifier transactionId,
43 final AbstractThreePhaseCommitCohort<?> delegate, final Throwable debugContext) {
44 this.delegate = requireNonNull(delegate);
45 this.debugContext = requireNonNull(debugContext);
46 this.transactionId = requireNonNull(transactionId);
49 private <V> ListenableFuture<V> addFutureCallback(final ListenableFuture<V> future) {
50 Futures.addCallback(future, new FutureCallback<V>() {
52 public void onSuccess(final V result) {
57 public void onFailure(final Throwable failure) {
58 log.warn("Transaction {} failed with error \"{}\" - was allocated in the following context",
59 transactionId, failure, debugContext);
61 }, MoreExecutors.directExecutor());
67 public ListenableFuture<Boolean> canCommit() {
68 return addFutureCallback(delegate.canCommit());
72 public ListenableFuture<Empty> preCommit() {
73 return addFutureCallback(delegate.preCommit());
77 public ListenableFuture<? extends CommitInfo> commit() {
78 return addFutureCallback(delegate.commit());
82 public ListenableFuture<Empty> abort() {
83 return delegate.abort();
86 @SuppressWarnings({ "rawtypes", "unchecked" })
88 List<Future<Object>> getCohortFutures() {
89 return ((AbstractThreePhaseCommitCohort)delegate).getCohortFutures();
93 void setLogger(final Logger logger) {