Skip to content

os/bluestore: implement object content recompression/defragmentation when scrubbing - #57631

Open
gardran wants to merge 17 commits into
ceph:mainfrom
gardran:wip-gdran-defregment
Open

gardran wants to merge 17 commits into
ceph:mainfrom
gardran:wip-gdran-defregment

Conversation

@gardran

@gardran gardran commented May 22, 2024

Copy link
Copy Markdown
Contributor

This PR adds an ability for BlueStore to perform object content recompression and defragmentation when handling read requests during deep-scrubbing.
After reading object's chunk BlueStore assesses if it can improve space saving by recompressing an object and/or it can optimize physical layout of the object.
Per-pool option 'data_layout_reformatting' has been introduced to control the behavior. It support the following options:

  • recompress - enables object content recompression
  • defragment - enabled object defragmentation
  • both - enables both recompression and defragmentation
  • - original behavior, no object reformatting

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
  • jenkins retest this please
  • jenkins test classic perf
  • jenkins test crimson perf
  • jenkins test signed
  • jenkins test make check
  • jenkins test make check arm64
  • jenkins test submodules
  • jenkins test dashboard
  • jenkins test dashboard cephadm
  • jenkins test api
  • jenkins test docs
  • jenkins render docs
  • jenkins test ceph-volume all
  • jenkins test ceph-volume tox
  • jenkins test windows
  • jenkins test rook e2e

