2 * Copyright (c) 2016 Cisco 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.access.client;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.actor.Cancellable;
15 import akka.actor.Scheduler;
16 import com.google.common.annotations.Beta;
17 import com.google.common.base.Ticker;
18 import java.util.concurrent.TimeUnit;
19 import org.eclipse.jdt.annotation.NonNull;
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;
29 * An actor context associated with this {@link AbstractClientActor}.
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()}. This class is thread-safe.
36 * @author Robert Varga
39 public class ClientActorContext extends AbstractClientActorContext implements Identifiable<ClientIdentifier> {
40 private final ExecutionContext executionContext;
41 private final ClientIdentifier identifier;
42 private final Scheduler scheduler;
43 private final Dispatchers dispatchers;
44 private final ClientActorConfig config;
45 private final MessageSlicer messageSlicer;
47 // Hidden to avoid subclassing
48 ClientActorContext(final ActorRef self, final String persistenceId, final ActorSystem system,
49 final ClientIdentifier identifier, final ClientActorConfig config) {
50 super(self, persistenceId);
51 this.identifier = requireNonNull(identifier);
52 this.scheduler = requireNonNull(system).scheduler();
53 this.executionContext = system.dispatcher();
54 this.dispatchers = new Dispatchers(system.dispatchers());
55 this.config = requireNonNull(config);
57 messageSlicer = MessageSlicer.builder().messageSliceSize(config.getMaximumMessageSliceSize())
58 .logContext(persistenceId).expireStateAfterInactivity(config.getRequestTimeout(), TimeUnit.NANOSECONDS)
59 .fileBackedStreamFactory(new FileBackedOutputStreamFactory(config.getFileBackedStreamingThreshold(),
60 config.getTempFileDirectory())).build();
64 public ClientIdentifier getIdentifier() {
68 public @NonNull ClientActorConfig config() {
72 public @NonNull Dispatchers dispatchers() {
76 public @NonNull MessageSlicer messageSlicer() {
81 * Return the time ticker for this {@link ClientActorContext}. This should be used for in all time-tracking
82 * done within a client actor. Subclasses of {@link ClientActorBehavior} are encouraged to use
83 * {@link com.google.common.base.Stopwatch}.
85 * @return Client actor time source
87 public @NonNull Ticker ticker() {
88 return Ticker.systemTicker();
92 * Execute a command in the context of the client actor.
94 * @param command Block of code which needs to be execute
95 * @param <T> BackendInfo type
97 public <T extends BackendInfo> void executeInActor(final @NonNull InternalCommand<T> command) {
98 self().tell(requireNonNull(command), ActorRef.noSender());
101 public <T extends BackendInfo> Cancellable executeInActor(final @NonNull InternalCommand<T> command,
102 final FiniteDuration delay) {
103 return scheduler.scheduleOnce(requireNonNull(delay), self(), requireNonNull(command),
104 executionContext, ActorRef.noSender());