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