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 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.common.Empty;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
26 final class CohortEntry {
27 private final ReadWriteShardDataTreeTransaction transaction;
28 private final TransactionIdentifier transactionId;
29 private final short clientVersion;
31 private RuntimeException lastBatchedModificationsException;
32 private int totalBatchedModificationsReceived;
33 private int totalOperationsProcessed;
34 private ShardDataTreeCohort cohort;
35 private boolean doImmediateCommit;
36 private ActorRef replySender;
39 private CohortEntry(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
41 this.transaction = requireNonNull(transaction);
42 transactionId = transaction.getIdentifier();
43 this.clientVersion = clientVersion;
46 private CohortEntry(final ShardDataTreeCohort cohort, final short clientVersion) {
47 this.cohort = requireNonNull(cohort);
48 transactionId = cohort.getIdentifier();
50 this.clientVersion = clientVersion;
53 static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
54 return new CohortEntry(transaction, clientVersion);
57 static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
58 return new CohortEntry(cohort, clientVersion);
61 TransactionIdentifier getTransactionId() {
65 short getClientVersion() {
70 return cohort != null && cohort.isFailed();
73 DataTreeModification getDataTreeModification() {
74 return cohort.getDataTreeModification();
77 ReadWriteShardDataTreeTransaction getTransaction() {
81 int getTotalBatchedModificationsReceived() {
82 return totalBatchedModificationsReceived;
85 int getTotalOperationsProcessed() {
86 return totalOperationsProcessed;
89 RuntimeException getLastBatchedModificationsException() {
90 return lastBatchedModificationsException;
93 @SuppressWarnings("checkstyle:IllegalCatch")
94 void applyModifications(final List<Modification> modifications) {
95 totalBatchedModificationsReceived++;
96 if (lastBatchedModificationsException == null) {
97 totalOperationsProcessed += modifications.size();
98 for (Modification modification : modifications) {
100 modification.apply(transaction.getSnapshot());
101 } catch (RuntimeException e) {
102 lastBatchedModificationsException = e;
109 void canCommit(final FutureCallback<Empty> callback) {
110 cohort.canCommit(callback);
113 void preCommit(final FutureCallback<DataTreeCandidate> callback) {
114 cohort.preCommit(callback);
117 void commit(final FutureCallback<UnsignedLong> callback) {
118 cohort.commit(callback);
121 void abort(final FutureCallback<Empty> callback) {
122 cohort.abort(callback);
125 void ready(final Optional<SortedSet<String>> participatingShardNames, final CohortDecorator cohortDecorator) {
126 checkState(cohort == null, "cohort was already set");
128 cohort = transaction.ready(participatingShardNames);
130 if (cohortDecorator != null) {
131 // Call the hook for unit tests.
132 cohort = cohortDecorator.decorate(transactionId, cohort);
137 return cohort != null;
140 Optional<SortedSet<String>> getParticipatingShardNames() {
141 return cohort != null ? cohort.getParticipatingShardNames() : Optional.empty();
144 boolean isDoImmediateCommit() {
145 return doImmediateCommit;
148 void setDoImmediateCommit(final boolean doImmediateCommit) {
149 this.doImmediateCommit = doImmediateCommit;
152 ActorRef getReplySender() {
156 void setReplySender(final ActorRef replySender) {
157 this.replySender = replySender;
164 void setShard(final Shard shard) {
169 public String toString() {
170 final StringBuilder builder = new StringBuilder();
171 builder.append("CohortEntry [transactionId=").append(transactionId).append(", doImmediateCommit=")
172 .append(doImmediateCommit).append("]");
173 return builder.toString();