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 java.util.List;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import scala.concurrent.Future;
23 * An AbstractThreePhaseCommitCohort implementation used for debugging. If a failure occurs, the transaction
24 * call site is printed.
26 * @author Thomas Pantelis
28 class DebugThreePhaseCommitCohort extends AbstractThreePhaseCommitCohort<Object> {
29 private static final Logger LOG = LoggerFactory.getLogger(DebugThreePhaseCommitCohort.class);
31 private final AbstractThreePhaseCommitCohort<?> delegate;
32 private final Throwable debugContext;
33 private final TransactionIdentifier transactionId;
34 private Logger log = LOG;
36 DebugThreePhaseCommitCohort(final TransactionIdentifier transactionId,
37 final AbstractThreePhaseCommitCohort<?> delegate, final Throwable debugContext) {
38 this.delegate = Preconditions.checkNotNull(delegate);
39 this.debugContext = Preconditions.checkNotNull(debugContext);
40 this.transactionId = Preconditions.checkNotNull(transactionId);
43 private <V> ListenableFuture<V> addFutureCallback(final ListenableFuture<V> future) {
44 Futures.addCallback(future, new FutureCallback<V>() {
46 public void onSuccess(final V result) {
51 public void onFailure(final Throwable failure) {
52 log.warn("Transaction {} failed with error \"{}\" - was allocated in the following context",
53 transactionId, failure, debugContext);
55 }, MoreExecutors.directExecutor());
61 public ListenableFuture<Boolean> canCommit() {
62 return addFutureCallback(delegate.canCommit());
66 public ListenableFuture<Void> preCommit() {
67 return addFutureCallback(delegate.preCommit());
71 public ListenableFuture<Void> commit() {
72 return addFutureCallback(delegate.commit());
76 public ListenableFuture<Void> abort() {
77 return delegate.abort();
80 @SuppressWarnings({ "rawtypes", "unchecked" })
82 List<Future<Object>> getCohortFutures() {
83 return ((AbstractThreePhaseCommitCohort)delegate).getCohortFutures();
87 void setLogger(final Logger logger) {