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 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;
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()}.
36 * @author Robert Varga
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;
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);
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();
66 public ClientIdentifier getIdentifier() {
71 public ClientActorConfig config() {
76 public Dispatchers dispatchers() {
81 public MessageSlicer messageSlicer() {
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}.
90 * @return Client actor time source
93 public Ticker ticker() {
94 return Ticker.systemTicker();
98 * Execute a command in the context of the client actor.
100 * @param command Block of code which needs to be execute
101 * @param <T> BackendInfo type
103 public <T extends BackendInfo> void executeInActor(@Nonnull final InternalCommand<T> command) {
104 self().tell(Preconditions.checkNotNull(command), ActorRef.noSender());
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());