Slice front-end request messages
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / ClientActorContext.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.access.client;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.Cancellable;
13 import akka.actor.Scheduler;
14 import com.google.common.annotations.Beta;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Ticker;
17 import java.util.concurrent.TimeUnit;
18 import javax.annotation.Nonnull;
19 import javax.annotation.concurrent.ThreadSafe;
20 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
21 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
22 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
23 import org.opendaylight.controller.cluster.messaging.MessageSlicer;
24 import org.opendaylight.yangtools.concepts.Identifiable;
25 import scala.concurrent.ExecutionContext;
26 import scala.concurrent.duration.FiniteDuration;
27
28 /**
29  * An actor context associated with this {@link AbstractClientActor}.
30  *
31  * <p>
32  * Time-keeping in a client actor is based on monotonic time. The precision of this time can be expected to be the
33  * same as {@link System#nanoTime()}, but it is not tied to that particular clock. Actor clock is exposed as
34  * a {@link Ticker}, which can be obtained via {@link #ticker()}.
35  *
36  * @author Robert Varga
37  */
38 @Beta
39 @ThreadSafe
40 public class ClientActorContext extends AbstractClientActorContext implements Identifiable<ClientIdentifier> {
41     private final ExecutionContext executionContext;
42     private final ClientIdentifier identifier;
43     private final Scheduler scheduler;
44     private final Dispatchers dispatchers;
45     private final ClientActorConfig config;
46     private final MessageSlicer messageSlicer;
47
48     // Hidden to avoid subclassing
49     ClientActorContext(final ActorRef self, final String persistenceId, final ActorSystem system,
50             final ClientIdentifier identifier, final ClientActorConfig config) {
51         super(self, persistenceId);
52         this.identifier = Preconditions.checkNotNull(identifier);
53         this.scheduler = Preconditions.checkNotNull(system).scheduler();
54         this.executionContext = system.dispatcher();
55         this.dispatchers = new Dispatchers(system.dispatchers());
56         this.config = Preconditions.checkNotNull(config);
57
58         messageSlicer = MessageSlicer.builder().messageSliceSize(config.getMaximumMessageSliceSize())
59             .logContext(persistenceId).expireStateAfterInactivity(config.getRequestTimeout(), TimeUnit.NANOSECONDS)
60                 .fileBackedStreamFactory(new FileBackedOutputStreamFactory(config.getFileBackedStreamingThreshold(),
61                     config.getTempFileDirectory())).build();
62     }
63
64     @Override
65     @Nonnull
66     public ClientIdentifier getIdentifier() {
67         return identifier;
68     }
69
70     @Nonnull
71     public ClientActorConfig config() {
72         return config;
73     }
74
75     @Nonnull
76     public Dispatchers dispatchers() {
77         return dispatchers;
78     }
79
80     @Nonnull
81     public MessageSlicer messageSlicer() {
82         return messageSlicer;
83     }
84
85     /**
86      * Return the time ticker for this {@link ClientActorContext}. This should be used for in all time-tracking
87      * done within a client actor. Subclasses of {@link ClientActorBehavior} are encouraged to use
88      * {@link com.google.common.base.Stopwatch}.
89      *
90      * @return Client actor time source
91      */
92     @Nonnull
93     public Ticker ticker() {
94         return Ticker.systemTicker();
95     }
96
97     /**
98      * Execute a command in the context of the client actor.
99      *
100      * @param command Block of code which needs to be execute
101      * @param <T> BackendInfo type
102      */
103     public <T extends BackendInfo> void executeInActor(@Nonnull final InternalCommand<T> command) {
104         self().tell(Preconditions.checkNotNull(command), ActorRef.noSender());
105     }
106
107     public <T extends BackendInfo> Cancellable executeInActor(@Nonnull final InternalCommand<T> command,
108             final FiniteDuration delay) {
109         return scheduler.scheduleOnce(Preconditions.checkNotNull(delay), self(), Preconditions.checkNotNull(command),
110             executionContext, ActorRef.noSender());
111     }
112 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.