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

fortino 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 f4c557c1d3 OAK-10782: query getSize() can lock writes when is called 
before an update (#1436)
f4c557c1d3 is described below

commit f4c557c1d3c20ccee1211033621a7da02c5a768e
Author: Fabrizio Fortino <[email protected]>
AuthorDate: Thu May 2 16:16:39 2024 +0200

    OAK-10782: query getSize() can lock writes when is called before an update 
(#1436)
---
 .../plugins/index/elastic/query/ElasticIndex.java  |  9 ++++--
 .../oak/plugins/index/PropertyIndexCommonTest.java | 37 ++++++++++++++++++++++
 2 files changed, 43 insertions(+), 3 deletions(-)

diff --git 
a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticIndex.java
 
b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticIndex.java
index 6ed59d20a2..ace07625f1 100644
--- 
a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticIndex.java
+++ 
b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticIndex.java
@@ -18,7 +18,6 @@ package org.apache.jackrabbit.oak.plugins.index.elastic.query;
 
 import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.plugins.index.elastic.ElasticIndexNode;
-import org.apache.jackrabbit.oak.plugins.index.elastic.ElasticIndexStatistics;
 import org.apache.jackrabbit.oak.plugins.index.elastic.ElasticIndexTracker;
 import 
org.apache.jackrabbit.oak.plugins.index.elastic.query.async.ElasticResultRowAsyncIterator;
 import org.apache.jackrabbit.oak.plugins.index.search.IndexNode;
@@ -63,8 +62,12 @@ class ElasticIndex extends FulltextIndex {
     @Override
     protected SizeEstimator getSizeEstimator(IndexPlan plan) {
         return () -> {
-            ElasticIndexStatistics indexStatistics = 
acquireIndexNode(plan).getIndexStatistics();
-            return indexStatistics.getDocCountFor(new 
ElasticRequestHandler(plan, getPlanResult(plan), null).baseQuery());
+            ElasticIndexNode indexNode = acquireIndexNode(plan);
+            try {
+                return indexNode.getIndexStatistics().getDocCountFor(new 
ElasticRequestHandler(plan, getPlanResult(plan), null).baseQuery());
+            } finally {
+                indexNode.release();
+            }
         };
     }
 
diff --git 
a/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/PropertyIndexCommonTest.java
 
b/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/PropertyIndexCommonTest.java
index ca7493fe4e..099c2cbee5 100644
--- 
a/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/PropertyIndexCommonTest.java
+++ 
b/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/PropertyIndexCommonTest.java
@@ -19,6 +19,7 @@ package org.apache.jackrabbit.oak.plugins.index;
 import org.apache.jackrabbit.guava.common.collect.ImmutableList;
 import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.Result;
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
@@ -38,6 +39,7 @@ import static java.util.Arrays.asList;
 import static java.util.Collections.emptyList;
 import static java.util.Collections.singletonList;
 import static javax.jcr.PropertyType.TYPENAME_DATE;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
 import static 
org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROPDEF_PROP_NODE_NAME;
 import static 
org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_NAME;
 import static 
org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_NODE;
@@ -45,6 +47,7 @@ import static 
org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConsta
 import static 
org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_NULL_CHECK_ENABLED;
 import static 
org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_PROPERTY_INDEX;
 import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 
 public abstract class PropertyIndexCommonTest extends AbstractQueryTest {
@@ -173,6 +176,40 @@ public abstract class PropertyIndexCommonTest extends 
AbstractQueryTest {
                 containsString("/oak:index/test1")));
     }
 
+    @Test
+    public void sizeQuery() throws Exception {
+        indexOptions.setIndex(root, "test1", 
indexOptions.createIndex(indexOptions.createIndexDefinitionBuilder(), false, 
"propa"));
+        root.commit();
+
+        Tree test = root.getTree("/").addChild("test");
+        test.addChild("a").setProperty("propa", "foo");
+        test.addChild("b").setProperty("propa", "bar");
+        root.commit();
+
+        assertEventually(() -> {
+            try {
+                Result result = executeQuery("select [jcr:path] from [nt:base] 
where [propa] = 'foo'", SQL2, NO_BINDINGS);
+                assertThat(result.getSize(Result.SizePrecision.APPROXIMATION, 
0), is(1L));
+            } catch (ParseException e) {
+                throw new RuntimeException(e);
+            }
+        });
+
+        // this verifies OAK-10782 is fixed
+        test.addChild("c").setProperty("propa", "foo");
+        test.addChild("d").setProperty("propb", "bar");
+        root.commit();
+
+        assertEventually(() -> {
+            try {
+                Result result = executeQuery("select [jcr:path] from [nt:base] 
where [propa] = 'foo'", SQL2, NO_BINDINGS);
+                assertThat(result.getSize(Result.SizePrecision.APPROXIMATION, 
0), is(2L));
+            } catch (ParseException e) {
+                throw new RuntimeException(e);
+            }
+        });
+    }
+
     @Test
     public void propertyExistenceQuery() throws Exception {
         indexOptions.setIndex(root, "test1", 
indexOptions.createIndex(indexOptions.createIndexDefinitionBuilder(),

Reply via email to