Re: [PR] fix(server): make query result batch boundaries explicit [hugegraph]

2026-09-17 Thread via GitHub


imbajin merged PR #3193:
URL: https://github.com/apache/hugegraph/pull/3193


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


-
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]



Re: [PR] fix(server): make query result batch boundaries explicit [hugegraph]

2026-09-16 Thread via GitHub


contrueCT commented on PR #3193:
URL: https://github.com/apache/hugegraph/pull/3193#issuecomment-5708677492

   @imbajin This PR may be ready to merge ?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


-
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]



Re: [PR] fix(server): make query result batch boundaries explicit [hugegraph]

2026-09-09 Thread via GitHub


bitflicker64 commented on code in PR #3193:
URL: https://github.com/apache/hugegraph/pull/3193#discussion_r3972183962


##
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/QueryResults.java:
##
@@ -18,234 +18,362 @@
 package org.apache.hugegraph.backend.query;
 
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
-import java.util.Set;
+import java.util.function.BiPredicate;
 import java.util.function.Function;
 
 import org.apache.hugegraph.HugeException;
 import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.backend.query.QueryBatch.BatchIterator;
 import org.apache.hugegraph.iterator.CIter;
-import org.apache.hugegraph.iterator.FlatMapperIterator;
 import org.apache.hugegraph.iterator.ListIterator;
-import org.apache.hugegraph.iterator.MapperIterator;
-import org.apache.hugegraph.iterator.WrappedIterator;
 import org.apache.hugegraph.perf.PerfUtil.Watched;
 import org.apache.hugegraph.type.Idfiable;
-import org.apache.hugegraph.util.E;
-import org.apache.hugegraph.util.InsertionOrderUtil;
-import org.apache.tinkerpop.gremlin.structure.util.CloseableIterator;
 
