Bump to odlparent 2.0.0
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DebugThreePhaseCommitCohort.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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;
21
22 /**
23  * An AbstractThreePhaseCommitCohort implementation used for debugging. If a failure occurs, the transaction
24  * call site is printed.
25  *
26  * @author Thomas Pantelis
27  */
28 class DebugThreePhaseCommitCohort extends AbstractThreePhaseCommitCohort<Object> {
29     private static final Logger LOG = LoggerFactory.getLogger(DebugThreePhaseCommitCohort.class);
30
31     private final AbstractThreePhaseCommitCohort<?> delegate;
32     private final Throwable debugContext;
33     private final TransactionIdentifier transactionId;
34     private Logger log = LOG;
35
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);
41     }
42
43     private <V> ListenableFuture<V> addFutureCallback(final ListenableFuture<V> future) {
44         Futures.addCallback(future, new FutureCallback<V>() {
45             @Override
46             public void onSuccess(final V result) {
47                 // no-op
48             }
49
50             @Override
51             public void onFailure(final Throwable failure) {
52                 log.warn("Transaction {} failed with error \"{}\" - was allocated in the following context",
53                         transactionId, failure, debugContext);
54             }
55         }, MoreExecutors.directExecutor());
56
57         return future;
58     }
59
60     @Override
61     public ListenableFuture<Boolean> canCommit() {
62         return addFutureCallback(delegate.canCommit());
63     }
64
65     @Override
66     public ListenableFuture<Void> preCommit() {
67         return addFutureCallback(delegate.preCommit());
68     }
69
70     @Override
71     public ListenableFuture<Void> commit() {
72         return addFutureCallback(delegate.commit());
73     }
74
75     @Override
76     public ListenableFuture<Void> abort() {
77         return delegate.abort();
78     }
79
80     @SuppressWarnings({ "rawtypes", "unchecked" })
81     @Override
82     List<Future<Object>> getCohortFutures() {
83         return ((AbstractThreePhaseCommitCohort)delegate).getCohortFutures();
84     }
85
86     @VisibleForTesting
87     void setLogger(final Logger log) {
88         this.log = log;
89     }
90 }