[ 
https://issues.apache.org/jira/browse/OAK-12558?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Rishabh Daim updated OAK-12558:
-------------------------------
    Description: 
h3. Problem
OAK-11451 replaced Guava Iterables.concat with Apache Commons 
Collections-backed chaining. Nested concatenations can now perform 
exponentially many hasNext() calls because LazyIteratorChain checks the current 
iterator in both updateCurrentIterator() and hasNext(), without unpacking 
nested chains.

OAK-12532 fixes one affected caller in Mongo revision GC by collecting query 
iterables and chaining once. The utility APIs remain vulnerable, including the 
Iterable<Iterable<E>> overload and the IteratorUtils helpers changed by 
OAK-12410.

A deterministic reproduction with one counting leaf and 20 nested 
concatenations makes 1,048,576 leaf hasNext() calls for one successful outer 
hasNext(). The Guava 33.4.0 implementation used before OAK-11451 makes one leaf 
call by traversing nested concatenations iteratively.

h3. Caller audit
Latest trunk was checked at 29366bbaf7373bd7a76df8d89ee1f9f9ca1e0c95: 49 
production-source call sites and 46 test-source call sites. MapRecord.getKeys() 
and getEntries() also recursively compose chains. Their trie depth is capped at 
seven, so their amplification is bounded; no second unbounded accumulating 
production caller was found beyond the GC case already fixed by OAK-12532.

h3. Required fix
* Fix concatenation centrally in oak-commons, covering all five 
IterableUtils.chainedIterable overloads and all four 
IteratorUtils.chainedIterator overloads.
* Avoid exponential polling and recursive iterator-call stack growth without 
requiring caller-side changes or a Guava dependency.
* Preserve iteration order, lazy input opening, repeatable Iterable behavior, 
null handling, supported remove behavior, and already-polled or partially 
consumed child iterators (including the OAK-12410 regression).
* Add deterministic counting regressions and deep-nesting tests, plus 
compatibility controls.
* Run the full oak-commons test suite, coverage and bundle baseline 
verification.

h3. References
* [OAK-12532|https://issues.apache.org/jira/browse/OAK-12532]
* [OAK-11451|https://issues.apache.org/jira/browse/OAK-11451]
* [OAK-12410|https://issues.apache.org/jira/browse/OAK-12410]
* [COLLECTIONS-722|https://issues.apache.org/jira/browse/COLLECTIONS-722]
* [COLLECTIONS-900|https://issues.apache.org/jira/browse/COLLECTIONS-900]


h3. Implementation and verification
Implemented in [PR #3319|https://github.com/apache/jackrabbit-oak/pull/3319]. 
Latest commit: {{b14e289f61a72f262c478bfa754ffbf97bfd6c26}}.

h3. API-level fix

* Route all five {{IterableUtils.chainedIterable()}} overloads and all four 
{{IteratorUtils.chainedIterator()}} overloads through the shared implementation.
* Represent concatenated iterable views explicitly. Traverse nested Oak views 
lazily before opening their leaf iterators, avoiding intermediate concatenation 
iterators.
* Cache the active leaf and retain an explicit traversal path for nested 
iterator inputs. Ordinary iteration reuses that state instead of walking the 
full nesting depth for every element; traversal stacks are allocated only when 
needed.
* Invalidate a cached path when a wrapped child is used independently. Track 
removal state by ownership generation, avoiding updates to every ancestor for 
each returned value. Already-polled, partially consumed, and subsequently 
reused child iterators retain their state.
* Avoid the temporary list and list iterator in the two-iterator overload.

Normal traversal scales with the number of input nodes and elements, as Guava's 
concatenation does. Independent child access can require rebuilding the cached 
path; preserving that behavior adds some overhead compared with Guava's 
iterator-state flattening. Order, lazy source opening, repeatable iterable 
views, null handling, supported removal, and existing input-array view/snapshot 
behavior are preserved. No caller-side changes, public signature changes, or 
Guava dependency are required.

The implementation is unconditional: *there is no feature toggle or legacy 
fallback*.

h3. Tests and verification

Java 17:

{code:language=text}
mvn -o -B -pl oak-commons clean verify apache-rat:check
{code}

*722 tests passed; zero failures, errors, or skips.* Includes 20 new regression 
and compatibility tests:

* Deterministic leaf-poll counting across all nine overloads, with left and 
right nesting. The unfixed 20-level case failed with 1,048,576 polls instead of 
one.
* Accumulating 128 nested concatenations and consuming all 129 values across 
all nine overloads. This timed out after 10 seconds against the old 
Commons-backed implementation; its first-element recurrence would require 
approximately {{2^128}} leaf polls.
* 100,000 nested chains; 100,000 values behind 100,000 nested iterator chains; 
and 100,000 flat inputs. These detect recursive stack growth, repeated 
full-path traversal, and excessive leaf polling respectively. The deep 
multi-value case also timed out with a path rebuilt on every operation.
* Already-polled and partially consumed children, alternating parents sharing a 
child, and continued child use through opaque iterable wrappers.
* Removal through parent and child iterators, including after crossing input 
boundaries and borrowing an already-traversed child.
* Lazy opening of both data iterators and nested outer sources, infinite input 
sources, repeated iteration, empty inputs, null values, eager null validation, 
exhaustion and unsupported removal.
* The iterable varargs array remains a live view; iterator varargs retain their 
existing snapshot behavior.

Coverage checks and Apache RAT passed. Bundle baseline completed with zero 
errors and warnings. The new {{ConcatenatingIterator}} has 100% line coverage 
and over 90% branch coverage.

h3. Comparison with Guava

Local JMH 1.34, OpenJDK 17, fixed 512 MB heap, two forks, four 400 ms warmup 
and four 400 ms measurement iterations per fork, GC profiler. Guava 
*33.4.0-jre* is the version Oak used before OAK-11451. Times are microseconds 
per complete operation; lower is better.

||Workload||Oak µs/op||Guava µs/op||Oak bytes/op||Guava bytes/op||
|Flat iterable, 10,000 values|36.76|43.93|176|120|
|128 nested iterable views, 10,000 values|39.87|49.24|10,033|13,025|
|128 appended singleton iterable inputs|3.46|3.16|10,033|13,025|
|128 nested iterator chains, 10,000 values|41.89|49.52|12,025|12,025|
|128 appended singleton iterator inputs|4.80|3.97|19,138|19,138|
|4,096 nested iterable views, 10,000 values|114.46|143.29|313,073|411,308|

Iterable rows include iterator creation and traversal of prebuilt views. 
Iterator rows include construction of the iterator chains as well as traversal. 
The flat/deep cases were faster locally, with about 23% less allocation for 
nested iterable views. Short appended cases remain about 9–21% slower; the flat 
iterable case allocates 56 extra bytes per traversal. These are local 
comparisons, not a claim of universal performance superiority.


  was:
h3. Problem
OAK-11451 replaced Guava Iterables.concat with Apache Commons 
Collections-backed chaining. Nested concatenations can now perform 
exponentially many hasNext() calls because LazyIteratorChain checks the current 
iterator in both updateCurrentIterator() and hasNext(), without unpacking 
nested chains.

OAK-12532 fixes one affected caller in Mongo revision GC by collecting query 
iterables and chaining once. The utility APIs remain vulnerable, including the 
Iterable<Iterable<E>> overload and the IteratorUtils helpers changed by 
OAK-12410.

A deterministic reproduction with one counting leaf and 20 nested 
concatenations makes 1,048,576 leaf hasNext() calls for one successful outer 
hasNext(). The Guava 33.4.0 implementation used before OAK-11451 makes one leaf 
call by traversing nested concatenations iteratively.

h3. Caller audit
Latest trunk was checked at 29366bbaf7373bd7a76df8d89ee1f9f9ca1e0c95: 49 
production-source call sites and 46 test-source call sites. MapRecord.getKeys() 
and getEntries() also recursively compose chains. Their trie depth is capped at 
seven, so their amplification is bounded; no second unbounded accumulating 
production caller was found beyond the GC case already fixed by OAK-12532.

h3. Required fix
* Fix concatenation centrally in oak-commons, covering all five 
IterableUtils.chainedIterable overloads and all four 
IteratorUtils.chainedIterator overloads.
* Avoid exponential polling and recursive iterator-call stack growth without 
requiring caller-side changes or a Guava dependency.
* Preserve iteration order, lazy input opening, repeatable Iterable behavior, 
null handling, supported remove behavior, and already-polled or partially 
consumed child iterators (including the OAK-12410 regression).
* Add deterministic counting regressions and deep-nesting tests, plus 
compatibility controls.
* Run the full oak-commons test suite, coverage and bundle baseline 
verification.

h3. References
* [OAK-12532|https://issues.apache.org/jira/browse/OAK-12532]
* [OAK-11451|https://issues.apache.org/jira/browse/OAK-11451]
* [OAK-12410|https://issues.apache.org/jira/browse/OAK-12410]
* [COLLECTIONS-722|https://issues.apache.org/jira/browse/COLLECTIONS-722]
* [COLLECTIONS-900|https://issues.apache.org/jira/browse/COLLECTIONS-900]


h3. Local implementation and verification
The fix is implemented locally on issue/OAK-12558, based on trunk 
29366bbaf7373bd7a76df8d89ee1f9f9ca1e0c95. All nine public chaining overloads 
use one package-private concatenation engine. It walks nested child states 
using an explicit traversal path, checking the active leaf once per hasNext() 
and retaining child iterator state rather than draining pending inputs. 
Bookkeeping is linear in nesting depth rather than exponential, and traversal 
does not recurse through Oak concatenation iterators.

No callers or public API signatures are changed. The implementation retains 
lazy iterable input opening, existing eager validation/snapshot behavior of 
iterator overloads, ordering, repeatable iterable views, null element handling, 
and supported removal. The implementation is enabled by default; the startup 
property {{-DFT_OAK-12558=false}} restores the previous engine, following the 
repository toggle guidance.

Verification on Java 17:
* A deterministic 20-level regression failed before the fix: expected 1 leaf 
poll, observed 1,048,576.
* A 128-level accumulating-chain regression timed out after 10 seconds before 
the fix. With the fix it validates all nine overloads and all 129 values; the 
local run completed in 0.005 seconds. The unfixed recurrence would require 
approximately 2^128 leaf polls before the first value.
* 100,000 nested iterable/iterator chains complete without recursive stack 
growth.
* 100,000 flat inputs retain linear leaf polling.
* Compatibility controls cover already-polled and partially consumed children, 
continued child use after parent polling, eager null validation, lazy and 
infinite input sources, repeated iteration, null values, exhaustion and 
supported/unsupported removal.
* {{mvn -o -B -pl oak-commons clean verify apache-rat:check}}: 715 tests, zero 
failures/errors/skips. Includes 13 new tests.
* Coverage checks passed; new iterator has 100% line and 95% branch coverage in 
the default-mode suite.
* Bundle baseline: zero errors and warnings. Apache RAT: zero unapproved or 
unknown files.
* Existing IterableUtilsTest and IteratorUtilsTest also pass with the feature 
disabled: 252 tests, zero failures/errors/skips.



> Avoid exponential polling in nested oak-commons concatenation APIs
> ------------------------------------------------------------------
>
>                 Key: OAK-12558
>                 URL: https://issues.apache.org/jira/browse/OAK-12558
>             Project: Jackrabbit Oak
>          Issue Type: Bug
>            Reporter: Rishabh Daim
>            Assignee: Rishabh Daim
>            Priority: Major
>
> h3. Problem
> OAK-11451 replaced Guava Iterables.concat with Apache Commons 
> Collections-backed chaining. Nested concatenations can now perform 
> exponentially many hasNext() calls because LazyIteratorChain checks the 
> current iterator in both updateCurrentIterator() and hasNext(), without 
> unpacking nested chains.
> OAK-12532 fixes one affected caller in Mongo revision GC by collecting query 
> iterables and chaining once. The utility APIs remain vulnerable, including 
> the Iterable<Iterable<E>> overload and the IteratorUtils helpers changed by 
> OAK-12410.
> A deterministic reproduction with one counting leaf and 20 nested 
> concatenations makes 1,048,576 leaf hasNext() calls for one successful outer 
> hasNext(). The Guava 33.4.0 implementation used before OAK-11451 makes one 
> leaf call by traversing nested concatenations iteratively.
> h3. Caller audit
> Latest trunk was checked at 29366bbaf7373bd7a76df8d89ee1f9f9ca1e0c95: 49 
> production-source call sites and 46 test-source call sites. 
> MapRecord.getKeys() and getEntries() also recursively compose chains. Their 
> trie depth is capped at seven, so their amplification is bounded; no second 
> unbounded accumulating production caller was found beyond the GC case already 
> fixed by OAK-12532.
> h3. Required fix
> * Fix concatenation centrally in oak-commons, covering all five 
> IterableUtils.chainedIterable overloads and all four 
> IteratorUtils.chainedIterator overloads.
> * Avoid exponential polling and recursive iterator-call stack growth without 
> requiring caller-side changes or a Guava dependency.
> * Preserve iteration order, lazy input opening, repeatable Iterable behavior, 
> null handling, supported remove behavior, and already-polled or partially 
> consumed child iterators (including the OAK-12410 regression).
> * Add deterministic counting regressions and deep-nesting tests, plus 
> compatibility controls.
> * Run the full oak-commons test suite, coverage and bundle baseline 
> verification.
> h3. References
> * [OAK-12532|https://issues.apache.org/jira/browse/OAK-12532]
> * [OAK-11451|https://issues.apache.org/jira/browse/OAK-11451]
> * [OAK-12410|https://issues.apache.org/jira/browse/OAK-12410]
> * [COLLECTIONS-722|https://issues.apache.org/jira/browse/COLLECTIONS-722]
> * [COLLECTIONS-900|https://issues.apache.org/jira/browse/COLLECTIONS-900]
> h3. Implementation and verification
> Implemented in [PR #3319|https://github.com/apache/jackrabbit-oak/pull/3319]. 
> Latest commit: {{b14e289f61a72f262c478bfa754ffbf97bfd6c26}}.
> h3. API-level fix
> * Route all five {{IterableUtils.chainedIterable()}} overloads and all four 
> {{IteratorUtils.chainedIterator()}} overloads through the shared 
> implementation.
> * Represent concatenated iterable views explicitly. Traverse nested Oak views 
> lazily before opening their leaf iterators, avoiding intermediate 
> concatenation iterators.
> * Cache the active leaf and retain an explicit traversal path for nested 
> iterator inputs. Ordinary iteration reuses that state instead of walking the 
> full nesting depth for every element; traversal stacks are allocated only 
> when needed.
> * Invalidate a cached path when a wrapped child is used independently. Track 
> removal state by ownership generation, avoiding updates to every ancestor for 
> each returned value. Already-polled, partially consumed, and subsequently 
> reused child iterators retain their state.
> * Avoid the temporary list and list iterator in the two-iterator overload.
> Normal traversal scales with the number of input nodes and elements, as 
> Guava's concatenation does. Independent child access can require rebuilding 
> the cached path; preserving that behavior adds some overhead compared with 
> Guava's iterator-state flattening. Order, lazy source opening, repeatable 
> iterable views, null handling, supported removal, and existing input-array 
> view/snapshot behavior are preserved. No caller-side changes, public 
> signature changes, or Guava dependency are required.
> The implementation is unconditional: *there is no feature toggle or legacy 
> fallback*.
> h3. Tests and verification
> Java 17:
> {code:language=text}
> mvn -o -B -pl oak-commons clean verify apache-rat:check
> {code}
> *722 tests passed; zero failures, errors, or skips.* Includes 20 new 
> regression and compatibility tests:
> * Deterministic leaf-poll counting across all nine overloads, with left and 
> right nesting. The unfixed 20-level case failed with 1,048,576 polls instead 
> of one.
> * Accumulating 128 nested concatenations and consuming all 129 values across 
> all nine overloads. This timed out after 10 seconds against the old 
> Commons-backed implementation; its first-element recurrence would require 
> approximately {{2^128}} leaf polls.
> * 100,000 nested chains; 100,000 values behind 100,000 nested iterator 
> chains; and 100,000 flat inputs. These detect recursive stack growth, 
> repeated full-path traversal, and excessive leaf polling respectively. The 
> deep multi-value case also timed out with a path rebuilt on every operation.
> * Already-polled and partially consumed children, alternating parents sharing 
> a child, and continued child use through opaque iterable wrappers.
> * Removal through parent and child iterators, including after crossing input 
> boundaries and borrowing an already-traversed child.
> * Lazy opening of both data iterators and nested outer sources, infinite 
> input sources, repeated iteration, empty inputs, null values, eager null 
> validation, exhaustion and unsupported removal.
> * The iterable varargs array remains a live view; iterator varargs retain 
> their existing snapshot behavior.
> Coverage checks and Apache RAT passed. Bundle baseline completed with zero 
> errors and warnings. The new {{ConcatenatingIterator}} has 100% line coverage 
> and over 90% branch coverage.
> h3. Comparison with Guava
> Local JMH 1.34, OpenJDK 17, fixed 512 MB heap, two forks, four 400 ms warmup 
> and four 400 ms measurement iterations per fork, GC profiler. Guava 
> *33.4.0-jre* is the version Oak used before OAK-11451. Times are microseconds 
> per complete operation; lower is better.
> ||Workload||Oak µs/op||Guava µs/op||Oak bytes/op||Guava bytes/op||
> |Flat iterable, 10,000 values|36.76|43.93|176|120|
> |128 nested iterable views, 10,000 values|39.87|49.24|10,033|13,025|
> |128 appended singleton iterable inputs|3.46|3.16|10,033|13,025|
> |128 nested iterator chains, 10,000 values|41.89|49.52|12,025|12,025|
> |128 appended singleton iterator inputs|4.80|3.97|19,138|19,138|
> |4,096 nested iterable views, 10,000 values|114.46|143.29|313,073|411,308|
> Iterable rows include iterator creation and traversal of prebuilt views. 
> Iterator rows include construction of the iterator chains as well as 
> traversal. The flat/deep cases were faster locally, with about 23% less 
> allocation for nested iterable views. Short appended cases remain about 9–21% 
> slower; the flat iterable case allocates 56 extra bytes per traversal. These 
> are local comparisons, not a claim of universal performance superiority.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to