You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by "hoslo (via GitHub)" <gi...@apache.org> on 2024/06/24 05:04:31 UTC
[PR] feat: add position write [opendal]
hoslo opened a new pull request, #4795:
URL: https://github.com/apache/opendal/pull/4795
Part #4526
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: add position write [opendal]
Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo commented on code in PR #4795:
URL: https://github.com/apache/opendal/pull/4795#discussion_r1650691033
##########
core/src/raw/oio/write/position_write.rs:
##########
@@ -0,0 +1,283 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::Arc;
+
+use futures::FutureExt;
+use futures::{select, Future};
+
+use crate::raw::*;
+use crate::*;
+
+/// PositionWrite is used to implement [`oio::Write`] based on position write.
+///
+/// # Services
+///
+/// Services like fs support position write.
+///
+/// # Architecture
+///
+/// The architecture after adopting [`PositionWrite`]:
+///
+/// - Services impl `PositionWrite`
+/// - `PositionWriter` impl `Write`
+/// - Expose `PositionWriter` as `Accessor::Writer`
+///
+/// # Requirements
+///
+/// Services that implement `PositionWrite` must fulfill the following requirements:
+///
+/// - Writing data based on position: `offset`.
+pub trait PositionWrite: Send + Sync + Unpin + 'static {
+ /// write_once is used to write the data to underlying storage at once.
+ ///
+ /// PositionWriter will call this API when:
+ ///
+ /// - All the data has been written to the buffer and we can perform the upload at once.
+ fn write_once(&self, body: Buffer) -> impl Future<Output = Result<()>> + MaybeSend;
+
+ /// write_all_at is used to write the data to underlying storage at the specified offset.
+ fn write_all_at(
+ &self,
+ offset: u64,
+ body: Buffer,
Review Comment:
Please use `buf` instead of `body` in API.
##########
core/src/raw/oio/write/position_write.rs:
##########
@@ -0,0 +1,283 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::Arc;
+
+use futures::FutureExt;
+use futures::{select, Future};
+
+use crate::raw::*;
+use crate::*;
+
+/// PositionWrite is used to implement [`oio::Write`] based on position write.
+///
+/// # Services
+///
+/// Services like fs support position write.
+///
+/// # Architecture
+///
+/// The architecture after adopting [`PositionWrite`]:
+///
+/// - Services impl `PositionWrite`
+/// - `PositionWriter` impl `Write`
+/// - Expose `PositionWriter` as `Accessor::Writer`
+///
+/// # Requirements
+///
+/// Services that implement `PositionWrite` must fulfill the following requirements:
+///
+/// - Writing data based on position: `offset`.
+pub trait PositionWrite: Send + Sync + Unpin + 'static {
+ /// write_once is used to write the data to underlying storage at once.
+ ///
+ /// PositionWriter will call this API when:
+ ///
+ /// - All the data has been written to the buffer and we can perform the upload at once.
+ fn write_once(&self, body: Buffer) -> impl Future<Output = Result<()>> + MaybeSend;
Review Comment:
I feel like `PositionWrite` don't need `write_once` anymore since it should be exactly the same with `write_all_at(0, buf)`
##########
core/src/raw/oio/write/position_write.rs:
##########
@@ -0,0 +1,283 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::Arc;
+
+use futures::FutureExt;
+use futures::{select, Future};
+
+use crate::raw::*;
+use crate::*;
+
+/// PositionWrite is used to implement [`oio::Write`] based on position write.
+///
+/// # Services
+///
+/// Services like fs support position write.
+///
+/// # Architecture
+///
+/// The architecture after adopting [`PositionWrite`]:
+///
+/// - Services impl `PositionWrite`
+/// - `PositionWriter` impl `Write`
+/// - Expose `PositionWriter` as `Accessor::Writer`
+///
+/// # Requirements
+///
+/// Services that implement `PositionWrite` must fulfill the following requirements:
+///
+/// - Writing data based on position: `offset`.
+pub trait PositionWrite: Send + Sync + Unpin + 'static {
+ /// write_once is used to write the data to underlying storage at once.
+ ///
+ /// PositionWriter will call this API when:
+ ///
+ /// - All the data has been written to the buffer and we can perform the upload at once.
+ fn write_once(&self, body: Buffer) -> impl Future<Output = Result<()>> + MaybeSend;
+
+ /// write_all_at is used to write the data to underlying storage at the specified offset.
+ fn write_all_at(
+ &self,
+ offset: u64,
+ body: Buffer,
+ ) -> impl Future<Output = Result<()>> + MaybeSend;
+}
+
+#[allow(dead_code)]
Review Comment:
Those code should not be `dead_code` since we have exposed them.
##########
core/src/raw/oio/write/position_write.rs:
##########
@@ -0,0 +1,283 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::Arc;
+
+use futures::FutureExt;
+use futures::{select, Future};
+
+use crate::raw::*;
+use crate::*;
+
+/// PositionWrite is used to implement [`oio::Write`] based on position write.
+///
+/// # Services
+///
+/// Services like fs support position write.
+///
+/// # Architecture
+///
+/// The architecture after adopting [`PositionWrite`]:
+///
+/// - Services impl `PositionWrite`
+/// - `PositionWriter` impl `Write`
+/// - Expose `PositionWriter` as `Accessor::Writer`
+///
+/// # Requirements
+///
+/// Services that implement `PositionWrite` must fulfill the following requirements:
+///
+/// - Writing data based on position: `offset`.
+pub trait PositionWrite: Send + Sync + Unpin + 'static {
+ /// write_once is used to write the data to underlying storage at once.
+ ///
+ /// PositionWriter will call this API when:
+ ///
+ /// - All the data has been written to the buffer and we can perform the upload at once.
+ fn write_once(&self, body: Buffer) -> impl Future<Output = Result<()>> + MaybeSend;
+
+ /// write_all_at is used to write the data to underlying storage at the specified offset.
+ fn write_all_at(
+ &self,
+ offset: u64,
+ body: Buffer,
+ ) -> impl Future<Output = Result<()>> + MaybeSend;
+}
+
+#[allow(dead_code)]
+struct WriteInput<W: PositionWrite> {
+ w: Arc<W>,
+ executor: Executor,
+
+ offset: u64,
+ bytes: Buffer,
+}
+
+/// PositionWriter will implements [`oio::Write`] based on position write.
+pub struct PositionWriter<W: PositionWrite> {
+ w: Arc<W>,
+ executor: Executor,
+
+ next_offset: u64,
+ cache: Option<Buffer>,
+ tasks: ConcurrentTasks<WriteInput<W>, ()>,
+}
+
+#[allow(dead_code)]
+impl<W: PositionWrite> PositionWriter<W> {
+ /// Create a new PositionWriter.
+ pub fn new(inner: W, executor: Option<Executor>, concurrent: usize) -> Self {
+ let executor = executor.unwrap_or_default();
+
+ Self {
+ w: Arc::new(inner),
+ executor: executor.clone(),
+ next_offset: 0,
+ cache: None,
+
+ tasks: ConcurrentTasks::new(executor, concurrent, |input| {
+ Box::pin(async move {
+ let fut = input.w.write_all_at(input.offset, input.bytes.clone());
+ match input.executor.timeout() {
+ None => {
+ let result = fut.await;
+ (input, result)
+ }
+ Some(timeout) => {
+ let result = select! {
+ result = fut.fuse() => {
+ result
+ }
+ _ = timeout.fuse() => {
+ Err(Error::new(
+ ErrorKind::Unexpected, "write position timeout")
+ .with_context("offset", input.offset.to_string())
+ .set_temporary())
+ }
+ };
+ (input, result)
+ }
+ }
+ })
+ }),
+ }
+ }
+
+ fn fill_cache(&mut self, bs: Buffer) -> usize {
+ let size = bs.len();
+ assert!(self.cache.is_none());
+ self.cache = Some(bs);
+ size
+ }
+}
+
+impl<W: PositionWrite> oio::Write for PositionWriter<W> {
+ async fn write(&mut self, bs: Buffer) -> Result<usize> {
+ if self.cache.is_none() {
+ let size = self.fill_cache(bs);
+ return Ok(size);
+ }
+
+ let bytes = self.cache.clone().expect("pending write must exist");
+ let length = bytes.len() as u64;
+ let offset = self.next_offset;
+
+ self.tasks
+ .execute(WriteInput {
+ w: self.w.clone(),
+ executor: self.executor.clone(),
+ offset,
+ bytes,
+ })
+ .await?;
+ self.cache = None;
+ self.next_offset += length;
+ let size = self.fill_cache(bs);
+ Ok(size)
+ }
+
+ async fn close(&mut self) -> Result<()> {
+ // Make sure all tasks are finished.
+ while self.tasks.next().await.transpose()?.is_some() {}
+
+ if let Some(buffer) = self.cache.clone() {
+ let offset = self.next_offset;
+ self.w.write_all_at(offset, buffer).await?;
+ self.cache = None;
+ }
+ Ok(())
+ }
+
+ async fn abort(&mut self) -> Result<()> {
+ Ok(())
Review Comment:
We might consider adding a `truncate` API to discard existing written data. Let's table this feature for future discussion. Would you like to create an issue for this?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org