BUG-5280: Create AbstractProxyHistory class
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / DistributedDataStoreClientBehavior.java
1 /*
2  * Copyright (c) 2016 Cisco 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.databroker.actors.dds;
9
10 import akka.actor.ActorRef;
11 import akka.actor.Status;
12 import com.google.common.base.Throwables;
13 import com.google.common.base.Verify;
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.atomic.AtomicLong;
17 import java.util.function.Consumer;
18 import org.opendaylight.controller.cluster.access.client.ClientActorBehavior;
19 import org.opendaylight.controller.cluster.access.client.ClientActorContext;
20 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
21 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
22 import org.opendaylight.controller.cluster.access.concepts.Response;
23 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
24 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * {@link ClientActorBehavior} acting as an intermediary between the backend actors and the DistributedDataStore
30  * frontend.
31  *
32  * This class is not visible outside of this package because it breaks the actor containment. Services provided to
33  * Java world outside of actor containment are captured in {@link DistributedDataStoreClient}.
34  *
35  * IMPORTANT: this class breaks actor containment via methods implementing {@link DistributedDataStoreClient} contract.
36  *            When touching internal state, be mindful of the execution context from which execution context, Actor
37  *            or POJO, is the state being accessed or modified.
38  *
39  * THREAD SAFETY: this class must always be kept thread-safe, so that both the Actor System thread and the application
40  *                threads can run concurrently. All state transitions must be made in a thread-safe manner. When in
41  *                doubt, feel free to synchronize on this object.
42  *
43  * PERFORMANCE: this class lies in a performance-critical fast path. All code needs to be concise and efficient, but
44  *              performance must not come at the price of correctness. Any optimizations need to be carefully analyzed
45  *              for correctness and performance impact.
46  *
47  * TRADE-OFFS: part of the functionality runs in application threads without switching contexts, which makes it ideal
48  *             for performing work and charging applications for it. That has two positive effects:
49  *             - CPU usage is distributed across applications, minimizing work done in the actor thread
50  *             - CPU usage provides back-pressure towards the application.
51  *
52  * @author Robert Varga
53  */
54 final class DistributedDataStoreClientBehavior extends ClientActorBehavior implements DistributedDataStoreClient {
55     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStoreClientBehavior.class);
56
57     private final Map<TransactionIdentifier, ClientTransaction> transactions = new ConcurrentHashMap<>();
58     private final Map<LocalHistoryIdentifier, ClientLocalHistory> histories = new ConcurrentHashMap<>();
59     private final AtomicLong nextHistoryId = new AtomicLong(1);
60     private final AtomicLong nextTransactionId = new AtomicLong();
61     private final ModuleShardBackendResolver resolver;
62     private final SingleClientHistory singleHistory;
63
64     private volatile Throwable aborted;
65
66     DistributedDataStoreClientBehavior(final ClientActorContext context, final ActorContext actorContext) {
67         super(context);
68         resolver = new ModuleShardBackendResolver(actorContext);
69         singleHistory = new SingleClientHistory(this, new LocalHistoryIdentifier(getIdentifier(), 0));
70     }
71
72     //
73     //
74     // Methods below are invoked from the client actor thread
75     //
76     //
77
78     @Override
79     protected void haltClient(final Throwable cause) {
80         // If we have encountered a previous problem there is not cleanup necessary, as we have already cleaned up
81         // Thread safely is not an issue, as both this method and any failures are executed from the same (client actor)
82         // thread.
83         if (aborted != null) {
84             abortOperations(cause);
85         }
86     }
87
88     private void abortOperations(final Throwable cause) {
89         // This acts as a barrier, application threads check this after they have added an entry in the maps,
90         // and if they observe aborted being non-null, they will perform their cleanup and not return the handle.
91         aborted = cause;
92
93         for (ClientLocalHistory h : histories.values()) {
94             h.localAbort(cause);
95         }
96         histories.clear();
97
98         for (ClientTransaction t : transactions.values()) {
99             t.localAbort(cause);
100         }
101         transactions.clear();
102     }
103
104     private DistributedDataStoreClientBehavior shutdown(final ClientActorBehavior currentBehavior) {
105         abortOperations(new IllegalStateException("Client " + getIdentifier() + " has been shut down"));
106         return null;
107     }
108
109     @Override
110     protected DistributedDataStoreClientBehavior onCommand(final Object command) {
111         if (command instanceof GetClientRequest) {
112             ((GetClientRequest) command).getReplyTo().tell(new Status.Success(this), ActorRef.noSender());
113         } else {
114             LOG.warn("{}: ignoring unhandled command {}", persistenceId(), command);
115         }
116
117         return this;
118     }
119
120     //
121     //
122     // Methods below are invoked from application threads
123     //
124     //
125
126     private static <K, V extends LocalAbortable> V returnIfOperational(final Map<K , V> map, final K key, final V value,
127             final Throwable aborted) {
128         Verify.verify(map.put(key, value) == null);
129
130         if (aborted != null) {
131             try {
132                 value.localAbort(aborted);
133             } catch (Exception e) {
134                 LOG.debug("Close of {} failed", value, e);
135             }
136             map.remove(key, value);
137             throw Throwables.propagate(aborted);
138         }
139
140         return value;
141     }
142
143     @Override
144     public ClientLocalHistory createLocalHistory() {
145         final LocalHistoryIdentifier historyId = new LocalHistoryIdentifier(getIdentifier(),
146             nextHistoryId.getAndIncrement());
147         final ClientLocalHistory history = new ClientLocalHistory(this, historyId);
148         LOG.debug("{}: creating a new local history {}", persistenceId(), history);
149
150         return returnIfOperational(histories, historyId, history, aborted);
151     }
152
153     @Override
154     public ClientTransaction createTransaction() {
155         final TransactionIdentifier txId = new TransactionIdentifier(singleHistory.getIdentifier(),
156             nextTransactionId.getAndIncrement());
157         final ClientTransaction tx = new ClientTransaction(singleHistory, txId);
158         LOG.debug("{}: creating a new transaction {}", persistenceId(), tx);
159
160         return returnIfOperational(transactions, txId, tx, aborted);
161     }
162
163     @Override
164     public void close() {
165         context().executeInActor(this::shutdown);
166     }
167
168     @Override
169     protected ModuleShardBackendResolver resolver() {
170         return resolver;
171     }
172
173     void transactionComplete(final ClientTransaction transaction) {
174         transactions.remove(transaction.getIdentifier());
175     }
176
177     void sendRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> completer) {
178         sendRequest(request, response -> {
179             completer.accept(response);
180             return this;
181         });
182     }
183
184 }