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 static akka.pattern.Patterns.ask;
12 import akka.actor.ActorPath;
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSelection;
15 import akka.actor.ActorSystem;
16 import akka.actor.Address;
17 import akka.actor.PoisonPill;
18 import akka.dispatch.Mapper;
19 import akka.pattern.AskTimeoutException;
20 import akka.util.Timeout;
21 import com.codahale.metrics.JmxReporter;
22 import com.codahale.metrics.MetricRegistry;
23 import com.codahale.metrics.Timer;
24 import com.google.common.base.Optional;
25 import com.google.common.base.Preconditions;
26 import com.google.common.base.Strings;
27 import com.google.common.util.concurrent.RateLimiter;
28 import java.util.concurrent.TimeUnit;
29 import org.opendaylight.controller.cluster.common.actor.CommonConfig;
30 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
31 import org.opendaylight.controller.cluster.datastore.Configuration;
32 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
33 import org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException;
34 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
35 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
36 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
37 import org.opendaylight.controller.cluster.datastore.exceptions.UnknownMessageException;
38 import org.opendaylight.controller.cluster.datastore.messages.ActorNotInitialized;
39 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
40 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
41 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
42 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
43 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
44 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
45 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import scala.concurrent.Await;
50 import scala.concurrent.Future;
51 import scala.concurrent.duration.Duration;
52 import scala.concurrent.duration.FiniteDuration;
55 * The ActorContext class contains utility methods which could be used by
56 * non-actors (like DistributedDataStore) to work with actors a little more
57 * easily. An ActorContext can be freely passed around to local object instances
58 * but should not be passed to actors especially remote actors
60 public class ActorContext {
61 private static final Logger LOG = LoggerFactory.getLogger(ActorContext.class);
62 private static final String UNKNOWN_DATA_STORE_TYPE = "unknown";
63 private static final String DISTRIBUTED_DATA_STORE_METRIC_REGISTRY = "distributed-data-store";
64 private static final String METRIC_RATE = "rate";
65 private static final String DOMAIN = "org.opendaylight.controller.cluster.datastore";
66 private static final Mapper<Throwable, Throwable> FIND_PRIMARY_FAILURE_TRANSFORMER =
67 new Mapper<Throwable, Throwable>() {
69 public Throwable apply(Throwable failure) {
70 Throwable actualFailure = failure;
71 if(failure instanceof AskTimeoutException) {
72 // A timeout exception most likely means the shard isn't initialized.
73 actualFailure = new NotInitializedException(
74 "Timed out trying to find the primary shard. Most likely cause is the " +
75 "shard is not initialized yet.");
81 public static final String MAILBOX = "bounded-mailbox";
83 private final ActorSystem actorSystem;
84 private final ActorRef shardManager;
85 private final ClusterWrapper clusterWrapper;
86 private final Configuration configuration;
87 private final DatastoreContext datastoreContext;
88 private final String dataStoreType;
89 private final FiniteDuration operationDuration;
90 private final Timeout operationTimeout;
91 private final String selfAddressHostPort;
92 private final RateLimiter txRateLimiter;
93 private final MetricRegistry metricRegistry = new MetricRegistry();
94 private final JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).inDomain(DOMAIN).build();
95 private final int transactionOutstandingOperationLimit;
96 private final Timeout transactionCommitOperationTimeout;
98 private volatile SchemaContext schemaContext;
100 public ActorContext(ActorSystem actorSystem, ActorRef shardManager,
101 ClusterWrapper clusterWrapper, Configuration configuration) {
102 this(actorSystem, shardManager, clusterWrapper, configuration,
103 DatastoreContext.newBuilder().build(), UNKNOWN_DATA_STORE_TYPE);
106 public ActorContext(ActorSystem actorSystem, ActorRef shardManager,
107 ClusterWrapper clusterWrapper, Configuration configuration,
108 DatastoreContext datastoreContext, String dataStoreType) {
109 this.actorSystem = actorSystem;
110 this.shardManager = shardManager;
111 this.clusterWrapper = clusterWrapper;
112 this.configuration = configuration;
113 this.datastoreContext = datastoreContext;
114 this.dataStoreType = dataStoreType;
115 this.txRateLimiter = RateLimiter.create(datastoreContext.getTransactionCreationInitialRateLimit());
117 operationDuration = Duration.create(datastoreContext.getOperationTimeoutInSeconds(), TimeUnit.SECONDS);
118 operationTimeout = new Timeout(operationDuration);
119 transactionCommitOperationTimeout = new Timeout(Duration.create(getDatastoreContext().getShardTransactionCommitTimeoutInSeconds(),
123 Address selfAddress = clusterWrapper.getSelfAddress();
124 if (selfAddress != null && !selfAddress.host().isEmpty()) {
125 selfAddressHostPort = selfAddress.host().get() + ":" + selfAddress.port().get();
127 selfAddressHostPort = null;
130 transactionOutstandingOperationLimit = new CommonConfig(this.getActorSystem().settings().config()).getMailBoxCapacity();
134 public DatastoreContext getDatastoreContext() {
135 return datastoreContext;
138 public ActorSystem getActorSystem() {
142 public ActorRef getShardManager() {
146 public ActorSelection actorSelection(String actorPath) {
147 return actorSystem.actorSelection(actorPath);
150 public ActorSelection actorSelection(ActorPath actorPath) {
151 return actorSystem.actorSelection(actorPath);
154 public void setSchemaContext(SchemaContext schemaContext) {
155 this.schemaContext = schemaContext;
157 if(shardManager != null) {
158 shardManager.tell(new UpdateSchemaContext(schemaContext), null);
162 public SchemaContext getSchemaContext() {
163 return schemaContext;
167 * Finds the primary shard for the given shard name
172 public Optional<ActorSelection> findPrimaryShard(String shardName) {
173 String path = findPrimaryPathOrNull(shardName);
175 return Optional.absent();
177 return Optional.of(actorSystem.actorSelection(path));
180 public Future<ActorSelection> findPrimaryShardAsync(final String shardName) {
181 Future<Object> future = executeOperationAsync(shardManager,
182 new FindPrimary(shardName, true).toSerializable(),
183 datastoreContext.getShardInitializationTimeout());
185 return future.transform(new Mapper<Object, ActorSelection>() {
187 public ActorSelection checkedApply(Object response) throws Exception {
188 if(response.getClass().equals(PrimaryFound.SERIALIZABLE_CLASS)) {
189 PrimaryFound found = PrimaryFound.fromSerializable(response);
191 LOG.debug("Primary found {}", found.getPrimaryPath());
192 return actorSystem.actorSelection(found.getPrimaryPath());
193 } else if(response instanceof ActorNotInitialized) {
194 throw new NotInitializedException(
195 String.format("Found primary shard %s but it's not initialized yet. " +
196 "Please try again later", shardName));
197 } else if(response instanceof PrimaryNotFound) {
198 throw new PrimaryNotFoundException(
199 String.format("No primary shard found for %S.", shardName));
202 throw new UnknownMessageException(String.format(
203 "FindPrimary returned unkown response: %s", response));
205 }, FIND_PRIMARY_FAILURE_TRANSFORMER, getActorSystem().dispatcher());
209 * Finds a local shard given its shard name and return it's ActorRef
211 * @param shardName the name of the local shard that needs to be found
212 * @return a reference to a local shard actor which represents the shard
213 * specified by the shardName
215 public Optional<ActorRef> findLocalShard(String shardName) {
216 Object result = executeOperation(shardManager, new FindLocalShard(shardName, false));
218 if (result instanceof LocalShardFound) {
219 LocalShardFound found = (LocalShardFound) result;
220 LOG.debug("Local shard found {}", found.getPath());
221 return Optional.of(found.getPath());
224 return Optional.absent();
228 * Finds a local shard async given its shard name and return a Future from which to obtain the
231 * @param shardName the name of the local shard that needs to be found
233 public Future<ActorRef> findLocalShardAsync( final String shardName) {
234 Future<Object> future = executeOperationAsync(shardManager,
235 new FindLocalShard(shardName, true), datastoreContext.getShardInitializationTimeout());
237 return future.map(new Mapper<Object, ActorRef>() {
239 public ActorRef checkedApply(Object response) throws Throwable {
240 if(response instanceof LocalShardFound) {
241 LocalShardFound found = (LocalShardFound)response;
242 LOG.debug("Local shard found {}", found.getPath());
243 return found.getPath();
244 } else if(response instanceof ActorNotInitialized) {
245 throw new NotInitializedException(
246 String.format("Found local shard for %s but it's not initialized yet.",
248 } else if(response instanceof LocalShardNotFound) {
249 throw new LocalShardNotFoundException(
250 String.format("Local shard for %s does not exist.", shardName));
253 throw new UnknownMessageException(String.format(
254 "FindLocalShard returned unkown response: %s", response));
256 }, getActorSystem().dispatcher());
259 private String findPrimaryPathOrNull(String shardName) {
260 Object result = executeOperation(shardManager, new FindPrimary(shardName, false).toSerializable());
262 if (result.getClass().equals(PrimaryFound.SERIALIZABLE_CLASS)) {
263 PrimaryFound found = PrimaryFound.fromSerializable(result);
265 LOG.debug("Primary found {}", found.getPrimaryPath());
266 return found.getPrimaryPath();
268 } else if (result.getClass().equals(ActorNotInitialized.class)){
269 throw new NotInitializedException(
270 String.format("Found primary shard[%s] but its not initialized yet. Please try again later", shardName)
280 * Executes an operation on a local actor and wait for it's response
284 * @return The response of the operation
286 public Object executeOperation(ActorRef actor, Object message) {
287 Future<Object> future = executeOperationAsync(actor, message, operationTimeout);
290 return Await.result(future, operationDuration);
291 } catch (Exception e) {
292 throw new TimeoutException("Sending message " + message.getClass().toString() +
293 " to actor " + actor.toString() + " failed. Try again later.", e);
297 public Future<Object> executeOperationAsync(ActorRef actor, Object message, Timeout timeout) {
298 Preconditions.checkArgument(actor != null, "actor must not be null");
299 Preconditions.checkArgument(message != null, "message must not be null");
301 LOG.debug("Sending message {} to {}", message.getClass(), actor);
302 return ask(actor, message, timeout);
306 * Execute an operation on a remote actor and wait for it's response
312 public Object executeOperation(ActorSelection actor, Object message) {
313 Future<Object> future = executeOperationAsync(actor, message);
316 return Await.result(future, operationDuration);
317 } catch (Exception e) {
318 throw new TimeoutException("Sending message " + message.getClass().toString() +
319 " to actor " + actor.toString() + " failed. Try again later.", e);
324 * Execute an operation on a remote actor asynchronously.
326 * @param actor the ActorSelection
327 * @param message the message to send
328 * @param timeout the operation timeout
329 * @return a Future containing the eventual result
331 public Future<Object> executeOperationAsync(ActorSelection actor, Object message,
333 Preconditions.checkArgument(actor != null, "actor must not be null");
334 Preconditions.checkArgument(message != null, "message must not be null");
336 LOG.debug("Sending message {} to {}", message.getClass(), actor);
338 return ask(actor, message, timeout);
342 * Execute an operation on a remote actor asynchronously.
344 * @param actor the ActorSelection
345 * @param message the message to send
346 * @return a Future containing the eventual result
348 public Future<Object> executeOperationAsync(ActorSelection actor, Object message) {
349 return executeOperationAsync(actor, message, operationTimeout);
353 * Sends an operation to be executed by a remote actor asynchronously without waiting for a
354 * reply (essentially set and forget).
356 * @param actor the ActorSelection
357 * @param message the message to send
359 public void sendOperationAsync(ActorSelection actor, Object message) {
360 Preconditions.checkArgument(actor != null, "actor must not be null");
361 Preconditions.checkArgument(message != null, "message must not be null");
363 LOG.debug("Sending message {} to {}", message.getClass(), actor);
365 actor.tell(message, ActorRef.noSender());
368 public void shutdown() {
369 shardManager.tell(PoisonPill.getInstance(), null);
370 actorSystem.shutdown();
373 public ClusterWrapper getClusterWrapper() {
374 return clusterWrapper;
377 public String getCurrentMemberName(){
378 return clusterWrapper.getCurrentMemberName();
382 * Send the message to each and every shard
386 public void broadcast(Object message){
387 for(String shardName : configuration.getAllShardNames()){
389 Optional<ActorSelection> primary = findPrimaryShard(shardName);
390 if (primary.isPresent()) {
391 primary.get().tell(message, ActorRef.noSender());
393 LOG.warn("broadcast failed to send message {} to shard {}. Primary not found",
394 message.getClass().getSimpleName(), shardName);
399 public FiniteDuration getOperationDuration() {
400 return operationDuration;
403 public boolean isPathLocal(String path) {
404 if (Strings.isNullOrEmpty(path)) {
408 int pathAtIndex = path.indexOf('@');
409 if (pathAtIndex == -1) {
410 //if the path is of local format, then its local and is co-located
413 } else if (selfAddressHostPort != null) {
414 // self-address and tx actor path, both are of remote path format
415 int slashIndex = path.indexOf('/', pathAtIndex);
417 if (slashIndex == -1) {
421 String hostPort = path.substring(pathAtIndex + 1, slashIndex);
422 return hostPort.equals(selfAddressHostPort);
425 // self address is local format and tx actor path is remote format
431 * @deprecated This method is present only to support backward compatibility with Helium and should not be
436 * @param localPathOfRemoteActor
440 public String resolvePath(final String primaryPath,
441 final String localPathOfRemoteActor) {
442 StringBuilder builder = new StringBuilder();
443 String[] primaryPathElements = primaryPath.split("/");
444 builder.append(primaryPathElements[0]).append("//")
445 .append(primaryPathElements[1]).append(primaryPathElements[2]);
446 String[] remotePathElements = localPathOfRemoteActor.split("/");
447 for (int i = 3; i < remotePathElements.length; i++) {
448 builder.append("/").append(remotePathElements[i]);
451 return builder.toString();
455 * Get the maximum number of operations that are to be permitted within a transaction before the transaction
456 * should begin throttling the operations
458 * Parking reading this configuration here because we need to get to the actor system settings
462 public int getTransactionOutstandingOperationLimit(){
463 return transactionOutstandingOperationLimit;
467 * This is a utility method that lets us get a Timer object for any operation. This is a little open-ended to allow
468 * us to create a timer for pretty much anything.
470 * @param operationName
473 public Timer getOperationTimer(String operationName){
474 final String rate = MetricRegistry.name(DISTRIBUTED_DATA_STORE_METRIC_REGISTRY, dataStoreType, operationName, METRIC_RATE);
475 return metricRegistry.timer(rate);
479 * Get the type of the data store to which this ActorContext belongs
483 public String getDataStoreType() {
484 return dataStoreType;
488 * Set the number of transaction creation permits that are to be allowed
490 * @param permitsPerSecond
492 public void setTxCreationLimit(double permitsPerSecond){
493 txRateLimiter.setRate(permitsPerSecond);
497 * Get the current transaction creation rate limit
500 public double getTxCreationLimit(){
501 return txRateLimiter.getRate();
505 * Try to acquire a transaction creation permit. Will block if no permits are available.
507 public void acquireTxCreationPermit(){
508 txRateLimiter.acquire();
512 * Return the operation timeout to be used when committing transactions
515 public Timeout getTransactionCommitOperationTimeout(){
516 return transactionCommitOperationTimeout;