-
Notifications
You must be signed in to change notification settings - Fork 204
Fast-slow deadlock #1978
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
Open
chrisstaite
wants to merge
3
commits into
TraceMachina:main
Choose a base branch
from
chrisstaite:bugfix/Fast-slow-deadlock
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
Fast-slow deadlock #1978
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -41,6 +41,12 @@ use crate::gcs_client::types::{ | |
| SIMPLE_UPLOAD_THRESHOLD, Timestamp, | ||
| }; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct UploadRef { | ||
| pub upload_ref: String, | ||
| pub(crate) _permit: OwnedSemaphorePermit, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is key because if means that connections are only held during active uploads, I think. |
||
| } | ||
|
|
||
| /// A trait that defines the required GCS operations. | ||
| /// This abstraction allows for easier testing by mocking GCS responses. | ||
| pub trait GcsOperations: Send + Sync + Debug { | ||
|
|
@@ -71,12 +77,12 @@ pub trait GcsOperations: Send + Sync + Debug { | |
| fn start_resumable_write( | ||
| &self, | ||
| object_path: &ObjectPath, | ||
| ) -> impl Future<Output = Result<String, Error>> + Send; | ||
| ) -> impl Future<Output = Result<UploadRef, Error>> + Send; | ||
|
|
||
| /// Upload a chunk of data in a resumable upload session | ||
| fn upload_chunk( | ||
| &self, | ||
| upload_url: &str, | ||
| upload_url: &UploadRef, | ||
| object_path: &ObjectPath, | ||
| data: Bytes, | ||
| offset: u64, | ||
|
|
@@ -336,12 +342,21 @@ impl GcsClient { | |
| } | ||
|
|
||
| // Check if the object exists | ||
| match self.read_object_metadata(object_path).await? { | ||
| Some(_) => Ok(()), | ||
| None => Err(make_err!( | ||
| Code::Internal, | ||
| "Upload completed but object not found" | ||
| )), | ||
| let request = GetObjectRequest { | ||
| bucket: object_path.bucket.clone(), | ||
| object: object_path.path.clone(), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| match self.client.get_object(&request).await { | ||
| Ok(_) => Ok(()), | ||
| Err(GcsError::Response(resp)) if resp.code == 404 => { | ||
| return Err(make_err!( | ||
| Code::Internal, | ||
| "Upload completed but object not found" | ||
| )); | ||
| } | ||
| Err(err) => Err(Self::handle_gcs_error(&err)), | ||
| } | ||
| }) | ||
| .await | ||
|
|
@@ -470,55 +485,58 @@ impl GcsOperations for GcsClient { | |
| .await | ||
| } | ||
|
|
||
| async fn start_resumable_write(&self, object_path: &ObjectPath) -> Result<String, Error> { | ||
| self.with_connection(|| async { | ||
| let request = UploadObjectRequest { | ||
| bucket: object_path.bucket.clone(), | ||
| ..Default::default() | ||
| }; | ||
| async fn start_resumable_write(&self, object_path: &ObjectPath) -> Result<UploadRef, Error> { | ||
| let permit = | ||
| self.semaphore.clone().acquire_owned().await.map_err(|e| { | ||
| make_err!(Code::Internal, "Failed to acquire connection permit: {}", e) | ||
| })?; | ||
| let request = UploadObjectRequest { | ||
| bucket: object_path.bucket.clone(), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let upload_type = UploadType::Multipart(Box::new(Object { | ||
| name: object_path.path.clone(), | ||
| content_type: Some(DEFAULT_CONTENT_TYPE.to_string()), | ||
| ..Default::default() | ||
| })); | ||
| let upload_type = UploadType::Multipart(Box::new(Object { | ||
| name: object_path.path.clone(), | ||
| content_type: Some(DEFAULT_CONTENT_TYPE.to_string()), | ||
| ..Default::default() | ||
| })); | ||
|
|
||
| // Start resumable upload session | ||
| let uploader = self | ||
| .client | ||
| .prepare_resumable_upload(&request, &upload_type) | ||
| .await | ||
| .map_err(|e| Self::handle_gcs_error(&e))?; | ||
| // Start resumable upload session | ||
| let uploader = self | ||
| .client | ||
| .prepare_resumable_upload(&request, &upload_type) | ||
| .await | ||
| .map_err(|e| Self::handle_gcs_error(&e))?; | ||
|
|
||
| Ok(uploader.url().to_string()) | ||
| Ok(UploadRef { | ||
| upload_ref: uploader.url().to_string(), | ||
| _permit: permit, | ||
| }) | ||
| .await | ||
| } | ||
|
|
||
| async fn upload_chunk( | ||
| &self, | ||
| upload_url: &str, | ||
| upload_url: &UploadRef, | ||
| _object_path: &ObjectPath, | ||
| data: Bytes, | ||
| offset: u64, | ||
| end_offset: u64, | ||
| total_size: Option<u64>, | ||
| ) -> Result<(), Error> { | ||
| self.with_connection(|| async { | ||
| let uploader = self.client.get_resumable_upload(upload_url.to_string()); | ||
| let uploader = self | ||
| .client | ||
| .get_resumable_upload(upload_url.upload_ref.clone()); | ||
|
|
||
| let last_byte = if end_offset == 0 { 0 } else { end_offset - 1 }; | ||
| let chunk_def = ChunkSize::new(offset, last_byte, total_size); | ||
| let last_byte = if end_offset == 0 { 0 } else { end_offset - 1 }; | ||
| let chunk_def = ChunkSize::new(offset, last_byte, total_size); | ||
|
|
||
| // Upload chunk | ||
| uploader | ||
| .upload_multiple_chunk(data, &chunk_def) | ||
| .await | ||
| .map_err(|e| Self::handle_gcs_error(&e))?; | ||
| // Upload chunk | ||
| uploader | ||
| .upload_multiple_chunk(data, &chunk_def) | ||
| .await | ||
| .map_err(|e| Self::handle_gcs_error(&e))?; | ||
|
|
||
| Ok(()) | ||
| }) | ||
| .await | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn upload_from_reader( | ||
|
|
||
This file contains hidden or 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.
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.
Important fix.