Remove unneded RoutingTable time tracking 07/50607/11
authorRobert Varga <rovarga@cisco.com>
Wed, 18 Jan 2017 00:49:13 +0000 (01:49 +0100)
committerRobert Varga <rovarga@cisco.com>
Mon, 30 Jan 2017 13:52:44 +0000 (14:52 +0100)
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 <rovarga@cisco.com>
opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RoutingTable.java
opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java
opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/gossip/BucketData.java

index 9e69fa66df463c25f0c9c29562c9d1f015e642a8..0e68eb87096828f89892b338969aaaea24f13954 100644 (file)
@@ -8,70 +8,69 @@
 package org.opendaylight.controller.remote.rpc.registry;
 
 import akka.actor.ActorRef;
 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.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
 import java.io.Serializable;
 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 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<RoutingTable>, Serializable {
 import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
 
 public class RoutingTable implements BucketData<RoutingTable>, Serializable {
-    private static final long serialVersionUID = 5592610415175278760L;
+    private static final long serialVersionUID = 1L;
 
 
-    private final Map<RouteIdentifier<?, ?, ?>, Long> table;
-    private final ActorRef router;
+    private final Set<RouteIdentifier<?, ?, ?>> rpcs;
+    private final ActorRef rpcInvoker;
 
 
-    private RoutingTable(final ActorRef router, final Map<RouteIdentifier<?, ?, ?>, Long> table) {
-        this.router = Preconditions.checkNotNull(router);
-        this.table = Preconditions.checkNotNull(table);
+    private RoutingTable(final ActorRef rpcInvoker, final Set<RouteIdentifier<?, ?, ?>> 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<ActorRef> getWatchActor() {
     }
 
     @Override
     public Optional<ActorRef> getWatchActor() {
-        return Optional.of(router);
-    }
-
-    public Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRoutes() {
-        return table.keySet();
+        return Optional.of(rpcInvoker);
     }
 
     }
 
-    public void addRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
-        table.put(routeId, System.currentTimeMillis());
+    public Set<RouteIdentifier<?, ?, ?>> 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<RouteIdentifier<?, ?, ?>> toAdd) {
+        final Set<RouteIdentifier<?, ?, ?>> newRpcs = new HashSet<>(rpcs);
+        newRpcs.addAll(toAdd);
+        return new RoutingTable(rpcInvoker, newRpcs);
     }
 
     }
 
-    public boolean isEmpty() {
-        return table.isEmpty();
+    RoutingTable removeRpcs(final Collection<RouteIdentifier<?, ?, ?>> toRemove) {
+        final Set<RouteIdentifier<?, ?, ?>> 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() {
     }
 
     @Override
     public String toString() {
-        return "RoutingTable{" + "table=" + table + ", router=" + router + '}';
+        return "RoutingTable{" + "rpcs=" + rpcs + ", rpcInvoker=" + rpcInvoker + '}';
     }
 }
     }
 }
index 1545eb00d2cc9af4bda80881d197bad132f0e667..c8415cc818733ff67bcc9828eaf96eceac916f7c 100644 (file)
@@ -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.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;
 
 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<RoutingTable> {
 
     private void receiveAddRoutes(final AddOrUpdateRoutes msg) {
         LOG.debug("AddOrUpdateRoutes: {}", msg.getRouteIdentifiers());
 
     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<RoutingTable> {
      * @param msg contains list of route ids to remove
      */
     private void receiveRemoveRoutes(final RemoveRoutes msg) {
      * @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
     }
 
     @Override
@@ -120,7 +109,7 @@ public class RpcRegistry extends BucketStore<RoutingTable> {
             }
 
             endpoints.put(e.getKey(), rpcs.isEmpty() ? Optional.empty()
             }
 
             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()) {
         }
 
         if (!endpoints.isEmpty()) {
@@ -151,15 +140,15 @@ public class RpcRegistry extends BucketStore<RoutingTable> {
      */
     public static class Messages {
         abstract static class AbstractRouteMessage {
      */
     public static class Messages {
         abstract static class AbstractRouteMessage {
-            final List<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdentifiers;
+            final List<RouteIdentifier<?, ?, ?>> routeIdentifiers;
 
 
-            AbstractRouteMessage(final List<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdentifiers) {
+            AbstractRouteMessage(final List<RouteIdentifier<?, ?, ?>> routeIdentifiers) {
                 Preconditions.checkArgument(routeIdentifiers != null && !routeIdentifiers.isEmpty(),
                         "Route Identifiers must be supplied");
                 this.routeIdentifiers = routeIdentifiers;
             }
 
                 Preconditions.checkArgument(routeIdentifiers != null && !routeIdentifiers.isEmpty(),
                         "Route Identifiers must be supplied");
                 this.routeIdentifiers = routeIdentifiers;
             }
 
-            List<RpcRouter.RouteIdentifier<?, ?, ?>> getRouteIdentifiers() {
+            List<RouteIdentifier<?, ?, ?>> getRouteIdentifiers() {
                 return this.routeIdentifiers;
             }
 
                 return this.routeIdentifiers;
             }
 
@@ -170,13 +159,13 @@ public class RpcRegistry extends BucketStore<RoutingTable> {
         }
 
         public static final class AddOrUpdateRoutes extends AbstractRouteMessage {
         }
 
         public static final class AddOrUpdateRoutes extends AbstractRouteMessage {
-            public AddOrUpdateRoutes(final List<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdentifiers) {
+            public AddOrUpdateRoutes(final List<RouteIdentifier<?, ?, ?>> routeIdentifiers) {
                 super(routeIdentifiers);
             }
         }
 
         public static final class RemoveRoutes extends AbstractRouteMessage {
                 super(routeIdentifiers);
             }
         }
 
         public static final class RemoveRoutes extends AbstractRouteMessage {
-            public RemoveRoutes(final List<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdentifiers) {
+            public RemoveRoutes(final List<RouteIdentifier<?, ?, ?>> routeIdentifiers) {
                 super(routeIdentifiers);
             }
         }
                 super(routeIdentifiers);
             }
         }
index 54e947123372259c036f3868d9cd625325452bd8..0dea3d1da4139614b0d1a47ed82685af84e248da 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.controller.remote.rpc.registry.gossip;
 
 import akka.actor.ActorRef;
 import java.util.Optional;
 
 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}.
 
 /**
  * Marker interface for data which is able to be held in a {@link Bucket}.
@@ -17,7 +18,7 @@ import java.util.Optional;
  *
  * @param <T> Concrete BucketData type
  */
  *
  * @param <T> Concrete BucketData type
  */
-public interface BucketData<T extends BucketData<T>> {
+public interface BucketData<T extends BucketData<T>> 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.
     /**
      * 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.