Skip to content

osd/SnapMapper: stop rescanning purged_snaps per mapping key - #72544

Merged
ronen-fr merged 1 commit into
ceph:mainfrom
Antek74:osd-snapmapper-linear-purged-scrub
Nov 11, 2026
Merged

ronen-fr merged 1 commit into
ceph:mainfrom
Antek74:osd-snapmapper-linear-purged-scrub

Conversation

@Antek74

@Antek74 Antek74 commented Oct 9, 2026 •

Copy link
Copy Markdown

Scrubber::run() opened a fresh omap_iterate over purged_snaps,
from its start, for every mapping key: O(M*P). On snapshot-heavy
OSDs this pinned a core for days.

Collect the purged_snaps rows once up front, sort them numerically by
(pool, begin), and merge overlapping and adjacent rows of one pool
into disjoint ranges. Then each mapping key needs only one binary
search: O(M log P) instead of O(M*P).

The numeric sort also fixes missed strays: with pools ordered as
decimal strings, the old walk could stop at a pool-20 row and never
reach pool 3.

The sort is needed because the on-disk key order is not numeric in
the pool: the pool id is a decimal string, so pool 20 sorts before
pool 3. The merge is needed because the writer can leave rows that
overlap or touch: record_purged_snaps() merges a new interval only
with rows already on disk, so intervals carried in one call do not
see each other. Once the ranges are disjoint and sorted, the last
row that starts at or before the snap is the only one that can
cover it.

Adds unittest_scrubber_run, a unit test for Scrubber::run(): it
builds a SnapMapper over a MemStore and fills it through the same
writer methods ceph-osd uses (record_purged_snaps(), add_oid()),
then checks every stray the scrub reports.

Fixes: https://tracker.ceph.com/issues/81592
Fixes: https://tracker.ceph.com/issues/81798

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.

@Antek74
Antek74 requested a review from a team as a code owner October 9, 2026 16:40
@github-actions

github-actions Bot commented Oct 9, 2026

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.

@rzarzynski

Copy link
Copy Markdown
Contributor

Hi @ronen-fr! Would mind a quick look?

@ronen-fr

Copy link
Copy Markdown
Contributor

Hi @ronen-fr! Would mind a quick look?

will do. Interesting premise

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

Thanks for catching this issue.
Please open a tracker for the scenario where entries are missed.
Apart from that: the production code is almost there. See some comments.
The test needs some work.

Comment thread src/osd/SnapMapper.cc Outdated
// ADJACENT rows), so maxend[] carries the max end of the same-pool
// rows up to each row: every same-pool row with begin <= snap may
// cover the snap, not just the last one.
std::vector<std::tuple<int64_t, snapid_t, snapid_t>> purged;

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.

Please declare a structure, with meaningful member names, instead of a tuple.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done, the rows are now a purged_range_t struct with pool/begin/end.

Comment thread src/osd/SnapMapper.cc Resolved
Comment thread src/osd/SnapMapper.cc Resolved
Comment thread src/test/test_scrubber_run.cc Outdated
// Build-day integration copy of tests/test_scrubber_run_draft.cc
// (draft kept intact in the lab pack; fixes live HERE only).
//
// Fixes applied vs the draft, all verified against stock v20.2.1 sources:

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.

There are many development-time details here (like this line) and below.
A cleanup pass is required.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Stripped, the header now says what the test does, nothing about how it got here.

Comment thread src/test/CMakeLists.txt Resolved
Comment thread src/test/test_scrubber_run.cc Resolved
Comment thread src/test/test_scrubber_run.cc Outdated

// one purged row byte-identical to what make_purged_snap_key()/
// make_purged_snap_key_value() (SnapMapper.cc) write — bypasses
// record_purged_snaps()'s adjacent-join, which can never leave the

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.

The comment seems to be wrong

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated to reflect the rewrite.

Comment thread src/test/test_scrubber_run.cc Resolved
@ronen-fr

Copy link
Copy Markdown
Contributor

@rzarzynski - seems that the reason we do not have this problem in Crimson, is that we didn't yet get to implementing the whole 'purge left-over snaps' loop. Is it just a matter of priorities?

