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