Skip to content

mgr: read cached maps in prometheus, pg_autoscaler and balancer - #72754

Open
NitzanMordhai wants to merge 3 commits into
ceph:mainfrom
NitzanMordhai:wip-nitzan-mgr-modules-api-cache
Open

NitzanMordhai wants to merge 3 commits into
ceph:mainfrom
NitzanMordhai:wip-nitzan-mgr-modules-api-cache

Conversation

@NitzanMordhai

@NitzanMordhai NitzanMordhai commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Three mgr modules rebuilt large Python objects (the full pg_dump or a full
OSDMap dump) on every loop, while holding the GIL, just to read a few fields.
The same data is already available, either from a smaller key or from the
mgr's api cache.

  • prometheus: get_pool_repaired_objects() reads pool_stats instead of
    the whole pg_dump (same PGMap::dump_pool_stats() output). Also adds
    @profile_method() to the three collect() steps that were missing from
    prometheus_collect_duration_seconds.
  • pg_autoscaler: flags, pools and require_osd_release come from the
    cached osd_map instead of OSDMap.dump(). The pool list is used with the
    OSDMap object, so it is taken from the cache only when the epochs match.
  • balancer: Plan uses the cached osd_map instead of osdmap.dump(),
    with the same epoch check.

Testing

  • Unit tests: pg_autoscaler, balancer and prometheus pass. A new pg_autoscaler
    test covers a cached dump that is newer than the OSDMap.
  • Scale test with fake OSDs (vstart, 1 mon, 1 mgr, prometheus enabled,
    mgr_stats_period=5, autotune off, 600s sampling):
Case Metric base perf Change
100 OSDs / 32K PGs RSS mean 1021 MB 387 MB -62%
RSS max 1187 MB 391 MB -67%
CPU mean 93% 28% -70%
CPU max 132% 55% -59%
prometheus collect 9.4 s 0.76 s -92%
300 OSDs / 64K PGs RSS mean 1946 MB 771 MB -60%
RSS max 2378 MB 772 MB -68%
CPU mean 138% 61% -55%
CPU max 180% 121% -33%
prometheus collect 127 s 2.4 s -98%

Fixes: https://tracker.ceph.com/issues/81902

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.

@anthonyeleven

Copy link
Copy Markdown
Contributor

@ljflores when you have a moment, could you review this? It has been waiting since 10-15.

return self.get('osd_map')

def get_osdmap_flags(self) -> str:
return self.get_osdmap_dump().get('flags', '')

@idryomov idryomov Nov 2, 2026 •

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.

Just curious, why is "the cache may already hold a newer osdmap" logic applied only for pools (in get_osdmap_pools_by_name()) but not for flags or require_osd_release fields? Is it not a concern that e.g. noautoscale flag may be set in OSDMap object that _maybe_adjust() is called on but unset in the cache that has_noautoscale_flag() would go by when _maybe_adjust() calls it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good question. The flags were never read from the OSDMap object passed to _maybe_adjust().
Before this change, has_noautoscale_flag() and has_norecover_flag() called self.get_osdmap().dump(), which fetches a fresh map on every call, so they could already disagree with the osdmap argument. serve() also checks them before it calls get_osdmap().
I kept that behavior and just read the same latest map from the cache without the dump. For a gate like noautoscale, the newest value is the one we want anyway: if the flag was set an epoch later, we should honor it.

Pools are different because the code uses them together with the OSDMap object (CRUSH rule lookups, pool ids, pg_num in _get_pool_status()).
If the two came from different epochs, a pool created or deleted in between would exist in one and not the other, and that can break the calculation. That's why only the pools get the epoch check.

require_osd_release only ever moves forward, and the check is < 'nautilus', so being an epoch apart can't change the result. I moved it to the cache because OSDMap.get_require_osd_release() does a full _dump() internally. If you'd prefer it to come from the same epoch for consistency, I can add the epoch check there too.

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.

Before this change, has_noautoscale_flag() and has_norecover_flag() called self.get_osdmap().dump(), which fetches a fresh map on every call, so they could already disagree with the osdmap argument.

Ah, I didn't realize that. This means that they could disagree not only with osdmap which is passed in but also with each other (has_noautoscale_flag() using epoch N and has_norecover_flag() using epoch N + 1). This PR doesn't make it worse in any way.

If you'd prefer it to come from the same epoch for consistency, I can add the epoch check there too.

Given the above, I don't think it's necessary. Plus this check against nautilus OSDs is more or less dead anyway.

