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 static java.util.Objects.requireNonNull;
12 import akka.actor.ActorSelection;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.Optional;
18 import java.util.SortedSet;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
21 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
22 import org.opendaylight.mdsal.common.api.ReadFailedException;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
25 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
26 import scala.concurrent.Future;
29 * Processes front-end transaction operations locally before being committed to the destination shard.
30 * Instances of this class are used when the destination shard is local to the caller.
32 * @author Thomas Pantelis
34 abstract class LocalTransactionContext extends AbstractTransactionContext {
35 private final DOMStoreTransaction txDelegate;
36 private final LocalTransactionReadySupport readySupport;
37 private Exception operationError;
39 LocalTransactionContext(final DOMStoreTransaction txDelegate, final TransactionIdentifier identifier,
40 final LocalTransactionReadySupport readySupport) {
42 this.txDelegate = requireNonNull(txDelegate);
43 this.readySupport = readySupport;
46 protected abstract DOMStoreWriteTransaction getWriteDelegate();
48 protected abstract DOMStoreReadTransaction getReadDelegate();
51 @SuppressWarnings("checkstyle:IllegalCatch")
52 public void executeModification(final AbstractModification modification, final Boolean havePermit) {
53 incrementModificationCount();
54 if (operationError == null) {
56 modification.apply(getWriteDelegate());
57 } catch (Exception e) {
64 public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture,
65 final Boolean havePermit) {
66 Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
68 public void onSuccess(final T result) {
69 proxyFuture.set(result);
73 public void onFailure(final Throwable failure) {
74 proxyFuture.setException(failure instanceof Exception
75 ? ReadFailedException.MAPPER.apply((Exception) failure) : failure);
77 }, MoreExecutors.directExecutor());
80 private LocalThreePhaseCommitCohort ready() {
81 logModificationCount();
82 return readySupport.onTransactionReady(getWriteDelegate(), operationError);
86 public Future<ActorSelection> readyTransaction(final Boolean havePermit,
87 final Optional<SortedSet<String>> participatingShardNames) {
88 final LocalThreePhaseCommitCohort cohort = ready();
89 return cohort.initiateCoordinatedCommit(participatingShardNames);
93 public Future<Object> directCommit(final Boolean havePermit) {
94 final LocalThreePhaseCommitCohort cohort = ready();
95 return cohort.initiateDirectCommit();
99 public void closeTransaction() {