Update QNameFactory caching 75/82275/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 28 May 2019 10:42:02 +0000 (12:42 +0200)
committerTom Pantelis <tompantelis@gmail.com>
Wed, 29 May 2019 16:31:53 +0000 (16:31 +0000)
This eliminates the use of soft references in favor of weak ones,
so that we do not rely on GC heuristics for retention.

We also make the maximum cache size runtime-configurable via
org.opendaylight.controller.cluster.datastore.node.utils.qname-cache.max-size
property.

Finally we use QName.intern() to defer to the global QName cache,
resulting in better QName instance sharing.

JIRA: CONTROLLER-1897
Change-Id: Ice3596d23b96c4ecfc5775cb21968233b042a984
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactory.java

index 5c7f6d1a5650333ca1cee7124ff0edd47a8c0378..1b9a21f85e1b1ba24014a51ed09c4e21492f36d2 100644 (file)
@@ -5,7 +5,6 @@
  * 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.controller.cluster.datastore.node.utils;
 
 import com.google.common.cache.CacheBuilder;
@@ -14,21 +13,22 @@ import com.google.common.cache.LoadingCache;
 import org.opendaylight.yangtools.yang.common.QName;
 
 public final class QNameFactory {
-
-    private static final int MAX_QNAME_CACHE_SIZE = 10000;
-
-    private QNameFactory() {
-    }
+    private static final int MAX_QNAME_CACHE_SIZE = Integer.getInteger(
+        "org.opendaylight.controller.cluster.datastore.node.utils.qname-cache.max-size", 10000);
 
     private static final LoadingCache<String, QName> CACHE = CacheBuilder.newBuilder().maximumSize(MAX_QNAME_CACHE_SIZE)
-            .softValues().build(new CacheLoader<String, QName>() {
+            .weakValues().build(new CacheLoader<String, QName>() {
                 @Override
-                public QName load(String key) {
-                    return QName.create(key);
+                public QName load(final String key) {
+                    return QName.create(key).intern();
                 }
             });
 
-    public static QName create(String name) {
+    private QNameFactory() {
+
+    }
+
+    public static QName create(final String name) {
         return CACHE.getUnchecked(name);
     }
 }