2 * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.datastore.utils;
11 import akka.actor.ActorPath;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import akka.actor.ActorSystem;
15 import akka.util.Timeout;
16 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
17 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
18 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
19 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import scala.concurrent.Await;
23 import scala.concurrent.Future;
24 import scala.concurrent.duration.Duration;
25 import scala.concurrent.duration.FiniteDuration;
27 import java.util.concurrent.TimeUnit;
29 import static akka.pattern.Patterns.ask;
32 * The ActorContext class contains utility methods which could be used by
33 * non-actors (like DistributedDataStore) to work with actors a little more
34 * easily. An ActorContext can be freely passed around to local object instances
35 * but should not be passed to actors especially remote actors
37 public class ActorContext {
38 private static final Logger
39 LOG = LoggerFactory.getLogger(ActorContext.class);
41 public static final FiniteDuration ASK_DURATION = Duration.create(5, TimeUnit.SECONDS);
42 public static final Duration AWAIT_DURATION = Duration.create(5, TimeUnit.SECONDS);
44 private final ActorSystem actorSystem;
45 private final ActorRef shardManager;
47 public ActorContext(ActorSystem actorSystem, ActorRef shardManager){
48 this.actorSystem = actorSystem;
49 this.shardManager = shardManager;
52 public ActorSystem getActorSystem() {
56 public ActorRef getShardManager() {
60 public ActorSelection actorSelection(String actorPath){
61 return actorSystem.actorSelection(actorPath);
64 public ActorSelection actorSelection(ActorPath actorPath){
65 return actorSystem.actorSelection(actorPath);
70 * Finds the primary for a given shard
75 public ActorSelection findPrimary(String shardName) {
76 Object result = executeLocalOperation(shardManager,
77 new FindPrimary(shardName), ASK_DURATION);
79 if(result instanceof PrimaryFound){
80 PrimaryFound found = (PrimaryFound) result;
82 LOG.error("Primary found {}", found.getPrimaryPath());
84 return actorSystem.actorSelection(found.getPrimaryPath());
86 throw new PrimaryNotFoundException();
90 * Executes an operation on a local actor and wait for it's response
94 * @return The response of the operation
96 public Object executeLocalOperation(ActorRef actor, Object message,
97 FiniteDuration duration){
98 Future<Object> future =
99 ask(actor, message, new Timeout(duration));
102 return Await.result(future, AWAIT_DURATION);
103 } catch (Exception e) {
104 throw new TimeoutException(e);
109 * Execute an operation on a remote actor and wait for it's response
115 public Object executeRemoteOperation(ActorSelection actor, Object message,
116 FiniteDuration duration){
117 Future<Object> future =
118 ask(actor, message, new Timeout(duration));
121 return Await.result(future, AWAIT_DURATION);
122 } catch (Exception e) {
123 throw new TimeoutException(e);
128 * Execute an operation on the primary for a given shard
130 * This method first finds the primary for a given shard ,then sends
131 * the message to the remote shard and waits for a response
136 * @throws org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException if the message to the remote shard times out
137 * @throws org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException if the primary shard is not found
141 public Object executeShardOperation(String shardName, Object message, FiniteDuration duration){
142 ActorSelection primary = findPrimary(shardName);
144 return executeRemoteOperation(primary, message, duration);