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 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;
23 final class CohortEntry {
24 private final ReadWriteShardDataTreeTransaction transaction;
25 private final TransactionIdentifier transactionId;
26 private final short clientVersion;
28 private RuntimeException lastBatchedModificationsException;
29 private int totalBatchedModificationsReceived;
30 private int totalOperationsProcessed;
31 private ShardDataTreeCohort cohort;
32 private boolean doImmediateCommit;
33 private ActorRef replySender;
36 private CohortEntry(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
38 this.transaction = Preconditions.checkNotNull(transaction);
39 this.transactionId = transaction.getIdentifier();
40 this.clientVersion = clientVersion;
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;
50 static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
51 return new CohortEntry(transaction, clientVersion);
54 static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
55 return new CohortEntry(cohort, clientVersion);
58 TransactionIdentifier getTransactionId() {
62 short getClientVersion() {
67 return cohort != null && cohort.isFailed();
70 DataTreeModification getDataTreeModification() {
71 return cohort.getDataTreeModification();
74 ReadWriteShardDataTreeTransaction getTransaction() {
78 int getTotalBatchedModificationsReceived() {
79 return totalBatchedModificationsReceived;
82 int getTotalOperationsProcessed() {
83 return totalOperationsProcessed;
86 RuntimeException getLastBatchedModificationsException() {
87 return lastBatchedModificationsException;
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) {
97 modification.apply(transaction.getSnapshot());
98 } catch (RuntimeException e) {
99 lastBatchedModificationsException = e;
106 void canCommit(final FutureCallback<Void> callback) {
107 cohort.canCommit(callback);
110 void preCommit(final FutureCallback<DataTreeCandidate> callback) {
111 cohort.preCommit(callback);
114 void commit(final FutureCallback<UnsignedLong> callback) {
115 cohort.commit(callback);
118 void abort(final FutureCallback<Void> callback) {
119 cohort.abort(callback);
122 void ready(final Optional<SortedSet<String>> participatingShardNames, final CohortDecorator cohortDecorator) {
123 Preconditions.checkState(cohort == null, "cohort was already set");
125 cohort = transaction.ready(participatingShardNames);
127 if (cohortDecorator != null) {
128 // Call the hook for unit tests.
129 cohort = cohortDecorator.decorate(transactionId, cohort);
134 return cohort != null;
137 Optional<SortedSet<String>> getParticipatingShardNames() {
138 return cohort != null ? cohort.getParticipatingShardNames() : Optional.empty();
141 boolean isDoImmediateCommit() {
142 return doImmediateCommit;
145 void setDoImmediateCommit(final boolean doImmediateCommit) {
146 this.doImmediateCommit = doImmediateCommit;
149 ActorRef getReplySender() {
153 void setReplySender(final ActorRef replySender) {
154 this.replySender = replySender;
161 void setShard(final Shard shard) {
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();