Skip to content

OSDMap::calc_pg_maps: fix balancing for some (important) corner cases - #65191

Merged
SrinivasaBharath merged 3 commits into
ceph:mainfrom
dvanders:dvanders_calc_pg_upmaps
Sep 18, 2026
Merged

SrinivasaBharath merged 3 commits into
ceph:mainfrom
dvanders:dvanders_calc_pg_upmaps

Conversation

@dvanders

@dvanders dvanders commented Aug 23, 2025 •

Copy link
Copy Markdown
Contributor

This PR fixes upmap balancing for two existing balancer problems: stall out due to stddev not decreasing, and undervaluing the deviation of small OSDs.

https://tracker.ceph.com/issues/78314

Accept changes that don't decrease stddev

In some cases the balancer can stall out, unable to find any
changes that will help overfull or underfull OSDs get closer
to their target num PGs.

Here's an example from the wild. These changes are not
accepted because the overall stddev is not decreasing:

  will try dropping existing remapping pair 264 -> 137 which remapped 24.f9d into overfull osd.137
  existing pg_upmap_items [766,799,264,137] remapped 24.f9d into overfull osd.137, new_pg_upmap_items now [766,799]
  stddev 138085.453125 -> 138085.453125
  will try dropping existing remapping pair 496 -> 637 which remapped 24.5ca out from underfull osd.496
  existing pg_upmap_items [729,790,496,637] remapped 24.5ca out from underfull osd.496, new_pg_upmap_items now [729,790]
  stddev 138085.453125 -> 138085.453125

Those changes will now be accepted, which is good because those
OSDs will improve, even though the sum of squared deviations does
not.

Also fix some debug outputs.

Improved OSDMap balancing "score"

Currently we score how balanced we are using a sum of the absolute
deviations, treating small and large OSDs equally. This strongly
under values relatively small PGs, which we really need to be
accurately balancing with priority to avoid them getting overfull.

Consider this scenario:

  • osd.0 weight 100.0 pgs 100 target 100 -> deviation 0
  • osd.1 weight 1.0 pgs 2 target 1 -> deviation 1

The tiny OSD osd.1 is 2x overfull -- we should prioritize moving
one of its PGs to osd.0, which would only go 1% overfull. The
new values would become:

  • osd.0 weight 100.0 pgs 101 target 100 -> deviation 1
  • osd.1 weight 1.0 pgs 1 target 1 -> deviation 0

With the "stddev" calculation, we have these before/after scores:

  • stddev = 0*0 + 1*1 = 1
  • new_stddev = 1*1 + 0*0 = 1

(Until recently, that change would be rejected because it does not
decrease the stddev).

To improve things, we calculate a bal_score which incorporates the
relative deviation and OSD weights.

Here's the new scores for the example change above:

  • bal_score = (100/100 - 1)^2 * 100 + (2/1 - 1)^2 * 1 = 2
  • new_bal_score = (101/100 - 1)^2 * 100 + (1/1 - 1)^2 * 1 = 0.01

With this new approach the change is highly favourable, as it
should be.

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)

@dvanders
dvanders requested a review from a team as a code owner August 23, 2025 01:35
@github-actions github-actions Bot added the core label Aug 23, 2025
@dvanders
dvanders requested a review from ljflores August 23, 2025 01:35
@dvanders
dvanders requested a review from JoshSalomon August 23, 2025 01:40
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs for another 30 days.
If you are a maintainer or core committer, please follow-up on this pull request to identify what steps should be taken by the author to move this proposed change forward.
If you are the author of this pull request, thank you for your proposed contribution. If you believe this change is still appropriate, please ensure that any feedback has been addressed and ask for a code review.

@github-actions github-actions Bot added the stale label Oct 22, 2025

@JoshSalomon JoshSalomon 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 - both cases are important for relatively complex balancing scenarios.
@ljflores - if it looks OK to you, please merge.

@JoshSalomon JoshSalomon removed the stale label Oct 22, 2025
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs for another 30 days.
If you are a maintainer or core committer, please follow-up on this pull request to identify what steps should be taken by the author to move this proposed change forward.
If you are the author of this pull request, thank you for your proposed contribution. If you believe this change is still appropriate, please ensure that any feedback has been addressed and ask for a code review.

@github-actions github-actions Bot added the stale label Dec 21, 2025
@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because there has been no activity for 90 days. Please feel free to reopen this pull request (or open a new one) if the proposed change is still appropriate. Thank you for your contribution!

@github-actions github-actions Bot closed this Jan 20, 2026
@ljflores ljflores reopened this Jan 20, 2026
@github-actions github-actions Bot removed the stale label Jan 20, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs for another 30 days.
If you are a maintainer or core committer, please follow-up on this pull request to identify what steps should be taken by the author to move this proposed change forward.
If you are the author of this pull request, thank you for your proposed contribution. If you believe this change is still appropriate, please ensure that any feedback has been addressed and ask for a code review.

@github-actions github-actions Bot added the stale label Mar 21, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because there has been no activity for 90 days. Please feel free to reopen this pull request (or open a new one) if the proposed change is still appropriate. Thank you for your contribution!

