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.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import scala.concurrent.Future;
25 * An AbstractThreePhaseCommitCohort implementation used for debugging. If a failure occurs, the transaction
26 * call site is printed.
28 * @author Thomas Pantelis
30 class DebugThreePhaseCommitCohort extends AbstractThreePhaseCommitCohort<Object> {
31 private static final Logger LOG = LoggerFactory.getLogger(DebugThreePhaseCommitCohort.class);
33 private final AbstractThreePhaseCommitCohort<?> delegate;
34 private final Throwable debugContext;
35 private final TransactionIdentifier transactionId;
37 @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_FINAL")
38 private Logger log = LOG;
40 DebugThreePhaseCommitCohort(final TransactionIdentifier transactionId,
41 final AbstractThreePhaseCommitCohort<?> delegate, final Throwable debugContext) {
42 this.delegate = requireNonNull(delegate);
43 this.debugContext = requireNonNull(debugContext);
44 this.transactionId = requireNonNull(transactionId);
47 private <V> ListenableFuture<V> addFutureCallback(final ListenableFuture<V> future) {
48 Futures.addCallback(future, new FutureCallback<V>() {
50 public void onSuccess(final V result) {
55 public void onFailure(final Throwable failure) {
56 log.warn("Transaction {} failed with error \"{}\" - was allocated in the following context",
57 transactionId, failure, debugContext);
59 }, MoreExecutors.directExecutor());
65 public ListenableFuture<Boolean> canCommit() {
66 return addFutureCallback(delegate.canCommit());
70 public ListenableFuture<Void> preCommit() {
71 return addFutureCallback(delegate.preCommit());
75 public ListenableFuture<Void> commit() {
76 return addFutureCallback(delegate.commit());
80 public ListenableFuture<Void> abort() {
81 return delegate.abort();
84 @SuppressWarnings({ "rawtypes", "unchecked" })
86 List<Future<Object>> getCohortFutures() {
87 return ((AbstractThreePhaseCommitCohort)delegate).getCohortFutures();
91 void setLogger(final Logger logger) {