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

feat(service/azdls): add append support for azdls #3186

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions core/src/services/azdls/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use super::error::parse_error;
use super::pager::AzdlsPager;
use super::writer::AzdlsWriter;
use crate::raw::*;
use crate::services::azdls::writer::AzdlsWriters;
use crate::*;

/// Known endpoint suffix Azure Data Lake Storage Gen2 URI syntax.
Expand Down Expand Up @@ -230,7 +231,7 @@ pub struct AzdlsBackend {
impl Accessor for AzdlsBackend {
type Reader = IncomingAsyncBody;
type BlockingReader = ();
type Writer = oio::OneShotWriter<AzdlsWriter>;
type Writer = AzdlsWriters;
type BlockingWriter = ();
type Pager = AzdlsPager;
type BlockingPager = ();
Expand All @@ -248,6 +249,7 @@ impl Accessor for AzdlsBackend {
read_with_range: true,

write: true,
write_can_append: true,
create_dir: true,
delete: true,
rename: true,
Expand Down Expand Up @@ -299,10 +301,13 @@ impl Accessor for AzdlsBackend {
}

async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
Ok((
RpWrite::default(),
oio::OneShotWriter::new(AzdlsWriter::new(self.core.clone(), args, path.to_string())),
))
let w = AzdlsWriter::new(self.core.clone(), args.clone(), path.to_string());
let w = if args.append() {
AzdlsWriters::Two(oio::AppendObjectWriter::new(w))
} else {
AzdlsWriters::One(oio::OneShotWriter::new(w))
};
Ok((RpWrite::default(), w))
}

async fn rename(&self, from: &str, to: &str, _args: OpRename) -> Result<RpRename> {
Expand Down
8 changes: 5 additions & 3 deletions core/src/services/azdls/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,20 @@ impl AzdlsCore {
pub fn azdls_update_request(
&self,
path: &str,
size: Option<usize>,
size: Option<u64>,
position: u64,
body: AsyncBody,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);

// - close: Make this is the final action to this file.
// - flush: Flush the file directly.
let url = format!(
"{}/{}/{}?action=append&close=true&flush=true&position=0",
"{}/{}/{}?action=append&close=true&flush=true&position={}",
self.endpoint,
self.filesystem,
percent_encode_path(&p)
percent_encode_path(&p),
position
);

let mut req = Request::patch(&url);
Expand Down
43 changes: 42 additions & 1 deletion core/src/services/azdls/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ use crate::raw::oio::WriteBuf;
use crate::raw::*;
use crate::*;

pub type AzdlsWriters =
oio::TwoWaysWriter<oio::OneShotWriter<AzdlsWriter>, oio::AppendObjectWriter<AzdlsWriter>>;

pub struct AzdlsWriter {
core: Arc<AzdlsCore>,

Expand Down Expand Up @@ -65,7 +68,8 @@ impl oio::OneShotWrite for AzdlsWriter {
let bs = oio::ChunkedBytes::from_vec(bs.vectored_bytes(bs.remaining()));
let mut req = self.core.azdls_update_request(
&self.path,
Some(bs.len()),
Some(bs.len() as u64),
0,
AsyncBody::ChunkedBytes(bs),
)?;

Expand All @@ -85,3 +89,40 @@ impl oio::OneShotWrite for AzdlsWriter {
}
}
}

#[async_trait]
impl oio::AppendObjectWrite for AzdlsWriter {
async fn offset(&self) -> Result<u64> {
let resp = self.core.azdls_get_properties(&self.path).await?;

let status = resp.status();
let headers = resp.headers();

match status {
StatusCode::OK => Ok(parse_content_length(headers)?.unwrap_or_default()),
StatusCode::NOT_FOUND => Ok(0),
_ => Err(parse_error(resp).await?),
}
}

async fn append(&self, offset: u64, size: u64, body: AsyncBody) -> Result<()> {
let mut req = self
.core
.azdls_update_request(&self.path, Some(size), offset, body)?;

self.core.sign(&mut req).await?;

let resp = self.core.send(req).await?;

let status = resp.status();
match status {
StatusCode::OK | StatusCode::ACCEPTED => {
resp.into_body().consume().await?;
Ok(())
}
_ => Err(parse_error(resp)
.await?
.with_operation("Backend::azdls_update_request")),
}
}
}