Speed up DatastoreContextIntrospector a bit
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalTransactionContext.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import static java.util.Objects.requireNonNull;
11
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 java.util.function.Consumer;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import scala.concurrent.Future;
29
30 /**
31  * Processes front-end transaction operations locally before being committed to the destination shard.
32  * Instances of this class are used when the destination shard is local to the caller.
33  *
34  * @author Thomas Pantelis
35  */
36 abstract class LocalTransactionContext extends AbstractTransactionContext {
37     private final DOMStoreTransaction txDelegate;
38     private final LocalTransactionReadySupport readySupport;
39     private Exception operationError;
40
41     LocalTransactionContext(final DOMStoreTransaction txDelegate, final TransactionIdentifier identifier,
42             final LocalTransactionReadySupport readySupport) {
43         super(identifier);
44         this.txDelegate = requireNonNull(txDelegate);
45         this.readySupport = readySupport;
46     }
47
48     protected abstract DOMStoreWriteTransaction getWriteDelegate();
49
50     protected abstract DOMStoreReadTransaction getReadDelegate();
51
52     @SuppressWarnings("checkstyle:IllegalCatch")
53     private void executeModification(Consumer<DOMStoreWriteTransaction> consumer) {
54         incrementModificationCount();
55         if (operationError == null) {
56             try {
57                 consumer.accept(getWriteDelegate());
58             } catch (Exception e) {
59                 operationError = e;
60             }
61         }
62     }
63
64     @Override
65     public void executeDelete(final YangInstanceIdentifier path, final Boolean havePermit) {
66         executeModification(transaction -> transaction.delete(path));
67     }
68
69     @Override
70     public void executeMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data,
71             final Boolean havePermit) {
72         executeModification(transaction -> transaction.merge(path, data));
73     }
74
75     @Override
76     public void executeWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data,
77             final Boolean havePermit) {
78         executeModification(transaction -> transaction.write(path, data));
79     }
80
81     @Override
82     public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture,
83             final Boolean havePermit) {
84         Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
85             @Override
86             public void onSuccess(final T result) {
87                 proxyFuture.set(result);
88             }
89
90             @Override
91             public void onFailure(final Throwable failure) {
92                 proxyFuture.setException(failure instanceof Exception
93                         ? ReadFailedException.MAPPER.apply((Exception) failure) : failure);
94             }
95         }, MoreExecutors.directExecutor());
96     }
97
98     private LocalThreePhaseCommitCohort ready() {
99         logModificationCount();
100         return readySupport.onTransactionReady(getWriteDelegate(), operationError);
101     }
102
103     @Override
104     public Future<ActorSelection> readyTransaction(final Boolean havePermit,
105             final Optional<SortedSet<String>> participatingShardNames) {
106         final LocalThreePhaseCommitCohort cohort = ready();
107         return cohort.initiateCoordinatedCommit(participatingShardNames);
108     }
109
110     @Override
111     public Future<Object> directCommit(final Boolean havePermit) {
112         final LocalThreePhaseCommitCohort cohort = ready();
113         return cohort.initiateDirectCommit();
114     }
115
116     @Override
117     public void closeTransaction() {
118         txDelegate.close();
119     }
120 }