You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by th...@apache.org on 2026/09/23 09:05:43 UTC

(jackrabbit-oak) branch trunk updated: OAK-12401: cap the ISCHILDNODE cost estimate at 1000 (#3131) (#3138)

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

thomasmueller pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 25d9463e79 OAK-12401: cap the ISCHILDNODE cost estimate at 1000 (#3131) (#3138)
25d9463e79 is described below

commit 25d9463e79ccea0f21ce6533506bb306ddfd0d73
Author: Thomas Mueller <th...@apache.org>
AuthorDate: Wed Sep 23 11:05:38 2026 +0200

    OAK-12401: cap the ISCHILDNODE cost estimate at 1000 (#3131) (#3138)
    
    Co-authored-by: Theia Vlad <39...@users.noreply.github.com>
---
 .../index/lucene/LuceneIndexProviderService.java   |  3 ++
 .../index/elastic/ElasticIndexProviderService.java |  3 ++
 .../search/spi/query/FulltextIndexPlanner.java     | 22 +++++++++--
 .../oak/plugins/index/IndexPlannerCommonTest.java  | 45 ++++++++++++++++++++++
 4 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java b/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java
index 09061c8b68..c768ca8449 100644
--- a/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java
+++ b/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java
@@ -386,6 +386,9 @@ public class LuceneIndexProviderService {
         oakRegs.add(whiteboard.register(FeatureToggle.class,
                 new FeatureToggle(FulltextIndexPlanner.FT_OAK_12221, FulltextIndexPlanner.FT_OAK_12221_ENABLE),
                 emptyMap()));
+        oakRegs.add(whiteboard.register(FeatureToggle.class,
+                new FeatureToggle(FulltextIndexPlanner.FT_OAK_12401, FulltextIndexPlanner.FT_OAK_12401_DISABLE),
+                emptyMap()));
         oakRegs.add(whiteboard.register(FeatureToggle.class,
                 new FeatureToggle(FulltextIndexEditor.FT_OAK_12193, FulltextIndexEditor.FT_OAK_12193_DISABLE),
                 emptyMap()));
diff --git a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java
index 90684f321c..7d82ad4ae5 100644
--- a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java
+++ b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java
@@ -225,6 +225,9 @@ public class ElasticIndexProviderService {
         oakRegs.add(whiteboard.register(FeatureToggle.class,
                 new FeatureToggle(FulltextIndexPlanner.FT_OAK_12221, FulltextIndexPlanner.FT_OAK_12221_ENABLE),
                 emptyMap()));
+        oakRegs.add(whiteboard.register(FeatureToggle.class,
+                new FeatureToggle(FulltextIndexPlanner.FT_OAK_12401, FulltextIndexPlanner.FT_OAK_12401_DISABLE),
+                emptyMap()));
         oakRegs.add(whiteboard.register(FeatureToggle.class,
                 new FeatureToggle(ElasticIndexEditorProvider.FT_OAK_12206, ElasticIndexEditorProvider.FT_OAK_12206_DISABLE),
                 emptyMap()));
diff --git a/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndexPlanner.java b/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndexPlanner.java
index b81422df43..8f089566fa 100644
--- a/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndexPlanner.java
+++ b/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndexPlanner.java
@@ -72,6 +72,11 @@ public class FulltextIndexPlanner {
 
     public static final int DEFAULT_PROPERTY_WEIGHT = Integer.getInteger("oak.fulltext.defaultPropertyWeight", 5);
 
+    /**
+     * Upper bound for the direct-children (ISCHILDNODE) cost estimate (OAK-12401).
+     */
+    static final int DIRECT_CHILDREN_ESTIMATE_CAP = 1000;
+
     /**
      * Default weight used for null/not-null cost estimation when no explicit weight is configured.
      * Corresponds to the heuristic that such conditions match ~20% of indexed entries.
@@ -107,6 +112,16 @@ public class FulltextIndexPlanner {
      */
     public static final AtomicBoolean FT_OAK_12221_ENABLE = new AtomicBoolean(false);
 
+    /**
+     * Feature toggle for OAK-12401: caps the direct-children (ISCHILDNODE) cost estimate at 1000.
+     */
+    public static final String FT_OAK_12401 = "FT_OAK-12401";
+
+    /**
+     * Kill switch for the OAK-12401 cap; {@code true} reverts to the uncapped {@code minNumDocs / 2}.
+     */
+    public static final AtomicBoolean FT_OAK_12401_DISABLE = new AtomicBoolean(false);
+
     /**
      * IndexPlan Attribute name which refers to the name of the fields that should be used for facets.
      */
@@ -960,8 +975,8 @@ public class FulltextIndexPlanner {
                 // then the result size is at most 1.
                 minNumDocs = 1;
             } else if (pathRestriction == PathRestriction.DIRECT_CHILDREN) {
-                // We restrict to direct children: assume at most 50% are there.
-                minNumDocs /= 2;
+                // We restrict to direct children: assume at most 50%, but at most 1000 (OAK-12401).
+                minNumDocs = FT_OAK_12401_DISABLE.get() ? minNumDocs / 2 : Math.min(minNumDocs / 2, DIRECT_CHILDREN_ESTIMATE_CAP);
             } else if (pathRestriction != PathRestriction.NO_RESTRICTION) {
                 // Other restriction: assume at most 90%.
                 // This is important if we have
@@ -1079,7 +1094,8 @@ public class FulltextIndexPlanner {
             if (pathRestriction == PathRestriction.EXACT || pathRestriction == PathRestriction.PARENT) {
                 minNumDocs = 1;
             } else if (pathRestriction == PathRestriction.DIRECT_CHILDREN) {
-                minNumDocs /= 2;
+                // direct children: at most 50%, but at most 1000 (OAK-12401)
+                minNumDocs = FT_OAK_12401_DISABLE.get() ? minNumDocs / 2 : Math.min(minNumDocs / 2, DIRECT_CHILDREN_ESTIMATE_CAP);
             } else if (pathRestriction != PathRestriction.NO_RESTRICTION) {
                 minNumDocs = (int) (minNumDocs * 0.9);
             }
diff --git a/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexPlannerCommonTest.java b/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexPlannerCommonTest.java
index a767ee9687..4aa3ee2dfe 100644
--- a/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexPlannerCommonTest.java
+++ b/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexPlannerCommonTest.java
@@ -224,6 +224,51 @@ public abstract class IndexPlannerCommonTest {
         assertNotNull(planner.getPlan());
     }
 
+    @Test
+    public void directChildrenCostCappedAtThousand() throws Exception {
+        // node-scope property so a path-only restriction yields a plan; numDocs/2 exceeds the cap
+        getPropertyIndexDefinitionNodeBuilder(builder, indexName, Set.of("foo"), "async");
+        builder = builder.getNodeState().builder();
+        NodeBuilder defn = IndexDefinition.updateDefinition(builder.getChildNode("oak:index").getChildNode(indexName));
+        defn.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        getNode(defn, "indexRules/nt:base/properties/foo").setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        long numOfDocs = 3000;
+        IndexNode node = createIndexNode(getIndexDefinition(root, defn.getNodeState(), "/oak:index/" + indexName), numOfDocs);
+        FilterImpl filter = createFilter("nt:base");
+        filter.restrictPath("/content", Filter.PathRestriction.DIRECT_CHILDREN);
+
+        // capped at 1000 under both estimation paths (legacy and the OAK-12221 selectivity model)
+        assertDirectChildrenEstimate(node, filter, 1000);
+        FulltextIndexPlanner.FT_OAK_12221_ENABLE.set(true);
+        try {
+            assertDirectChildrenEstimate(node, filter, 1000);
+        } finally {
+            FulltextIndexPlanner.FT_OAK_12221_ENABLE.set(false);
+        }
+
+        // kill-switch disabled the cap: estimate is the uncapped numDocs/2, above the cap.
+        // (exact numDocs differs per backend - Elastic also indexes the repo nodes - so assert '>')
+        FulltextIndexPlanner.FT_OAK_12401_DISABLE.set(true);
+        try {
+            TestUtil.assertEventually(() -> {
+                FulltextIndexPlanner planner = getIndexPlanner(node, "/oak:index/" + indexName, filter,
+                        Collections.<QueryIndex.OrderEntry>emptyList());
+                assertTrue(planner.getPlan().getEstimatedEntryCount() > 1000);
+            }, 5000 * 5);
+        } finally {
+            FulltextIndexPlanner.FT_OAK_12401_DISABLE.set(false);
+        }
+    }
+
+    private void assertDirectChildrenEstimate(IndexNode node, FilterImpl filter, long expected) {
+        TestUtil.assertEventually(() -> {
+            FulltextIndexPlanner planner = getIndexPlanner(node, "/oak:index/" + indexName, filter,
+                    Collections.<QueryIndex.OrderEntry>emptyList());
+            assertEquals(expected, planner.getPlan().getEstimatedEntryCount());
+        }, 5000 * 5);
+    }
+
     @Test
     public void fulltextIndexAndNodeTypeRestriction() throws Exception {
         getPropertyIndexDefinitionNodeBuilder(builder, indexName, Set.of("foo"), "async");