Skip to content

osd/PeeringState: Record the departing primary's rebuild segment on handover & survive a PG split. - #71439

Merged
sseshasa merged 2 commits into
ceph:mainfrom
sseshasa:wip-fix-pg-vulnerability-primary-handoff
Sep 9, 2026
Merged

sseshasa merged 2 commits into
ceph:mainfrom
sseshasa:wip-fix-pg-vulnerability-primary-handoff

Conversation

@sseshasa

@sseshasa sseshasa commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Changes:

  1. Whenever a PG's primary changes hands while it is vulnerable, the in-memory
    rebuild-time latch was discarded incorrectly by start_peering_interval()'s
    reset with no recording step at all. Every OSD that touched a multi-handover
    PG ended up with pg_rebuild_duration avgcount=0 despite real, confirmed
    recovery activity, while the one PG whose primary never changed hands recorded
    the correct full duration. This is not a rare corner case: it reproduces
    whenever the failed OSD was a primary for the affected PG, which is the common
    case for any OSD restart, not the exception.

    This is a scoped mitigation, for the interim solution where the departing
    primary's rebuild segment is now recorded on handover.

  2. osd_perf_counters.cc: Rename the exposed pg_rebuild_duration perf counter
    to pg_vulnerability_duration and extend the description string. This only
    changes the externally-visible Prometheus/perf-schema facing name and its
    description.

    Rationale: The counter has always measured a vulnerability window (onset
    of redundancy loss to resolution), not literal rebuild/data-movement time.
    This accounts for the case where a recorded duration includes several
    minutes of a PG sitting undersized+degraded with zero recovery activity
    possible (no spare OSD) before any data moved at all. However, note that in
    this scenario if no data was moved at all and the PG returns to active+clean,
    the window is excluded and not recorded (see caveats below).

  3. A PG split (split_into()), triggered by a pg_num increase during map advance
    should not leave a child PG's rebuild-stats latch fields (rebuild_start_time,
    rebuild_base_recovered, rebuild_had_redundancy_loss) at their default, unarmed
    values regardless of whether the parent was already latched as vulnerable at
    split time. Leaving it at default would cause it to understate its true
    duration or vanish from delta_recovered's filter.

Fixes: https://tracker.ceph.com/issues/80117
Signed-off-by: Sridhar Seshasayee [email protected]

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.

…andover

Whenever a PG's primary changes hands while it is vulnerable, the in-memory
rebuild-time latch was discarded incorrectly by start_peering_interval()'s
reset with no recording step at all. Every OSD that touched a multi-handover
PG ended up with pg_rebuild_duration avgcount=0 despite real, confirmed
recovery activity, while the one PG whose primary never changed hands recorded
the correct full duration.  This is not a rare corner case: it reproduces
whenever the failed OSD was a primary for the affected PG, which is the common
case for any OSD restart, not the exception.

This is a scoped mitigation, for the interim solution where the departing
primary's rebuild segment is now recorded on handover.

Changes:

 - New PeeringState::try_record_rebuild_segment(end_time, reason): factored
   out of prepare_stats_for_publish()'s existing "reached clean" record
   branch (no behavior change there beyond an added trailing "reason=" field
   in the log line.

- start_peering_interval(): When an OSD is losing the primary role
  (was_old_primary && !is_primary()) while rebuild_start_time is still
  armed, call try_record_rebuild_segment() before the existing unconditional
  reset. The "gaining primary" side needs no change which already reliably
  latches the fresh primary while mid-rebuild. Only the losing-primary side
  was silently dropping collected stats data.

- osd_perf_counters.cc: Rename the exposed pg_rebuild_duration perf counter
  to pg_vulnerability_duration and extend the description string. This only
  changes the externally-visible Prometheus/perf-schema facing name and its
  description.

  Rationale: The counter has always measured a vulnerability window (onset
  of redundancy loss to resolution), not literal rebuild/data-movement time.
  This accounts for the case where a recorded duration includes several
  minutes of a PG sitting undersized+degraded with zero recovery activity
  possible (no spare OSD) before any data moved at all. However, note that in
  this scenario if no data was moved at all and the PG returns to active+clean,
  the window is excluded and not recorded (see caveats below).

  Known caveats:
  - excludes windows where no data was ever recovered or lost (empty PGs), and
  - a primary handover mid-episode is recorded as a separate sample per OSD
    segment, so avgcount can exceed the true incident count.

- TestPeeringState.cc:
  a) RebuildStatsLatchClearedOnRoleChange renamed to
     RebuildStatsRecordsSegmentOnRoleChange and rewritten -- it previously
     asserted count==0 after a primary handover as the *correct* behavior;
     that assertion is now the opposite (count==1, sum>0), reflecting the
     deliberate behavior change, not a regression. The other five
     RebuildStats* tests are unaffected.

  b) Adds RebuildStatsMultiHopHandoverRecordsEachSegment: a PG whose primary
     changes twice while continuously vulnerable (OSD 0 -> OSD 7 -> OSD 8, PG
     degraded throughout, recovery only completing under the final primary)
     must record three separate partial segments -- one per primary -- rather
     than losing the whole episode.