@rzarzynski

Copy link
Copy Markdown
Contributor

@ronen-fr: I bet so.

@Antek74

Antek74 commented Oct 15, 2026

Copy link
Copy Markdown
Author

@ronen-fr thanks. I'm working through all the comments now. I'll create the missed-entries scenario in its own tracker, as you suggested.

@ronen-fr

Copy link
Copy Markdown
Contributor

References: https://tracker.ceph.com/issues/81798 can be 'Fixes:', too. You do fix that one.

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

Almost there. Some minor nits and fixes.
If you can finish the next round of changes before I go on PTO on Tuesday - I'll try to reread it with priority.

Comment thread src/osd/SnapMapper.cc Resolved
Comment thread src/osd/SnapMapper.cc Outdated
// can cover it
auto it = std::upper_bound(
purged.begin(), purged.end(),
std::pair(mapping.hoid.pool, mapping.snap),

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.

This would also work:
auto it = std::upper_bound(purged.begin(), purged.end(),
purged_range_t{mapping.hoid.pool, mapping.snap, 0});

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 43ce7a9.

Comment thread src/test/test_scrubber_run.cc Outdated
m["QSN_garbage"] = garbage;
raw_omap_set(purged_snaps_hoid, std::move(m));
expect_strays({stray_of(1, 15), stray_of(3, 35)}, scrub());
} No newline at end of file

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.

Note the '(-)' marker: missing a trailing \n

Comment thread src/osd/SnapMapper.cc Outdated
pool, mapping.snap, mapping.hoid.get_hash(),
shard
mapping.hoid.pool, mapping.snap,
mapping.hoid.get_hash(), shard

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.

use spaces for the indentation

Comment thread src/osd/SnapMapper.cc Outdated
derr << "omap_iterate() on purged_snaps_hoid returns " << r << dendl;
return;
}
std::sort(purged.begin(), purged.end());

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.

switch order with the is-empty check.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 43ce7a9.

Comment thread src/test/test_scrubber_run.cc Outdated
using stray_tuple_t =
std::tuple<int64_t, snapid_t, uint32_t, shard_id_t>;

// one purged row as it sits on disk: key (by end-1) and the decoded

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.

no 'key'

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Dropped in 43ce7a9. The comment described an earlier draft of the struct.

Comment thread src/test/test_scrubber_run.cc Outdated

#include <algorithm>
#include <map>
#include <set>

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.

used?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed in 43ce7a9.

Comment thread src/test/CMakeLists.txt Outdated
target_link_libraries(ceph_test_scrubber_run osd global ${BLKID_LIBRARIES})

