Skip to content

crimson/osd/object_context_loader: Fix obc cache existence usage - #57725

Merged
athanatos merged 10 commits into
ceph:mainfrom
Matan-B:wip-matanb-crimson-load-exist
Jun 6, 2024
Merged

athanatos merged 10 commits into
ceph:mainfrom
Matan-B:wip-matanb-crimson-load-exist

Conversation

@Matan-B

@Matan-B Matan-B commented May 27, 2024 •

Copy link
Copy Markdown
Contributor

with_head_obc() uses get_cached_obc() to get_or_create obc instances.

If the obc exists in cache, get_or_load_obc is called with existed=true.
The assumption above is wrong.
Cache existence (existed) only guarantees that the obc instance was created (and inserted) in the obc_registery. However, it does not assure that the obc was actually loaded.

As obc-loading is now concurrent, it's possible for the first user to only create the obc in cache (without loading yet) and the second concurrent user to assume it was already loaded.

With this patch, we verify that the obc was loaded for get_or_load_obc usage.

Fixes: https://tracker.ceph.com/issues/64206
Fixes: https://tracker.ceph.com/issues/66214


  // obc loading is a concurrent phase. In case this obc is being loaded,
  // make other useres of this obc to await for the loading to complete.

Since we now await for loading to finish (in-case in progress), we can
also assert is_loaded().

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


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

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

Matan-B added 2 commits May 27, 2024 15:24
with_head_obc() uses get_cached_obc() to get_or_create obc instances.

If the obc exists in cache, get_or_load_obc is called with `existed`=true.
The assumption above is wrong.
Cache existence (`existed`) only guarantees that the obc instance was created (and inserted) in the obc_registery.
However, it does **not** assure that the obc was actually loaded.

As obc-loading is now concurrent, it's possible for the first user to only create the obc in
cache (without loading yet) and the second concurrent user to assume it was already loaded.

With this patch, we verify that the obc was loaded in get_or_load_obc.

* make loading-obc concurrent PR: ceph#55488

Fixes: https://tracker.ceph.com/issues/64206
Fixes: https://tracker.ceph.com/issues/66214

