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.actor.PoisonPill;
16 import akka.util.Timeout;
17 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
18 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
19 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
20 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import scala.concurrent.Await;
24 import scala.concurrent.Future;
25 import scala.concurrent.duration.Duration;
26 import scala.concurrent.duration.FiniteDuration;
28 import java.util.concurrent.TimeUnit;
30 import static akka.pattern.Patterns.ask;
33 * The ActorContext class contains utility methods which could be used by
34 * non-actors (like DistributedDataStore) to work with actors a little more
35 * easily. An ActorContext can be freely passed around to local object instances
36 * but should not be passed to actors especially remote actors
38 public class ActorContext {
39 private static final Logger
40 LOG = LoggerFactory.getLogger(ActorContext.class);
42 public static final FiniteDuration ASK_DURATION = Duration.create(5, TimeUnit.SECONDS);
43 public static final Duration AWAIT_DURATION = Duration.create(5, TimeUnit.SECONDS);
45 private final ActorSystem actorSystem;
46 private final ActorRef shardManager;
48 public ActorContext(ActorSystem actorSystem, ActorRef shardManager){
49 this.actorSystem = actorSystem;
50 this.shardManager = shardManager;
53 public ActorSystem getActorSystem() {
57 public ActorRef getShardManager() {
61 public ActorSelection actorSelection(String actorPath){
62 return actorSystem.actorSelection(actorPath);
65 public ActorSelection actorSelection(ActorPath actorPath){
66 return actorSystem.actorSelection(actorPath);
71 * Finds the primary for a given shard
76 public ActorSelection findPrimary(String shardName) {
77 Object result = executeLocalOperation(shardManager,
78 new FindPrimary(shardName), ASK_DURATION);
80 if(result instanceof PrimaryFound){
81 PrimaryFound found = (PrimaryFound) result;
83 LOG.error("Primary found {}", found.getPrimaryPath());
85 return actorSystem.actorSelection(found.getPrimaryPath());
87 throw new PrimaryNotFoundException();
91 * Executes an operation on a local actor and wait for it's response
95 * @return The response of the operation
97 public Object executeLocalOperation(ActorRef actor, Object message,
98 FiniteDuration duration){
99 Future<Object> future =
100 ask(actor, message, new Timeout(duration));
103 return Await.result(future, AWAIT_DURATION);
104 } catch (Exception e) {
105 throw new TimeoutException(e);
110 * Execute an operation on a remote actor and wait for it's response
116 public Object executeRemoteOperation(ActorSelection actor, Object message,
117 FiniteDuration duration){
118 Future<Object> future =
119 ask(actor, message, new Timeout(duration));
122 return Await.result(future, AWAIT_DURATION);
123 } catch (Exception e) {
124 throw new TimeoutException(e);
129 * Execute an operation on the primary for a given shard
131 * This method first finds the primary for a given shard ,then sends
132 * the message to the remote shard and waits for a response
137 * @throws org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException if the message to the remote shard times out
138 * @throws org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException if the primary shard is not found
142 public Object executeShardOperation(String shardName, Object message, FiniteDuration duration){
143 ActorSelection primary = findPrimary(shardName);
145 return executeRemoteOperation(primary, message, duration);
148 public void shutdown() {
149 shardManager.tell(PoisonPill.getInstance(), null);
150 actorSystem.shutdown();