BUG-2383: cache effective attributes in import 92/19792/2
authorRobert Varga <rovarga@cisco.com>
Thu, 7 May 2015 08:26:12 +0000 (10:26 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 7 May 2015 13:51:06 +0000 (13:51 +0000)
This patch introduces a simple identity cache decorator around
AbstractImportPolicy. It is then used by ImportPolicyPeerTracker and
EffectiveRibInWriter to make sure we do not needlessly explode
attribute objects. This would typically happen when we receive multiple
routes within a single message and the import policy modifies the
attributes -- leading to a per-route attribute object.

Change-Id: I8581df3e3a2aae07a16d44f2554026afbad264fd
Signed-off-by: Robert Varga <rovarga@cisco.com>
(cherry picked from commit ff7a5daa1e8a22f46e0423f7e6317bf8ea7ea078)

bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/CachingImportPolicy.java [new file with mode: 0644]
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/ImportPolicyPeerTracker.java

diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/CachingImportPolicy.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/CachingImportPolicy.java
new file mode 100644 (file)
index 0000000..0c63355
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.rib.impl;
+
+import com.google.common.base.Preconditions;
+import java.util.IdentityHashMap;
+import javax.annotation.concurrent.NotThreadSafe;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+
+/**
+ * A caching decorator for {@link AbstractImportPolicy}. Performs caching of effective
+ * attributes using an {@link IdentityHashMap} for fast lookup and reuse of resulting
+ * objects.
+ */
+@NotThreadSafe
+final class CachingImportPolicy extends AbstractImportPolicy {
+    private final IdentityHashMap<ContainerNode, ContainerNode> cache = new IdentityHashMap<>();
+    private final AbstractImportPolicy delegate;
+
+    CachingImportPolicy(final AbstractImportPolicy delegate) {
+        this.delegate = Preconditions.checkNotNull(delegate);
+    }
+
+    @Override
+    ContainerNode effectiveAttributes(final ContainerNode attributes) {
+        ContainerNode ret = cache.get(attributes);
+        if (ret == null) {
+            ret = delegate.effectiveAttributes(attributes);
+            if (ret != null) {
+                cache.put(attributes, ret);
+            }
+        }
+
+        return ret;
+    }
+}
index 99e091e76c9dd63f3d490a0fc46ddd6e38881fc4..86fb935323e7aef1ab712775a7e67d93191a669a 100644 (file)
@@ -44,6 +44,6 @@ final class ImportPolicyPeerTracker extends AbstractPeerRoleTracker {
     }
 
     AbstractImportPolicy policyFor(final PeerId peerId) {
-        return policies.get(peerId);
+        return new CachingImportPolicy(policies.get(peerId));
     }
 }
\ No newline at end of file