BUG-5280: define RetiredGenerationException
[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 java.util.concurrent.CompletableFuture;
13 import java.util.concurrent.CompletionStage;
14 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
15 import org.opendaylight.controller.cluster.datastore.actors.client.ClientActorBehavior;
16 import org.opendaylight.controller.cluster.datastore.actors.client.ClientActorContext;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * {@link ClientActorBehavior} acting as an intermediary between the backend actors and the DistributedDataStore
22  * frontend.
23  *
24  * This class is not visible outside of this package because it breaks the actor containment. Services provided to
25  * Java world outside of actor containment are captured in {@link DistributedDataStoreClient}.
26  *
27  * IMPORTANT: this class breaks actor containment via methods implementing {@link DistributedDataStoreClient} contract.
28  *            When touching internal state, be mindful of the execution context from which execution context, Actor
29  *            or POJO, is the state being accessed or modified.
30  *
31  * THREAD SAFETY: this class must always be kept thread-safe, so that both the Actor System thread and the application
32  *                threads can run concurrently. All state transitions must be made in a thread-safe manner. When in
33  *                doubt, feel free to synchronize on this object.
34  *
35  * PERFORMANCE: this class lies in a performance-critical fast path. All code needs to be concise and efficient, but
36  *              performance must not come at the price of correctness. Any optimizations need to be carefully analyzed
37  *              for correctness and performance impact.
38  *
39  * TRADE-OFFS: part of the functionality runs in application threads without switching contexts, which makes it ideal
40  *             for performing work and charging applications for it. That has two positive effects:
41  *             - CPU usage is distributed across applications, minimizing work done in the actor thread
42  *             - CPU usage provides back-pressure towards the application.
43  *
44  * @author Robert Varga
45  */
46 final class DistributedDataStoreClientBehavior extends ClientActorBehavior implements DistributedDataStoreClient {
47     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStoreClientBehavior.class);
48     private static final Object SHUTDOWN = new Object() {
49         @Override
50         public String toString() {
51             return "SHUTDOWN";
52         }
53     };
54
55     private long nextHistoryId;
56
57     DistributedDataStoreClientBehavior(final ClientActorContext context) {
58         super(context);
59     }
60
61     //
62     //
63     // Methods below are invoked from the client actor thread
64     //
65     //
66
67     @Override
68     protected void haltClient(final Throwable cause) {
69         // FIXME: Add state flushing here once we have state
70     }
71
72     private void createLocalHistory(final CreateLocalHistoryCommand command) {
73         final CompletableFuture<ClientLocalHistory> future = command.future();
74         final LocalHistoryIdentifier historyId = new LocalHistoryIdentifier(getIdentifier(), nextHistoryId++);
75         LOG.debug("{}: creating a new local history {} for {}", persistenceId(), historyId, future);
76
77         // FIXME: initiate backend instantiation
78         future.completeExceptionally(new UnsupportedOperationException("Not implemented yet"));
79     }
80
81     @Override
82     protected ClientActorBehavior onCommand(final Object command) {
83         if (command instanceof CreateLocalHistoryCommand) {
84             createLocalHistory((CreateLocalHistoryCommand) command);
85         } else if (command instanceof GetClientRequest) {
86             ((GetClientRequest) command).getReplyTo().tell(new Status.Success(this), ActorRef.noSender());
87         } else if (SHUTDOWN.equals(command)) {
88             // FIXME: Add shutdown procedures here
89             return null;
90         } else {
91             LOG.warn("{}: ignoring unhandled command {}", persistenceId(), command);
92         }
93
94         return this;
95     }
96
97     //
98     //
99     // Methods below are invoked from application threads
100     //
101     //
102
103     @Override
104     public CompletionStage<ClientLocalHistory> createLocalHistory() {
105         final CreateLocalHistoryCommand command = new CreateLocalHistoryCommand();
106         self().tell(command, ActorRef.noSender());
107         return command.future();
108     }
109
110     @Override
111     public void close() {
112         self().tell(SHUTDOWN, ActorRef.noSender());
113     }
114 }