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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.