-
Notifications
You must be signed in to change notification settings - Fork 454
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
refactor: async writer + multi-part #3255
Open
ion-elgreco
wants to merge
3
commits into
delta-io:main
Choose a base branch
from
ion-elgreco:refactor/writer-async
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! Async Sharable Buffer for async writer | ||
//! | ||
use std::sync::Arc; | ||
|
||
use futures::TryFuture; | ||
|
||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
use tokio::io::AsyncWrite; | ||
use tokio::sync::RwLock as TokioRwLock; | ||
|
||
/// An in-memory buffer that allows for shared ownership and interior mutability. | ||
/// The underlying buffer is wrapped in an `Arc` and `RwLock`, so cloning the instance | ||
/// allows multiple owners to have access to the same underlying buffer. | ||
#[derive(Debug, Default, Clone)] | ||
pub struct AsyncShareableBuffer { | ||
buffer: Arc<TokioRwLock<Vec<u8>>>, | ||
} | ||
|
||
impl AsyncShareableBuffer { | ||
/// Consumes this instance and returns the underlying buffer. | ||
/// Returns `None` if there are other references to the instance. | ||
pub async fn into_inner(self) -> Option<Vec<u8>> { | ||
Arc::try_unwrap(self.buffer) | ||
.ok() | ||
.map(|lock| lock.into_inner()) | ||
} | ||
|
||
/// Returns a clone of the underlying buffer as a `Vec`. | ||
pub async fn to_vec(&self) -> Vec<u8> { | ||
let inner = self.buffer.read().await; | ||
inner.clone() | ||
} | ||
|
||
/// Returns the number of bytes in the underlying buffer. | ||
pub async fn len(&self) -> usize { | ||
let inner = self.buffer.read().await; | ||
inner.len() | ||
} | ||
|
||
/// Returns `true` if the underlying buffer is empty. | ||
pub async fn is_empty(&self) -> bool { | ||
let inner = self.buffer.read().await; | ||
inner.is_empty() | ||
} | ||
|
||
/// Creates a new instance with the buffer initialized from the provided bytes. | ||
pub fn from_bytes(bytes: &[u8]) -> Self { | ||
Self { | ||
buffer: Arc::new(TokioRwLock::new(bytes.to_vec())), | ||
} | ||
} | ||
} | ||
|
||
impl AsyncWrite for AsyncShareableBuffer { | ||
fn poll_write( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<std::io::Result<usize>> { | ||
let this = self.clone(); | ||
let buf = buf.to_vec(); | ||
|
||
let fut = async move { | ||
let mut buffer = this.buffer.write().await; | ||
buffer.extend_from_slice(&buf); | ||
Ok(buf.len()) | ||
}; | ||
|
||
tokio::pin!(fut); | ||
fut.try_poll(cx) | ||
} | ||
|
||
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really such a major behavior change. I am not terribly familiar with the maturity level of multipart uploads in
object_store
I don't think this is necessarily a bad change, but I am doubtful of this addressing the originally linked issue.As best as I can tell the buffers are still going to fill up memory until the flush, and then the flush is going to fan out to have parallel uploads
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, in its current form we indeed still buffer longer until the flush, but the buffering and writing has more parallelism now, so it should be faster.
We could do two things here btw: