mrhhsg opened a new pull request, #70863: URL: https://github.com/apache/doris/pull/70863
### Background Doris keeps aggregate states in memtables, hash tables, and schema-change mergers. An aggregate state must remain live while result columns are produced and must be destroyed exactly once after its owner finishes with it. ObjectPool takes responsibility for deleting objects once they are registered. ### What problem does this PR solve? Issue Number: None Related PR: None PR Type: Bugfix Problem Summary: Allocation, output, or registration failures can occur between constructing a BE-owned resource and publishing or clearing its ownership marker. On these paths, aggregation or schema change may destroy a state twice, roll back a previously registered buffer, or leak an unregistered object. This patch makes each ownership transition explicit: resources remain locally owned until registration succeeds, and destroyed states are unpublished before another operation can fail. Normal aggregate results are unchanged. ### Details - **Change map:** Production code: +94/-59 lines across five BE files. Tests: +391/-2 lines across five BE unit-test files and two SQL suites; generated regression output: +16/-0 lines. The production changes cover memtable finalization, nullable hash reset, aggregate container expansion, ObjectPool registration, and aggregate schema-change merging. - **Frequency:** Exceptional paths only. The ObjectPool leak was reproduced by injecting a registration allocation failure into both scalar and array registration; the other failure windows were identified from their cleanup order. - **Root cause:** `MemTable::_finalize_one_row` destroyed a state before all result columns succeeded while its row still advertised live states. Nullable `AggSharedState::reset_hash_table` retained a live NULL marker after destruction; `AggregateDataContainer::_expand` treated allocated buffers as registered entries; ObjectPool and schema-change merging had gaps before ownership transfer. - **Why existing tests missed it:** Existing BE unit tests and SQL suites covered successful aggregation and schema change, not failures between allocation, registration, output, and cleanup. - **Fix:** Complete all memtable result columns before destroying states; clear nullable NULL-state publication before further allocation; roll back only successfully registered aggregate buffers and publish the new cursor only after both registrations succeed; take ObjectPool ownership on entry; and use the existing `AggregateFunctionGuard` for schema-change states. - **Other paths and branches:** Both ObjectPool registration methods are covered. This change targets master; release branches need separate validation. No wire, storage, configuration, or public API change. - **Behavior change:** On failure, partially constructed resources are cleaned up once instead of being leaked or destroyed twice; successful aggregate results are unchanged. ### Testing - `./build.sh --be --fe -j 48` on the local ASAN worktree: passed, including FE checkstyle. - Focused ASAN BE unit tests for memtable failure, hash reset, container expansion, and ObjectPool registration: 40/40 passed. Schema-change guard and partitioned aggregation tests: 51/51 passed. Container tests after the final test-helper refactor: 13/13 passed. - The load and sorting schema-change SQL suites each passed generated-output and ordinary verification runs on an isolated local ASAN cluster. The existing `spill_p0/aggregate_spill` suite passed. - The standalone ObjectPool allocation-failure probe observed retained live objects in both unmodified registration paths; with this patch, both paths released their objects. Probe compilation and execution passed on the patched header. - BE formatting, build hygiene, changed-line clang-tidy, and source/suite whitespace checks passed. - The complete production schema-change merger was not directly subjected to a forced construction or registration failure; guard failure handling and the normal sorting path were tested separately. Full CI and release-branch tests were not run locally. ### Notes for Reviewers Start with the transitions from allocated to registered to published in `AggregateDataContainer::_expand` and the guard/function destruction order in the schema-change merger. The ObjectPool temporary owner is declared before its mutex guard so exception unwinding unlocks before deleting an unregistered object. ### Release note Fixes potential BE crashes and memory leaks when aggregation or schema change encounters an error. ### Check List (For Author) - Behavior changed: Yes (see Details) - Does this need documentation: No; there is no new user-facing interface or configuration. - Pre-submission status: Partially verified: forced failure of the complete schema-change merger and full CI were not run locally. ### AI Disclosure - Tool: OpenAI Codex - Generated: BE changes, unit and SQL tests, and this description - Human review: None recorded -- 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]
