Skip to content

librados/asio: support 'deferred' and 'use_awaitable' completions - #60576

Merged
yuriw merged 6 commits into
ceph:mainfrom
cbodley:wip-librados-asio-awaitable
Feb 6, 2025
Merged

yuriw merged 6 commits into
ceph:mainfrom
cbodley:wip-librados-asio-awaitable

Conversation

@cbodley

@cbodley cbodley commented Oct 31, 2024

Copy link
Copy Markdown
Contributor

when called with boost::asio::use_awaitable for c++20 coroutines, these functions fail to compile because async_initiate() tries to pass the IoCtx argument by rvalue reference, but our lambda expects lvalue reference:

src/librados/librados_asio.h:196:7: note:   template argument deduction/substitution failed:
boost/asio/impl/use_awaitable.hpp:276:26: note:   cannot convert ‘std::move<librados::v14_2_0::IoCtx&>((* & args#1))’ (type ‘std::remove_reference<librados::v14_2_0::IoCtx&>::type’ {aka ‘librados::v14_2_0::IoCtx’}) to type ‘librados::v14_2_0::IoCtx&’
 276 |     std::move(initiation)(std::move(handler), std::move(args)...);
     |     ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

async_initiate() for boost::asio::deferred shows the same issue:

src/librados/librados_asio.h:196:7: note:   template argument deduction/substitution failed:
boost/asio/async_result.hpp:322:9: note:   cannot convert ‘args#1’ (type ‘const librados::v14_2_0::IoCtx’) to type ‘librados::v14_2_0::IoCtx&’
 322 |         static_cast<Args&&>(args)...);
     |         ^~~~~~~~~~~~~~~~~~~~~~~~~

in both cases, async_initiate() has to copy the arguments for deferred/lazy initiation, then move them into our lambda when invoked. asio documentation doesn't really cover how this works, but i did find this release note from boost 1.70:

The initiate member function must: (a) transform the token into a completion handler object handler; (b) cause the invocation of the function object initiation as if by calling std::forward<Initiation>(initiation)(std::move(handler), std::forward<Args>(args)...). Note that the invocation of initiation may be deferred (e.g. lazily evaluated), in which case initiation and args must be decay-copied and moved as required.

because our lambdas take mutable references, they won't accept these rvalue references. taking them as const references instead allows the caller to pass either lvalue- or rvalue references

this requires internal const_casts to convert them back to mutable references. while casting away const risks undefined behavior, these casts are safe because:

  • in the deferred/lazy case, the arguments were decay-copied (const removed) into temporary storage
  • in the normal case, the initial arguments were passed as mutable refs
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

@cbodley

cbodley commented Oct 31, 2024

Copy link
Copy Markdown
Contributor Author

this requires internal const_casts to convert them back to mutable references. while casting away const risks undefined behavior, these casts are safe because...

i generally only consider const_cast as a last resort, but the only alternatives seemed to be:

  • take arguments by value and move into async_initiate(), which requires a copy in all cases (not just deferred)
  • wrap arguments in std::reference_wrapper so the reference is preserved over decay-copy, which risks a dangling reference in the deferred case

@cbodley

cbodley commented Oct 31, 2024

Copy link
Copy Markdown
Contributor Author

with deferred initiation, the async_operate() overloads risk dangling the pointers for ObjectReadOperation *read_op and ObjectWriteOperation *write_op. ObjectOperation is move-only, so we can't use the same const_cast trick to make these safe. since we can't make a copy, i think we'll have to take the ops by value and move them in

@cbodley
cbodley force-pushed the wip-librados-asio-awaitable branch 3 times, most recently from 2372fec to 24f553e Compare October 31, 2024 22:18
@github-actions

github-actions Bot commented Jan 8, 2025

Copy link
Copy Markdown

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

@cbodley
cbodley force-pushed the wip-librados-asio-awaitable branch from 24f553e to bcc8a6f Compare January 8, 2025 17:52
@cbodley

cbodley commented Jan 8, 2025

Copy link
Copy Markdown
Contributor Author

rebased after merge of #60379

@github-actions

github-actions Bot commented Jan 9, 2025

Copy link
Copy Markdown

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

all these functions required of the ExecutionContext was a
get_executor() member function. just pass the executor instead

c++20 coroutines in asio have easy access to their executor through
`co_await asio::this_coro::executor`, but nothing provides that as
a member function

Signed-off-by: Casey Bodley <[email protected]>
when called with 'use_awaitable' for c++20 coroutines, these functions
fail to compile because async_initiate() tries to pass the IoCtx
argument by rvalue reference, but our lambda expects lvalue reference:

> src/librados/librados_asio.h:196:7: note:   template argument deduction/substitution failed:
> boost/asio/impl/use_awaitable.hpp:276:26: note:   cannot convert ‘std::move<librados::v14_2_0::IoCtx&>((* & args#1))’ (type ‘std::remove_reference<librados::v14_2_0::IoCtx&>::type’ {aka ‘librados::v14_2_0::IoCtx’}) to type ‘librados::v14_2_0::IoCtx&’
>  276 |     std::move(initiation)(std::move(handler), std::move(args)...);
>      |     ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

async_initiate() for 'deferred' shows the same issue:

> src/librados/librados_asio.h:196:7: note:   template argument deduction/substitution failed:
> boost/asio/async_result.hpp:322:9: note:   cannot convert ‘args#1’ (type ‘const librados::v14_2_0::IoCtx’) to type ‘librados::v14_2_0::IoCtx&’
>  322 |         static_cast<Args&&>(args)...);
>      |         ^~~~~~~~~~~~~~~~~~~~~~~~~

in both cases, async_initiate() has to copy the arguments for
deferred/lazy initiation, then move them into our lambda when invoked

because our lambdas take mutable references, they won't accept these
rvalue references. taking them as const references instead allows the
caller to pass either lvalue- or rvalue references

this requires internal const_casts to convert them back to mutable
references. while casting away const risks undefined behavior, these
casts are safe because:
- in the deferred/lazy case, the arguments were decay-copied (const
  removed) into temporary storage
- in the normal case, the initial arguments were passed as mutable refs

Signed-off-by: Casey Bodley <[email protected]>
for these async_operate() overloads, deferred initiation makes it
possible for the ObjectOperations arguments, passed by pointer, to go
out of scope before use

because ObjectOperation is move-only, we can't pass it by reference
like IoCtx& and expect the initiation functions to make a copy when
necessary. so our only option is to use move operations instead,
requiring that the caller pass an rvalue in the first place

Signed-off-by: Casey Bodley <[email protected]>
@cbodley
cbodley force-pushed the wip-librados-asio-awaitable branch from bcc8a6f to 9331694 Compare January 9, 2025 16:44
@cbodley

cbodley commented Jan 9, 2025

Copy link
Copy Markdown
Contributor Author

rebased over conflicts from #60330

@ljflores

ljflores commented Feb 6, 2025

Copy link
Copy Markdown
Member

@yuriw
yuriw merged commit 6275935 into ceph:main Feb 6, 2025
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