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.data.api.schema.tree.DataTreeCandidate;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25 final class CohortEntry {
26 private final ReadWriteShardDataTreeTransaction transaction;
27 private final TransactionIdentifier transactionId;
28 private final short clientVersion;
30 private RuntimeException lastBatchedModificationsException;
31 private int totalBatchedModificationsReceived;
32 private int totalOperationsProcessed;
33 private ShardDataTreeCohort cohort;
34 private boolean doImmediateCommit;
35 private ActorRef replySender;
38 private CohortEntry(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
40 this.transaction = requireNonNull(transaction);
41 this.transactionId = transaction.getIdentifier();
42 this.clientVersion = clientVersion;
45 private CohortEntry(final ShardDataTreeCohort cohort, final short clientVersion) {
46 this.cohort = requireNonNull(cohort);
47 this.transactionId = cohort.getIdentifier();
48 this.transaction = null;
49 this.clientVersion = clientVersion;
52 static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
53 return new CohortEntry(transaction, clientVersion);
56 static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
57 return new CohortEntry(cohort, clientVersion);
60 TransactionIdentifier getTransactionId() {
64 short getClientVersion() {
69 return cohort != null && cohort.isFailed();
72 DataTreeModification getDataTreeModification() {
73 return cohort.getDataTreeModification();
76 ReadWriteShardDataTreeTransaction getTransaction() {
80 int getTotalBatchedModificationsReceived() {
81 return totalBatchedModificationsReceived;
84 int getTotalOperationsProcessed() {
85 return totalOperationsProcessed;
88 RuntimeException getLastBatchedModificationsException() {
89 return lastBatchedModificationsException;
92 @SuppressWarnings("checkstyle:IllegalCatch")
93 void applyModifications(final List<Modification> modifications) {
94 totalBatchedModificationsReceived++;
95 if (lastBatchedModificationsException == null) {
96 totalOperationsProcessed += modifications.size();
97 for (Modification modification : modifications) {
99 modification.apply(transaction.getSnapshot());
100 } catch (RuntimeException e) {
101 lastBatchedModificationsException = e;
108 void canCommit(final FutureCallback<Void> callback) {
109 cohort.canCommit(callback);
112 void preCommit(final FutureCallback<DataTreeCandidate> callback) {
113 cohort.preCommit(callback);
116 void commit(final FutureCallback<UnsignedLong> callback) {
117 cohort.commit(callback);
120 void abort(final FutureCallback<Void> callback) {
121 cohort.abort(callback);
124 void ready(final Optional<SortedSet<String>> participatingShardNames, final CohortDecorator cohortDecorator) {
125 checkState(cohort == null, "cohort was already set");
127 cohort = transaction.ready(participatingShardNames);
129 if (cohortDecorator != null) {
130 // Call the hook for unit tests.
131 cohort = cohortDecorator.decorate(transactionId, cohort);
136 return cohort != null;
139 Optional<SortedSet<String>> getParticipatingShardNames() {
140 return cohort != null ? cohort.getParticipatingShardNames() : Optional.empty();
143 boolean isDoImmediateCommit() {
144 return doImmediateCommit;
147 void setDoImmediateCommit(final boolean doImmediateCommit) {
148 this.doImmediateCommit = doImmediateCommit;
151 ActorRef getReplySender() {
155 void setReplySender(final ActorRef replySender) {
156 this.replySender = replySender;
163 void setShard(final Shard shard) {
168 public String toString() {
169 final StringBuilder builder = new StringBuilder();
170 builder.append("CohortEntry [transactionId=").append(transactionId).append(", doImmediateCommit=")
171 .append(doImmediateCommit).append("]");
172 return builder.toString();