2 * Copyright (c) 2015 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.ActorSelection;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.Optional;
17 import java.util.SortedSet;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
20 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
24 import scala.concurrent.Future;
27 * Processes front-end transaction operations locally before being committed to the destination shard.
28 * Instances of this class are used when the destination shard is local to the caller.
30 * @author Thomas Pantelis
32 abstract class LocalTransactionContext extends AbstractTransactionContext {
33 private final DOMStoreTransaction txDelegate;
34 private final LocalTransactionReadySupport readySupport;
35 private Exception operationError;
37 LocalTransactionContext(final DOMStoreTransaction txDelegate, final TransactionIdentifier identifier,
38 final LocalTransactionReadySupport readySupport) {
40 this.txDelegate = Preconditions.checkNotNull(txDelegate);
41 this.readySupport = readySupport;
44 protected abstract DOMStoreWriteTransaction getWriteDelegate();
46 protected abstract DOMStoreReadTransaction getReadDelegate();
49 @SuppressWarnings("checkstyle:IllegalCatch")
50 public void executeModification(final AbstractModification modification, final Boolean havePermit) {
51 incrementModificationCount();
52 if (operationError == null) {
54 modification.apply(getWriteDelegate());
55 } catch (Exception e) {
62 public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture,
63 final Boolean havePermit) {
64 Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
66 public void onSuccess(final T result) {
67 proxyFuture.set(result);
71 public void onFailure(final Throwable failure) {
72 proxyFuture.setException(failure);
74 }, MoreExecutors.directExecutor());
77 private LocalThreePhaseCommitCohort ready() {
78 logModificationCount();
79 return readySupport.onTransactionReady(getWriteDelegate(), operationError);
83 public Future<ActorSelection> readyTransaction(final Boolean havePermit,
84 final Optional<SortedSet<String>> participatingShardNames) {
85 final LocalThreePhaseCommitCohort cohort = ready();
86 return cohort.initiateCoordinatedCommit(participatingShardNames);
90 public Future<Object> directCommit(final Boolean havePermit) {
91 final LocalThreePhaseCommitCohort cohort = ready();
92 return cohort.initiateDirectCommit();
96 public void closeTransaction() {