+/** A lazy stream of query batches. Only the final consumer flattens the 
stream. */
 public class QueryResults {
 
-private static final Iterator EMPTY_ITERATOR = new EmptyIterator<>();
-
-private static final QueryResults EMPTY = new QueryResults<>(
-emptyIterator(), Query.NONE);
-
-private final Iterator results;
+private final BatchIterator> batches;
 private final List queries;
-private List currentQueries;
-private long queryVersion;
+private final Object metadata;
+private Iterator results;
 
 public QueryResults(Iterator results, Query query) {
-this(results);
-this.addQuery(query);
+this(results, new QueryResultContext(query));
 }
 
-private QueryResults(Iterator results) {
-this.results = results;
-this.queries = InsertionOrderUtil.newList();
-this.currentQueries = Collections.emptyList();
-this.queryVersion = 0L;
+public QueryResults(Iterator results, QueryResultContext context) {
+QueryBatch batch = new QueryBatch<>(results, context);
+this.queries = new 
ArrayList<>(Collections.singletonList(context.queries().get(0)));
+this.batches = this.trackBatches(new BatchIterator>() {
+private boolean fetched;
+
+@Override
+protected QueryBatch fetch() {
+if (this.fetched) {
+return null;
+}
+this.fetched = true;
+return batch;
+}
+
+@Override
+protected void closeResources() throws Exception {
+batch.close();
+}
+});
+this.metadata = batch.results();
 }
 
-public void setQuery(Query query) {
-if (!this.queries.isEmpty()) {
-this.queries.clear();
-}
-this.addQuery(query);
+private QueryResults(Iterator> batches, Object metadata) {
+this.queries = new ArrayList<>();
+this.batches = this.trackBatches(batches);
+this.metadata = metadata;
 }
 
-private void addQuery(Query query) {
-E.checkNotNull(query, "query");
-this.addQueries(Collections.singletonList(query));
+private BatchIterator> trackBatches(Iterator> 
origin) {
+return new BatchIterator>() {
+private QueryBatch active;
+
+@Override
+protected QueryBatch fetch() throws Exception {
+QueryBatch previous = this.active;
+this.active = null;
+QueryBatch.closeAll(previous);
+if (!origin.hasNext()) {
+return null;
+}
+this.active = origin.next();
+queries.clear();
+queries.add(this.active.context().queries().get(0));
+return this.active;
+}
+
+@Override
+protected void closeResources() throws Exception {
+QueryBatch previous = this.active;
+this.active = null;
+queries.clear();
+QueryBatch.closeAll(previous, origin);
+}
+
+@Override
+public Object metadata(String meta, Object... args) {
+return QueryBatch.metadataOf(origin, meta, args);
+}
+};
 }
 
-private void addQueries(List queries) {
-assert !queries.isEmpty();
-for (Query query : queries) {
-E.checkNotNull(query, "query");
-this.queries.add(query);
-}
-this.currentQueries = new ArrayList<>(queries);
-this.queryVersion++;
+public static  

Re: [PR] fix(server): make query result batch boundaries explicit [hugegraph]

2026-09-08 Thread via GitHub


bitflicker64 commented on code in PR #3193:
URL: https://github.com/apache/hugegraph/pull/3193#discussion_r3955851303


##
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java:
##
@@ -2009,45 +1981,13 @@ private boolean rightResultFromIndexQuery(Query query, 
HugeElement elem) {
 return false;
 }
 
-protected static boolean queryNeedsPostFilter(Query query) {
-while (query != null) {
-if (query instanceof ConditionQuery) {
-ConditionQuery cq = (ConditionQuery) query;
-/*
- * Search conditions need post-filtering even before query
- * optimization marks the query with an optimized type.
- */
-boolean edgeIndexWithLabel =
-cq.resultType().isEdge() &&
-cq.optimized() == OptimizedType.INDEX &&
-cq.condition(HugeKeys.LABEL) != null;
-if (cq.hasSearchCondition() ||
-(conditionQueryNeedsPostFilter(cq) &&
- !edgeIndexWithLabel)) {
-return true;
-}
-}
-query = query.originQuery();
-}
-return false;
-}
-
-private static boolean conditionQueryNeedsPostFilter(ConditionQuery query) 
{
-return query.optimized() != OptimizedType.NONE;
-}
-
-private  Iterator filterExpiredResultFromBackend(
-Query query, Iterator results) {
-if (this.store().features().supportsTtl() || query.showExpired()) {
-return results;
-}
-// Filter expired vertices/edges with TTL
-return new FilterIterator<>(results, elem -> {
-if (elem.expired()) {
-DeleteExpiredJob.asyncDeleteExpiredObject(this.graph(), elem);
-return false;
+protected  QueryResults 
filterExpiredBatches(QueryResults batches) {

Review Comment:
   🧹 The TTL stage is now unconditional.
   
   Base `filterExpiredResultFromBackend` returned the source iterator untouched 
when `store().features().supportsTtl()` or `query.showExpired()` held, so a 
TTL-capable store carried no wrapper at all. This version always adds a 
`filter` stage, which is one of the four counted in the `QueryBatch` comment, 
and moves the store-feature test into the per-element predicate.
   
   Requested change: read `storeFeatures().supportsTtl()` once and return 
`batches` unchanged when it is true, leaving `context.showExpired()` and 
`elem.expired()` in the predicate since those do vary.



##
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/QueryBatch.java:
##
@@ -0,0 +1,262 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hugegraph.backend.query;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.function.BiPredicate;
+import java.util.function.Function;
+
+import org.apache.hugegraph.HugeException;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.iterator.CIter;
+import org.apache.hugegraph.iterator.Metadatable;
+import org.apache.hugegraph.type.Idfiable;
+import org.apache.hugegraph.util.InsertionOrderUtil;
+
+/** A single query's results, with exclusive ownership of its iterator chain. 
*/
+public final class QueryBatch implements AutoCloseable {
+
+private final BatchIterator results;
+private final QueryResultContext context;
+
+public QueryBatch(Iterator results, QueryResultContext context) {
+this.context = context;
+this.results = new BatchIterator() {
+@Override
+protected R fetch() {
+return results.hasNext() ? results.next() : null;
+}
+
+@Override
+protected void closeResources() throws Exception {
+closeAll(results);
+}
+
+@Override
+public Object metadata(String meta, Object... args) {
+return 

Re: [PR] fix(server): make query result batch boundaries explicit [hugegraph]

2026-09-07 Thread via GitHub


bitflicker64 commented on code in PR #3193:
URL: https://github.com/apache/hugegraph/pull/3193#discussion_r3949905190


##
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/page/QueryList.java:
##
@@ -344,10 +354,11 @@ public boolean hasNextPage() {
 }
 
 public Query query() {
-List queries = this.results.queries();
-E.checkState(queries.size() == 1,
- "Expect query size 1, but got: %s", queries);
-return queries.get(0);
+return this.query;

Review Comment:
   🧹 `query()` and `get()` have no callers left after this change.
   
   `query()` existed at base, deriving from `this.results.queries()` behind an 
`E.checkState(queries.size() == 1)`. This commit reworks it onto a new `query` 
field (line 339) and a third constructor parameter (line 341), with 
`emptyIterator()` passing `null` for it (line 374).
   
   But the rewritten `PageEntryIterator` is the only `PageResults` consumer in 
main code (`PageEntryIterator.java:62`), and it reads `results()`, 
`hasNextPage()`, `page()` and `total()` only. Base `PageEntryIterator:79` 
called `pageResults.query()`, and `:60/81/97/106` called `pageResults.get()`; 
both are gone. `git grep` over this head finds no call to either accessor, and 
`QueryListTest:49-50` uses `.results()`.
   
   Suggest dropping `get()`, `query()`, the field and the constructor 
parameter, returning `PageResults` to `(results, pageState)`, which also 
removes the `null` argument in `emptyIterator()`.



##
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/cache/CachedGraphTransaction.java:
##
@@ -316,135 +318,143 @@ private boolean needCacheVertex(HugeVertex vertex) {
 
 @Override
 @Watched(prefix = "graphcache")
-protected Iterator queryVerticesFromBackend(Query query) {
-if (this.enableCacheVertex() &&
-query.idsSize() > 0 && query.conditionsSize() == 0 &&
-!queryNeedsPostFilter(query)) {
-return this.queryVerticesByIds((IdQuery) query);
-} else {
-return super.queryVerticesFromBackend(query);
+protected QueryResults fetchVertexBatch(Query query) {
+if (!this.enableCacheVertex() || query.paging() ||
+query.idsSize() == 0 || query.conditionsSize() != 0) {
+return super.fetchVertexBatch(query);
 }
-}
-
-@Watched(prefix = "graphcache")
-private Iterator queryVerticesByIds(IdQuery query) {
-if (query.idsSize() == 1) {
-Id vertexId = query.ids().iterator().next();
-HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
-if (vertex != null) {
-if (!vertex.expired()) {
-return QueryResults.iterator(vertex);
-}
-this.verticesCache.invalidate(vertexId);
-}
-Iterator rs = super.queryVerticesFromBackend(query);
-vertex = QueryResults.one(rs);
-if (vertex == null) {
-return QueryResults.emptyIterator();
-}
-if (needCacheVertex(vertex)) {
-this.verticesCache.update(vertex.id(), vertex);
-}
-return QueryResults.iterator(vertex);
-}
-
-IdQuery newQuery = new IdQuery(HugeType.VERTEX, query);
+QueryResultContext context = new QueryResultContext(query);
+IdQuery missing = new IdQuery(query.resultType(), query);
 List vertices = new ArrayList<>();
-for (Id vertexId : query.ids()) {
-HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
-if (vertex == null) {
-newQuery.query(vertexId);
-} else if (vertex.expired()) {
-newQuery.query(vertexId);
-this.verticesCache.invalidate(vertexId);
+for (Id id : query.ids()) {
+HugeVertex vertex = (HugeVertex) this.verticesCache.get(id);
+if (vertex == null || vertex.expired()) {
+missing.query(id);
+if (vertex != null) {
+this.verticesCache.invalidate(id);
+}
 } else {
 vertices.add(vertex);
 }
 }
-
-// Join results from cache and backend
-ExtendableIterator results = new ExtendableIterator<>();
-if (!vertices.isEmpty()) {
-results.extend(vertices.iterator());
-} else {
-// Just use the origin query if find none from the cache
-newQuery = query;
-}
-
-if (!newQuery.empty()) {
-Iterator rs = super.queryVerticesFromBackend(newQuery);
-// Generally there are not too much data with id query
-ListIterator listIterator = QueryResults.toList(rs);
-for (HugeVertex vertex : listIterator.list()) {
-// 

Re: [PR] fix(server): make query result batch boundaries explicit [hugegraph]

2026-09-05 Thread via GitHub


bitflicker64 commented on code in PR #3193:
URL: https://github.com/apache/hugegraph/pull/3193#discussion_r3940228184


##
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/cache/CachedGraphTransaction.java:
##
@@ -316,135 +318,142 @@ private boolean needCacheVertex(HugeVertex vertex) {
 
 @Override
 @Watched(prefix = "graphcache")
-protected Iterator queryVerticesFromBackend(Query query) {
-if (this.enableCacheVertex() &&
-query.idsSize() > 0 && query.conditionsSize() == 0 &&
-!queryNeedsPostFilter(query)) {
-return this.queryVerticesByIds((IdQuery) query);
-} else {
-return super.queryVerticesFromBackend(query);
+protected QueryResults fetchVertexBatch(Query query) {
+if (!this.enableCacheVertex() || query.idsSize() == 0 || 
query.conditionsSize() != 0) {

Review Comment:
   ‼️ This bypass needs `|| query.paging()`.
   
   Unlike the `queryVerticesFromBackend` override it replaces, this method is 
the QueryList *fetcher*, so it runs on the inner query. 
`GraphTransaction.optimizeQuery:1564` rewrites `label + primary-key` into `new 
IdQuery(query, id)`, and `Query(HugeType, Query originQuery)` 
(`Query.java:84-109`) copies neither page nor conditions, so that leaf reaches 
line 322 with `idsSize()==1` and `conditionsSize()==0` even after 
`QueryList.java:187-188` sets its page.
   
   Line 353 then returns a `QueryResults` whose `metadata` is the batch's 
`BatchIterator` delegating to a plain `ArrayList` iterator, so 
`metadata(PageInfo.PAGE)` is null, and `mapBatches` passes that null through 
`filterExpiredBatches`. `QueryList.java:198` calls 
`PageInfo.pageState(results.iterator())`, which is `E.checkState(page 
instanceof PageState, "Invalid PageState '%s'", page)`. So `g.V().hasLabel().has(, v).has("~page", "")` fails with `Invalid PageState 'null'` 
wherever `supportsQueryByPage()` is true. The only escape is a cache miss that 
returns nothing, via the early return at 341-343.
   
   The green paging tests do not contradict this. Index-paged queries go 
through `IndexQuery.iterator`, which takes its cursor from 
`pageIds.pageState()` (`QueryList.java:297`) rather than from result metadata, 
and no `VertexCoreTest` case pairs a primary-key label with its own primary-key 
property under `~page`. `InMemoryDBStore.supportsQueryByPage()` is false, so 
the unit tests cannot reach it either.



##
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/cache/CachedGraphTransaction.java:
##
@@ -316,135 +318,142 @@ private boolean needCacheVertex(HugeVertex vertex) {
 
 @Override
 @Watched(prefix = "graphcache")
-protected Iterator queryVerticesFromBackend(Query query) {
-if (this.enableCacheVertex() &&
-query.idsSize() > 0 && query.conditionsSize() == 0 &&
-!queryNeedsPostFilter(query)) {
-return this.queryVerticesByIds((IdQuery) query);
-} else {
-return super.queryVerticesFromBackend(query);
+protected QueryResults fetchVertexBatch(Query query) {
+if (!this.enableCacheVertex() || query.idsSize() == 0 || 
query.conditionsSize() != 0) {
+return super.fetchVertexBatch(query);
 }
-}
-
-@Watched(prefix = "graphcache")
-private Iterator queryVerticesByIds(IdQuery query) {
-if (query.idsSize() == 1) {
-Id vertexId = query.ids().iterator().next();
-HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
-if (vertex != null) {
-if (!vertex.expired()) {
-return QueryResults.iterator(vertex);
-}
-this.verticesCache.invalidate(vertexId);
-}
-Iterator rs = super.queryVerticesFromBackend(query);
-vertex = QueryResults.one(rs);
-if (vertex == null) {
-return QueryResults.emptyIterator();
-}
-if (needCacheVertex(vertex)) {
-this.verticesCache.update(vertex.id(), vertex);
-}
-return QueryResults.iterator(vertex);
-}
-
-IdQuery newQuery = new IdQuery(HugeType.VERTEX, query);
+QueryResultContext context = new QueryResultContext(query);
+IdQuery missing = new IdQuery(query.resultType(), query);
 List vertices = new ArrayList<>();
-for (Id vertexId : query.ids()) {
-HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
-if (vertex == null) {
-newQuery.query(vertexId);
-} else if (vertex.expired()) {
-newQuery.query(vertexId);
-this.verticesCache.invalidate(vertexId);
+for (Id id : query.ids()) {
+HugeVertex vertex = (HugeVertex) this.verticesCache.get(id);
+if (vertex == null || vertex.expired()) {
+

Re: [PR] fix(server): make query result batch boundaries explicit [hugegraph]

2026-09-04 Thread via GitHub


codecov[bot] commented on PR #3193:
URL: https://github.com/apache/hugegraph/pull/3193#issuecomment-5549295196

   ## 
[Codecov](https://app.codecov.io/gh/apache/hugegraph/pull/3193?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
 Report
   :white_check_mark: All modified and coverable lines are covered by tests.
   :white_check_mark: Project coverage is 93.25%. Comparing base 
([`3681148`](https://app.codecov.io/gh/apache/hugegraph/commit/36811483a2040f70ca6923a288a8c28cad0086c0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache))
 to head 
([`6b0469b`](https://app.codecov.io/gh/apache/hugegraph/commit/6b0469bcc90094fdb68417f13360c88a9e4eb4e9?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   Additional details and impacted files
   
   
   
   ```diff
   @@  Coverage Diff  @@
   ## master#3193   +/-   ##
   =
   + Coverage 37.77%   93.25%   +55.48% 
   + Complexity 6560   65 -6495 
   =
 Files   8009  -791 
 Lines 68960  267-68693 
 Branches   9166   22 -9144 
   =
   - Hits  26052  249-25803 
   + Misses398418-39833 
   + Partials   3067   10 -3057 
   ```
   
   
   [:umbrella: View full report in Codecov by 
Harness](https://app.codecov.io/gh/apache/hugegraph/pull/3193?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   
   :loudspeaker: Have feedback on the report? [Share it 
here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:rocket: New features to boost your workflow: 
   
   - :snowflake: [Test 
Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, 
report on failures, and find test suite problems.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


-
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]