BUG-8665: fix memory leak around RangeSets
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / CohortEntry.java
1 /*
2  * Copyright (c) 2014 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.actor.ActorRef;
11 import com.google.common.base.Preconditions;
12 import com.google.common.primitives.UnsignedLong;
13 import com.google.common.util.concurrent.FutureCallback;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.ShardCommitCoordinator.CohortDecorator;
16 import org.opendaylight.controller.cluster.datastore.modification.Modification;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
19
20 final class CohortEntry {
21     private final ReadWriteShardDataTreeTransaction transaction;
22     private final TransactionIdentifier transactionId;
23     private final short clientVersion;
24
25     private RuntimeException lastBatchedModificationsException;
26     private int totalBatchedModificationsReceived;
27     private ShardDataTreeCohort cohort;
28     private boolean doImmediateCommit;
29     private ActorRef replySender;
30     private Shard shard;
31
32     private CohortEntry(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
33         this.transaction = Preconditions.checkNotNull(transaction);
34         this.transactionId = transaction.getIdentifier();
35         this.clientVersion = clientVersion;
36     }
37
38     private CohortEntry(final ShardDataTreeCohort cohort, final short clientVersion) {
39         this.cohort = Preconditions.checkNotNull(cohort);
40         this.transactionId = cohort.getIdentifier();
41         this.transaction = null;
42         this.clientVersion = clientVersion;
43     }
44
45     static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
46         return new CohortEntry(transaction, clientVersion);
47     }
48
49     static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
50         return new CohortEntry(cohort, clientVersion);
51     }
52
53     TransactionIdentifier getTransactionId() {
54         return transactionId;
55     }
56
57     short getClientVersion() {
58         return clientVersion;
59     }
60
61     boolean isFailed() {
62         return cohort != null && cohort.isFailed();
63     }
64
65     DataTreeModification getDataTreeModification() {
66         return cohort.getDataTreeModification();
67     }
68
69     ReadWriteShardDataTreeTransaction getTransaction() {
70         return transaction;
71     }
72
73     int getTotalBatchedModificationsReceived() {
74         return totalBatchedModificationsReceived;
75     }
76
77     RuntimeException getLastBatchedModificationsException() {
78         return lastBatchedModificationsException;
79     }
80
81     @SuppressWarnings("checkstyle:IllegalCatch")
82     void applyModifications(final Iterable<Modification> modifications) {
83         totalBatchedModificationsReceived++;
84         if (lastBatchedModificationsException == null) {
85             for (Modification modification : modifications) {
86                 try {
87                     modification.apply(transaction.getSnapshot());
88                 } catch (RuntimeException e) {
89                     lastBatchedModificationsException = e;
90                     throw e;
91                 }
92             }
93         }
94     }
95
96     void canCommit(final FutureCallback<Void> callback) {
97         cohort.canCommit(callback);
98     }
99
100     void preCommit(final FutureCallback<DataTreeCandidate> callback) {
101         cohort.preCommit(callback);
102     }
103
104     void commit(final FutureCallback<UnsignedLong> callback) {
105         cohort.commit(callback);
106     }
107
108     void abort(final FutureCallback<Void> callback) {
109         cohort.abort(callback);
110     }
111
112     void ready(final CohortDecorator cohortDecorator) {
113         Preconditions.checkState(cohort == null, "cohort was already set");
114
115         cohort = transaction.ready();
116
117         if (cohortDecorator != null) {
118             // Call the hook for unit tests.
119             cohort = cohortDecorator.decorate(transactionId, cohort);
120         }
121     }
122
123     boolean isDoImmediateCommit() {
124         return doImmediateCommit;
125     }
126
127     void setDoImmediateCommit(final boolean doImmediateCommit) {
128         this.doImmediateCommit = doImmediateCommit;
129     }
130
131     ActorRef getReplySender() {
132         return replySender;
133     }
134
135     void setReplySender(final ActorRef replySender) {
136         this.replySender = replySender;
137     }
138
139     Shard getShard() {
140         return shard;
141     }
142
143     void setShard(final Shard shard) {
144         this.shard = shard;
145     }
146
147     @Override
148     public String toString() {
149         final StringBuilder builder = new StringBuilder();
150         builder.append("CohortEntry [transactionId=").append(transactionId).append(", doImmediateCommit=")
151                 .append(doImmediateCommit).append("]");
152         return builder.toString();
153     }
154 }