[ 
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
[PR #3319|https://github.com/apache/jackrabbit-oak/pull/3319], commit 
{{38af0606235ae11fe312ec7dc759bc16a804707e}}.

Fix exponential polling in Oak's concatenation APIs without caller-side 
changes. Commons {{LazyIteratorChain}} polls its active child twice, so one 
check through 20 nested chains can reach the leaf 1,048,576 times.

All nine overloads use the fixed engine. It reuses the active leaf and 
traversal state, preserves independent child use and removal, and opens sources 
lazily. Two-input chains now hold their inputs directly; iterable pairs 
traverse left/right references without allocating a metadata iterator for each 
nested view. There is no toggle or legacy fallback.

h3. Validation

* Full {{oak-commons}} verification: *724 tests passed*, zero failures, errors 
or skips; includes 22 new tests.
* Regression coverage includes all overloads, 100,000-level nesting, mixed 
overloads, child reuse, laziness, nulls and removal.
* Coverage checks, Apache RAT and bundle baseline passed; baseline reported 
zero errors and warnings.

h3. Performance

Side-by-side JMH 1.34 against Guava 33.4.0-jre on Java 17: two forks, four 400 
ms warmup and four 400 ms measurement iterations per fork, fixed 512 MB heap, 
GC profiler.

One operation consumes 128 appended singleton inputs. The iterator row also 
includes chain construction.

||API||Oak µs/op||Guava µs/op||Oak bytes/op||Guava bytes/op||
|Iterable|2.122|3.128|5,945|13,025|
|Iterator|3.425|3.961|17,089|19,138|

Both previously slower appended cases now beat Guava in this run. Eight-input 
cases were also comparable or faster. Flat/deep controls remained faster than 
Guava; the flat iterable case still allocates 32 extra bytes per traversal. 
These are local measurements, not a universal performance guarantee.


  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. 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.



> 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
> [PR #3319|https://github.com/apache/jackrabbit-oak/pull/3319], commit 
> {{38af0606235ae11fe312ec7dc759bc16a804707e}}.
> Fix exponential polling in Oak's concatenation APIs without caller-side 
> changes. Commons {{LazyIteratorChain}} polls its active child twice, so one 
> check through 20 nested chains can reach the leaf 1,048,576 times.
> All nine overloads use the fixed engine. It reuses the active leaf and 
> traversal state, preserves independent child use and removal, and opens 
> sources lazily. Two-input chains now hold their inputs directly; iterable 
> pairs traverse left/right references without allocating a metadata iterator 
> for each nested view. There is no toggle or legacy fallback.
> h3. Validation
> * Full {{oak-commons}} verification: *724 tests passed*, zero failures, 
> errors or skips; includes 22 new tests.
> * Regression coverage includes all overloads, 100,000-level nesting, mixed 
> overloads, child reuse, laziness, nulls and removal.
> * Coverage checks, Apache RAT and bundle baseline passed; baseline reported 
> zero errors and warnings.
> h3. Performance
> Side-by-side JMH 1.34 against Guava 33.4.0-jre on Java 17: two forks, four 
> 400 ms warmup and four 400 ms measurement iterations per fork, fixed 512 MB 
> heap, GC profiler.
> One operation consumes 128 appended singleton inputs. The iterator row also 
> includes chain construction.
> ||API||Oak µs/op||Guava µs/op||Oak bytes/op||Guava bytes/op||
> |Iterable|2.122|3.128|5,945|13,025|
> |Iterator|3.425|3.961|17,089|19,138|
> Both previously slower appended cases now beat Guava in this run. Eight-input 
> cases were also comparable or faster. Flat/deep controls remained faster than 
> Guava; the flat iterable case still allocates 32 extra bytes per traversal. 
> These are local measurements, not a universal performance guarantee.



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

Reply via email to