2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
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;
27 final class CohortEntry {
28 private final ReadWriteShardDataTreeTransaction transaction;
29 private final TransactionIdentifier transactionId;
30 private final short clientVersion;
32 private RuntimeException lastBatchedModificationsException;
33 private int totalBatchedModificationsReceived;
34 private int totalOperationsProcessed;
35 private ShardDataTreeCohort cohort;
36 private boolean doImmediateCommit;
37 private ActorRef replySender;
40 private CohortEntry(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
42 this.transaction = requireNonNull(transaction);
43 transactionId = transaction.getIdentifier();
44 this.clientVersion = clientVersion;
47 private CohortEntry(final ShardDataTreeCohort cohort, final short clientVersion) {
48 this.cohort = requireNonNull(cohort);
49 transactionId = cohort.transactionId();
51 this.clientVersion = clientVersion;
54 static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
55 return new CohortEntry(transaction, clientVersion);
58 static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
59 return new CohortEntry(cohort, clientVersion);
62 TransactionIdentifier getTransactionId() {
66 short getClientVersion() {
71 return cohort != null && cohort.isFailed();
74 DataTreeModification getDataTreeModification() {
75 return cohort.getDataTreeModification();
78 ReadWriteShardDataTreeTransaction getTransaction() {
82 int getTotalBatchedModificationsReceived() {
83 return totalBatchedModificationsReceived;
86 int getTotalOperationsProcessed() {
87 return totalOperationsProcessed;
90 RuntimeException getLastBatchedModificationsException() {
91 return lastBatchedModificationsException;
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) {
102 modification.apply(transaction.getSnapshot());
103 } catch (RuntimeException e) {
104 lastBatchedModificationsException = e;
111 void canCommit(final FutureCallback<Empty> callback) {
112 cohort.canCommit(callback);
115 void preCommit(final FutureCallback<DataTreeCandidate> callback) {
116 cohort.preCommit(callback);
119 void commit(final FutureCallback<UnsignedLong> callback) {
120 cohort.commit(callback);
123 void abort(final FutureCallback<Empty> callback) {
124 cohort.abort(callback);
127 void ready(final Optional<SortedSet<String>> participatingShardNames, final CohortDecorator cohortDecorator) {
128 checkState(cohort == null, "cohort was already set");
130 cohort = transaction.ready(participatingShardNames);
132 if (cohortDecorator != null) {
133 // Call the hook for unit tests.
134 cohort = cohortDecorator.decorate(transactionId, cohort);
139 return cohort != null;
142 Optional<SortedSet<String>> getParticipatingShardNames() {
143 return cohort != null ? cohort.getParticipatingShardNames() : Optional.empty();
146 boolean isDoImmediateCommit() {
147 return doImmediateCommit;
150 void setDoImmediateCommit(final boolean doImmediateCommit) {
151 this.doImmediateCommit = doImmediateCommit;
154 ActorRef getReplySender() {
158 void setReplySender(final ActorRef replySender) {
159 this.replySender = replySender;
166 void setShard(final Shard shard) {
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();