Skip to content

osd/OSDMap: don't abort on float rounding in read balancer - #71772

Merged
ljflores merged 1 commit into
ceph:mainfrom
lumir-sliva:wip-80443-read-balancer-assert
Sep 23, 2026
Merged

ljflores merged 1 commit into
ceph:mainfrom
lumir-sliva:wip-80443-read-balancer-assert

Conversation

@lumir-sliva

Copy link
Copy Markdown
Contributor

With the balancer in upmap-read or read mode, the active mgr can abort in
the balancer thread at

FAILED ceph_assert(fabs(distribution_sum_desired - pool->get_pg_num()) < 0.01)

in OSDMap::calc_desired_primary_distribution_simple(). Each standby mgr then
aborts the same way on its own next balancer tick, so the cluster loses all mgr
daemons in a rotating crash loop until the balancer mode is changed. This
happens on perfectly healthy clusters (all PGs active+clean, every OSD at
primary affinity 1) once a pool is large enough.

The asserted quantity is an algebraic identity: the per-OSD desired primary
counts are scaled so that they sum to pg_num, and the assert merely re-sums
them. The re-summation, however, used a float accumulator, which rounds once
per OSD, while the tolerance stayed a fixed absolute 0.01. The accumulated
rounding error grows with both pg_num and OSD count, so the check becomes
progressively unsatisfiable as clusters grow. The sibling
ceph_assert(factor >= 1.0) is fragile the same way: with uniform primary
affinity 1 the exact factor is 1.0, and rounding can push the computed value
just below it.

Fix: compute the stretch factor and both accumulators in double, tolerate tiny
negative rounding on the factor before clamping it back to 1.0, and scale the
sum tolerance with pg_num. The two new regression tests use OSD count / pg_num
combinations (500/32768 and 100/131072) that deterministically trip the old
asserts.

Validated beyond the unit tests on a 60-OSD vstart cluster with a size-3
pool at pg_num 65536: on an osdmap that trips the old assert, enabling
balancer mode upmap-read aborts the active mgr within one balancer tick
with the reported backtrace, while the patched mgr on the same map
completes the optimization and applies the pg_upmap_primary entries.

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

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)
    • Includes unit test(s)
    • Includes integration test(s)
    • Includes bug reproducer
    • No tests

@lumir-sliva
lumir-sliva requested a review from a team as a code owner September 14, 2026 15:19
@github-actions

Copy link
Copy Markdown

Thank you for your contribution. Since you are not yet a member of the Ceph organization with write permissions on ceph/ceph.git, our CI will not automatically run. Any member of the Ceph organization may label this PR ci-approved to allow Jenkins CI jobs to run.

@ljflores

ljflores commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

In progress reviewing. I requested the osdmap on the ticket for further verification of the fix.

@lumir-sliva

Copy link
Copy Markdown
Contributor Author

Attached the osdmap from my vstart reproduction to the tracker, with the osdmaptool one-liner that aborts on main and exits 0 with this branch.

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

Looks good! I added some suggestions about replacing the asserts with log messages.

Comment thread src/osd/OSDMap.cc
Comment on lines +5533 to +5537
// factor is mathematically >= 1.0 (distribution_sum <= pg_num), but
// float rounding of the per-OSD terms can push distribution_sum a
// fraction above pg_num; clamp instead of asserting on rounding noise
ceph_assert(factor >= 1.0 - 1e-6);
factor = std::max(factor, 1.0);

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.

If distribution_sum ends up being slightly more than pg_num (ideally distribution_sum <= pg_num) due to float rounding, then factor ends up being slightly less than 1, when it should always be >= 1.

Change the assert to be tolerant of any rounding (even though floats were already replaced by doubles) so the factor is confirmed >= slightly less than 1. THEN pin the factor to either 1.0 (no stretch needed) or the larger computed value (primary_affinity was less than 1 for some OSDs, so we need to stretch/increase the primary PG load on the other OSDs).

ACK

@ljflores ljflores Sep 16, 2026 •

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 think it would even be OK to remove the assert altogether and change it into an error log message at debug 10. Then the function can return -EINVAL. The original assumption was that this value would always be true, but given the limitations with C++ primitive data types, I think a log message is appropriate.

The worst that would happen in this case is that the error message would be logged, and the pool would be skipped.

