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