Comment on lines +346 to +354
def get_osdmap_pools_by_name(self, osdmap: OSDMap) -> Dict[str, Dict[str, Any]]:
# the cache may already hold a newer osdmap
dump = self.get_osdmap_dump()
if dump.get('epoch') != osdmap.get_epoch():
dump = osdmap.dump()
return {p['pool_name']: p for p in dump.get('pools', [])}

def get_osdmap_require_osd_release(self) -> str:
return self.get_osdmap_dump().get('require_osd_release', '')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ack, this make sense

@kamoltat kamoltat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

autoscaler change LGTM, maybe add a stale cache test case?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we include a stale cached scenario as well? Where OSDMAP is 11 and cache is 10?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the mgr cache is invalidating old maps as soon as new maps arrived, we have set of checks inside the mgr cache

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@rkachach
rkachach requested review from epuertat and jmolmo November 16, 2026 15:45

@epuertat epuertat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Prom-only approval!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGMT!

Comment thread src/pybind/mgr/balancer/module.py Outdated

class Plan(object):
def __init__(self, name, mode, osdmap, pools):
def __init__(self, name, mode, osdmap, pools, osdmap_dump=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm fine with the overall idea, but passing in a "None" object to get the osd dump to update to the latest dump isn't totally intuitive, IMO.

Maybe passing in an optional boolean would be a bit better, like:

        def __init__(self, name, mode, osdmap, pools, refresh_osdmap=False):

            self.osdmap_dump = osdmap.dump() if refresh_osdmap else osdmap_dump

            # the cache may already hold a newer osdmap
            refresh_osdmap = False
            dump = self.get('osd_map')
            if dump.get('epoch') != osdmap.get_epoch():
                refresh_osdmap = True
            plan = Plan(name, mode, osdmap, pools, refresh_osdmap)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Otherwise, LGTM! I'll approve for the balancer after this is addressed. Again, it's not a problem with the concept but just to make the code more intuitive.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ljflores thanks! i used your idea a bit differently - so i think its more readable now - use_cached_dump

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

docs n/a

get_pool_repaired_objects() only uses the pool_stats array, but fetched
the whole pg_dump, which builds a Python object for every PG and OSD
while holding the GIL. get('pool_stats') returns the same array, as
both come from PGMap::dump_pool_stats(), so the metric is unchanged.
With 50 OSDs and 16k PGs, get('pg_dump') took 144ms and
get('pool_stats') 0.04ms.

Also add @profile_method() to get_pool_repaired_objects(),
get_all_daemon_health_metrics() and get_perf_counters(), the only
collect() steps missing from prometheus_collect_duration_seconds.

https://tracker.ceph.com/issues/81902
Signed-off-by: Nitzan Mordechai <[email protected]>
Assisted-by: Claude
has_noautoscale_flag(), has_norecover_flag(), get_pools_by_name() and
get_require_osd_release() each built a Python object for the whole
OSDMap just to read a few fields, several times per serve() loop and
while holding the GIL.

Read those fields from self.get('osd_map') instead: the same
OSDMap::dump() output, served from the api cache and dropped on every
new osdmap. The OSDMap object is still used for pool_raw_used_rate()
and get_crush(), so the pool list is taken from the cache only when its
epoch matches that object; otherwise a pool created after get_osdmap()
would make pool_raw_used_rate() fail and stop serve().

With 24 OSDs and 8192 PGs, py-spy --gil put get_pools_by_name() and
get_require_osd_release() at about 40% of all mgr module GIL time.

https://tracker.ceph.com/issues/81902
Signed-off-by: Nitzan Mordechai <[email protected]>
Assisted-by: Claude
Plan.__init__() calls osdmap.dump(), which builds a Python object for
the whole OSDMap on every balancer run. With 24 OSDs and 8192 PGs,
py-spy --gil put it at about 22% of all mgr module GIL time.

plan_create() now passes self.get('osd_map'), the same OSDMap::dump()
output served from the api cache, when its epoch matches the map
being passed. A new osdmap can land after get_osdmap(), and a pool
missing from that map would make balance_primaries() fail, so on a
mismatch Plan dumps the map as before.

The parameter is optional: MsPlan and show() work from a modified or
stored map and still dump that map.

Fixes: https://tracker.ceph.com/issues/81902
Signed-off-by: Nitzan Mordechai <[email protected]>
Assisted-by: Claude
@NitzanMordhai
NitzanMordhai force-pushed the wip-nitzan-mgr-modules-api-cache branch from 82785bb to ffa53d3 Compare November 22, 2026 10:11
@NitzanMordhai
NitzanMordhai requested a review from a team as a code owner November 22, 2026 10:11

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

docs lgtm

@ljflores ljflores left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath3-testing-20261127.055027-main.

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.

7 participants