Comment thread src/osd/OSDMap.cc
Comment on lines +5530 to +5531
double factor = (double)pool->get_pg_num() / distribution_sum;
double distribution_sum_desired = 0.0;

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.

Change these values to doubles to lessen float rounding impacts on larger PGs.

ACK

Comment thread src/osd/OSDMap.cc
Comment on lines +5538 to +5542
for (auto & [osd, osd_primary_count] : desired_primary_distribution) {
double scaled = (double)osd_primary_count * factor;
osd_primary_count = (float)scaled;
distribution_sum_desired += scaled;
}

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.

Instead of iterating through desired_primary_distribution in const/read-only mode, only to end up changing the values within the loop, this area removes const and uses osd_primary_count from the for loop to update desired_primary_distribution. It also names the calculation beforehand scaled. IIUC this is all just for better readability but not really addressing the bug (aside from float-->double changes).

ACK

Comment thread src/osd/OSDMap.cc Outdated
Comment on lines +5546 to +5547
ceph_assert(fabs(distribution_sum_desired - pool->get_pg_num()) <
std::max(0.01, (double)pool->get_pg_num() * 1e-4));

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.

Adds more tolerance so the absolute value. ACK

Same with the above assert; I think this is appropriate to change into a debug 10 error message if the condition is not met.

}
}

