atomic-storage: remove type dependency at segment level I/O
[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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorRef;
14 import com.google.common.primitives.UnsignedLong;
15 import com.google.common.util.concurrent.FutureCallback;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.SortedSet;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.ShardCommitCoordinator.CohortDecorator;
22 import org.opendaylight.controller.cluster.datastore.modification.Modification;
23 import org.opendaylight.yangtools.yang.common.Empty;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
26
27 @Deprecated(since = "9.0.0", forRemoval = true)
28 final class CohortEntry {
29     private final ReadWriteShardDataTreeTransaction transaction;
30     private final TransactionIdentifier transactionId;
31     private final short clientVersion;
32
33     private RuntimeException lastBatchedModificationsException;
34     private int totalBatchedModificationsReceived;
35     private int totalOperationsProcessed;
36     private ShardDataTreeCohort cohort;
37     private boolean doImmediateCommit;
38     private ActorRef replySender;
39     private Shard shard;
40
41     private CohortEntry(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
42         cohort = null;
43         this.transaction = requireNonNull(transaction);
44         transactionId = transaction.getIdentifier();
45         this.clientVersion = clientVersion;
46     }
47
48     private CohortEntry(final ShardDataTreeCohort cohort, final short clientVersion) {
49         this.cohort = requireNonNull(cohort);
50         transactionId = cohort.transactionId();
51         transaction = null;
52         this.clientVersion = clientVersion;
53     }
54
55     static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
56         return new CohortEntry(transaction, clientVersion);
57     }
58
59     static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
60         return new CohortEntry(cohort, clientVersion);
61     }
62
63     TransactionIdentifier getTransactionId() {
64         return transactionId;
65     }
66
67     short getClientVersion() {
68         return clientVersion;
69     }
70
71     boolean isFailed() {
72         return cohort != null && cohort.isFailed();
73     }
74
75     DataTreeModification getDataTreeModification() {
76         return cohort.getDataTreeModification();
77     }
78
79     ReadWriteShardDataTreeTransaction getTransaction() {
80         return transaction;
81     }
82
83     int getTotalBatchedModificationsReceived() {
84         return totalBatchedModificationsReceived;
85     }
86
87     int getTotalOperationsProcessed() {
88         return totalOperationsProcessed;
89     }
90
91     RuntimeException getLastBatchedModificationsException() {
92         return lastBatchedModificationsException;
93     }
94
95     @SuppressWarnings("checkstyle:IllegalCatch")
96     @SuppressFBWarnings(value = "THROWS_METHOD_THROWS_RUNTIMEEXCEPTION", justification = "Re-thrown")
97     void applyModifications(final List<Modification> modifications) {
98         totalBatchedModificationsReceived++;
99         if (lastBatchedModificationsException == null) {
100             totalOperationsProcessed += modifications.size();
101             for (Modification modification : modifications) {
102                 try {
103                     modification.apply(transaction.getSnapshot());
104                 } catch (RuntimeException e) {
105                     lastBatchedModificationsException = e;
106                     throw e;
107                 }
108             }
109         }
110     }
111
112     void canCommit(final FutureCallback<Empty> callback) {
113         cohort.canCommit(callback);
114     }
115
116     void preCommit(final FutureCallback<DataTreeCandidate> callback) {
117         cohort.preCommit(callback);
118     }
119
120     void commit(final FutureCallback<UnsignedLong> callback) {
121         cohort.commit(callback);
122     }
123
124     void abort(final FutureCallback<Empty> callback) {
125         cohort.abort(callback);
126     }
127
128     void ready(final Optional<SortedSet<String>> participatingShardNames, final CohortDecorator cohortDecorator) {
129         checkState(cohort == null, "cohort was already set");
130
131         cohort = transaction.ready(participatingShardNames);
132
133         if (cohortDecorator != null) {
134             // Call the hook for unit tests.
135             cohort = cohortDecorator.decorate(transactionId, cohort);
136         }
137     }
138
139     boolean isSealed() {
140         return cohort != null;
141     }
142
143     Optional<SortedSet<String>> getParticipatingShardNames() {
144         return cohort != null ? cohort.getParticipatingShardNames() : Optional.empty();
145     }
146
147     boolean isDoImmediateCommit() {
148         return doImmediateCommit;
149     }
150
151     void setDoImmediateCommit(final boolean doImmediateCommit) {
152         this.doImmediateCommit = doImmediateCommit;
153     }
154
155     ActorRef getReplySender() {
156         return replySender;
157     }
158
159     void setReplySender(final ActorRef replySender) {
160         this.replySender = replySender;
161     }
162
163     Shard getShard() {
164         return shard;
165     }
166
167     void setShard(final Shard shard) {
168         this.shard = shard;
169     }
170
171     @Override
172     public String toString() {
173         final StringBuilder builder = new StringBuilder();
174         builder.append("CohortEntry [transactionId=").append(transactionId).append(", doImmediateCommit=")
175                 .append(doImmediateCommit).append("]");
176         return builder.toString();
177     }
178 }