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.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import scala.concurrent.Await;
25 import scala.concurrent.Future;
26 import scala.concurrent.duration.Duration;
27 import scala.concurrent.duration.FiniteDuration;
29 import java.util.concurrent.TimeUnit;
31 import static akka.pattern.Patterns.ask;
34 * The ActorContext class contains utility methods which could be used by
35 * non-actors (like DistributedDataStore) to work with actors a little more
36 * easily. An ActorContext can be freely passed around to local object instances
37 * but should not be passed to actors especially remote actors
39 public class ActorContext {
40 private static final Logger
41 LOG = LoggerFactory.getLogger(ActorContext.class);
43 public static final FiniteDuration ASK_DURATION = Duration.create(5, TimeUnit.SECONDS);
44 public static final Duration AWAIT_DURATION = Duration.create(5, TimeUnit.SECONDS);
46 private final ActorSystem actorSystem;
47 private final ActorRef shardManager;
49 private SchemaContext schemaContext = null;
51 public ActorContext(ActorSystem actorSystem, ActorRef shardManager){
52 this.actorSystem = actorSystem;
53 this.shardManager = shardManager;
56 public ActorSystem getActorSystem() {
60 public ActorRef getShardManager() {
64 public ActorSelection actorSelection(String actorPath){
65 return actorSystem.actorSelection(actorPath);
68 public ActorSelection actorSelection(ActorPath actorPath){
69 return actorSystem.actorSelection(actorPath);
74 * Finds the primary for a given shard
79 public ActorSelection findPrimary(String shardName) {
80 Object result = executeLocalOperation(shardManager,
81 new FindPrimary(shardName), ASK_DURATION);
83 if(result instanceof PrimaryFound){
84 PrimaryFound found = (PrimaryFound) result;
86 LOG.error("Primary found {}", found.getPrimaryPath());
88 return actorSystem.actorSelection(found.getPrimaryPath());
90 throw new PrimaryNotFoundException();
94 * Executes an operation on a local actor and wait for it's response
98 * @return The response of the operation
100 public Object executeLocalOperation(ActorRef actor, Object message,
101 FiniteDuration duration){
102 Future<Object> future =
103 ask(actor, message, new Timeout(duration));
106 return Await.result(future, AWAIT_DURATION);
107 } catch (Exception e) {
108 throw new TimeoutException(e);
113 * Execute an operation on a remote actor and wait for it's response
119 public Object executeRemoteOperation(ActorSelection actor, Object message,
120 FiniteDuration duration){
121 Future<Object> future =
122 ask(actor, message, new Timeout(duration));
125 return Await.result(future, AWAIT_DURATION);
126 } catch (Exception e) {
127 throw new TimeoutException(e);
132 * Execute an operation on the primary for a given shard
134 * This method first finds the primary for a given shard ,then sends
135 * the message to the remote shard and waits for a response
140 * @throws org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException if the message to the remote shard times out
141 * @throws org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException if the primary shard is not found
145 public Object executeShardOperation(String shardName, Object message, FiniteDuration duration){
146 ActorSelection primary = findPrimary(shardName);
148 return executeRemoteOperation(primary, message, duration);
151 public void shutdown() {
152 shardManager.tell(PoisonPill.getInstance(), null);
153 actorSystem.shutdown();