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 7f22556  SLING-11343: add metrics related to vanity path lookup and 
bloom filter behavior (#70)
7f22556 is described below

commit 7f2255633f02c56e2a43f8574bbbab1d4649c84a
Author: Julian Reschke <[email protected]>
AuthorDate: Thu Jul 7 14:38:06 2022 +0200

    SLING-11343: add metrics related to vanity path lookup and bloom filter 
behavior (#70)
    
    * 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-11343: add metrics related to vanity path lookup and bloom filter 
behavior
    
    * SLING-11343: fix javadoc for existing vanity path metrics
    
    * SLING-11343: do not overflow metrics counters
    
    * SLING-11343: remove unneeded sync
---
 .../impl/ResourceResolverMetrics.java              | 61 +++++++++++++++++++---
 .../resourceresolver/impl/mapping/MapEntries.java  | 45 ++++++++++++++--
 2 files changed, 95 insertions(+), 11 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverMetrics.java
 
b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverMetrics.java
index 1476987..8f0bbdf 100644
--- 
a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverMetrics.java
+++ 
b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverMetrics.java
@@ -34,10 +34,13 @@ import org.osgi.service.component.annotations.Reference;
 
 /**
  *  Export metrics for the resource resolver bundle:
- *  
- *  org.apache.sling.resourceresolver.numberOfVanityPaths - the total number 
of vanity paths
- *  org.apache.sling.resourceresolver.numberOfAliases  -- the total number of 
aliases
- *  org.apache.sling.resourceresolver.unclosedResourceResolvers  -- the total 
number of unclosed resource resolvers
+ *
+ *  org.apache.sling.resourceresolver.numberOfVanityPaths -- the total number 
of vanity paths in the cache
+ *  org.apache.sling.resourceresolver.numberOfVanityPathLookups -- the total 
number of vanity path lookups
+ *  org.apache.sling.resourceresolver.numberOfVanityPathBloomNegatives -- the 
total number of vanity path lookups filtered by the bloom filter
+ *  org.apache.sling.resourceresolver.numberOfVanityPathBloomFalsePositives -- 
the total number of vanity path lookup that passed the bloom filter but were 
false positives
+ *  org.apache.sling.resourceresolver.numberOfAliases -- the total number of 
aliases
+ *  org.apache.sling.resourceresolver.unclosedResourceResolvers -- the total 
number of unclosed resource resolvers
  *
  */
 
@@ -55,7 +58,19 @@ public class ResourceResolverMetrics {
     // number of vanity paths
     private ServiceRegistration<Gauge<Long>> numberOfVanityPathsGauge;
     private Supplier<Long> numberOfVanityPathsSupplier = ZERO_SUPPLIER;
-    
+
+    // total number of vanity path lookups
+    private ServiceRegistration<Gauge<Long>> numberOfVanityPathLookupsGauge;
+    private Supplier<Long> numberOfVanityPathLookupsSupplier = ZERO_SUPPLIER;
+
+    // number of vanity path lookups filtered by Bloom filter
+    private ServiceRegistration<Gauge<Long>> 
numberOfVanityPathBloomNegativeGauge;
+    private Supplier<Long> numberOfVanityPathBloomNegativeSupplier = 
ZERO_SUPPLIER;
+
+    // number of vanity path lookups passing the Bloom filter but being false 
positives
+    private ServiceRegistration<Gauge<Long>> 
numberOfVanityPathBloomFalsePositiveGauge;
+    private Supplier<Long> numberOfVanityPathBloomFalsePositiveSupplier = 
ZERO_SUPPLIER;
+
     // number of aliases
     private ServiceRegistration<Gauge<Long>> numberOfAliasesGauge;
     private Supplier<Long> numberOfAliasesSupplier = ZERO_SUPPLIER;
@@ -66,16 +81,22 @@ public class ResourceResolverMetrics {
     @Activate
     protected void activate(BundleContext bundleContext) {
         numberOfVanityPathsGauge = registerGauge(bundleContext, METRICS_PREFIX 
+ ".numberOfVanityPaths", () -> numberOfVanityPathsSupplier );
+        numberOfVanityPathLookupsGauge = registerGauge(bundleContext, 
METRICS_PREFIX + ".numberOfVanityPathLookups", () -> 
numberOfVanityPathLookupsSupplier );
+        numberOfVanityPathBloomNegativeGauge = registerGauge(bundleContext, 
METRICS_PREFIX + ".numberOfVanityPathBloomNegatives", () -> 
numberOfVanityPathBloomNegativeSupplier );
+        numberOfVanityPathBloomFalsePositiveGauge = 
registerGauge(bundleContext, METRICS_PREFIX + 
".numberOfVanityPathBloomFalsePositives", () -> 
numberOfVanityPathBloomFalsePositiveSupplier );
         numberOfAliasesGauge = registerGauge(bundleContext, METRICS_PREFIX + 
".numberOfAliases", () -> numberOfAliasesSupplier );
         unclosedResourceResolvers = metricsService.counter(METRICS_PREFIX  + 
".unclosedResourceResolvers");
     }
-    
+
     @Deactivate
     protected void deactivate() {
         numberOfVanityPathsGauge.unregister();
+        numberOfVanityPathLookupsGauge.unregister();
+        numberOfVanityPathBloomNegativeGauge.unregister();
+        numberOfVanityPathBloomFalsePositiveGauge.unregister();
         numberOfAliasesGauge.unregister();
     }
-    
+
     /**
      * Set the number of vanity paths in the system
      * @param supplier a supplier returning the number of vanity paths
@@ -83,7 +104,31 @@ public class ResourceResolverMetrics {
     public void setNumberOfVanityPathsSupplier(Supplier<Long> supplier) {
         numberOfVanityPathsSupplier = supplier;
     }
-    
+
+    /**
+     * Set the number of vanity path lookups in the system
+     * @param supplier a supplier returning the number of vanity path lookups
+     */
+    public void setNumberOfVanityPathLookupsSupplier(Supplier<Long> supplier) {
+        numberOfVanityPathLookupsSupplier = supplier;
+    }
+
+    /**
+     * Set the number of vanity path lookups in the system that were filtered
+     * @param supplier a supplier returning the number of vanity path lookups
+     */
+    public void setNumberOfVanityPathBloomNegativeSupplier(Supplier<Long> 
supplier) {
+        numberOfVanityPathBloomNegativeSupplier = supplier;
+    }
+
+    /**
+     * Set the number of vanity path lookups in the system that were not 
catched by the Bloom filter
+     * @param supplier a supplier returning the number of vanity path lookups
+     */
+    public void setNumberOfVanityPathBloomFalsePositiveSupplier(Supplier<Long> 
supplier) {
+        numberOfVanityPathBloomFalsePositiveSupplier = supplier;
+    }
+
     /**
      * Set the number of aliases in the system
      * @param supplier a supplier returning the number of aliases
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 7c47a93..748b03e 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
@@ -144,6 +144,9 @@ public class MapEntries implements
     private final ReentrantLock initializing = new ReentrantLock();
 
     private final AtomicLong vanityCounter;
+    private final AtomicLong vanityPathLookups;
+    private final AtomicLong vanityPathBloomNegative;
+    private final AtomicLong vanityPathBloomFalsePositive;
 
     private byte[] vanityBloomFilter;
 
@@ -175,11 +178,17 @@ public class MapEntries implements
         this.registration = registerResourceChangeListener(bundleContext);
 
         this.vanityCounter = new AtomicLong(0);
+        this.vanityPathLookups = new AtomicLong(0);
+        this.vanityPathBloomNegative = new AtomicLong(0);
+        this.vanityPathBloomFalsePositive = new AtomicLong(0);
         initializeVanityPaths();
 
         this.metrics = metrics;
         if (metrics.isPresent()) {
             
this.metrics.get().setNumberOfVanityPathsSupplier(vanityCounter::get);
+            
this.metrics.get().setNumberOfVanityPathLookupsSupplier(vanityPathLookups::get);
+            
this.metrics.get().setNumberOfVanityPathBloomNegativeSupplier(vanityPathBloomNegative::get);
+            
this.metrics.get().setNumberOfVanityPathBloomFalsePositiveSupplier(vanityPathBloomFalsePositive::get);
             this.metrics.get().setNumberOfAliasesSupplier(() -> (long) 
aliasMap.size());
         }
     }
@@ -708,10 +717,36 @@ public class MapEntries implements
     private List<MapEntry> getMapEntryList(String vanityPath) {
         List<MapEntry> mapEntries = null;
 
-        if (!vanityPathsProcessed.get() || 
BloomFilterUtils.probablyContains(vanityBloomFilter, vanityPath)) {
+        boolean initFinished = vanityPathsProcessed.get();
+        boolean probablyPresent = false;
+
+        if (initFinished) {
+            // total number of lookups after init (and when cache not complete)
+            long current = this.vanityPathLookups.incrementAndGet();
+            if (current >= Long.MAX_VALUE - 100000) {
+                // reset counters when we get close the limit
+                this.vanityPathLookups.set(1);
+                this.vanityPathBloomNegative.set(0);
+                this.vanityPathBloomFalsePositive.set(0);
+                log.info("Vanity Path metrics reset to 0");
+            }
+
+            // init is done - check the bloom filter
+            probablyPresent = 
BloomFilterUtils.probablyContains(vanityBloomFilter, vanityPath);
+            log.trace("bloom filter lookup for {} -> {}", vanityPath, 
probablyPresent);
+
+            if (!probablyPresent) {
+                // filtered by Bloom filter
+                this.vanityPathBloomNegative.incrementAndGet();
+            }
+        }
+
+        if (!initFinished || probablyPresent) {
+
             mapEntries = this.resolveMapsMap.get(vanityPath);
+
             if (mapEntries == null) {
-                if (!vanityPathsProcessed.get() && temporaryResolveMapsMap != 
null) {
+                if (!initFinished && temporaryResolveMapsMap != null) {
                     mapEntries = temporaryResolveMapsMap.get(vanityPath);
                     if (mapEntries != null) {
                         temporaryResolveMapsMapHits.incrementAndGet();
@@ -723,12 +758,16 @@ public class MapEntries implements
                 if (mapEntries == null) {
                     Map<String, List<MapEntry>> mapEntry = 
getVanityPaths(vanityPath);
                     mapEntries = mapEntry.get(vanityPath);
-                    if (!vanityPathsProcessed.get() && temporaryResolveMapsMap 
!= null) {
+                    if (!initFinished && temporaryResolveMapsMap != null) {
                         log.trace("getMapEntryList: caching map entries for {} 
-> {}", vanityPath, mapEntries);
                         temporaryResolveMapsMap.put(vanityPath, mapEntries == 
null ? NO_MAP_ENTRIES : mapEntries);
                     }
                 }
             }
+            if (mapEntries == null && probablyPresent) {
+                // Bloom filter had a false positive
+                this.vanityPathBloomFalsePositive.incrementAndGet();
+            }
         }
 
         return mapEntries == NO_MAP_ENTRIES ? null : mapEntries;

Reply via email to