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.dispatch.OnComplete;
20 import akka.pattern.AskTimeoutException;
21 import akka.util.Timeout;
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 java.util.concurrent.TimeUnit;
28 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
29 import org.opendaylight.controller.cluster.datastore.Configuration;
30 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
31 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
32 import org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException;
33 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
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.FindLocalShard;
39 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
40 import org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound;
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.PrimaryShardInfo;
44 import org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound;
45 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
46 import org.opendaylight.controller.cluster.reporting.MetricsReporter;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import scala.concurrent.Await;
52 import scala.concurrent.ExecutionContext;
53 import scala.concurrent.Future;
54 import scala.concurrent.duration.Duration;
55 import scala.concurrent.duration.FiniteDuration;
58 * The ActorContext class contains utility methods which could be used by
59 * non-actors (like DistributedDataStore) to work with actors a little more
60 * easily. An ActorContext can be freely passed around to local object instances
61 * but should not be passed to actors especially remote actors
63 public class ActorContext {
64 private static final Logger LOG = LoggerFactory.getLogger(ActorContext.class);
65 private static final String DISTRIBUTED_DATA_STORE_METRIC_REGISTRY = "distributed-data-store";
66 private static final String METRIC_RATE = "rate";
67 private static final Mapper<Throwable, Throwable> FIND_PRIMARY_FAILURE_TRANSFORMER =
68 new Mapper<Throwable, Throwable>() {
70 public Throwable apply(Throwable failure) {
71 Throwable actualFailure = failure;
72 if(failure instanceof AskTimeoutException) {
73 // A timeout exception most likely means the shard isn't initialized.
74 actualFailure = new NotInitializedException(
75 "Timed out trying to find the primary shard. Most likely cause is the " +
76 "shard is not initialized yet.");
82 public static final String MAILBOX = "bounded-mailbox";
83 public static final String COMMIT = "commit";
85 private final ActorSystem actorSystem;
86 private final ActorRef shardManager;
87 private final ClusterWrapper clusterWrapper;
88 private final Configuration configuration;
89 private DatastoreContext datastoreContext;
90 private FiniteDuration operationDuration;
91 private Timeout operationTimeout;
92 private final String selfAddressHostPort;
93 private TransactionRateLimiter txRateLimiter;
94 private Timeout transactionCommitOperationTimeout;
95 private Timeout shardInitializationTimeout;
96 private final Dispatchers dispatchers;
98 private volatile SchemaContext schemaContext;
99 private volatile boolean updated;
100 private final MetricRegistry metricRegistry = MetricsReporter.getInstance(DatastoreContext.METRICS_DOMAIN).getMetricsRegistry();
102 private final PrimaryShardInfoFutureCache primaryShardInfoCache;
104 public ActorContext(ActorSystem actorSystem, ActorRef shardManager,
105 ClusterWrapper clusterWrapper, Configuration configuration) {
106 this(actorSystem, shardManager, clusterWrapper, configuration,
107 DatastoreContext.newBuilder().build(), new PrimaryShardInfoFutureCache());
110 public ActorContext(ActorSystem actorSystem, ActorRef shardManager,
111 ClusterWrapper clusterWrapper, Configuration configuration,
112 DatastoreContext datastoreContext, PrimaryShardInfoFutureCache primaryShardInfoCache) {
113 this.actorSystem = actorSystem;
114 this.shardManager = shardManager;
115 this.clusterWrapper = clusterWrapper;
116 this.configuration = configuration;
117 this.datastoreContext = datastoreContext;
118 this.dispatchers = new Dispatchers(actorSystem.dispatchers());
119 this.primaryShardInfoCache = primaryShardInfoCache;
121 setCachedProperties();
123 Address selfAddress = clusterWrapper.getSelfAddress();
124 if (selfAddress != null && !selfAddress.host().isEmpty()) {
125 selfAddressHostPort = selfAddress.host().get() + ":" + selfAddress.port().get();
127 selfAddressHostPort = null;
132 private void setCachedProperties() {
133 txRateLimiter = new TransactionRateLimiter(this);
135 operationDuration = Duration.create(datastoreContext.getOperationTimeoutInSeconds(), TimeUnit.SECONDS);
136 operationTimeout = new Timeout(operationDuration);
138 transactionCommitOperationTimeout = new Timeout(Duration.create(
139 datastoreContext.getShardTransactionCommitTimeoutInSeconds(), TimeUnit.SECONDS));
141 shardInitializationTimeout = new Timeout(datastoreContext.getShardInitializationTimeout().duration().$times(2));
144 public DatastoreContext getDatastoreContext() {
145 return datastoreContext;
148 public ActorSystem getActorSystem() {
152 public ActorRef getShardManager() {
156 public ActorSelection actorSelection(String actorPath) {
157 return actorSystem.actorSelection(actorPath);
160 public ActorSelection actorSelection(ActorPath actorPath) {
161 return actorSystem.actorSelection(actorPath);
164 public void setSchemaContext(SchemaContext schemaContext) {
165 this.schemaContext = schemaContext;
167 if(shardManager != null) {
168 shardManager.tell(new UpdateSchemaContext(schemaContext), ActorRef.noSender());
172 public void setDatastoreContext(DatastoreContext context) {
173 this.datastoreContext = context;
174 setCachedProperties();
176 // We write the 'updated' volatile to trigger a write memory barrier so that the writes above
177 // will be published immediately even though they may not be immediately visible to other
178 // threads due to unsynchronized reads. That's OK though - we're going for eventual
179 // consistency here as immediately visible updates to these members aren't critical. These
180 // members could've been made volatile but wanted to avoid volatile reads as these are
181 // accessed often and updates will be infrequent.
185 if(shardManager != null) {
186 shardManager.tell(context, ActorRef.noSender());
190 public SchemaContext getSchemaContext() {
191 return schemaContext;
194 public Future<PrimaryShardInfo> findPrimaryShardAsync(final String shardName) {
195 Future<PrimaryShardInfo> ret = primaryShardInfoCache.getIfPresent(shardName);
199 Future<Object> future = executeOperationAsync(shardManager,
200 new FindPrimary(shardName, true), shardInitializationTimeout);
202 return future.transform(new Mapper<Object, PrimaryShardInfo>() {
204 public PrimaryShardInfo checkedApply(Object response) throws Exception {
205 if(response instanceof RemotePrimaryShardFound) {
206 LOG.debug("findPrimaryShardAsync received: {}", response);
207 RemotePrimaryShardFound found = (RemotePrimaryShardFound)response;
208 return onPrimaryShardFound(shardName, found.getPrimaryPath(), found.getPrimaryVersion(), null);
209 } else if(response instanceof LocalPrimaryShardFound) {
210 LOG.debug("findPrimaryShardAsync received: {}", response);
211 LocalPrimaryShardFound found = (LocalPrimaryShardFound)response;
212 return onPrimaryShardFound(shardName, found.getPrimaryPath(), DataStoreVersions.CURRENT_VERSION,
213 found.getLocalShardDataTree());
214 } else if(response instanceof NotInitializedException) {
215 throw (NotInitializedException)response;
216 } else if(response instanceof PrimaryNotFoundException) {
217 throw (PrimaryNotFoundException)response;
218 } else if(response instanceof NoShardLeaderException) {
219 throw (NoShardLeaderException)response;
222 throw new UnknownMessageException(String.format(
223 "FindPrimary returned unkown response: %s", response));
225 }, FIND_PRIMARY_FAILURE_TRANSFORMER, getClientDispatcher());
228 private PrimaryShardInfo onPrimaryShardFound(String shardName, String primaryActorPath,
229 short primaryVersion, DataTree localShardDataTree) {
230 ActorSelection actorSelection = actorSystem.actorSelection(primaryActorPath);
231 PrimaryShardInfo info = new PrimaryShardInfo(actorSelection, primaryVersion,
232 Optional.fromNullable(localShardDataTree));
233 primaryShardInfoCache.putSuccessful(shardName, info);
238 * Finds a local shard given its shard name and return it's ActorRef
240 * @param shardName the name of the local shard that needs to be found
241 * @return a reference to a local shard actor which represents the shard
242 * specified by the shardName
244 public Optional<ActorRef> findLocalShard(String shardName) {
245 Object result = executeOperation(shardManager, new FindLocalShard(shardName, false));
247 if (result instanceof LocalShardFound) {
248 LocalShardFound found = (LocalShardFound) result;
249 LOG.debug("Local shard found {}", found.getPath());
250 return Optional.of(found.getPath());
253 return Optional.absent();
257 * Finds a local shard async given its shard name and return a Future from which to obtain the
260 * @param shardName the name of the local shard that needs to be found
262 public Future<ActorRef> findLocalShardAsync( final String shardName) {
263 Future<Object> future = executeOperationAsync(shardManager,
264 new FindLocalShard(shardName, true), shardInitializationTimeout);
266 return future.map(new Mapper<Object, ActorRef>() {
268 public ActorRef checkedApply(Object response) throws Throwable {
269 if(response instanceof LocalShardFound) {
270 LocalShardFound found = (LocalShardFound)response;
271 LOG.debug("Local shard found {}", found.getPath());
272 return found.getPath();
273 } else if(response instanceof NotInitializedException) {
274 throw (NotInitializedException)response;
275 } else if(response instanceof LocalShardNotFound) {
276 throw new LocalShardNotFoundException(
277 String.format("Local shard for %s does not exist.", shardName));
280 throw new UnknownMessageException(String.format(
281 "FindLocalShard returned unkown response: %s", response));
283 }, getClientDispatcher());
287 * Executes an operation on a local actor and wait for it's response
291 * @return The response of the operation
293 public Object executeOperation(ActorRef actor, Object message) {
294 Future<Object> future = executeOperationAsync(actor, message, operationTimeout);
297 return Await.result(future, operationDuration);
298 } catch (Exception e) {
299 throw new TimeoutException("Sending message " + message.getClass().toString() +
300 " to actor " + actor.toString() + " failed. Try again later.", e);
304 public Future<Object> executeOperationAsync(ActorRef actor, Object message, Timeout timeout) {
305 Preconditions.checkArgument(actor != null, "actor must not be null");
306 Preconditions.checkArgument(message != null, "message must not be null");
308 LOG.debug("Sending message {} to {}", message.getClass(), actor);
309 return doAsk(actor, message, timeout);
313 * Execute an operation on a remote actor and wait for it's response
319 public Object executeOperation(ActorSelection actor, Object message) {
320 Future<Object> future = executeOperationAsync(actor, message);
323 return Await.result(future, operationDuration);
324 } catch (Exception e) {
325 throw new TimeoutException("Sending message " + message.getClass().toString() +
326 " to actor " + actor.toString() + " failed. Try again later.", e);
331 * Execute an operation on a remote actor asynchronously.
333 * @param actor the ActorSelection
334 * @param message the message to send
335 * @param timeout the operation timeout
336 * @return a Future containing the eventual result
338 public Future<Object> executeOperationAsync(ActorSelection actor, Object message,
340 Preconditions.checkArgument(actor != null, "actor must not be null");
341 Preconditions.checkArgument(message != null, "message must not be null");
343 LOG.debug("Sending message {} to {}", message.getClass(), actor);
345 return doAsk(actor, message, timeout);
349 * Execute an operation on a remote actor asynchronously.
351 * @param actor the ActorSelection
352 * @param message the message to send
353 * @return a Future containing the eventual result
355 public Future<Object> executeOperationAsync(ActorSelection actor, Object message) {
356 return executeOperationAsync(actor, message, operationTimeout);
360 * Sends an operation to be executed by a remote actor asynchronously without waiting for a
361 * reply (essentially set and forget).
363 * @param actor the ActorSelection
364 * @param message the message to send
366 public void sendOperationAsync(ActorSelection actor, Object message) {
367 Preconditions.checkArgument(actor != null, "actor must not be null");
368 Preconditions.checkArgument(message != null, "message must not be null");
370 LOG.debug("Sending message {} to {}", message.getClass(), actor);
372 actor.tell(message, ActorRef.noSender());
375 public void shutdown() {
376 shardManager.tell(PoisonPill.getInstance(), ActorRef.noSender());
379 public ClusterWrapper getClusterWrapper() {
380 return clusterWrapper;
383 public String getCurrentMemberName(){
384 return clusterWrapper.getCurrentMemberName();
388 * Send the message to each and every shard
392 public void broadcast(final Object message){
393 for(final String shardName : configuration.getAllShardNames()){
395 Future<PrimaryShardInfo> primaryFuture = findPrimaryShardAsync(shardName);
396 primaryFuture.onComplete(new OnComplete<PrimaryShardInfo>() {
398 public void onComplete(Throwable failure, PrimaryShardInfo primaryShardInfo) {
399 if(failure != null) {
400 LOG.warn("broadcast failed to send message {} to shard {}: {}",
401 message.getClass().getSimpleName(), shardName, failure);
403 primaryShardInfo.getPrimaryShardActor().tell(message, ActorRef.noSender());
406 }, getClientDispatcher());
410 public FiniteDuration getOperationDuration() {
411 return operationDuration;
414 public boolean isPathLocal(String path) {
415 if (Strings.isNullOrEmpty(path)) {
419 int pathAtIndex = path.indexOf('@');
420 if (pathAtIndex == -1) {
421 //if the path is of local format, then its local and is co-located
424 } else if (selfAddressHostPort != null) {
425 // self-address and tx actor path, both are of remote path format
426 int slashIndex = path.indexOf('/', pathAtIndex);
428 if (slashIndex == -1) {
432 String hostPort = path.substring(pathAtIndex + 1, slashIndex);
433 return hostPort.equals(selfAddressHostPort);
436 // self address is local format and tx actor path is remote format
442 * @deprecated This method is present only to support backward compatibility with Helium and should not be
447 * @param localPathOfRemoteActor
451 public String resolvePath(final String primaryPath,
452 final String localPathOfRemoteActor) {
453 StringBuilder builder = new StringBuilder();
454 String[] primaryPathElements = primaryPath.split("/");
455 builder.append(primaryPathElements[0]).append("//")
456 .append(primaryPathElements[1]).append(primaryPathElements[2]);
457 String[] remotePathElements = localPathOfRemoteActor.split("/");
458 for (int i = 3; i < remotePathElements.length; i++) {
459 builder.append("/").append(remotePathElements[i]);
462 return builder.toString();
466 * This is a utility method that lets us get a Timer object for any operation. This is a little open-ended to allow
467 * us to create a timer for pretty much anything.
469 * @param operationName
472 public Timer getOperationTimer(String operationName){
473 return getOperationTimer(datastoreContext.getDataStoreType(), operationName);
476 public Timer getOperationTimer(String dataStoreType, String operationName){
477 final String rate = MetricRegistry.name(DISTRIBUTED_DATA_STORE_METRIC_REGISTRY, dataStoreType,
478 operationName, METRIC_RATE);
479 return metricRegistry.timer(rate);
483 * Get the type of the data store to which this ActorContext belongs
487 public String getDataStoreType() {
488 return datastoreContext.getDataStoreType();
492 * Get the current transaction creation rate limit
495 public double getTxCreationLimit(){
496 return txRateLimiter.getTxCreationLimit();
500 * Try to acquire a transaction creation permit. Will block if no permits are available.
502 public void acquireTxCreationPermit(){
503 txRateLimiter.acquire();
507 * Return the operation timeout to be used when committing transactions
510 public Timeout getTransactionCommitOperationTimeout(){
511 return transactionCommitOperationTimeout;
515 * An akka dispatcher that is meant to be used when processing ask Futures which were triggered by client
516 * code on the datastore
519 public ExecutionContext getClientDispatcher() {
520 return this.dispatchers.getDispatcher(Dispatchers.DispatcherType.Client);
523 public String getNotificationDispatcherPath(){
524 return this.dispatchers.getDispatcherPath(Dispatchers.DispatcherType.Notification);
527 public Configuration getConfiguration() {
528 return configuration;
531 protected Future<Object> doAsk(ActorRef actorRef, Object message, Timeout timeout){
532 return ask(actorRef, message, timeout);
535 protected Future<Object> doAsk(ActorSelection actorRef, Object message, Timeout timeout){
536 return ask(actorRef, message, timeout);
539 public PrimaryShardInfoFutureCache getPrimaryShardInfoCache() {
540 return primaryShardInfoCache;