zhiqiang-hhhh commented on PR #60850: URL: https://github.com/apache/doris/pull/60850#issuecomment-4333259770
Memory risk in the current ANN result cache implementation: `AnnIndexResultCache::insert()` builds a `CacheValue`, computes `charge`, and then calls `LRUCachePolicy::insert()`. The underlying `LRUCache::insert()` is best-effort capacity control rather than a hard limit: after evicting available LRU entries, it still inserts the new entry even if not enough space was freed. This is explicitly allowed by the existing LRU implementation comment: the cache may become larger than its capacity if enough space cannot be freed. For ANN result cache this means: - If a single result entry is larger than `ann_index_result_cache_limit`, it can still be cached and make cache usage exceed the configured capacity. - Concurrent queries can allocate/build multiple large `CacheValue` objects before insertion, so memory peak can exceed the configured cache limit even when no entry is pinned. - The current lookup path copies internal `shared_ptr` objects out of the cache and releases the LRU handle immediately. After eviction, cache accounting may be released while the actual result buffers are still held by query-side `shared_ptr`s, so RSS can exceed the cache tracker's view. Suggested fixes: - Reject caching when the estimated entry size is larger than the cache capacity. - Add an admission check before constructing or inserting large values, ideally using reservation/limit checking so cache insertion can fail instead of always accepting the entry. - Avoid exposing cache-owned `shared_ptr`s to query code. Prefer a pinned cache handle model similar to `StoragePageCache`/`PageCacheHandle`, or deep-copy results out of the cache so cache accounting and object lifetime stay consistent. Pinning would fix the accounting/lifetime mismatch, but it still should be combined with admission control because pinned entries only prevent premature eviction; they do not make LRU capacity a strict hard limit under large entries or concurrent inserts. -- 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]
