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.Optional;
15 import java.util.SortedSet;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.ShardCommitCoordinator.CohortDecorator;
18 import org.opendaylight.controller.cluster.datastore.modification.Modification;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
22 final class CohortEntry {
23 private final ReadWriteShardDataTreeTransaction transaction;
24 private final TransactionIdentifier transactionId;
25 private final short clientVersion;
27 private RuntimeException lastBatchedModificationsException;
28 private int totalBatchedModificationsReceived;
29 private ShardDataTreeCohort cohort;
30 private boolean doImmediateCommit;
31 private ActorRef replySender;
34 private CohortEntry(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
36 this.transaction = Preconditions.checkNotNull(transaction);
37 this.transactionId = transaction.getIdentifier();
38 this.clientVersion = clientVersion;
41 private CohortEntry(final ShardDataTreeCohort cohort, final short clientVersion) {
42 this.cohort = Preconditions.checkNotNull(cohort);
43 this.transactionId = cohort.getIdentifier();
44 this.transaction = null;
45 this.clientVersion = clientVersion;
48 static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
49 return new CohortEntry(transaction, clientVersion);
52 static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
53 return new CohortEntry(cohort, clientVersion);
56 TransactionIdentifier getTransactionId() {
60 short getClientVersion() {
65 return cohort != null && cohort.isFailed();
68 DataTreeModification getDataTreeModification() {
69 return cohort.getDataTreeModification();
72 ReadWriteShardDataTreeTransaction getTransaction() {
76 int getTotalBatchedModificationsReceived() {
77 return totalBatchedModificationsReceived;
80 RuntimeException getLastBatchedModificationsException() {
81 return lastBatchedModificationsException;
84 @SuppressWarnings("checkstyle:IllegalCatch")
85 void applyModifications(final Iterable<Modification> modifications) {
86 totalBatchedModificationsReceived++;
87 if (lastBatchedModificationsException == null) {
88 for (Modification modification : modifications) {
90 modification.apply(transaction.getSnapshot());
91 } catch (RuntimeException e) {
92 lastBatchedModificationsException = e;
99 void canCommit(final FutureCallback<Void> callback) {
100 cohort.canCommit(callback);
103 void preCommit(final FutureCallback<DataTreeCandidate> callback) {
104 cohort.preCommit(callback);
107 void commit(final FutureCallback<UnsignedLong> callback) {
108 cohort.commit(callback);
111 void abort(final FutureCallback<Void> callback) {
112 cohort.abort(callback);
115 void ready(final Optional<SortedSet<String>> participatingShardNames, final CohortDecorator cohortDecorator) {
116 Preconditions.checkState(cohort == null, "cohort was already set");
118 cohort = transaction.ready(participatingShardNames);
120 if (cohortDecorator != null) {
121 // Call the hook for unit tests.
122 cohort = cohortDecorator.decorate(transactionId, cohort);
127 return cohort != null;
130 Optional<SortedSet<String>> getParticipatingShardNames() {
131 return cohort != null ? cohort.getParticipatingShardNames() : Optional.empty();
134 boolean isDoImmediateCommit() {
135 return doImmediateCommit;
138 void setDoImmediateCommit(final boolean doImmediateCommit) {
139 this.doImmediateCommit = doImmediateCommit;
142 ActorRef getReplySender() {
146 void setReplySender(final ActorRef replySender) {
147 this.replySender = replySender;
154 void setShard(final Shard shard) {
159 public String toString() {
160 final StringBuilder builder = new StringBuilder();
161 builder.append("CohortEntry [transactionId=").append(transactionId).append(", doImmediateCommit=")
162 .append(doImmediateCommit).append("]");
163 return builder.toString();