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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.util.List;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import scala.concurrent.Future;
24 * An AbstractThreePhaseCommitCohort implementation used for debugging. If a failure occurs, the transaction
25 * call site is printed.
27 * @author Thomas Pantelis
29 class DebugThreePhaseCommitCohort extends AbstractThreePhaseCommitCohort<Object> {
30 private static final Logger LOG = LoggerFactory.getLogger(DebugThreePhaseCommitCohort.class);
32 private final AbstractThreePhaseCommitCohort<?> delegate;
33 private final Throwable debugContext;
34 private final TransactionIdentifier transactionId;
36 @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_FINAL")
37 private Logger log = LOG;
39 DebugThreePhaseCommitCohort(final TransactionIdentifier transactionId,
40 final AbstractThreePhaseCommitCohort<?> delegate, final Throwable debugContext) {
41 this.delegate = Preconditions.checkNotNull(delegate);
42 this.debugContext = Preconditions.checkNotNull(debugContext);
43 this.transactionId = Preconditions.checkNotNull(transactionId);
46 private <V> ListenableFuture<V> addFutureCallback(final ListenableFuture<V> future) {
47 Futures.addCallback(future, new FutureCallback<V>() {
49 public void onSuccess(final V result) {
54 public void onFailure(final Throwable failure) {
55 log.warn("Transaction {} failed with error \"{}\" - was allocated in the following context",
56 transactionId, failure, debugContext);
58 }, MoreExecutors.directExecutor());
64 public ListenableFuture<Boolean> canCommit() {
65 return addFutureCallback(delegate.canCommit());
69 public ListenableFuture<Void> preCommit() {
70 return addFutureCallback(delegate.preCommit());
74 public ListenableFuture<Void> commit() {
75 return addFutureCallback(delegate.commit());
79 public ListenableFuture<Void> abort() {
80 return delegate.abort();
83 @SuppressWarnings({ "rawtypes", "unchecked" })
85 List<Future<Object>> getCohortFutures() {
86 return ((AbstractThreePhaseCommitCohort)delegate).getCohortFutures();
90 void setLogger(final Logger logger) {