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

Julian Reschke updated OAK-12558:
---------------------------------
    Component/s: commons

> 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
>          Components: commons
>            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