install(TARGETS
ceph_test_scrubber_run

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.

Important note from Claude (which fixed what I thought to be true):
You must not 'install' the new test. Here is the explanation:

Good questions — I checked, and my nit was understated. It's not dead weight; it would break the package build.

What add_ceph_unittest() actually does

cmake/modules/AddCephTest.cmake:85-93 calls add_ceph_test(), which does three things — the second is the one that matters:

add_test(NAME ${test_name} COMMAND ${test_path} ...)
if(TARGET ${test_name})
  add_dependencies(tests ${test_name})
  set_property(TARGET ${test_name} PROPERTY EXCLUDE_FROM_ALL TRUE)   # <--
endif()

So a add_ceph_unittest target is removed from all and only built by the tests target. Verified in your own build dir: src/test/all contains bin/ceph_test_snap_mapper but not bin/unittest_osdmap.

Why combining it with install() breaks

CMake documents installing an EXCLUDE_FROM_ALL target as undefined behavior. In practice, isolated repro of exactly this pattern:

$ ninja          # 'all' — binary not built
$ ninja install
CMake Error at cmake_install.cmake:52 (file):
  file INSTALL cannot find "/tmp/cmexp/b/ceph_test_demo"
FAILED: CMakeFiles/install.util

And the packaging paths never build tests:

  • ceph.spec.in: %build → %make_build (= all), %install → %make_install. The %check section only runs ctest, and is conditional on with make_check.
  • debian/rules: plain dh $@ --buildsystem=cmake, so dh_auto_build (= all) then dh_auto_install.

The only thing that builds the test binaries is run-make-check.sh:90 → build tests — the make-check job, which never installs.

Across all of src/, 260 targets use add_ceph_unittest; zero are also install(TARGETS ...)'d. The two non-unittest_-named ones I cited earlier (ceph_test_object_map, ceph_test_fragmentation_sim) are precedent for the name but neither is installed.

No — teuthology does not use the installed list to run unit tests

Two completely separate pipelines:

Unit tests Teuthology
Built by ninja tests (via run-make-check.sh) ninja all → ninja install → ceph-test pkg
Run by ctest in the build tree explicit references in qa/
Discovery automatic (add_test) none — must be named in a yaml or workunit

Nothing is auto-discovered on the teuthology side. debian/ceph-test.install has a usr/bin/ceph_test_* glob, so packaging is automatic, but running is not. ceph_test_snap_mapper is run only because one file names it:

qa/suites/rados/singleton-nomsgr/all/ceph-snapmapper.yaml:24

- exec:
    client.0:
    - ceph_test_snap_mapper

For scale: 162 distinct ceph_test_* targets exist in src/ CMakeLists; only 85 distinct names appear anywhere in qa/. The rest ship in ceph-test and are only ever run by hand on a test node.

So what belongs in the installed set

Install a ceph_test_* binary when it needs something make check can't give it — a live cluster, a real OSD/mon, RADOS/RBD/RGW clients, long runtimes — and when a qa yaml or workunit will actually invoke it. Everything that is a self-contained gtest belongs in make check as unittest_*, not installed.

This PR's test is a gtest over a MemStore that finishes in 19 ms. It's a unit test.

Concrete suggestion for the author

Rename to unittest_scrubber_run (or unittest_snapmapper_scrub), keep add_ceph_unittest, drop the install(TARGETS ...) block:

add_executable(unittest_scrubber_run
  test_scrubber_run.cc
  $<TARGET_OBJECTS:unit-main>
  )
add_ceph_unittest(unittest_scrubber_run)
target_link_libraries(unittest_scrubber_run osd global ${BLKID_LIBRARIES})

Worth noting this also retroactively justifies their decision not to fold it into test_snap_mapper.cc, as you'd offered: ceph_test_snap_mapper is installed and is not registered with add_ceph_unittest, so new cases added there would run only in that one teuthology job and never in make check. A separate binary was the right call — the name and the install() are what need fixing.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 43ce7a9: renamed to unittest_scrubber_run and the install() block is dropped.

Comment thread src/test/test_scrubber_run.cc Outdated
ASSERT_NE(nullptr, d);
path = d;

store = ObjectStore::create(cct, "memstore", path);

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.

You may want to consider running without a path argument ('(cct, "memstore", "")').
You will save the automatic dumping of the whole store on every test.

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.

If you do accept his change: you do not need and .
And the whole mkdtemp thing

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 43ce7a9: the store now uses an empty path, and the mkdtemp machinery is gone along with , and <system_error>.

Scrubber::run() opened a fresh omap_iterate over purged_snaps, from its
start, for every mapping key: O(M*P).  On snapshot-heavy OSDs this
pinned a core for days.

Collect the purged_snaps rows once up front, sort them numerically by
(pool, begin), merge overlapping and adjacent same-pool rows into
disjoint ranges, and binary-search them per mapping key: O(M log P).

The numeric sort also fixes missed strays: with pools ordered as
decimal strings, the old walk could stop at a pool-20 row and never
reach pool 3.

The on-disk key order is not numeric in the pool (decimal string), and
rows of one pool may overlap or touch (record_purged_snaps() merges a
new interval only with rows already on disk, so intervals carried in
one call do not see each other), so the merged ranges stay sorted by
(pool, begin) and disjoint: one upper_bound per mapping key, and the
last row that starts at or before the snap is the only one that can
cover it.

Adds unittest_scrubber_run, a unit test for Scrubber::run(): it
builds a SnapMapper over a MemStore and fills it through the same
writer methods ceph-osd uses (record_purged_snaps(), add_oid()),
then checks every stray the scrub reports.

Fixes: https://tracker.ceph.com/issues/81592
Fixes: https://tracker.ceph.com/issues/81798
Signed-off-by: Ante Kuspilic <[email protected]>
@Antek74
Antek74 force-pushed the osd-snapmapper-linear-purged-scrub branch from b381d21 to 43ce7a9 Compare October 19, 2026 19:57
@Antek74

Antek74 commented Oct 19, 2026

Copy link
Copy Markdown
Author

43ce7a9: carries a few unasked changes. Found by my review pass before the push.

  • r -> result for the purged_snaps omap_iterate result, matching the two sibling calls in this file
  • test file mode line: indent-tabs-mode:t -> nil (the body is spaces, and nil is the src/test default)
  • removed a dead using namespace std;
  • CEPH_MAXSNAP macro instead of a hand-copied literal
  • one 83-column line rewrapped to the 80-column limit
  • corrected a wrong comment: a key that is not PSN_ does not necessarily sort after every PSN_ row
  • new test ManyDisjointPurgedRows: 100 disjoint rows from one record_purged_snaps() call, strays checked at row edges and gaps.

@Antek74
Antek74 requested a review from ronen-fr October 19, 2026 20:09
@kamoltat

Copy link
Copy Markdown
Member

@ronen-fr need your final approval

@ronen-fr

Copy link
Copy Markdown
Contributor

LGTM. Minor nits that should not block approval. I'll add them as comments.

mounted = true;

ch = store->open_collection(coll_t::meta());
if (!ch) {

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.

nitti nit: open_collection(), when called without the path, can't fail. So - no real need for the 'if'.
Still - not sure this should be fixed. open_collection() implementation may possibly be changed in the future.

}

void make_mappings(
std::vector<std::pair<snapid_t, hobject_t>> entries) {

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.

nit: this is just a loop over make_mapping()

TEST_F(ScrubberRunTest, StrayInsideInterval) {
make_purged(1, {{snapid_t(10), snapid_t(20)}});
make_mappings({{snapid_t(15), mk_hobj(1, "a")}});
ASSERT_EQ(1, scrub().size());

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.

A general nit: when comparing to size(), sometimes you use '0', and sometimes '0u'

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath11-testing-20261027.070430-main.

@connorfawcett

Copy link
Copy Markdown
Contributor

@ronen-fr
ronen-fr merged commit 15101d9 into ceph:main Nov 11, 2026
23 of 30 checks passed
@github-actions

Copy link
Copy Markdown

This is an automated message by src/script/redmine-upkeep.py.

I have resolved the following tracker ticket due to the merge of this PR:

No backports are pending for the ticket. If this is incorrect, please update the tracker
ticket and reset to Pending Backport state.

Update Log: https://github.com/ceph/ceph/actions/runs/43042915920

lhns added a commit to lhns/ceph-nix that referenced this pull request Dec 3, 2026
…to-stop, crash dir owner) (#40)

* Tentacle: carry three upstream fixes as source patches (ADR 0015)

Purged-snaps scrub rewrite (ceph/ceph#72544, backport #74456), cephadm OSD
redeploy/reconfig ok-to-stop check (#71640, backport #72033) and cephadm crash
dir owner (#66559).

* Tentacle: carry the OSD/MDS key rotation fix (#72755, ADR 0015)

* ADR 0002: punctuation

* Tentacle: carry the MDS rogue-reference-to-purging-inode fix (#73861, ADR 0015)
lhns added a commit to lhns/ceph-nix that referenced this pull request Dec 3, 2026
…to-stop, crash dir owner) (#40)

* Tentacle: carry three upstream fixes as source patches (ADR 0015)

Purged-snaps scrub rewrite (ceph/ceph#72544, backport #74456), cephadm OSD
redeploy/reconfig ok-to-stop check (#71640, backport #72033) and cephadm crash
dir owner (#66559).

* Tentacle: carry the OSD/MDS key rotation fix (#72755, ADR 0015)

* ADR 0002: punctuation

* Tentacle: carry the MDS rogue-reference-to-purging-inode fix (#73861, ADR 0015)
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