Fix findbugs warnings
[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 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;
22
23 /**
24  * An AbstractThreePhaseCommitCohort implementation used for debugging. If a failure occurs, the transaction
25  * call site is printed.
26  *
27  * @author Thomas Pantelis
28  */
29 class DebugThreePhaseCommitCohort extends AbstractThreePhaseCommitCohort<Object> {
30     private static final Logger LOG = LoggerFactory.getLogger(DebugThreePhaseCommitCohort.class);
31
32     private final AbstractThreePhaseCommitCohort<?> delegate;
33     private final Throwable debugContext;
34     private final TransactionIdentifier transactionId;
35
36     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_FINAL")
37     private Logger log = LOG;
38
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);
44     }
45
46     private <V> ListenableFuture<V> addFutureCallback(final ListenableFuture<V> future) {
47         Futures.addCallback(future, new FutureCallback<V>() {
48             @Override
49             public void onSuccess(final V result) {
50                 // no-op
51             }
52
53             @Override
54             public void onFailure(final Throwable failure) {
55                 log.warn("Transaction {} failed with error \"{}\" - was allocated in the following context",
56                         transactionId, failure, debugContext);
57             }
58         }, MoreExecutors.directExecutor());
59
60         return future;
61     }
62
63     @Override
64     public ListenableFuture<Boolean> canCommit() {
65         return addFutureCallback(delegate.canCommit());
66     }
67
68     @Override
69     public ListenableFuture<Void> preCommit() {
70         return addFutureCallback(delegate.preCommit());
71     }
72
73     @Override
74     public ListenableFuture<Void> commit() {
75         return addFutureCallback(delegate.commit());
76     }
77
78     @Override
79     public ListenableFuture<Void> abort() {
80         return delegate.abort();
81     }
82
83     @SuppressWarnings({ "rawtypes", "unchecked" })
84     @Override
85     List<Future<Object>> getCohortFutures() {
86         return ((AbstractThreePhaseCommitCohort)delegate).getCohortFutures();
87     }
88
89     @VisibleForTesting
90     void setLogger(final Logger logger) {
91         this.log = logger;
92     }
93 }