@github-actions github-actions Bot closed this Apr 20, 2026
@JoshuaGabriel JoshuaGabriel reopened this Jul 16, 2026
@JoshuaGabriel

Copy link
Copy Markdown
Contributor

jenkins test docs

@github-actions github-actions Bot removed the stale label Jul 16, 2026
@dvanders
dvanders force-pushed the dvanders_calc_pg_upmaps branch from aa87ed4 to e473526 Compare August 12, 2026 22:57
dvanders and others added 3 commits August 12, 2026 16:23
In some cases the balancer can stall out, unable to find any
changes that will help overfull or underfull OSDs get closer
to their target num PGs.

Here's an example from the wild. These changes are not accepted
because the overall stddev does not appear to be decreasing:

  will try dropping existing remapping pair 264 -> 137 which remapped 24.f9d into overfull osd.137
  existing pg_upmap_items [766,799,264,137] remapped 24.f9d into overfull osd.137, new_pg_upmap_items now [766,799]
  stddev 138085.453125 -> 138085.453125

  will try dropping existing remapping pair 496 -> 637 which remapped 24.5ca out from underfull osd.496
  existing pg_upmap_items [729,790,496,637] remapped 24.5ca out from underfull osd.496, new_pg_upmap_items now [729,790]
  stddev 138085.453125 -> 138085.453125

Those changes will now be accepted, which is good because those
OSDs will improve, even though the sum of squared deviations does
not.

Note that a change scoring exactly equal still moves data, so this
trades some churn for the ability to escape a plateau. The two
following patches make the score both more meaningful and precise
enough to tell many of these cases apart on their own merits.

Signed-off-by: Dan van der Ster <[email protected]>
Currently we score how balanced we are using a sum of the absolute
deviations, treating small and large OSDs equally. This strongly
under values relatively small PGs, which we really need to be
accurately balancing with priority to avoid them getting overfull.

Consider this scenario:

  osd.0 weight 100.0 pgs 100 target 100 -> deviation 0
  osd.1 weight 1.0   pgs 2   target 1   -> deviation 1

The tiny OSD osd.1 is 2x overfull -- we should prioritize moving
one of its PGs to osd.0, which would only go 1% overfull. The
new values would become:

  osd.0 weight 100.0 pgs 101 target 100 -> deviation 1
  osd.1 weight 1.0   pgs 1   target 1   -> deviation 0

With the "stddev" calculation, we have these before/after scores:

  stddev = 0*0 + 1*1 = 1
  new_stddev = 1*1 + 0*0 = 1

(Until recently, that change would be rejected because it does not
decrease the stddev).

To improve things, we calculate a bal_score which incorporates the
relative deviation and OSD weights.

Here's the new scores for the example change above:

  bal_score = (100/100 - 1)^2 * 100 + (2/1 - 1)^2 * 1 = 1
  new_bal_score = (101/100 - 1)^2 * 100 + (1/1 - 1)^2 * 1 = 0.01

With this new approach the change is highly favourable, as it
should be.

Note that this changes only how an already proposed change is
scored. Deciding whether to run at all, which OSDs are overfull or
underfull, and which remap to propose are all still done in absolute
PG deviations against max_deviation, so a small OSD like osd.1 above
only gets this attention once some OSD in the tree is more than
max_deviation PGs away from its target.

Signed-off-by: Dan van der Ster <[email protected]>
bal_score is a sum taken over every OSD in the crush tree, while a
proposed change modifies just two of its terms. In float the delta
can land below the ULP of the total, so two genuinely different
mappings compare equal and we cannot tell an improvement from a
regression.

That is what the stalled balancer in "accept more proposed changes"
was really hitting. try_drop_remap_overfull only proposes a drop
when osd_deviation[um_from] + 1 < osd_deviation[osd], and for a sum
of squared deviations that gives

  delta = 2 * (dev_from - dev_to + 1) < 0

so the change was strictly improving; it printed as unchanged only
because float at a magnitude of 138085 has a ULP of ~0.016.

The weighted score reduces the magnitude of the sum considerably,
but it does not remove the failure mode. Simulating a 3000 OSD tree
with ~100 PGs per OSD and a drop which satisfies the guard above:

  float  595.903381 -> 595.903381   delta +0            tie
  double 595.902895 -> 595.902893   delta -1.98364e-06  accepted

While here:

- Derive the relative deviation from the absolute one rather than
  computing pgs / target - 1, which loses precision to cancellation
  when pgs is close to target.

- Log the delta next to the two totals. The totals are printed with
  the stream's default 6 significant digits, which is exactly enough
  to hide the improvements of interest here.

- Assert target > 0 in calc_deviations, which now divides by it.
  Both callers already assert this before their own use.

- Rename the parameter in the header declaration to match the
  definition.

Signed-off-by: Dan van der Ster <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@SrinivasaBharath

Copy link
Copy Markdown
Contributor

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

@NitzanMordhai

Copy link
Copy Markdown
Contributor

@SrinivasaBharath
SrinivasaBharath merged commit b789b5e into ceph:main Sep 18, 2026
7 checks passed
@dvanders
dvanders deleted the dvanders_calc_pg_upmaps branch September 18, 2026 01:43
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.

6 participants