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;
18 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
19 import org.opendaylight.controller.cluster.datastore.Configuration;
20 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
21 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
22 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
23 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
24 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
25 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
26 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import scala.concurrent.Await;
32 import scala.concurrent.Future;
33 import scala.concurrent.duration.Duration;
34 import scala.concurrent.duration.FiniteDuration;
36 import java.util.concurrent.TimeUnit;
38 import static akka.pattern.Patterns.ask;
41 * The ActorContext class contains utility methods which could be used by
42 * non-actors (like DistributedDataStore) to work with actors a little more
43 * easily. An ActorContext can be freely passed around to local object instances
44 * but should not be passed to actors especially remote actors
46 public class ActorContext {
47 private static final Logger
48 LOG = LoggerFactory.getLogger(ActorContext.class);
50 public static final FiniteDuration ASK_DURATION =
51 Duration.create(5, TimeUnit.SECONDS);
52 public static final Duration AWAIT_DURATION =
53 Duration.create(5, TimeUnit.SECONDS);
55 public static final String MAILBOX = "bounded-mailbox";
57 private final ActorSystem actorSystem;
58 private final ActorRef shardManager;
59 private final ClusterWrapper clusterWrapper;
60 private final Configuration configuration;
61 private volatile SchemaContext schemaContext;
63 public ActorContext(ActorSystem actorSystem, ActorRef shardManager,
64 ClusterWrapper clusterWrapper,
65 Configuration configuration) {
66 this.actorSystem = actorSystem;
67 this.shardManager = shardManager;
68 this.clusterWrapper = clusterWrapper;
69 this.configuration = configuration;
72 public ActorSystem getActorSystem() {
76 public ActorRef getShardManager() {
80 public ActorSelection actorSelection(String actorPath) {
81 return actorSystem.actorSelection(actorPath);
84 public ActorSelection actorSelection(ActorPath actorPath) {
85 return actorSystem.actorSelection(actorPath);
88 public void setSchemaContext(SchemaContext schemaContext) {
89 this.schemaContext = schemaContext;
91 if(shardManager != null) {
92 shardManager.tell(new UpdateSchemaContext(schemaContext), null);
96 public SchemaContext getSchemaContext() {
101 * Finds the primary for a given shard
106 public ActorSelection findPrimary(String shardName) {
107 String path = findPrimaryPath(shardName);
108 return actorSystem.actorSelection(path);
112 * Finds a local shard given it's shard name and return it's ActorRef
114 * @param shardName the name of the local shard that needs to be found
115 * @return a reference to a local shard actor which represents the shard
116 * specified by the shardName
118 public ActorRef findLocalShard(String shardName) {
119 Object result = executeLocalOperation(shardManager,
120 new FindLocalShard(shardName), ASK_DURATION);
122 if (result instanceof LocalShardFound) {
123 LocalShardFound found = (LocalShardFound) result;
125 LOG.debug("Local shard found {}", found.getPath());
127 return found.getPath();
134 public String findPrimaryPath(String shardName) {
135 Object result = executeLocalOperation(shardManager,
136 new FindPrimary(shardName).toSerializable(), ASK_DURATION);
138 if (result.getClass().equals(PrimaryFound.SERIALIZABLE_CLASS)) {
139 PrimaryFound found = PrimaryFound.fromSerializable(result);
141 LOG.debug("Primary found {}", found.getPrimaryPath());
143 return found.getPrimaryPath();
145 throw new PrimaryNotFoundException("Could not find primary for shardName " + shardName);
150 * Executes an operation on a local actor and wait for it's response
155 * @return The response of the operation
157 public Object executeLocalOperation(ActorRef actor, Object message,
158 FiniteDuration duration) {
159 Future<Object> future =
160 ask(actor, message, new Timeout(duration));
163 return Await.result(future, AWAIT_DURATION);
164 } catch (Exception e) {
165 throw new TimeoutException("Sending message " + message.getClass().toString() + " to actor " + actor.toString() + " failed" , e);
170 * Execute an operation on a remote actor and wait for it's response
177 public Object executeRemoteOperation(ActorSelection actor, Object message,
178 FiniteDuration duration) {
180 LOG.debug("Sending remote message {} to {}", message.getClass().toString(), actor.toString());
182 Future<Object> future =
183 ask(actor, message, new Timeout(duration));
186 return Await.result(future, AWAIT_DURATION);
187 } catch (Exception e) {
188 throw new TimeoutException("Sending message " + message.getClass().toString() + " to actor " + actor.toString() + " failed" , e);
193 * Execute an operation on a remote actor asynchronously.
195 * @param actor the ActorSelection
196 * @param message the message to send
197 * @param duration the maximum amount of time to send he message
198 * @return a Future containing the eventual result
200 public Future<Object> executeRemoteOperationAsync(ActorSelection actor, Object message,
201 FiniteDuration duration) {
203 LOG.debug("Sending remote message {} to {}", message.getClass().toString(), actor.toString());
205 return ask(actor, message, new Timeout(duration));
209 * Sends an operation to be executed by a remote actor asynchronously without waiting for a
210 * reply (essentially set and forget).
212 * @param actor the ActorSelection
213 * @param message the message to send
215 public void sendRemoteOperationAsync(ActorSelection actor, Object message) {
216 actor.tell(message, ActorRef.noSender());
220 * Execute an operation on the primary for a given shard
222 * This method first finds the primary for a given shard ,then sends
223 * the message to the remote shard and waits for a response
230 * @throws org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException if the message to the remote shard times out
231 * @throws org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException if the primary shard is not found
233 public Object executeShardOperation(String shardName, Object message,
234 FiniteDuration duration) {
235 ActorSelection primary = findPrimary(shardName);
237 return executeRemoteOperation(primary, message, duration);
241 * Execute an operation on the the local shard only
243 * This method first finds the address of the local shard if any. It then
244 * executes the operation on it.
247 * @param shardName the name of the shard on which the operation needs to be executed
248 * @param message the message that needs to be sent to the shard
249 * @param duration the time duration in which this operation should complete
250 * @return the message that was returned by the local actor on which the
251 * the operation was executed. If a local shard was not found then
253 * @throws org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException
254 * if the operation does not complete in a specified time duration
256 public Object executeLocalShardOperation(String shardName, Object message,
257 FiniteDuration duration) {
258 ActorRef local = findLocalShard(shardName);
261 return executeLocalOperation(local, message, duration);
268 public void shutdown() {
269 shardManager.tell(PoisonPill.getInstance(), null);
270 actorSystem.shutdown();
274 * @deprecated Need to stop using this method. There are ways to send a
275 * remote ActorRef as a string which should be used instead of this hack
278 * @param localPathOfRemoteActor
282 public String resolvePath(final String primaryPath,
283 final String localPathOfRemoteActor) {
284 StringBuilder builder = new StringBuilder();
285 String[] primaryPathElements = primaryPath.split("/");
286 builder.append(primaryPathElements[0]).append("//")
287 .append(primaryPathElements[1]).append(primaryPathElements[2]);
288 String[] remotePathElements = localPathOfRemoteActor.split("/");
289 for (int i = 3; i < remotePathElements.length; i++) {
290 builder.append("/").append(remotePathElements[i]);
293 return builder.toString();
297 public ActorPath actorFor(String path){
298 return actorSystem.actorFor(path).path();
301 public String getCurrentMemberName(){
302 return clusterWrapper.getCurrentMemberName();