package org.opendaylight.controller.remote.rpc.registry;
import akka.actor.ActorRef;
-import akka.event.Logging;
-import akka.event.LoggingAdapter;
+import akka.actor.Cancellable;
+import akka.actor.Props;
+import akka.japi.Creator;
import akka.japi.Option;
import akka.japi.Pair;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
+import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig;
import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.AddOrUpdateRoutes;
import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.FindRouters;
import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.RemoveRoutes;
import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.SetLocalRouter;
import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStore;
+import org.opendaylight.controller.remote.rpc.registry.mbeans.RemoteRpcRegistryMXBean;
+import org.opendaylight.controller.remote.rpc.registry.mbeans.RemoteRpcRegistryMXBeanImpl;
import org.opendaylight.controller.sal.connector.api.RpcRouter;
import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
+import scala.concurrent.duration.FiniteDuration;
/**
* Registry to look up cluster nodes that have registered for a given rpc.
* cluster wide information.
*/
public class RpcRegistry extends BucketStore<RoutingTable> {
+ private final Set<Runnable> routesUpdatedCallbacks = new HashSet<>();
+ private final FiniteDuration findRouterTimeout;
- final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
-
- public RpcRegistry() {
+ public RpcRegistry(RemoteRpcProviderConfig config) {
+ super(config);
getLocalBucket().setData(new RoutingTable());
+ findRouterTimeout = getConfig().getGossipTickInterval().$times(10);
+ }
+
+ public static Props props(RemoteRpcProviderConfig config) {
+ return Props.create(new RpcRegistryCreator(config));
}
@Override
receiveRemoveRoutes((RemoveRoutes) message);
} else if (message instanceof Messages.FindRouters) {
receiveGetRouter((FindRouters) message);
+ } else if (message instanceof Runnable) {
+ ((Runnable)message).run();
} else {
super.handleReceive(message);
}
}
updateLocalBucket(table);
+
+ onBucketsUpdated();
}
/**
/**
* Finds routers for the given rpc.
*
- * @param msg
+ * @param findRouters
*/
- private void receiveGetRouter(FindRouters msg) {
+ private void receiveGetRouter(final FindRouters findRouters) {
+ log.debug("receiveGetRouter for {}", findRouters.getRouteIdentifier());
+
+ final ActorRef sender = getSender();
+ if(!findRouters(findRouters, sender)) {
+ log.debug("No routers found for {} - scheduling {} ms timer", findRouters.getRouteIdentifier(),
+ findRouterTimeout.toMillis());
+
+ final AtomicReference<Cancellable> timer = new AtomicReference<>();
+ final Runnable routesUpdatedRunnable = new Runnable() {
+ @Override
+ public void run() {
+ if(findRouters(findRouters, sender)) {
+ routesUpdatedCallbacks.remove(this);
+ timer.get().cancel();
+ }
+ }
+ };
+
+ routesUpdatedCallbacks.add(routesUpdatedRunnable);
+
+ Runnable timerRunnable = new Runnable() {
+ @Override
+ public void run() {
+ log.warn("Timed out finding routers for {}", findRouters.getRouteIdentifier());
+
+ routesUpdatedCallbacks.remove(routesUpdatedRunnable);
+ sender.tell(new Messages.FindRoutersReply(
+ Collections.<Pair<ActorRef, Long>>emptyList()), self());
+ }
+ };
+
+ timer.set(getContext().system().scheduler().scheduleOnce(findRouterTimeout, self(), timerRunnable,
+ getContext().dispatcher(), self()));
+ }
+ }
+
+ private boolean findRouters(FindRouters findRouters, ActorRef sender) {
List<Pair<ActorRef, Long>> routers = new ArrayList<>();
- RouteIdentifier<?, ?, ?> routeId = msg.getRouteIdentifier();
+ RouteIdentifier<?, ?, ?> routeId = findRouters.getRouteIdentifier();
findRoutes(getLocalBucket().getData(), routeId, routers);
for(Bucket<RoutingTable> bucket : getRemoteBuckets().values()) {
findRoutes(bucket.getData(), routeId, routers);
}
- getSender().tell(new Messages.FindRoutersReply(routers), getSelf());
+ log.debug("Found {} routers for {}", routers.size(), findRouters.getRouteIdentifier());
+
+ boolean foundRouters = !routers.isEmpty();
+ if(foundRouters) {
+ sender.tell(new Messages.FindRoutersReply(routers), getSelf());
+ }
+
+ return foundRouters;
}
private void findRoutes(RoutingTable table, RpcRouter.RouteIdentifier<?, ?, ?> routeId,
}
}
+ @Override
+ protected void onBucketsUpdated() {
+ for(Runnable callBack: routesUpdatedCallbacks) {
+ callBack.run();
+ }
+ }
+
/**
* All messages used by the RpcRegistry
*/
}
}
}
+
+ private static class RpcRegistryCreator implements Creator<RpcRegistry> {
+ private static final long serialVersionUID = 1L;
+ private final RemoteRpcProviderConfig config;
+
+ private RpcRegistryCreator(RemoteRpcProviderConfig config) {
+ this.config = config;
+ }
+
+ @Override
+ public RpcRegistry create() throws Exception {
+ RpcRegistry registry = new RpcRegistry(config);
+ RemoteRpcRegistryMXBean mxBean = new RemoteRpcRegistryMXBeanImpl(registry);
+ return registry;
+ }
+ }
}