Comment thread src/os/bluestore/BlueStore.cc Outdated
@@ -11776,11 +11841,131 @@ int BlueStore::read(
if (offset == length && offset == 0)
length = o->onode.size;

r = _do_read(c, o, offset, length, bl, op_flags);
r = _do_read(c, o, offset, length, bl, op_flags, 0,
might_need_reformatting ? &span_stat : nullptr);

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 we can potentially defrag on any read, it could be interesting once the extents are read off disk to make a decision about whether or not an object is fragmented enough to warrant defragmentation on-the-fly.

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.

@markhpc I am excited to see it. Opens interesting paths.

@dvanders

dvanders commented May 22, 2024 •

Copy link
Copy Markdown
Contributor

I'm not sure "data_layout_reformatting" is very clear to the users what it is doing.

Maybe instead use "optimize_during_scrub" with options "defrag", "recompress", "all", ...
Or perhaps better: a different boolean pool flag for each: defrag_during_scrub, recompress_during_scrub, ...

The second option is flexible if we want to allow additional optimizations or modes in the future.

Comment thread src/osd/osd_types.cc Resolved
@markhpc markhpc removed the bluestore label May 23, 2024
Comment thread src/mon/OSDMonitor.cc Outdated
@@ -6156,7 +6157,8 @@ bool OSDMonitor::preprocess_command(MonOpRequestRef op)
{"dedup_chunk_algorithm", DEDUP_CHUNK_ALGORITHM},
{"dedup_cdc_chunk_size", DEDUP_CDC_CHUNK_SIZE},
{"bulk", BULK},
{"read_ratio", READ_RATIO}
{"read_ratio", READ_RATIO},
{"data_layout_reformatting", DATA_LAYOUT_REFORMATTING},

@markhpc markhpc May 23, 2024 •

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.

This is missing corresponding "data_reformatting" get and set entries in MonCommands.h (easy to miss!)

@markhpc

markhpc commented May 23, 2024 •

Copy link
Copy Markdown
Member

I'm not sure "data_layout_reformatting" is very clear to the users what it is doing.

Maybe instead use "optimize_during_scrub" with options "defrag", "recompress", "all", ... Or perhaps better: a different boolean pool flag for each: defrag_during_scrub, recompress_during_scrub, ...

The second option is flexible if we want to allow additional optimizations or modes in the future.

Very much dislike "optimize_during_scrub". I'd favor the boolean flag approach with something like "scrub_defragmentation", and "scrub_recompression". The number of pool options we have and the way we display them in the command help has become pretty unwieldy though. This doesn't do anything to help it. Having said that, I agree that this is best configurable at the pool level, especially if we want to allow lazy compression of objects on scrub.

@markhpc

markhpc commented May 23, 2024 •

Copy link
Copy Markdown
Member

I did a very minimal initial test where I created 4 128GB RBD images, pre-filled them with 4MB writes, then did 5 minutes of 4K random writes to them in a pool with 512 PGs. This is on NVMe, so it's pretty quick. I then looked at a certain PG 2.1ff:

PG_STAT OBJECTS BYTES OMAP_BYTES* OMAP_KEYS* LOG LOG_DUPS DISK_LOG
2.1ff 273 1145044992 0 0 618 3000 618

and ran deep-scrub on it 3 times:

Start Time End Time Duration
2024-05-23T01:48:36.667+0000 2024-05-23T01:48:43.199+0000 6.532
2024-05-23T01:55:48.326+0000 2024-05-23T01:55:54.804+0000 6.478
2024-05-23T01:59:05.596+0000 2024-05-23T01:59:12.075+0000 6.479

And the fragmentation score and reformat counters at this point is:

"fragmentation_rating": 0.01635269932531749
"reformat_compress_attempted": 0,
"reformat_compress_omitted": 0,
"reformat_defragment_attempted": 0,
"reformat_defragment_omitted": 0,
"reformat_issued": 0

Next set the pool data_reformatting to defragment and rerun deep-scrub on 2.1ff:

Start Time End Time Duration
2024-05-23T04:01:18.327+0000 2024-05-23T04:01:24.845+0000 6.518
"fragmentation_rating": 0.016560792018378661
"reformat_compress_attempted": 0,
"reformat_compress_omitted": 0,
"reformat_defragment_attempted": 2184,
"reformat_defragment_omitted": 0,
"reformat_issued": 2184

Next set the pool data_reformatting to both and rerun deep-scrub on 2.1ff:

Start Time End Time Duration
2024-05-23T04:15:42.800+0000 2024-05-23T04:15:49.279+0000 6.479
"fragmentation_rating": 0.016560788158529967
"reformat_compress_attempted": 0,
"reformat_compress_omitted": 0,
"reformat_defragment_attempted": 2184,
"reformat_defragment_omitted": 0,
"reformat_issued": 2184

Next set the pool compression_mode to passive and rerun deep-scrub on 2.1ff. Results are similar to above.
Next set the pool compression_mode to aggressive and rerun deep-scrub on 2.1ff. (forgot to check results)
Next set the pool compression_mode to force and rerun deep-scrub on 2.1ff.

Start Time End Time Duration
2024-05-23T04:26:35.157+0000 2024-05-23T04:26:41.637+0000 6.48
"fragmentation_rating": 0.016575220146748098
"reformat_compress_attempted": 4368,
"reformat_compress_omitted": 4074,
"reformat_defragment_attempted": 2184,
"reformat_defragment_omitted": 0,
"reformat_issued": 2478

Running another deep scrub on that PG results in an attempt to recompress the objects again:

"reformat_compress_attempted": 6552,
"reformat_compress_omitted": 6111,
"reformat_defragment_attempted": 2184,
"reformat_defragment_omitted": 0,
"reformat_issued": 2625

A couple of thoughts:

  1. It would be nice if we had additional compression modes like "passive_lazy, aggressive_lazy, and force_lazy". The idea would be to allow objects to be written to the pool in an uncompressed state and only compress them on deep-scrub. Not sure how that would work with the global OSD level settings if we don't have the compress on scrub stuff set for the pool though.
  2. It looks like most of the re-compression attempts here were omitted. After re-reading the code, it looks like this should pretty much only happen if need > allocated or need == allocated.
  3. It's not clear to me why the fragmentation_rating goes up slightly after the first defrag pass. I probably should have been looking at onode_extents though. I'll try to do some better testing tomorrow.

Morning update: I tried deep-scrubbing another PG this morning with data_reformatting set to both and watched onode_extents:

pg onode_extents before onode_extents after
2.1fe 1259816 1276370

It looks like the number of extents actually went up?

@markhpc

markhpc commented May 23, 2024

Copy link
Copy Markdown
Member

Here's a debug_bluestore = 10 log while deep-scrubbing one of the other PGs (2.1fb).
osd.2.log.gz

@gardran

gardran commented May 24, 2024

Copy link
Copy Markdown
Contributor Author

extents actually went up?

Are you referring to onode_extens perf counter here? IMO that's a wrong metric as it's an amount of logical (not physical) extents in the cache(!)
Proper way would be to check the report from "ceph tell osd.0 bluestore allocator dump block" and see how fragmented it is.
Output of "bluestore allocator fragmentation histogram block" is another mean for that analysis.

@gardran

gardran commented May 24, 2024

Copy link
Copy Markdown
Contributor Author

Here's a debug_bluestore = 10 log while deep-scrubbing one of the other PGs (2.1fb). osd.2.log.gz

Looks good to me, the majority of reads has log output like:
75+0000 7fed59ea4700 10 bluestore(/tmp/cbt/mnt/osd-device-2-data) read layout reformatting = both span stat {s=524288 ch=0 a=524288 sc=0 ac=0 sh=0 e=8 f=28}
2024-05-23T12:58:06.176+0000 7fed59ea4700 10 bluestore(/tmp/cbt/mnt/osd-device-2-data) read recompress info, 0x80000 vs. 0x80000
2024-05-23T12:58:06.176+0000 7fed59ea4700 10 HybridAllocator allocate 0x80000/1000,80000,0
2024-05-23T12:58:06.176+0000 7fed59ea4700 10 bluestore(/tmp/cbt/mnt/osd-device-2-data) read preallocated: 0x80000 new frags:1 defragment: 1
Meaning there had been 28 physical fragments originally and a single one to be written out.

@gardran

gardran commented May 24, 2024

Copy link
Copy Markdown
Contributor Author

ted": 4368,
"reformat_compress_omitte

Here's a debug_bluestore = 10 log while deep-scrubbing one of the other PGs (2.1fb). osd.2.log.gz

I could see no initial compression, e.g.
2024-05-23T12:58:04.863+0000 7fed59ea4700 10 bluestore(/tmp/cbt/mnt/osd-device-2-data) read layout reformatting = both span stat {s=524288 ch=0 a=524288 sc=0 ac=0 sh=0 e=8 f=11}

sc (aka storage compressed) = 0
ac (aka allocated compressed) = 0
mean that no data has been compressed before scrubbing. And no recompression is applied:

2024-05-23T12:58:04.864+0000 7fed59ea4700 10 bluestore(/tmp/cbt/mnt/osd-device-2-data) read recompress info, 0x80000 vs. 0x80000

the above line shows no saving due to recompression - recompressed data needs the same 512K of space.

So I presume your input data flow isn't compressible. Or something wrong with compression settings...

@markhpc

markhpc commented May 24, 2024

Copy link
Copy Markdown
Member

extents actually went up?

Are you referring to onode_extens perf counter here? IMO that's a wrong metric as it's an amount of logical (not physical) extents in the cache(!) Proper way would be to check the report from "ceph tell osd.0 bluestore allocator dump block" and see how fragmented it is. Output of "bluestore allocator fragmentation histogram block" is another mean for that analysis.

Yikes! That's confusing. I don't know if I knew that at one point, but clearly I didn't remember it. :) We should rename it. I'll try to take a look with the histogram.

@markhpc

markhpc commented May 24, 2024

Copy link
Copy Markdown
Member

I could see no initial compression, e.g. 2024-05-23T12:58:04.863+0000 7fed59ea4700 10 bluestore(/tmp/cbt/mnt/osd-device-2-data) read layout reformatting = both span stat {s=524288 ch=0 a=524288 sc=0 ac=0 sh=0 e=8 f=11}

sc (aka storage compressed) = 0 ac (aka allocated compressed) = 0 mean that no data has been compressed before scrubbing. And no recompression is applied:

2024-05-23T12:58:04.864+0000 7fed59ea4700 10 bluestore(/tmp/cbt/mnt/osd-device-2-data) read recompress info, 0x80000 vs. 0x80000

the above line shows no saving due to recompression - recompressed data needs the same 512K of space.

So I presume your input data flow isn't compressible. Or something wrong with compression settings...

There was no initial compression. I wrote the data out uncompressed with data_reformatting off, then set it to "defragment", then "both". Next I set the compression mode to passive, then aggressive, then force. Each time, I ran a deep scrub on different PGs to see what would happen.

@dvanders

Copy link
Copy Markdown
Contributor

I could see no initial compression, e.g. 2024-05-23T12:58:04.863+0000 7fed59ea4700 10 bluestore(/tmp/cbt/mnt/osd-device-2-data) read layout reformatting = both span stat {s=524288 ch=0 a=524288 sc=0 ac=0 sh=0 e=8 f=11}
sc (aka storage compressed) = 0 ac (aka allocated compressed) = 0 mean that no data has been compressed before scrubbing. And no recompression is applied:
2024-05-23T12:58:04.864+0000 7fed59ea4700 10 bluestore(/tmp/cbt/mnt/osd-device-2-data) read recompress info, 0x80000 vs. 0x80000
the above line shows no saving due to recompression - recompressed data needs the same 512K of space.
So I presume your input data flow isn't compressible. Or something wrong with compression settings...

There was no initial compression. I wrote the data out uncompressed with data_reformatting off, then set it to "defragment", then "both". Next I set the compression mode to passive, then aggressive, then force. Each time, I ran a deep scrub on different PGs to see what would happen.

This is a great point. I think the intuitive user expectation is that this feature could be used to compress old objects written uncompressed before this feature was available.

Also earlier we spoke about a feature like:

  • pool compression is disabled, so initial writes are written uncompressed
  • scrub recompress works to compress the objects in the background

@gardran
gardran force-pushed the wip-gdran-defregment branch from 462da8c to 56f74a1 Compare May 27, 2024 16:48
Comment thread src/os/bluestore/BlueStore.cc Resolved
Comment thread src/os/bluestore/BlueStore.cc Resolved
Comment thread src/os/bluestore/BlueStore.cc Outdated
@@ -11776,11 +11841,134 @@ int BlueStore::read(
if (offset == length && offset == 0)
length = o->onode.size;

r = _do_read(c, o, offset, length, bl, op_flags);
r = _do_read(c, o, offset, length, bl, op_flags, 0,

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 implementation is very strongly tied to existing BlueStore execution paths.
This is a problem, because it gives very little room to actually work on optimization algorithms.

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.

That's true but we really want to save some cycles by not doing independent standalone optimization.

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.

I do not believe in optimizing cycles in function that optimizes metadata shape.
In almost all cases it is more profitable to pay for optimization and get the rewards elsewhere.

Comment thread src/os/bluestore/BlueStore.cc Resolved
Comment thread src/os/bluestore/BlueStore.cc Outdated
IOContext* ioc)
{
IOContext* ioc,
span_stat_t* res_span_stat)

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.

_prepare_read_ioc is modified to provide scanning for optimization.
We should have a separate function that will scan object and decide action.
For example it might happen that in object we have 3 chunks for total of 4MB,
and in attempt to defragment it we allocated 3 chunks.

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.

_prepare_read_ioc is modified to provide scanning for optimization. We should have a separate function that will scan object and decide action.

That's again an attempt to design things for an independent optimization process. Which still sounds questionable to me.

For example it might happen that in object we have 3 chunks for total of 4MB, and in attempt to defragment it we allocated 3 chunks.

Not sure I understand this example. When speaking of defragmentation only - it would happen if amount of new fragments is less than original one for a specific data span (512K when triggered from deep scrub).
If recompression is enabled as well -reformatting unconditionally occurs when new required space for a data span is less than the original one. Fragmentation is taken into consideration when recompression is not beneficial only. This recompression+defragmentation prioritization might look a bit inflexible - one might want prefer defragmentation over recompression but for now it looks like an overkill to me.

@markhpc markhpc Jun 3, 2024 •

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.

The big reason I think we want to piggyback on deep-scrub is because we're already reading the content of the object to do the scrub, so it's the perfect time to make decisions about de-fragmentation and compression as the most expensive part of this can easily be reading the fragmented extents. I don't think we should scan objects as a separate loop unless we have a very good reason. It's much better in my mind to opportunistically defrag/compress when we've already done the expensive fragmented read.

@aclamk aclamk Jun 12, 2024 •

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.

@markhpc
I (now) agree that performing optimization to object as an extra step during deep-scrub is good.
"I don't think we should scan objects as a separate loop unless we have a very good reason."
The code here is scanning the object - it's just piggybacking _read_cache to do it.
There is no change to behaviour of _read_cache.
The filling of span_stat_t could be done in separate loop at a cost of a loop.
Similarly, we now load shared blobs in _prepare_read_ioc - useless for reading process.
That would made optimization sense, if significant part of processing was reused - but its only the loop.

@gardran My opinion is that whenever we make a new feature we should make it as detached from existing code as possible to minimize accidental bug injection. Within reason of course...

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.

@gardran My opinion is that whenever we make a new feature we should make it as detached from existing code as possible to minimize accidental bug injection. Within reason of course...

Generally I agree with this point when we're talking about a severe intervention into the execution path: changing run flow, having new algorithms, etc...
Instead I just perform stats collection (by using simple arithmetic ops) here. So apart from a trivial null/invalid pointer dereferencing this shouldn't introduce any issues to existing functionality. Moreover if refactoring is disabled this function collects stats into a dummy local var which is an absolutely trivial operation.
So IMO that sort of changes (in this specific function) is quite safe and doesn't worth the separation - basic code review/testing should reveal any potential issues if any.

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.

I was slightly worried about performance drop from that unused span_stat_t in non-scrub case, but performance cost is low. It is about 1% more resources, different metrics are consistent here.

origin/main:

{
  "sample_count": 22863635,
  "context_switches": {
    "non_zero_count": 18006771,
    "zero_count": 4856864,
    "total": 19591775,
    "avg": 0.85689677078907178
  },
  "cpu_cycles": {
    "total": 1086421762527,
    "avg": 47517.455668225986
  },
  "cache_misses": {
    "total": 26163467742,
    "avg": 1144.3266891725659
  },
  "branch_misses": {
    "total": 3647919172,
    "avg": 159.55114626348785
  },
  "instructions": {
    "total": 164399693895,
    "avg": 7190.4442970245109
  }
}

this PR:

{
  "sample_count": 23010714,
  "context_switches": {
    "non_zero_count": 18123197,
    "zero_count": 4887517,
    "total": 19730884,
    "avg": 0.85746509213056143
  },
  "cpu_cycles": {
    "total": 1105964001154,
    "avg": 48063.002354207696
  },
  "cache_misses": {
    "total": 27135710009,
    "avg": 1179.2641466492521
  },
  "branch_misses": {
    "total": 3783880829,
    "avg": 164.43995736073205
  },
  "instructions": {
    "total": 166885546959,
    "avg": 7252.5149353905317
  }
}

Comment thread src/os/bluestore/BlueStore.cc Resolved
Comment thread src/os/bluestore/BlueStore.cc Resolved
Comment thread src/os/bluestore/OnodeReformat.cc Outdated
#undef dout_prefix
#define dout_prefix *_dout << "OnodeReformatDefragment "

bool OnodeReformatDefragmentAction::call(OnodeReformatContext* ctx,

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.

How does DEFRAGMENT engine work when RECOMPRESS engine is not enabled?
Please lead me through the logic.

@gardran gardran Jul 21, 2026 •

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.

Data is staged for overwriting with a goal to lower the number of fragments on disk. If the goal is achieved - the actual overwrite takes place. No recompression is applied along the path.

@github-actions

Copy link
Copy Markdown

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

@gardran

gardran commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@aclamk - your last comments have been resolved/answered. Please take another look.

@github-actions

github-actions Bot commented Sep 4, 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

@aclamk

aclamk commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

@gardran
I made a separate function BlueStore::_reformat_scan that does all stats-collecting actions:
aclamk/ceph@wip-gdran-defregment...aclamk-wip-gdran-defregment

The benefit is that we do not have to modify _read_cache, _prepare_read_ioc.

The performance, measured during deep-scrub:

avg cpu cycles _do_read (legacy) _do_read_stats_along(new) reduction
osd.0 2699475 2678301 0.78%
osd.1 2640590 2623087 0.66%
osd.2 2710383 2691707 0.68%

The performance, measured during random read-write testing:

avg cpu cycles _do_read (legacy) _do_read_stats_along(new) reduction
osd.0 87226 87424 -0.23
osd.1 96893 97006 -0.12
osd.2 94782 94980 -0.21
osd.3 99725 99858 -0.13
osd.4 96792 97059 -0.28
osd.5 93335 93507 -0.18
osd.6 100473 100638 -0.16
osd.7 94810 94983 -0.18

We have +0.7% more CPU on scrub, but -0.2% CPU on client IO reads.
It means if we have more than 3.5x more time spent in client reads than in scrub reads,
than having separated BlueStore::_reformat_scan is also more CPU efficient.

I am looking forward to discussion about benefits / problems of the solution I proposed here.

@github-actions

github-actions Bot commented Sep 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

@gardran

gardran commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor Author

We have +0.7% more CPU on scrub, but -0.2% CPU on client IO reads. It means if we have more than 3.5x more time spent in client reads than in scrub reads, than having separated BlueStore::_reformat_scan is also more CPU efficient.

I am looking forward to discussion about benefits / problems of the solution I proposed here.

The 0.2% drop on client load is rather cause by unconditinal passing span_stat_t parameter into _do_read. Originally this wasn't the case and it had been happening for scrubbing reads only.

Anyway I encorporated your changes into the PR, please keep reviewing.

@github-actions

Copy link
Copy Markdown

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

1 similar comment
@github-actions

Copy link
Copy Markdown

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

gardran and others added 17 commits September 30, 2026 18:32
Collect `span_stat_t` related code from _read_cache, _prepare_read_ioc
into single _reformat_scan function.

Signed-off-by: Adam Kupczyk <[email protected]>
Suggested by Adam to minimize exisiting code intervention

Signed-off-by: Garry Drankoivich <[email protected]>
Thus making general framework unaware of specific data layout preconditions
to apply reformatting engines.

Signed-off-by: Garry Drankovich <[email protected]>

@dvanders dvanders 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 @gardran for sticking with this. I still want this feature, and Fast EC makes it more important. aclamk's _reformat_scan split looks good. Before merge I see some blockers:

  1. An unknown deep_scrub_reformat token indexes new_engines[-1] (both from the description triggers it). The mon needs to validate the value.
  2. EC scrub reads past EOF. The reformat write uses the requested length instead of bl.length(), so it runs off the end of the bufferlist and grows onode.size.
  3. When _do_write fails, the txc is dropped but the punched extent map stays in cache.
  4. Compressor errors in the recompress engine are treated as success.
  5. Defrag allocates holes, which costs space on thin RBD and sparse EC shards.
  6. Now that a read with CEPH_OSD_OP_FLAG_SCRUB can cause writes, please strip that flag from client ops in the OSD. librados drops it, but a raw client could set it.

I'd also like a rate limit or OSD-side off switch, no exclusive c->lock unless we actually write, pool docs, and tests for EC-style reads, ENOSPC and fsck afterwards.
@aclamk could you re-review the current head, especially the _do_write_big / _do_alloc_write changes and the lazy-mode semantics?

engine = new OnodeReformatDefragmentEngine(args);
}
ceph_assert(e < MAX_REFORMAT_ENGINES);
new_engines[e].reset(engine);

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.

Any token other than recompress/defragment leaves e=-1. The assert passes and we index new_engines[-1]. ceph osd pool set X deep_scrub_reformat both (what the PR description says) would hit this on every OSD in the pool. Please reject unknown tokens here, and validate in OSDMonitor like compression_mode.

cct->_conf->bluestore_log_op_age);
}
if (r >= 0) {
_maybe_do_reformat_onode(reformat_ctx, c, o, offset, length, bl, op_flags);

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.

EC scrub reads 4M even near EOF. The reformat write uses the requested length, not bl.length(). That throws end_of_buffer in _do_write_data and would also grow the object. Please clamp to bl.length() and add a test that reads past EOF at a non-zero offset. Also, reformat_ctx is built before the 0~0 → onode.size fixup above, so it sees the unfixed length.

ceph_assert(o->exists);
int r = _do_write(txc, txc->ch, o, offset, length, bl, wctx);

if (r < 0) {

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 _do_write fails we drop the txc but keep the modified extent map in cache. Normal writes abort here. Either preallocate everything before touching the onode, or invalidate/reload the onode on failure.


res_len = from_bl.length();
if (l > min_alloc_size &&
wctx.compressor->compress(from_bl, wi.compressed_bl, wi.compressor_message) == 0) {

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 compress() fails in the engine, _do_alloc_write still treats compressed_bl as valid (r=0). Please carry the error through or clear compressed_bl.

#undef dout_prefix
#define dout_prefix *_dout << "OnodeReformatDefragment "

bool OnodeReformatDefragmentEngine::execute(OnodeReformatContext& ctx,

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.

Defrag turns holes into allocated zeros (the small-gap test shows it). That is a space regression for thin RBD and sparse EC shards. Skip holes, or require stored==length. Also please add a minimum gain, e.g. average fragment size below X.

OnodeReformatContext reformat_ctx(read_ctx, c->reformat_engines);
if (o && o->exists && reformat_ctx.is_enabled()) {
slock.unlock();
ulock.lock();

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 takes c->lock exclusive whenever an engine is enabled, so every aligned scrub read, even when nothing gets rewritten, and holds it across the compress, allocation and _txc_exec. Could we only take it exclusive once we've decided to write? Please also add an OSD-side cap (bytes/s or a fraction of scrub reads) and a global off switch.

@dvanders

Copy link
Copy Markdown
Contributor

Built it and ran some tests on Linux (store_test, v1 and v2 write paths):

  • the 6 new StoreTestReformatting cases pass;
  • a scrub read of 4M at offset 0 or 64K on a fragmented 127K object (what EC deep scrub does near EOF) throws End of buffer out of BlueStore::read() with defragment enabled, on both v1 and v2 (point 2);
  • a sparse object (4K every 16K over 1M) goes from 256K to 1012K allocated after one defragment scrub read (point 5);
  • when the pool's compressor can't be loaded (I was missing the lz4 plugin at first), OnodeReformatRecompressEngine::execute() segfaults instead of skipping, the way _do_alloc_write does. An OSD that can't load the algorithm would crash on deep scrub;
  • deep_scrub_reformat=both didn't crash in my run (no engine got enabled), but the new_engines[-1] write is still undefined behaviour.

Happy to share the test cases if useful.

@dvanders

Copy link
Copy Markdown
Contributor

Happy to share the test cases if useful.

@gardran the test cases are here, on top of your current head: https://github.com/dvanders/ceph/commits/wip-57631-repro-tests (one commit, test/objectstore only). The past-EOF and hole-fill ones fail at 9bfd726 and should pass once those are fixed. Run with --plugin-dir pointing at your build's lib/, or the compressor plugins won't load. Feel free to pick them into the PR.

@github-actions

Copy link
Copy Markdown

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

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