BUG-8665: fix memory leak around RangeSets
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / SingleCommitCohortProxy.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 akka.dispatch.OnComplete;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.util.Arrays;
15 import java.util.List;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import scala.concurrent.Future;
22
23 /**
24  * A cohort proxy implementation for a single-shard transaction commit. If the transaction was a direct commit
25  * to the shard, this implementation elides the CanCommitTransaction and CommitTransaction messages to the
26  * shard as an optimization.
27  *
28  * @author Thomas Pantelis
29  */
30 class SingleCommitCohortProxy extends AbstractThreePhaseCommitCohort<Object> {
31     private static final Logger LOG = LoggerFactory.getLogger(SingleCommitCohortProxy.class);
32
33     private final ActorContext actorContext;
34     private final Future<Object> cohortFuture;
35     private final TransactionIdentifier transactionId;
36     private volatile DOMStoreThreePhaseCommitCohort delegateCohort = NoOpDOMStoreThreePhaseCommitCohort.INSTANCE;
37     private final OperationCallback.Reference operationCallbackRef;
38
39     SingleCommitCohortProxy(ActorContext actorContext, Future<Object> cohortFuture, TransactionIdentifier transactionId,
40             OperationCallback.Reference operationCallbackRef) {
41         this.actorContext = actorContext;
42         this.cohortFuture = cohortFuture;
43         this.transactionId = Preconditions.checkNotNull(transactionId);
44         this.operationCallbackRef = operationCallbackRef;
45     }
46
47     @Override
48     public ListenableFuture<Boolean> canCommit() {
49         LOG.debug("Tx {} canCommit", transactionId);
50
51         final SettableFuture<Boolean> returnFuture = SettableFuture.create();
52
53         cohortFuture.onComplete(new OnComplete<Object>() {
54             @Override
55             public void onComplete(Throwable failure, Object cohortResponse) {
56                 if (failure != null) {
57                     operationCallbackRef.get().failure();
58                     returnFuture.setException(failure);
59                     return;
60                 }
61
62                 operationCallbackRef.get().success();
63
64                 LOG.debug("Tx {} successfully completed direct commit", transactionId);
65
66                 // The Future was the result of a direct commit to the shard, essentially eliding the
67                 // front-end 3PC coordination. We don't really care about the specific Future
68                 // response object, only that it completed successfully. At this point the Tx is complete
69                 // so return true. The subsequent preCommit and commit phases will be no-ops, ie return
70                 // immediate success, to complete the 3PC for the front-end.
71                 returnFuture.set(Boolean.TRUE);
72             }
73         }, actorContext.getClientDispatcher());
74
75         return returnFuture;
76     }
77
78     @Override
79     public ListenableFuture<Void> preCommit() {
80         return delegateCohort.preCommit();
81     }
82
83     @Override
84     public ListenableFuture<Void> abort() {
85         return delegateCohort.abort();
86     }
87
88     @Override
89     public ListenableFuture<Void> commit() {
90         return delegateCohort.commit();
91     }
92
93     @Override
94     List<Future<Object>> getCohortFutures() {
95         return Arrays.asList(cohortFuture);
96     }
97 }