-
Notifications
You must be signed in to change notification settings - Fork 804
feat(services/azblob): support if_match writes and honor preconditions on block commit #7990
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
shanielh
wants to merge
1
commit into
apache:main
Choose a base branch
from
shanielh:feat/azblob-conditional-writes
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.
+127
−1
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 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
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 |
|---|---|---|
|
|
@@ -57,7 +57,10 @@ pub fn tests(op: &Operator, tests: &mut Vec<Trial>) { | |
| test_writer_futures_copy, | ||
| test_writer_futures_copy_with_concurrent, | ||
| test_writer_return_metadata, | ||
| test_writer_write_non_contiguous_data | ||
| test_writer_write_non_contiguous_data, | ||
| test_writer_write_with_if_not_exists, | ||
| test_writer_write_with_if_none_match, | ||
| test_writer_write_with_if_match | ||
| )) | ||
| } | ||
|
|
||
|
|
@@ -810,6 +813,110 @@ pub async fn test_write_with_if_match(op: Operator) -> Result<()> { | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Writing more than once before `close()` commits through the service's multi-part | ||
| /// completion request instead of a single-shot upload. Preconditions must still be honored | ||
| /// on that path, otherwise a conditional write silently degrades to an unconditional | ||
| /// overwrite. | ||
| /// | ||
| /// Services normally evaluate the precondition at commit time, but some may reject earlier, | ||
| /// so an error from either `write()` or `close()` is accepted. | ||
| async fn write_conditionally_in_chunks( | ||
| w: &mut Writer, | ||
| content: &[u8], | ||
| ) -> opendal::Result<Metadata> { | ||
| w.write(content.to_vec()).await?; | ||
| w.write(content.to_vec()).await?; | ||
| w.close().await | ||
| } | ||
|
|
||
| /// Write an existing file through a chunked writer with if_not_exists should get a | ||
| /// ConditionNotMatch error. | ||
| pub async fn test_writer_write_with_if_not_exists(op: Operator) -> Result<()> { | ||
|
Member
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. Please check |
||
| let cap = op.info().capability(); | ||
| if !cap.write_with_if_not_exists || !cap.write_can_multi { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let (path, content, _) = TEST_FIXTURE.new_file(op.clone()); | ||
|
|
||
| op.write(&path, content.clone()) | ||
| .await | ||
| .expect("write must succeed"); | ||
|
|
||
| let mut w = op.writer_with(&path).if_not_exists(true).await?; | ||
| let res = write_conditionally_in_chunks(&mut w, &content).await; | ||
| assert!(res.is_err()); | ||
| assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Write an existing file through a chunked writer with its own etag as if_none_match | ||
| /// should get a ConditionNotMatch error. | ||
| pub async fn test_writer_write_with_if_none_match(op: Operator) -> Result<()> { | ||
| let cap = op.info().capability(); | ||
| if !cap.write_with_if_none_match || !cap.write_can_multi { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let (path, content, _) = TEST_FIXTURE.new_file(op.clone()); | ||
|
|
||
| op.write(&path, content.clone()) | ||
| .await | ||
| .expect("write must succeed"); | ||
|
|
||
| let meta = op.stat(&path).await?; | ||
| let etag = meta.etag().expect("etag must exist"); | ||
|
|
||
| let mut w = op.writer_with(&path).if_none_match(etag).await?; | ||
| let res = write_conditionally_in_chunks(&mut w, &content).await; | ||
| assert!(res.is_err()); | ||
| assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Write a file through a chunked writer with if_match should succeed with the file's own | ||
| /// etag and get a ConditionNotMatch error with a stale one. | ||
| pub async fn test_writer_write_with_if_match(op: Operator) -> Result<()> { | ||
| let cap = op.info().capability(); | ||
| if !cap.write_with_if_match || !cap.write_can_multi { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let (path_a, content_a, _) = TEST_FIXTURE.new_file(op.clone()); | ||
| let (path_b, content_b, _) = TEST_FIXTURE.new_file(op.clone()); | ||
|
|
||
| op.write(&path_a, content_a.clone()).await?; | ||
| op.write(&path_b, content_b.clone()).await?; | ||
|
|
||
| let etag_a = op | ||
| .stat(&path_a) | ||
| .await? | ||
| .etag() | ||
| .expect("etag must exist") | ||
| .to_string(); | ||
| let etag_b = op | ||
| .stat(&path_b) | ||
| .await? | ||
| .etag() | ||
| .expect("etag must exist") | ||
| .to_string(); | ||
|
|
||
| // Should succeed: writing to path_a with its own etag. | ||
| let mut w = op.writer_with(&path_a).if_match(&etag_a).await?; | ||
| let res = write_conditionally_in_chunks(&mut w, &content_a).await; | ||
| assert!(res.is_ok()); | ||
|
|
||
| // Should fail: writing to path_a with path_b's etag. | ||
| let mut w = op.writer_with(&path_a).if_match(&etag_b).await?; | ||
| let res = write_conditionally_in_chunks(&mut w, &content_a).await; | ||
| assert!(res.is_err()); | ||
| assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub async fn test_writer_write_non_contiguous_data(op: Operator) -> Result<()> { | ||
| let path = TEST_FIXTURE.new_file_path(); | ||
| let size = 1024 * 1024; // write file with 1 MiB | ||
|
|
||
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.
Please avoiding adding helper functions, it's easier for review and check by placing everything in the sane function.