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 void applyModifications(final Iterable<Modification> modifications) {
84 totalBatchedModificationsReceived++;
85 if(lastBatchedModificationsException == null) {
86 for (Modification modification : modifications) {
88 modification.apply(transaction.getSnapshot());
89 } catch (RuntimeException e) {
90 lastBatchedModificationsException = e;
97 void canCommit(final FutureCallback<Void> callback) {
98 cohort.canCommit(callback);
101 void preCommit(final FutureCallback<DataTreeCandidate> callback) {
102 cohort.preCommit(callback);
105 void commit(final FutureCallback<UnsignedLong> callback) {
106 cohort.commit(callback);
109 void abort() throws InterruptedException, ExecutionException, TimeoutException {
110 cohort.abort().get();
113 void ready(final CohortDecorator cohortDecorator) {
114 Preconditions.checkState(cohort == null, "cohort was already set");
116 cohort = transaction.ready();
118 if(cohortDecorator != null) {
119 // Call the hook for unit tests.
120 cohort = cohortDecorator.decorate(transactionID, cohort);
124 boolean isDoImmediateCommit() {
125 return doImmediateCommit;
128 void setDoImmediateCommit(final boolean doImmediateCommit) {
129 this.doImmediateCommit = doImmediateCommit;
132 ActorRef getReplySender() {
136 void setReplySender(final ActorRef replySender) {
137 this.replySender = replySender;
144 void setShard(final Shard shard) {
149 public String toString() {
150 final StringBuilder builder = new StringBuilder();
151 builder.append("CohortEntry [transactionID=").append(transactionID).append(", doImmediateCommit=")
152 .append(doImmediateCommit).append("]");
153 return builder.toString();