Signed-off-by: Matan Breizman <[email protected]>
@Matan-B
Matan-B force-pushed the wip-matanb-crimson-load-exist branch from 2e3c8d3 to ccc0001 Compare May 27, 2024 15:25
auto loaded =
load_obc_iertr::make_ready_future<ObjectContextRef>(obc);
if (existed) {
if (existed && obc->is_loaded()) {

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 this is the correct direction to return only until the obc is loaded.

As obc-loading is now concurrent, it's possible for the first user to only create the obc in cache (without loading yet) and the second concurrent user to assume it was already loaded.

But I think the first user must create and also start to load the obc at the same time. So the second (maybe-concurrent) user should assume it is already loading or already loaded.

(If I'm wrong above, can you help point to the violating logic?)

If I'm correct above, I think the accurate pseudo logic might be:

if (existed && obc->is_loaded()) {
  return load_obc_iertr::make_ready_future<ObjectContextRef>(obc);
} else if (existed) {
  // not the first user, the obc must be already loading,
  // return the loading future, instead of loading the obc again.
} else { // !existed: the first user
  return obc->template with_promoted_lock<State, IOInterruptCondition>(
    [obc, this] { return load_obc(obc); }
  );
}

However, the current version should be fine to address the assert failure.

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.

But I think the first user must create and also start to load the obc at the same time. So the second (maybe-concurrent) user should assume it is already loading or already loaded.

This is correct. Broadly speaking, the pseudo logic should work, though I'm now sure it's worth the additional logic. This will require maintaining the loading future from the "first-user" for the "second" one to use later, instead of loading again.
The main reason for avoiding implementing the pseudo logic is that this scenario is rare. This was quite frequent in testing was because the obc_lru_size is set to 10 (to reduce cache hits).

@cyx1231st, I suggest merging as is to resolve the bug. Later on, we could rework this bit if needed. What do you think?

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 correct. Broadly speaking, the pseudo logic should work, though I'm now sure it's worth the additional logic. This will require maintaining the loading future from the "first-user" for the "second" one to use later, instead of loading again.

@Matan-B #57687 is for this purpose, it avoid loading the obc again by making sure the exist check and the load happens synchronously, maybe we should consider 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.

@Matan-B #57687 is for this purpose, it avoid loading the obc again by making sure the exist check and the load happens synchronously, maybe we should consider it?

@xxhdx1985126 , To consider the benefit of avoiding the extra load we need to understand how frequent this scenario is. IIUC, a normal workload (not thrash test), with a decent size of obc_lru_size (default of 512) - should not be impacted by this.
For this reason, I'm leaning towards readability and maintainability here.

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.

Note: I'm scheduling a test run since this assert causes too many jobs to fail and merging is blocked, please let me know if you don't agree with my previous comments.

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.

@Matan-B @cyx1231st I think this pr may fail. According to the following code, we can't promote the lock to EXCL when there are multiple readers/writers. Am I missing something?

void tri_mutex::promote_from_read()    
{                                      
  assert(readers == 1);                
  --readers;                           
  exclusively_used = true;             
}                                      

void tri_mutex::promote_from_write()    
{                                       
  assert(writers == 1);                 
  --writers;                            
  exclusively_used = true;              
}                                       

@cyx1231st cyx1231st May 28, 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.

@cyx1231st, I suggest merging as is to resolve the bug. Later on, we could rework this bit if needed. What do you think?

I'm fine to address the specific assert failure with a simple fix, and then find a proper way to avoid loading the same obc.

And I think it is also related to https://tracker.ceph.com/issues/65451, because allowing the non-first user(s) to load the same obc (in the promoted/exclusive lock) concurrently should violate the assertion `readers/writers == 1' upon promotion. (@xxhdx1985126 also points it out above)

This will require maintaining the loading future from the "first-user" for the "second" one to use later, instead of loading again. The main reason for avoiding implementing the pseudo logic is that this scenario is rare. ..

A per-obc seastar::shared_mutex<> may be helpful to make the concurrent users (except the first user) to wait outside the promoted/exclusive lock until the obc is loaded, and also preserve the waiting order. Otherwise, we might need to rethink tri-mutex and the lock promotion. Anyway, the exclusive constraint to load the same obc makes more sense to me.

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.

@Matan-B @cyx1231st I think this pr may fail. According to the following code, we can't promote the lock to EXCL when there are multiple readers/writers. Am I missing something?

void tri_mutex::promote_from_read()    
{                                      
  assert(readers == 1);                
  --readers;                           
  exclusively_used = true;             
}                                      

void tri_mutex::promote_from_write()    
{                                       
  assert(writers == 1);                 
  --writers;                            
  exclusively_used = true;              
}                                       

Ah, that's correct.

A per-obc seastar::shared_mutex<> may be helpful to make the concurrent users (except the first user) to wait outside the promoted/exclusive lock until the obc is loaded, and also preserve the waiting order.

I like this approach, will update the PR. If this will result in unelegant code we could reconsider #57687.

This comment was marked as outdated.

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.

@cyx1231st Maybe SeaStore's Cache::get_extent()-like methods can also follow this approach?

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.

@xxhdx1985126 Sorry I'm not following. Which mutex/lock are you refering to in the Cache::get_extent() related path?

@xxhdx1985126 xxhdx1985126 May 29, 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.

@xxhdx1985126 Sorry I'm not following. Which mutex/lock are you refering to in the Cache::get_extent() related path?

Sorry, I didn't make myself clear. With regard to SeaStore::Cache, we have a similar issue and we need to make sure that the check for whether an extent is in Cache and the load of the missing extent are synchronous. Maybe we can take an approach like this pr, adding a mutex/lock for each extent and locking it when trying to load the extent from disk. What do you think?

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 there is no specific blocking problem that the current way cannot cover (or it result in unacceptabe complexity). Probably it's better to keep the existing way.

The mutex added in this case is because otherwise the code will break.

@Matan-B
Matan-B force-pushed the wip-matanb-crimson-load-exist branch 3 times, most recently from 36d6e05 to 622db94 Compare May 28, 2024 13:43
@Matan-B

Matan-B commented May 28, 2024

Copy link
Copy Markdown
Contributor Author

Approved commits left intact, I'll squash after CR.

@Matan-B
Matan-B requested a review from xxhdx1985126 May 28, 2024 13:47
@Matan-B
Matan-B force-pushed the wip-matanb-crimson-load-exist branch from c3de6dd to 6a1f5e4 Compare May 28, 2024 13:57
@Matan-B
Matan-B force-pushed the wip-matanb-crimson-load-exist branch from 6a1f5e4 to 1b11fb4 Compare May 28, 2024 14:39
@Matan-B

Matan-B commented Jun 4, 2024

Copy link
Copy Markdown
Contributor Author

Probably #57825 can address the specific atomic issue. Feel free to include that PR into this.

@cyx1231st, included. Thanks!

@Matan-B

Matan-B commented Jun 5, 2024 •

Copy link
Copy Markdown
Contributor Author

It seems that 55e7e13109c7085486efbea21b02324a3bb90352 couldn't ensure the atomic call.

  • The first user tries (16790888) to load:
    ObjectContextLoader::with_head_obc: object 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head existed false
  • with_lock called:
    tri_mutex benchmark_data_smithi120_40431_object722 writers 1 readers 0 exclusively_used false waiters: 0 tri_mutex::lock_for_write(): lock_for_write successfully
  • get_or_load called:
    ObjectContextLoader::get_or_load_obc: 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head -- fully_loaded=false, invalidated_by_interval_change=false
  • Second user tries (16790889) to load and assumes existed=true
    ObjectContextLoader::with_head_obc: object 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head existed true

Sanitized crash:

ClientRequest::process_op: client_request(id=16790889, detail=m=[osd_op(client.4840.0:5778 5.2 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head: not completed, entering get_obc stage
ClientRequest::process_op: client_request(id=16790888, detail=m=[osd_op(client.4840.0:5777 5.2 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head: past scrub blocker, getting obc
ObjectContextLoader::with_head_obc: object 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head existed false
tri_mutex benchmark_data_smithi120_40431_object722 writers 0 readers 0 exclusively_used false waiters: 0 tri_mutex::lock_for_write(): 
tri_mutex benchmark_data_smithi120_40431_object722 writers 0 readers 0 exclusively_used false waiters: 0 tri_mutex::try_lock_for_write(): 
tri_mutex benchmark_data_smithi120_40431_object722 writers 1 readers 0 exclusively_used false waiters: 0 tri_mutex::lock_for_write(): lock_for_write successfully
ObjectContextLoader::get_or_load_obc: 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head -- fully_loaded=false, invalidated_by_interval_change=false
ClientRequest::process_op: client_request(id=16790889, detail=m=[osd_op(client.4840.0:5778 5.2 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head: entered get_obc stage, about to wait_scrub
ClientRequest::process_op: client_request(id=16790889, detail=m=[osd_op(client.4840.0:5778 5.2 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head: past scrub blocker, getting obc
ObjectContextLoader::with_head_obc: object 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head existed true
tri_mutex benchmark_data_smithi120_40431_object722 writers 1 readers 0 exclusively_used false waiters: 0 tri_mutex::lock_for_write(): 
tri_mutex benchmark_data_smithi120_40431_object722 writers 1 readers 0 exclusively_used false waiters: 0 tri_mutex::try_lock_for_write(): 
tri_mutex benchmark_data_smithi120_40431_object722 writers 2 readers 0 exclusively_used false waiters: 0 tri_mutex::lock_for_write(): lock_for_write successfully
ObjectContextLoader::get_or_load_obc: 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head -- fully_loaded=false, invalidated_by_interval_change=false
ObjectContextLoader::get_or_load_obc: cache miss on 5:41d9d6fe:::benchmark_data_smithi120_40431_object722:head
tri_mutex benchmark_data_smithi120_40431_object722 writers 2 readers 0 exclusively_used false waiters: 0 tri_mutex::promote_from_write(): 

src/crimson/common/tri_mutex.cc:163: void tri_mutex::promote_from_write(): Assertion `writers == 1' failed.

https://pulpito.ceph.com/matan-2024-06-05_08:27:17-crimson-rados-wip-matanb-crimson-load-exist-distro-crimson-smithi/7742180/

CC: @cyx1231st


Update: It looks that the call interruptor::with_lock makes the func that is passed to _with_lock to be non-atomic.
Moreover, since with 55e7e13109c7085486efbea21b02324a3bb90352 the first user should load atomically - we may no longer need the per-obc mutex (e456bd7).
Assuming the first user (aka loader) will call with_promoted_lock atomically.
Testing: f0e148ad0de58a63822356d799db38f717232cae (Not squashed yet for CR)

@cyx1231st

cyx1231st commented Jun 6, 2024 •

Copy link
Copy Markdown
Member

Quote my previous comment.

A per-obc seastar::shared_mutex<> may be helpful to make the concurrent users (except the first user) to wait outside the promoted/exclusive lock until the obc is loaded, and also preserve the waiting order.

After a second thought, I think "preserve the waiting order" might not be a necessary requirement here, the design of phases (if implemented correctly) should already sufficient to recover the original order upon entering the next exclusive phase:

ihref.enter_stage_sync(
client_pp(*pg).lock_obc, *this);
auto process = pg->with_locked_obc(
m->get_hobj(), op_info,
[FNAME, this, pg, this_instance_id, &ihref] (
auto head, auto obc
) -> interruptible_future<> {
DEBUGDPP("{}.{}: got obc {}, entering process stage",
*pg, *this, this_instance_id, obc->obs);
return ihref.enter_stage<interruptor>(
client_pp(*pg).process, *this

(i.e. upon entering the next exclusive "process" phase)

Update: It looks that the call interruptor::with_lock makes the func that is passed to _with_lock to be non-atomic.

I agree with you, seastar::shared_mutex::lock() also has the same property that returns a future unconditionally (non-atomic), refering to https://github.com/scylladb/seastar/blob/47bfd73e304fd02ed91d7edf6c60d720e767eac5/include/seastar/core/shared_mutex.hh#L109-L129. Probably we should use the atomic version try_lock() -> bool instead.

Moreover, since with 55e7e13 the first user should load atomically - we may no longer need the per-obc mutex (e456bd7).
Assuming the first user (aka loader) will call with_promoted_lock atomically.

I think the assumption is incorrect, see the promoted lock interface:

class excl_lock_from_read {
public:
void lock();
void unlock();
};
// promote from write to excl
class excl_lock_from_write {
public:
void lock();
void unlock();
};

I don't think these promoted locks support waiting yet, their lock() simply returns void. They are more like placeholders (partial implementations) only asserting that the promotion behavior should be correct. The waiting is still needed, either it is the per-obc mutex, or by making the promoted lock classes more complete by adding a similar mechanism inside.

@Matan-B
Matan-B force-pushed the wip-matanb-crimson-load-exist branch from b801e75 to cf00dc7 Compare June 6, 2024 09:53
@Matan-B

Matan-B commented Jun 6, 2024 •

Copy link
Copy Markdown
Contributor Author

I think the assumption is incorrect, see the promoted lock interface:

I don't think these promoted locks support waiting yet, their lock() simply returns void. They are more like placeholders (partial implementations) only asserting that the promotion behavior should be correct. The waiting is still needed, either it is the per-obc mutex, or by making the promoted lock classes more complete by adding a similar mechanism inside.

I have switched to the atmoic version when using the per-obc mutex.

I will note that on local tests I was not able to trigger the per-obc mutex to actually make any user wait (with the new change). It looks to me that the call to promote was atomic and therefore the second user was waiting outside of get_or_load (thanks to ObjectContext::_with_lock). However, this might be simply luck and it's better to still have per-obc mutex to cover all cases (as rare as they might be).
Lastly, if we'll find per-obc to be redundant we can remove it in a followup PR.

@Matan-B

Matan-B commented Jun 6, 2024

Copy link
Copy Markdown
Contributor Author

@athanatos @cyx1231st, results look great!
https://pulpito.ceph.com/matan-2024-06-06_12:44:30-crimson-rados-wip-matanb-crimson-load-exist-distro-crimson-smithi/

I want to merge this asap in order to unblock other awaiting PRs, PTAL.

Matan-B and others added 7 commits June 6, 2024 13:57
to be used only for testing

Signed-off-by: Matan Breizman <[email protected]>
Signed-off-by: Yingxin Cheng <[email protected]>
Signed-off-by: Matan Breizman <[email protected]>
Otherwise, promotion cannot be atomic with the 1st locker.

Identified by: Matan Breizman <[email protected]>

Signed-off-by: Yingxin Cheng <[email protected]>
make use of try_lock in order to support atomicity when called in
ObjectContext::_with_lock()

Co-authored-by: Yingxin Cheng <[email protected]>
Signed-off-by: Matan Breizman <[email protected]>
@Matan-B
Matan-B requested a review from cyx1231st June 6, 2024 13:59
@Matan-B
Matan-B force-pushed the wip-matanb-crimson-load-exist branch from cf00dc7 to 1675ce8 Compare June 6, 2024 14:20
@Matan-B

Matan-B commented Jun 6, 2024

Copy link
Copy Markdown
Contributor Author

changeset: squashed a reverted commit (same diff)

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

@athanatos

Copy link
Copy Markdown
Contributor

jenkins test api

@mattbenjamin

Copy link
Copy Markdown
Contributor

am I correct that a "tri_mutex" is not a mutex but rather a future-enabled reader-writer lock?

@Matan-B

Matan-B commented Jun 19, 2024

Copy link
Copy Markdown
Contributor Author

am I correct that a "tri_mutex" is not a mutex but rather a future-enabled reader-writer lock?

Yes, though not a traditional reader-writer lock, attaching the header comment:

/// shared/exclusive mutual exclusion
///
/// Unlike reader/write lock, tri_mutex does not enforce the exclusive access
/// of write operations, on the contrary, multiple write operations are allowed
/// to hold the same tri_mutex at the same time. Here, what we mean is that we
/// can pipeline reads, and we can pipeline writes, but we cannot allow a read
/// while writes are in progress or a write while reads are in progress.
///
/// tri_mutex is based on seastar::shared_mutex, but instead of two kinds of
/// waiters, tri_mutex keeps track of three kinds of lock users:
/// - readers
/// - writers
/// - exclusive users
///
/// For lock promotion, a read or a write lock is only allowed to be promoted
/// atomically upon the first locking.

@athanatos

Copy link
Copy Markdown
Contributor

@mattbenjamin Kinda, it's multi-writer, multi-reader, single-exclusive. It's there to model the OSD's rules for dealing with concurrent operations on a single object.

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