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.concurrent.ExecutionException;
15 import java.util.concurrent.TimeoutException;
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) {
35 this.transaction = Preconditions.checkNotNull(transaction);
36 this.transactionID = transaction.getId();
37 this.clientVersion = clientVersion;
40 private CohortEntry(final ShardDataTreeCohort cohort, final short clientVersion) {
41 this.cohort = Preconditions.checkNotNull(cohort);
42 this.transactionID = cohort.getIdentifier();
43 this.transaction = null;
44 this.clientVersion = clientVersion;
47 static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
48 return new CohortEntry(transaction, clientVersion);
51 static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
52 return new CohortEntry(cohort, clientVersion);
55 TransactionIdentifier getTransactionID() {
59 short getClientVersion() {
64 return cohort != null && cohort.isFailed();
67 DataTreeModification getDataTreeModification() {
68 return cohort.getDataTreeModification();
71 ReadWriteShardDataTreeTransaction getTransaction() {
75 int getTotalBatchedModificationsReceived() {
76 return totalBatchedModificationsReceived;
79 RuntimeException getLastBatchedModificationsException() {
80 return lastBatchedModificationsException;
83 @SuppressWarnings("checkstyle:IllegalCatch")
84 void applyModifications(final Iterable<Modification> modifications) {
85 totalBatchedModificationsReceived++;
86 if (lastBatchedModificationsException == null) {
87 for (Modification modification : modifications) {
89 modification.apply(transaction.getSnapshot());
90 } catch (RuntimeException e) {
91 lastBatchedModificationsException = e;
98 void canCommit(final FutureCallback<Void> callback) {
99 cohort.canCommit(callback);
102 void preCommit(final FutureCallback<DataTreeCandidate> callback) {
103 cohort.preCommit(callback);
106 void commit(final FutureCallback<UnsignedLong> callback) {
107 cohort.commit(callback);
110 void abort() throws InterruptedException, ExecutionException, TimeoutException {
111 cohort.abort().get();
114 void ready(final CohortDecorator cohortDecorator) {
115 Preconditions.checkState(cohort == null, "cohort was already set");
117 cohort = transaction.ready();
119 if (cohortDecorator != null) {
120 // Call the hook for unit tests.
121 cohort = cohortDecorator.decorate(transactionID, cohort);
125 boolean isDoImmediateCommit() {
126 return doImmediateCommit;
129 void setDoImmediateCommit(final boolean doImmediateCommit) {
130 this.doImmediateCommit = doImmediateCommit;
133 ActorRef getReplySender() {
137 void setReplySender(final ActorRef replySender) {
138 this.replySender = replySender;
145 void setShard(final Shard shard) {
150 public String toString() {
151 final StringBuilder builder = new StringBuilder();
152 builder.append("CohortEntry [transactionID=").append(transactionID).append(", doImmediateCommit=")
153 .append(doImmediateCommit).append("]");
154 return builder.toString();