Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Slice<_> in write path instead of B: BoundedBuf<...> #8225

Closed
wants to merge 6 commits into from

Conversation

arpad-m
Copy link
Member

@arpad-m arpad-m commented Jul 1, 2024

Adopts Slice<_> instead of BoundedBuf<_> in the write path. This is a smaller version of what we could have done, as the OwnedAsyncWriter trait still uses the old API, but we leave this for the future. Mainly, this change is meant to unblock #8106.

Prior PR: #8186 which did this for the read path.

@arpad-m arpad-m requested a review from a team as a code owner July 1, 2024 17:02
@arpad-m arpad-m requested a review from problame July 1, 2024 17:02
Copy link

github-actions bot commented Jul 1, 2024

3000 tests run: 2885 passed, 0 failed, 115 skipped (full report)


Flaky tests (1)

Postgres 14

Code coverage* (full report)

  • functions: 32.7% (6912 of 21132 functions)
  • lines: 50.1% (54241 of 108348 lines)

* collected from Rust tests only


The comment gets automatically updated with the latest test results
80f68a0 at 2024-07-02T13:49:38.717Z :recycle:

Copy link
Contributor

@problame problame left a comment

Choose a reason for hiding this comment

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

I strongly object returning Slice


I'm wondering if we should assert! that the slice that is passed in to write_all has bytes_init() == bytes_total().

I think all the calling code adheres to this.
Orthogonal to this PR though.

Comment on lines 674 to 681
/// Returns the IoBuf that is underlying the BoundedBuf `buf`.
/// I.e., the returned value's `bytes_init()` method returns something different than the `bytes_init()` that was passed in.
/// It's quite brittle and easy to mis-use, so, we return the size in the Ok() variant.
pub async fn write_all<B: BoundedBuf<Buf = Buf>, Buf: IoBuf + Send>(
pub async fn write_all<Buf: IoBuf + Send>(
&mut self,
buf: B,
buf: Slice<Buf>,
ctx: &RequestContext,
) -> (B::Buf, Result<usize, Error>) {
) -> (Slice<Buf>, Result<usize, Error>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. The doc comment is out of date.
  2. I think we should return the Buf instead of the Slice, so, the doc comments aren't out of date if you follow that suggestion.

Reason why we should return the Buf instead of the Slice<Buf>: the bounds aren't the same when we return, that could mis-lead users of this API.

If you want to keep the bounds, please take inspiration from the doc comment and assertions in read_exact_at.

Copy link
Member Author

Choose a reason for hiding this comment

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

We had a 1:1 call and we agreed it is fine to have an API that mirrors the read API as long as the returned slice matches precisely the passed slice (like in the read API, see read_exact_at).

@arpad-m arpad-m requested a review from problame July 1, 2024 23:21
ctx: &RequestContext,
) -> (B::Buf, Result<usize, Error>) {
) -> (Slice<Buf>, Result<usize, Error>) {
let begin_end = (buf.begin(), buf.end());
Copy link
Contributor

Choose a reason for hiding this comment

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

Should use buf.bounds()

&mut self,
buf: B,
buf: Slice<Buf>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Please rename to slice

macro_rules! return_ {
($buf:expr, $val:expr) => {{
let buf = $buf.into_inner();
return (buf.slice(begin_end.0..begin_end.1), $val);
Copy link
Contributor

Choose a reason for hiding this comment

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

After using buf.bounds() above, you can just pass buf.slice(begin_end) here.

Comment on lines +228 to +232
pub async fn write_blob<Buf: IoBuf + Send>(
&mut self,
srcbuf: B,
srcbuf: Slice<Buf>,
ctx: &RequestContext,
) -> (B::Buf, Result<u64, Error>) {
) -> (Slice<Buf>, Result<u64, Error>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

No user of blob_io needs the Slice. Why change it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess there was a misunderstanding in our 1:1 call: I was strictly speaking about the VirtualFile API, you were probably thinking callers should be changed as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at the PR for why you're doing this one (#8106) I think we should leave write_blob's signature and blob_io.rs' write_all signature unchanged.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was strictly speaking about the VirtualFile API, you were probably thinking callers should be changed as well.

Ah yeah there was a misunderstanding. The reason why I made this PR was to change the API of BlobWriter::write_blob to workaround neondatabase/tokio-epoll-uring#46.

I didn't hit issues with VirtualFile, I only changed it because it was the path of least resistance to getting something that builds and where tests pass (and I thought you told me that that API design is better).

Copy link
Member Author

Choose a reason for hiding this comment

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

No user of blob_io needs the Slice. Why change it?

to expand, there is no way to obtain B::Buf from B without slicing (please tell me if there is such a way). If B is an empty buffer, slicing panics. With a Slice, at least I can return srcbuf here.

Copy link
Contributor

Choose a reason for hiding this comment

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

(please tell me if there is such a way)

let foo: B = ...;
let underlying: B::Buf = foo.slice_full().into_inner();

@arpad-m
Copy link
Member Author

arpad-m commented Jul 2, 2024

closing as Christian pointed out a way to merge #8106 without this PR (.slice_full()) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants