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