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.mdsal.common.api.ReadFailedException;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
25 import scala.concurrent.Future;
28 * Processes front-end transaction operations locally before being committed to the destination shard.
29 * Instances of this class are used when the destination shard is local to the caller.
31 * @author Thomas Pantelis
33 abstract class LocalTransactionContext extends AbstractTransactionContext {
34 private final DOMStoreTransaction txDelegate;
35 private final LocalTransactionReadySupport readySupport;
36 private Exception operationError;
38 LocalTransactionContext(final DOMStoreTransaction txDelegate, final TransactionIdentifier identifier,
39 final LocalTransactionReadySupport readySupport) {
41 this.txDelegate = Preconditions.checkNotNull(txDelegate);
42 this.readySupport = readySupport;
45 protected abstract DOMStoreWriteTransaction getWriteDelegate();
47 protected abstract DOMStoreReadTransaction getReadDelegate();
50 @SuppressWarnings("checkstyle:IllegalCatch")
51 public void executeModification(final AbstractModification modification, final Boolean havePermit) {
52 incrementModificationCount();
53 if (operationError == null) {
55 modification.apply(getWriteDelegate());
56 } catch (Exception e) {
63 public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture,
64 final Boolean havePermit) {
65 Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
67 public void onSuccess(final T result) {
68 proxyFuture.set(result);
72 public void onFailure(final Throwable failure) {
73 proxyFuture.setException(failure instanceof Exception
74 ? ReadFailedException.MAPPER.apply((Exception) failure) : failure);
76 }, MoreExecutors.directExecutor());
79 private LocalThreePhaseCommitCohort ready() {
80 logModificationCount();
81 return readySupport.onTransactionReady(getWriteDelegate(), operationError);
85 public Future<ActorSelection> readyTransaction(final Boolean havePermit,
86 final Optional<SortedSet<String>> participatingShardNames) {
87 final LocalThreePhaseCommitCohort cohort = ready();
88 return cohort.initiateCoordinatedCommit(participatingShardNames);
92 public Future<Object> directCommit(final Boolean havePermit) {
93 final LocalThreePhaseCommitCohort cohort = ready();
94 return cohort.initiateDirectCommit();
98 public void closeTransaction() {