TEST_F(OSDMapTest, read_balance_large_pg_num) {

@ljflores ljflores Sep 16, 2026 •

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.

Tests look good. In addition, verified against the osdmaptool with a map provided in the tracker (this tripped the assert on the old code):

[lflores@folio01 build]$ ./bin/osdmaptool ~/osdmap-60osd-65536pg-pgp60800.bin --read /tmp/read.out --read-pool bigpool
./bin/osdmaptool: osdmap file '/home/lflores/osdmap-60osd-65536pg-pgp60800.bin'
writing upmap command output to: /tmp/read.out
 
---------- BEFORE ------------ 
 osd.0 | primary affinity: 1 | number of prims: 1040
 osd.1 | primary affinity: 1 | number of prims: 1092
 osd.2 | primary affinity: 1 | number of prims: 1109
 osd.3 | primary affinity: 1 | number of prims: 1132
 osd.4 | primary affinity: 1 | number of prims: 1084
 osd.5 | primary affinity: 1 | number of prims: 1055
 osd.6 | primary affinity: 1 | number of prims: 1100
 osd.7 | primary affinity: 1 | number of prims: 1144
 osd.8 | primary affinity: 1 | number of prims: 1168
 osd.9 | primary affinity: 1 | number of prims: 1078
 osd.10 | primary affinity: 1 | number of prims: 1077
 osd.11 | primary affinity: 1 | number of prims: 1081
 osd.12 | primary affinity: 1 | number of prims: 1101
 osd.13 | primary affinity: 1 | number of prims: 1134
 osd.14 | primary affinity: 1 | number of prims: 1063
 osd.15 | primary affinity: 1 | number of prims: 1069
 osd.16 | primary affinity: 1 | number of prims: 1109
 osd.17 | primary affinity: 1 | number of prims: 1123
 osd.18 | primary affinity: 1 | number of prims: 1073
 osd.19 | primary affinity: 1 | number of prims: 1088
 osd.20 | primary affinity: 1 | number of prims: 1137
 osd.21 | primary affinity: 1 | number of prims: 1073
 osd.22 | primary affinity: 1 | number of prims: 1105
 osd.23 | primary affinity: 1 | number of prims: 1088
 osd.24 | primary affinity: 1 | number of prims: 1155
 osd.25 | primary affinity: 1 | number of prims: 1106
 osd.26 | primary affinity: 1 | number of prims: 1066
 osd.27 | primary affinity: 1 | number of prims: 1081
 osd.28 | primary affinity: 1 | number of prims: 1086
 osd.29 | primary affinity: 1 | number of prims: 1140
 osd.30 | primary affinity: 1 | number of prims: 1047
 osd.31 | primary affinity: 1 | number of prims: 1059
 osd.32 | primary affinity: 1 | number of prims: 1111
 osd.33 | primary affinity: 1 | number of prims: 1072
 osd.34 | primary affinity: 1 | number of prims: 1058
 osd.35 | primary affinity: 1 | number of prims: 1094
 osd.36 | primary affinity: 1 | number of prims: 1080
 osd.37 | primary affinity: 1 | number of prims: 1072
 osd.38 | primary affinity: 1 | number of prims: 1102
 osd.39 | primary affinity: 1 | number of prims: 1136
 osd.40 | primary affinity: 1 | number of prims: 1121
 osd.41 | primary affinity: 1 | number of prims: 1062
 osd.42 | primary affinity: 1 | number of prims: 1136
 osd.43 | primary affinity: 1 | number of prims: 1073
 osd.44 | primary affinity: 1 | number of prims: 1090
 osd.45 | primary affinity: 1 | number of prims: 1063
 osd.46 | primary affinity: 1 | number of prims: 1065
 osd.47 | primary affinity: 1 | number of prims: 1087
 osd.48 | primary affinity: 1 | number of prims: 1082
 osd.49 | primary affinity: 1 | number of prims: 1103
 osd.50 | primary affinity: 1 | number of prims: 1064
 osd.51 | primary affinity: 1 | number of prims: 1072
 osd.52 | primary affinity: 1 | number of prims: 1119
 osd.53 | primary affinity: 1 | number of prims: 1093
 osd.54 | primary affinity: 1 | number of prims: 1093
 osd.55 | primary affinity: 1 | number of prims: 1133
 osd.56 | primary affinity: 1 | number of prims: 1143
 osd.57 | primary affinity: 1 | number of prims: 1044
 osd.58 | primary affinity: 1 | number of prims: 1048
 osd.59 | primary affinity: 1 | number of prims: 1057
 
read_balance_score of 'bigpool': 1.07


---------- AFTER ------------ 
 osd.0 | primary affinity: 1 | number of prims: 1101
 osd.1 | primary affinity: 1 | number of prims: 1094
 osd.2 | primary affinity: 1 | number of prims: 1091
 osd.3 | primary affinity: 1 | number of prims: 1107
 osd.4 | primary affinity: 1 | number of prims: 1068
 osd.5 | primary affinity: 1 | number of prims: 1116
 osd.6 | primary affinity: 1 | number of prims: 1095
 osd.7 | primary affinity: 1 | number of prims: 1089
 osd.8 | primary affinity: 1 | number of prims: 1101
 osd.9 | primary affinity: 1 | number of prims: 1107
 osd.10 | primary affinity: 1 | number of prims: 1111
 osd.11 | primary affinity: 1 | number of prims: 1071
 osd.12 | primary affinity: 1 | number of prims: 1077
 osd.13 | primary affinity: 1 | number of prims: 1105
 osd.14 | primary affinity: 1 | number of prims: 1085
 osd.15 | primary affinity: 1 | number of prims: 1073
 osd.16 | primary affinity: 1 | number of prims: 1087
 osd.17 | primary affinity: 1 | number of prims: 1118
 osd.18 | primary affinity: 1 | number of prims: 1077
 osd.19 | primary affinity: 1 | number of prims: 1119
 osd.20 | primary affinity: 1 | number of prims: 1110
 osd.21 | primary affinity: 1 | number of prims: 1102
 osd.22 | primary affinity: 1 | number of prims: 1094
 osd.23 | primary affinity: 1 | number of prims: 1088
 osd.24 | primary affinity: 1 | number of prims: 1120
 osd.25 | primary affinity: 1 | number of prims: 1125
 osd.26 | primary affinity: 1 | number of prims: 1082
 osd.27 | primary affinity: 1 | number of prims: 1084
 osd.28 | primary affinity: 1 | number of prims: 1110
 osd.29 | primary affinity: 1 | number of prims: 1081
 osd.30 | primary affinity: 1 | number of prims: 1074
 osd.31 | primary affinity: 1 | number of prims: 1077
 osd.32 | primary affinity: 1 | number of prims: 1094
 osd.33 | primary affinity: 1 | number of prims: 1069
 osd.34 | primary affinity: 1 | number of prims: 1077
 osd.35 | primary affinity: 1 | number of prims: 1094
 osd.36 | primary affinity: 1 | number of prims: 1066
 osd.37 | primary affinity: 1 | number of prims: 1072
 osd.38 | primary affinity: 1 | number of prims: 1098
 osd.39 | primary affinity: 1 | number of prims: 1110
 osd.40 | primary affinity: 1 | number of prims: 1067
 osd.41 | primary affinity: 1 | number of prims: 1090
 osd.42 | primary affinity: 1 | number of prims: 1114
 osd.43 | primary affinity: 1 | number of prims: 1099
 osd.44 | primary affinity: 1 | number of prims: 1070
 osd.45 | primary affinity: 1 | number of prims: 1078
 osd.46 | primary affinity: 1 | number of prims: 1071
 osd.47 | primary affinity: 1 | number of prims: 1096
 osd.48 | primary affinity: 1 | number of prims: 1095
 osd.49 | primary affinity: 1 | number of prims: 1105
 osd.50 | primary affinity: 1 | number of prims: 1099
 osd.51 | primary affinity: 1 | number of prims: 1073
 osd.52 | primary affinity: 1 | number of prims: 1113
 osd.53 | primary affinity: 1 | number of prims: 1101
 osd.54 | primary affinity: 1 | number of prims: 1069
 osd.55 | primary affinity: 1 | number of prims: 1103
 osd.56 | primary affinity: 1 | number of prims: 1104
 osd.57 | primary affinity: 1 | number of prims: 1094
 osd.58 | primary affinity: 1 | number of prims: 1091
 osd.59 | primary affinity: 1 | number of prims: 1085
 
read_balance_score of 'bigpool': 1.03


num changes: 967

calc_desired_primary_distribution_simple() scales the per-OSD desired
primary counts so that they sum to pg_num, then re-sums them into a
float and asserts the total is within 0.01 of pg_num. The quantity is
an algebraic identity, but the float accumulator rounds once per OSD
while the tolerance stays absolute, so the accumulated rounding error
exceeds 0.01 on large pools and the assert aborts the active mgr's
balancer thread. With the balancer in upmap-read or read mode every
standby mgr aborts in turn on its next tick, leaving the cluster with
no mgr until the balancer mode is changed.

At pg_num 65536 a float only resolves 0.0078125, so the 0.01 tolerance
is barely more than one rounding step: on a 60-OSD pool of that size
the re-sum lands at 65535.98828125 while distribution_sum and factor
are still exactly 65536 and 1.0.

The sibling ceph_assert(factor >= 1.0) is fragile the same way: with
uniform primary affinity 1 the exact factor is 1.0, and rounding in
distribution_sum can push the computed value just below it.

Compute the stretch factor and both accumulators in double, tolerate
tiny negative rounding on factor before clamping it back to 1.0, and
scale the sum tolerance with pg_num. Neither condition is worth
aborting the process over: when a check still fails, log it at debug
level 10 and return -EINVAL, which the caller already treats as "skip
this pool" for the current balancer round.

The two new tests use OSD count / pg_num combinations (500/32768 and
100/131072) that deterministically trip the old asserts.

Fixes: https://tracker.ceph.com/issues/80443
Reported-by: Armsby Li
Signed-off-by: Lumir Sliva <[email protected]>
@lumir-sliva

Copy link
Copy Markdown
Contributor Author

I think it would even be OK to remove the assert altogether and change it into an error log message at debug 10. Then the function can return -EINVAL.

Done, both of them. Each check now logs at debug 10 with the pool name, the offending sum and the tolerance, and returns -EINVAL. calc_desired_primary_distribution() already treats any negative rc as "skip this pool", so the worst case is one pool sitting out a balancer round. Kept the double conversion and the clamp to 1.0 for the legitimate rounding case.

Also the exact numbers from osdmap-60osd-65536pg-pgp60800.bin on the old code, printed right before the asserts:

  • distribution_sum = 65536.0000000000, factor = 1.0000000000 (both exact)
  • distribution_sum_desired = 65535.9882812500, diff = -0.0117187500

A float at magnitude 65536 has a resolution of 0.0078125, so the old 0.01 tolerance was barely one rounding step. That's why it trips with every OSD at primary affinity 1.

Re-verified with the new commit: unittest_osdmap 44/44, and the osdmaptool replay on that map exits 0 with 967 changes, same as your run.

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath17-testing-20260919.063401-main.

@NitzanMordhai

Copy link
Copy Markdown
Contributor

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.

4 participants