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 @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;
33 private RuntimeException lastBatchedModificationsException;
34 private int totalBatchedModificationsReceived;
35 private int totalOperationsProcessed;
36 private ShardDataTreeCohort cohort;
37 private boolean doImmediateCommit;
38 private ActorRef replySender;
41 private CohortEntry(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
43 this.transaction = requireNonNull(transaction);
44 transactionId = transaction.getIdentifier();
45 this.clientVersion = clientVersion;
48 private CohortEntry(final ShardDataTreeCohort cohort, final short clientVersion) {
49 this.cohort = requireNonNull(cohort);
50 transactionId = cohort.transactionId();
52 this.clientVersion = clientVersion;
55 static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
56 return new CohortEntry(transaction, clientVersion);
59 static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
60 return new CohortEntry(cohort, clientVersion);
63 TransactionIdentifier getTransactionId() {
67 short getClientVersion() {
72 return cohort != null && cohort.isFailed();
75 DataTreeModification getDataTreeModification() {
76 return cohort.getDataTreeModification();
79 ReadWriteShardDataTreeTransaction getTransaction() {
83 int getTotalBatchedModificationsReceived() {
84 return totalBatchedModificationsReceived;
87 int getTotalOperationsProcessed() {
88 return totalOperationsProcessed;
91 RuntimeException getLastBatchedModificationsException() {
92 return lastBatchedModificationsException;
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) {
103 modification.apply(transaction.getSnapshot());
104 } catch (RuntimeException e) {
105 lastBatchedModificationsException = e;
112 void canCommit(final FutureCallback<Empty> callback) {
113 cohort.canCommit(callback);
116 void preCommit(final FutureCallback<DataTreeCandidate> callback) {
117 cohort.preCommit(callback);
120 void commit(final FutureCallback<UnsignedLong> callback) {
121 cohort.commit(callback);
124 void abort(final FutureCallback<Empty> callback) {
125 cohort.abort(callback);
128 void ready(final Optional<SortedSet<String>> participatingShardNames, final CohortDecorator cohortDecorator) {
129 checkState(cohort == null, "cohort was already set");
131 cohort = transaction.ready(participatingShardNames);
133 if (cohortDecorator != null) {
134 // Call the hook for unit tests.
135 cohort = cohortDecorator.decorate(transactionId, cohort);
140 return cohort != null;
143 Optional<SortedSet<String>> getParticipatingShardNames() {
144 return cohort != null ? cohort.getParticipatingShardNames() : Optional.empty();
147 boolean isDoImmediateCommit() {
148 return doImmediateCommit;
151 void setDoImmediateCommit(final boolean doImmediateCommit) {
152 this.doImmediateCommit = doImmediateCommit;
155 ActorRef getReplySender() {
159 void setReplySender(final ActorRef replySender) {
160 this.replySender = replySender;
167 void setShard(final Shard shard) {
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();