From a884499896d6a146743272b08b4a7e504d9e8b9e Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 18 Jan 2017 01:49:13 +0100 Subject: [PATCH 1/1] Remove unneded RoutingTable time tracking Since we no longer use the time stored in internal entries, we can use a Set instead of a Map. Adjust internal structure of RoutingTable and tighten its exported methods. Change-Id: Ifa2ed53ffc9d05c658927167a4e7dd331c3c2006 Signed-off-by: Robert Varga --- .../remote/rpc/registry/RoutingTable.java | 65 +++++++++---------- .../remote/rpc/registry/RpcRegistry.java | 29 +++------ .../rpc/registry/gossip/BucketData.java | 3 +- 3 files changed, 43 insertions(+), 54 deletions(-) diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RoutingTable.java b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RoutingTable.java index 9e69fa66df..0e68eb8709 100644 --- a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RoutingTable.java +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RoutingTable.java @@ -8,70 +8,69 @@ package org.opendaylight.controller.remote.rpc.registry; import akka.actor.ActorRef; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; +import java.util.Collection; +import java.util.HashSet; import java.util.Optional; import java.util.Set; import org.opendaylight.controller.remote.rpc.registry.gossip.BucketData; -import org.opendaylight.controller.sal.connector.api.RpcRouter; import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier; public class RoutingTable implements BucketData, Serializable { - private static final long serialVersionUID = 5592610415175278760L; + private static final long serialVersionUID = 1L; - private final Map, Long> table; - private final ActorRef router; + private final Set> rpcs; + private final ActorRef rpcInvoker; - private RoutingTable(final ActorRef router, final Map, Long> table) { - this.router = Preconditions.checkNotNull(router); - this.table = Preconditions.checkNotNull(table); + private RoutingTable(final ActorRef rpcInvoker, final Set> table) { + this.rpcInvoker = Preconditions.checkNotNull(rpcInvoker); + this.rpcs = ImmutableSet.copyOf(table); } - RoutingTable(final ActorRef router) { - this(router, new HashMap<>()); - } - - RoutingTable copy() { - return new RoutingTable(router, new HashMap<>(table)); + RoutingTable(final ActorRef rpcInvoker) { + this(rpcInvoker, ImmutableSet.of()); } @Override public Optional getWatchActor() { - return Optional.of(router); - } - - public Set> getRoutes() { - return table.keySet(); + return Optional.of(rpcInvoker); } - public void addRoute(final RpcRouter.RouteIdentifier routeId) { - table.put(routeId, System.currentTimeMillis()); + public Set> getRoutes() { + return rpcs; } - public void removeRoute(final RpcRouter.RouteIdentifier routeId) { - table.remove(routeId); + ActorRef getRpcInvoker() { + return rpcInvoker; } - public boolean contains(final RpcRouter.RouteIdentifier routeId) { - return table.containsKey(routeId); + RoutingTable addRpcs(final Collection> toAdd) { + final Set> newRpcs = new HashSet<>(rpcs); + newRpcs.addAll(toAdd); + return new RoutingTable(rpcInvoker, newRpcs); } - public boolean isEmpty() { - return table.isEmpty(); + RoutingTable removeRpcs(final Collection> toRemove) { + final Set> newRpcs = new HashSet<>(rpcs); + newRpcs.removeAll(toRemove); + return new RoutingTable(rpcInvoker, newRpcs); } - public int size() { - return table.size(); + @VisibleForTesting + boolean contains(final RouteIdentifier routeId) { + return rpcs.contains(routeId); } - public ActorRef getRouter() { - return router; + @VisibleForTesting + int size() { + return rpcs.size(); } @Override public String toString() { - return "RoutingTable{" + "table=" + table + ", router=" + router + '}'; + return "RoutingTable{" + "rpcs=" + rpcs + ", rpcInvoker=" + rpcInvoker + '}'; } } diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java index 1545eb00d2..c8415cc818 100644 --- a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java @@ -29,7 +29,6 @@ import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.Remo import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.UpdateRemoteEndpoints; import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket; import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStore; -import org.opendaylight.controller.sal.connector.api.RpcRouter; import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier; import org.opendaylight.yangtools.yang.model.api.SchemaPath; @@ -74,13 +73,7 @@ public class RpcRegistry extends BucketStore { private void receiveAddRoutes(final AddOrUpdateRoutes msg) { LOG.debug("AddOrUpdateRoutes: {}", msg.getRouteIdentifiers()); - - RoutingTable table = getLocalBucket().getData().copy(); - for (RpcRouter.RouteIdentifier routeId : msg.getRouteIdentifiers()) { - table.addRoute(routeId); - } - - updateLocalBucket(table); + updateLocalBucket(getLocalBucket().getData().addRpcs(msg.getRouteIdentifiers())); } /** @@ -89,12 +82,8 @@ public class RpcRegistry extends BucketStore { * @param msg contains list of route ids to remove */ private void receiveRemoveRoutes(final RemoveRoutes msg) { - RoutingTable table = getLocalBucket().getData().copy(); - for (RpcRouter.RouteIdentifier routeId : msg.getRouteIdentifiers()) { - table.removeRoute(routeId); - } - - updateLocalBucket(table); + LOG.debug("RemoveRoutes: {}", msg.getRouteIdentifiers()); + updateLocalBucket(getLocalBucket().getData().removeRpcs(msg.getRouteIdentifiers())); } @Override @@ -120,7 +109,7 @@ public class RpcRegistry extends BucketStore { } endpoints.put(e.getKey(), rpcs.isEmpty() ? Optional.empty() - : Optional.of(new RemoteRpcEndpoint(table.getRouter(), rpcs))); + : Optional.of(new RemoteRpcEndpoint(table.getRpcInvoker(), rpcs))); } if (!endpoints.isEmpty()) { @@ -151,15 +140,15 @@ public class RpcRegistry extends BucketStore { */ public static class Messages { abstract static class AbstractRouteMessage { - final List> routeIdentifiers; + final List> routeIdentifiers; - AbstractRouteMessage(final List> routeIdentifiers) { + AbstractRouteMessage(final List> routeIdentifiers) { Preconditions.checkArgument(routeIdentifiers != null && !routeIdentifiers.isEmpty(), "Route Identifiers must be supplied"); this.routeIdentifiers = routeIdentifiers; } - List> getRouteIdentifiers() { + List> getRouteIdentifiers() { return this.routeIdentifiers; } @@ -170,13 +159,13 @@ public class RpcRegistry extends BucketStore { } public static final class AddOrUpdateRoutes extends AbstractRouteMessage { - public AddOrUpdateRoutes(final List> routeIdentifiers) { + public AddOrUpdateRoutes(final List> routeIdentifiers) { super(routeIdentifiers); } } public static final class RemoveRoutes extends AbstractRouteMessage { - public RemoveRoutes(final List> routeIdentifiers) { + public RemoveRoutes(final List> routeIdentifiers) { super(routeIdentifiers); } } diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/gossip/BucketData.java b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/gossip/BucketData.java index 54e9471233..0dea3d1da4 100644 --- a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/gossip/BucketData.java +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/gossip/BucketData.java @@ -9,6 +9,7 @@ package org.opendaylight.controller.remote.rpc.registry.gossip; import akka.actor.ActorRef; import java.util.Optional; +import org.opendaylight.yangtools.concepts.Immutable; /** * Marker interface for data which is able to be held in a {@link Bucket}. @@ -17,7 +18,7 @@ import java.util.Optional; * * @param Concrete BucketData type */ -public interface BucketData> { +public interface BucketData> extends Immutable { /** * Return the {@link ActorRef} which should be tracked as the authoritative source of this bucket's data. * The bucket will be invalidated should the actor be reported as Terminated. -- 2.36.6