You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2022/05/23 12:07:08 UTC

[sling-org-apache-sling-resourceresolver] branch master updated: SLING-11329: resource resolver: optionally run initial vanity path query asynchronously (#67)

This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git


The following commit(s) were added to refs/heads/master by this push:
     new 30a9396  SLING-11329: resource resolver: optionally run initial vanity path query asynchronously (#67)
30a9396 is described below

commit 30a9396395baa50355d1d7e62ef36e5d5b25c882
Author: Julian Reschke <ju...@gmx.de>
AuthorDate: Mon May 23 14:07:03 2022 +0200

    SLING-11329: resource resolver: optionally run initial vanity path query asynchronously (#67)
    
    * wip: run query in BG (causes test failures due to bad test assumptions)
    
    * Revert "wip: run query in BG (causes test failures due to bad test assumptions)"
    
    This reverts commit e90984c2b08279c330a0af84eb1fd194e85af608.
    
    * SLING-11330: resource resolver: refactor event listening - structure onChange method
    
    * Revert "SLING-11330: resource resolver: refactor event listening - structure onChange method"
    
    This reverts commit 12b77dc7554a7d3b8b676f661d7b3800a5d16c94.
    
    * SLING-11329: resource resolver: optionally run initial vanity path query asynchronously
---
 .../impl/CommonResourceResolverFactoryImpl.java    |  7 +++-
 .../impl/ResourceResolverFactoryActivator.java     |  4 ++
 .../impl/ResourceResolverFactoryConfig.java        |  4 ++
 .../impl/mapping/MapConfigurationProvider.java     |  2 +
 .../resourceresolver/impl/mapping/MapEntries.java  | 45 ++++++++++++----------
 .../impl/MockedResourceResolverImplTest.java       |  5 +++
 6 files changed, 46 insertions(+), 21 deletions(-)

diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/CommonResourceResolverFactoryImpl.java b/src/main/java/org/apache/sling/resourceresolver/impl/CommonResourceResolverFactoryImpl.java
index 4c6cfce..c677104 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/CommonResourceResolverFactoryImpl.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/CommonResourceResolverFactoryImpl.java
@@ -391,7 +391,12 @@ public class CommonResourceResolverFactoryImpl implements ResourceResolverFactor
         return this.activator.getDefaultVanityPathRedirectStatus();
     }
 
-    /**
+    @Override
+    public boolean isVanityPathCacheInitInBackground() {
+        return this.activator.isVanityPathCacheInitInBackground();
+    }
+
+   /**
      * get's the ServiceTracker of the ResourceAccessSecurity service
      */
     public ResourceAccessSecurityTracker getResourceAccessSecurityTracker () {
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java
index 8dc71b4..021c15a 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java
@@ -208,6 +208,10 @@ public class ResourceResolverFactoryActivator {
         return this.config.resource_resolver_enable_vanitypath();
     }
 
+    public boolean isVanityPathCacheInitInBackground() {
+        return this.config.resource_resolver_vanitypath_cache_in_background();
+    }
+
     public boolean isOptimizeAliasResolutionEnabled() {
         return this.config.resource_resolver_optimize_alias_resolution();
     }
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryConfig.java b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryConfig.java
index 827eccf..2cb864b 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryConfig.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryConfig.java
@@ -128,6 +128,10 @@ public @interface ResourceResolverFactoryConfig {
                             "are processed and added to the mappoing table.")
     boolean resource_resolver_enable_vanitypath() default true;
 
+    @AttributeDefinition(name = "Vanity Path Cache Init In Background",
+        description = "This flag controls whether the vanity path cache will be initialized immediately or later as a background task.")
+    boolean resource_resolver_vanitypath_cache_in_background() default false;
+
     @AttributeDefinition(name = "Maximum number of cached vanity path entries",
         description = "The maximum number of cached vanity path entries. " +
                     "Default is -1 (no limit)")
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java
index c8733a9..d549cf6 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java
@@ -49,6 +49,8 @@ public interface MapConfigurationProvider extends ResourceResolverFactory {
 
     long getMaxCachedVanityPathEntries();
 
+    boolean isVanityPathCacheInitInBackground();
+
     boolean isMaxCachedVanityPathEntriesStartup();
 
     int getVanityBloomFilterMaxBytes();
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
index 042eea9..e104fd2 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
@@ -255,14 +255,20 @@ public class MapEntries implements
             if (this.factory.isVanityPathEnabled()) {
                 this.vanityBloomFilter = createVanityBloomFilter();
                 VanityPathInitializer vpi = new VanityPathInitializer(this.factory);
-                vpi.load();
+                if (this.factory.isVanityPathCacheInitInBackground()) {
+                    this.log.debug("bg init starting");
+                    Thread vpinit = new Thread(vpi, "VanityPathInitializer");
+                    vpinit.start();
+                } else {
+                    vpi.load();
+                }
             }
         } finally {
             this.initializing.unlock();
         }
     }
 
-    private class VanityPathInitializer {
+    private class VanityPathInitializer implements Runnable {
 
         private MapConfigurationProvider factory;
 
@@ -270,6 +276,11 @@ public class MapEntries implements
             this.factory = factory;
         }
 
+        @Override
+        public void run() {
+            execute();
+        }
+
         public void load() {
             execute();
         }
@@ -505,16 +516,8 @@ public class MapEntries implements
     private boolean doAddVanity(final Resource resource) {
         log.debug("doAddVanity getting {}", resource.getPath());
 
-        boolean needsUpdate = false;
-        if (isAllVanityPathEntriesCached() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries()) {
-            // fill up the cache and the bloom filter
-            needsUpdate = loadVanityPath(resource, resolveMapsMap, vanityTargets, true);
-        } else {
-            // fill up the bloom filter
-            needsUpdate = loadVanityPath(resource, resolveMapsMap, vanityTargets, false);
-        }
-
-        return needsUpdate;
+        boolean updateTheCache = isAllVanityPathEntriesCached() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries();
+        return loadVanityPath(resource, resolveMapsMap, vanityTargets, updateTheCache);
     }
 
     private boolean doRemoveVanity(final String path) {
@@ -690,15 +693,15 @@ public class MapEntries implements
     }
 
     /**
-     * get the MapEnty containing all the nodes having a specific vanityPath
+     * get the MapEntry list containing all the nodes having a specific vanityPath
      */
-    private List<MapEntry> getMapEntryList(String vanityPath){
+    private List<MapEntry> getMapEntryList(String vanityPath) {
         List<MapEntry> mapEntries = null;
 
-        if (BloomFilterUtils.probablyContains(vanityBloomFilter, vanityPath)) {
+        if (!vanityPathsProcessed.get() || BloomFilterUtils.probablyContains(vanityBloomFilter, vanityPath)) {
             mapEntries = this.resolveMapsMap.get(vanityPath);
             if (mapEntries == null) {
-                Map<String, List<MapEntry>>  mapEntry = getVanityPaths(vanityPath);
+                Map<String, List<MapEntry>> mapEntry = getVanityPaths(vanityPath);
                 mapEntries = mapEntry.get(vanityPath);
             }
         }
@@ -892,7 +895,7 @@ public class MapEntries implements
                 }
                 if ( isValid ) {
                     totalValid += 1;
-                    if (this.factory.isMaxCachedVanityPathEntriesStartup() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries()) {
+                    if (this.vanityPathsProcessed.get() && (this.factory.isMaxCachedVanityPathEntriesStartup() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries())) {
                         loadVanityPath(resource, resolveMapsMap, vanityTargets, true);
                         entryMap = resolveMapsMap;
                     } else {
@@ -1226,6 +1229,7 @@ public class MapEntries implements
         long processStart = System.nanoTime();
         Supplier<Boolean> isCacheComplete = () -> isAllVanityPathEntriesCached()
                 || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries();
+
         while (i.hasNext() && isCacheComplete.get()) {
             count += 1;
             final Resource resource = i.next();
@@ -1492,6 +1496,7 @@ public class MapEntries implements
         }
 
     }
+
     private final class MapEntryIterator implements Iterator<MapEntry> {
 
         private final Map<String, List<MapEntry>> resolveMapsMap;
@@ -1562,11 +1567,11 @@ public class MapEntries implements
                     }
 
                     final List<MapEntry> special;
-                    if (MapEntries.this.isAllVanityPathEntriesCached()) {
+                    if (MapEntries.this.isAllVanityPathEntriesCached() && MapEntries.this.vanityPathsProcessed.get()) {
                         special = this.resolveMapsMap.get(key);
                     } else {
-                        special = MapEntries.this.getMapEntryList(key)
-;                    }
+                        special = MapEntries.this.getMapEntryList(key); 
+                    }
                     if (special != null) {
                         specialIterator = special.iterator();
                     }
diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java b/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java
index 1205b91..7b21023 100644
--- a/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java
+++ b/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java
@@ -299,6 +299,11 @@ public class MockedResourceResolverImplTest {
             public boolean resource_resolver_log_unclosed() {
                 return true;
             }
+
+            @Override
+            public boolean resource_resolver_vanitypath_cache_in_background() {
+                return false;
+            }
         });
 
         // configure using Bundle