Skip to content

mgr: make map cache ownership explicit - #73426

Merged
tchaikov merged 1 commit into
ceph:mainfrom
tchaikov:wip-mgr-cache
Nov 24, 2026
Merged

tchaikov merged 1 commit into
ceph:mainfrom
tchaikov:wip-mgr-cache

Conversation

@tchaikov

@tchaikov tchaikov commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

MgrMapCache uses raw PyObject pointers with different ownership rules for insertion and lookup. Callers must track reference counts by hand, and cache operations must remember to drain pending releases. Changes to error paths or cache operations can easily miss one of these steps.

Use a move-only PyObjectRef to make ownership explicit. Insertion takes ownership; lookup returns a separate reference through clone(). Keep raw reference adoption and transfer at the Python C API boundary.

Notification paths hold manager locks without the GIL, so they cannot safely release Python references. Mark entries stale and keep at most one stale reference per key. The next lookup or replacement releases it with the GIL after dropping the cache lock. Clear, disable and shutdown extract references before destroying them with the GIL, preserving the caller's Python context. Finalizers can then re-enter the cache safely. Reclamation stays synchronous and no longer needs a pending-release queue.

Remove the unused generic MgrMapCache and use LFUCache as a member. Let LFUCache hold move-only values and return removed values for destruction outside its lock. Prefer stale entries for eviction and reject writes when capacity is zero.

This is an ownership cleanup on top of the existing leak fix.

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@tchaikov

tchaikov commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@NitzanMordhai and @sseshasa , could you please review this change? While reading #72727, I found the code might be difficult to reason about long term, so I wanted to introduce this refactor modeled after Rust’s memory model. Thanks!

@NitzanMordhai NitzanMordhai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tchaikov thanks! looks much better, can you please check my comments?

Comment thread src/mgr/MgrMapCache.cc Resolved
Comment thread src/mgr/ActivePyModules.cc Resolved
Comment thread src/test/mgr/test_mgrmapcache.cc Resolved
@tchaikov

tchaikov commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@NitzanMordhai thanks for your constructive suggestions. all addressed. could you take another look?

@NitzanMordhai NitzanMordhai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fixes!

@sseshasa sseshasa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I just had a non-blocking comment.

Comment thread src/mgr/ActivePyModules.cc Resolved
@tchaikov

tchaikov commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test signed

@tchaikov

tchaikov commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check

@tchaikov

tchaikov commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

@tchaikov

tchaikov commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test submodules

@tchaikov

tchaikov commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test api

@tchaikov

tchaikov commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test windows

@tchaikov
tchaikov force-pushed the wip-mgr-cache branch 2 times, most recently from 7681a44 to 79cb942 Compare November 4, 2026 02:18
MgrMapCache uses raw PyObject pointers with different ownership rules
for insertion and lookup. Callers must track reference counts by hand,
and cache operations must remember to drain pending releases. Changes to
error paths or cache operations can easily miss one of these steps.

Use a move-only PyObjectRef to make ownership explicit. Insertion takes
ownership; lookup returns a separate reference through clone(). Keep raw
reference adoption and transfer at the Python C API boundary.

Pass the cache decision to get_python so snapshot formatting and
insertion use the same decision. Remove the duplicated predicate and
its redundant PyGILState_Check.

Notification paths hold manager locks without the GIL, so they cannot
safely release Python references. Mark entries stale and keep at most
one stale reference per key. The next lookup or replacement releases it
with the GIL after dropping the cache lock. Clear, disable and shutdown
extract references before destroying them with the GIL. On Python 3.13
and later, use PyThreadState_GetUnchecked to preserve an attached
interpreter, and acquire the GIL when no state is attached. On older
Python, use PyGILState_Ensure and let subinterpreter callers clear
explicitly with their GIL held. Avoid the private unchecked getter,
which can return another thread's state before Python 3.12. Finalizers
can re-enter the cache safely. Reclamation stays synchronous and no
longer needs a pending-release queue.

Remove the unused generic MgrMapCache and use LFUCache as a member. Let
LFUCache hold move-only values and return removed values for destruction
outside its lock. Prefer stale entries for eviction and reject writes
when capacity is zero.

Track a generation for each cacheable key so a notification during
snapshot creation prevents insertion of the stale snapshot. Bump the
generation even when the key has no entry, and check it under the same
lock as insertion. Clear, disable and erasure also reject snapshots
that started before them.

This builds on the existing leak fix.

Signed-off-by: Kefu Chai <[email protected]>
@NitzanMordhai

Copy link
Copy Markdown
Contributor

@tchaikov just to let you know - i checked that PR with #72728 it faking osds process and i wanted to make sure the cache holds up with large cluster and we are not missing things in mgr cache

@tchaikov

tchaikov commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check

@tchaikov

tchaikov commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

@tchaikov

tchaikov commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath10-testing-20261109.054620-main.

@lee-j-sanders

Copy link
Copy Markdown
Member

@tchaikov
tchaikov merged commit 7c5f96b into ceph:main Nov 24, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants