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