- osd-recovery-stats.sh:
  a) Modify the recoverystate_perf.pg_rebuild_duration references to
     recoverystate_perf.pg_vulnerability_duration in an existing test.
  b) Add TEST_rebuild_perf_multihop_handover and
     TEST_rebuild_perf_empty_pg_not_counted to cover the multi-hop primary
     handover and empty PG not recorded scenarios respectively.

Fixes: https://tracker.ceph.com/issues/80117
Signed-off-by: Sridhar Seshasayee <[email protected]>
A PG split (split_into()), triggered by a pg_num increase during map advance
should not leave a child PG's rebuild-stats latch fields (rebuild_start_time,
rebuild_base_recovered, rebuild_had_redundancy_loss) at their default, unarmed
values regardless of whether the parent was already latched as vulnerable at
split time. Leaving it at default would cause it to understate its true
duration or vanish from delta_recovered's filter.

changes:
 1. split_into(): Make the child inherit the parent's latch.

 2. finish_split_stats(): Re-anchor rebuild_base_recovered to the just-applied,
    already-redistributed value of num_objects_recovered. This preserves the
    true rebuild_start_time/duration while ensuring delta_recovered only ever
    reflects genuine recovery happening after this point.

 3. Add a standalone test called TEST_rebuild_perf_pg_split_inherits_latch()
    to exercise the split PG logic and verify that the latch is inherited by
    the child PG (confirm no latch was fired) and that the final child PG's
    recorded duration reflects the time that it alone spent rebuilding the data.

Signed-off-by: Sridhar Seshasayee <[email protected]>
@sseshasa
sseshasa requested a review from a team as a code owner August 31, 2026 15:38
Comment thread src/osd/PeeringState.cc
Comment on lines +4593 to +4603
* 5. A primary handover mid-vulnerability window closes out and records
* the departing OSD's own segment instead of silently discarding it.
* This means a PG that changes primary N times while continuously
* vulnerable is recorded as N separate rebuild_duration samples
* (whose durations sum to roughly, not exactly, the true total episode
* length) rather than one -- a real, understood, and accepted
* trade-off for this interim solution: avgcount can overcount the true
* number of distinct redundancy-loss incidents whenever a handover
* occurs mid-episode, though avgtime/the summed exposure duration stay
* roughly accurate. A PG whose primary never changes is entirely
* unaffected and continues to produce exactly one sample.

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.

ACK.

rs_perf.add_u64_counter(rs_merge_log_stats_invalidated, "merge_log_stats_invalidated", "Number of times pg stats received invalidations during merging of log entries");
rs_perf.add_time_avg(rs_pg_rebuild_duration, "pg_rebuild_duration",
"Average PG rebuild duration on this OSD (primary role only)",
rs_perf.add_time_avg(rs_pg_rebuild_duration, "pg_vulnerability_duration",

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.

We're replacing one perf counter with another. I assume there is no external dependency on 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.

This is a new counter created in the interim solution for consumption by Prometheus. In this PR, only the externally exposed name is changed to reflect it's true purpose. There's no other external dependency on this. Prometheus will consume the modified exposed name i.e. pg_vulnerability_duration. The full solution too will retain the same name.

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

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

@amathuria

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