Xuanwo commented on code in PR #2780: URL: https://github.com/apache/incubator-opendal/pull/2780#discussion_r1284283153
########## core/src/layers/blocking.rs: ########## @@ -0,0 +1,262 @@ +// 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 async_trait::async_trait; +use bytes; +use bytes::Bytes; +use tokio::runtime; + +use crate::raw::oio::ReadExt; +use crate::raw::*; +use crate::*; + +/// Add blocking API support for every operations. +/// +/// # Blocking API +/// +/// - This layer is auto-added to the operator if it's accessor doesn't support blocking APIs. +/// +/// Tracking issue: #2678 +#[derive(Debug, Clone)] +pub struct BlockingLayer { + runtime: Arc<runtime::Runtime>, +} + +impl Default for BlockingLayer { + fn default() -> Self { + let runtime = runtime::Builder::new_current_thread() Review Comment: Start a runtime in async context chould be a problem. Maybe we should spawn a new thread to do that? ########## core/src/layers/blocking.rs: ########## @@ -0,0 +1,262 @@ +// 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 async_trait::async_trait; +use bytes; +use bytes::Bytes; +use tokio::runtime; + +use crate::raw::oio::ReadExt; +use crate::raw::*; +use crate::*; + +/// Add blocking API support for every operations. +/// +/// # Blocking API +/// +/// - This layer is auto-added to the operator if it's accessor doesn't support blocking APIs. +/// +/// Tracking issue: #2678 +#[derive(Debug, Clone)] +pub struct BlockingLayer { + runtime: Arc<runtime::Runtime>, +} + +impl Default for BlockingLayer { + fn default() -> Self { + let runtime = runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + Self { + runtime: Arc::new(runtime), + } + } +} + +impl<A: Accessor> Layer<A> for BlockingLayer { + type LayeredAccessor = BlockingAccessor<A>; + + fn layer(&self, inner: A) -> Self::LayeredAccessor { + BlockingAccessor { + inner: inner, + runtime: self.runtime.clone(), + } + } +} + +#[derive(Clone, Debug)] +pub struct BlockingAccessor<A: Accessor> { + inner: A, + + runtime: Arc<runtime::Runtime>, +} + +#[async_trait] +impl<A: Accessor> LayeredAccessor for BlockingAccessor<A> { + type Inner = A; + type Reader = A::Reader; + type BlockingReader = BlockingReader<A::Reader>; + type Writer = A::Writer; + type BlockingWriter = BlockingWriter<A::Writer>; + type Appender = A::Appender; + type Pager = A::Pager; + type BlockingPager = BlockingPager<A::Pager>; + + fn inner(&self) -> &Self::Inner { + &self.inner + } + + fn metadata(&self) -> AccessorInfo { + let mut info = self.inner.info(); + let capability = info.capability_mut(); + capability.blocking = true; + capability.read_can_next = true; Review Comment: It's better not to change them. ########## core/src/layers/blocking.rs: ########## @@ -0,0 +1,262 @@ +// 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 async_trait::async_trait; +use bytes; +use bytes::Bytes; +use tokio::runtime; + +use crate::raw::oio::ReadExt; +use crate::raw::*; +use crate::*; + +/// Add blocking API support for every operations. +/// +/// # Blocking API +/// +/// - This layer is auto-added to the operator if it's accessor doesn't support blocking APIs. +/// +/// Tracking issue: #2678 +#[derive(Debug, Clone)] +pub struct BlockingLayer { + runtime: Arc<runtime::Runtime>, +} + +impl Default for BlockingLayer { + fn default() -> Self { + let runtime = runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + Self { + runtime: Arc::new(runtime), + } + } +} + +impl<A: Accessor> Layer<A> for BlockingLayer { + type LayeredAccessor = BlockingAccessor<A>; + + fn layer(&self, inner: A) -> Self::LayeredAccessor { + BlockingAccessor { + inner: inner, + runtime: self.runtime.clone(), + } + } +} + +#[derive(Clone, Debug)] +pub struct BlockingAccessor<A: Accessor> { + inner: A, + + runtime: Arc<runtime::Runtime>, +} + +#[async_trait] +impl<A: Accessor> LayeredAccessor for BlockingAccessor<A> { + type Inner = A; + type Reader = A::Reader; + type BlockingReader = BlockingReader<A::Reader>; + type Writer = A::Writer; + type BlockingWriter = BlockingWriter<A::Writer>; + type Appender = A::Appender; + type Pager = A::Pager; + type BlockingPager = BlockingPager<A::Pager>; + + fn inner(&self) -> &Self::Inner { + &self.inner + } + + fn metadata(&self) -> AccessorInfo { + let mut info = self.inner.info(); + let capability = info.capability_mut(); + capability.blocking = true; + capability.read_can_next = true; + capability.read_can_seek = true; + info + } + + async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> { + self.inner.create_dir(path, args).await + } + + async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> { + self.inner.read(path, args).await + } + + async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> { + self.inner.write(path, args).await + } + + async fn append(&self, path: &str, args: OpAppend) -> Result<(RpAppend, Self::Appender)> { + self.inner.append(path, args).await + } + + async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> { + self.inner.copy(from, to, args).await + } + + async fn rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> { + self.inner.rename(from, to, args).await + } + + async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> { + self.inner.stat(path, args).await + } + + async fn delete(&self, path: &str, args: OpDelete) -> Result<RpDelete> { + self.inner.delete(path, args).await + } + + async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Pager)> { + self.inner.list(path, args).await + } + + async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> { + self.inner.presign(path, args).await + } + + async fn batch(&self, args: OpBatch) -> Result<RpBatch> { + self.inner.batch(args).await + } + + fn blocking_create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> { + self.runtime.block_on(self.inner.create_dir(path, args)) + } + + fn blocking_read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::BlockingReader)> { + self.runtime.block_on(async { + let (rp, reader) = self.inner.read(path, args).await?; + let blocking_reader = BlockingReader::new(self.runtime.clone(), reader); + + Ok((rp, blocking_reader)) + }) + } + + fn blocking_write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::BlockingWriter)> { + self.runtime.block_on(async { + let (rp, writer) = self.inner.write(path, args).await?; + let blocking_writer = Self::BlockingWriter::new(self.runtime.clone(), writer); + Ok((rp, blocking_writer)) + }) + } + + fn blocking_copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> { + self.runtime.block_on(self.inner.copy(from, to, args)) + } + + fn blocking_rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> { + self.runtime.block_on(self.inner.rename(from, to, args)) + } + + fn blocking_stat(&self, path: &str, args: OpStat) -> Result<RpStat> { + self.runtime.block_on(self.inner.stat(path, args)) + } + + fn blocking_delete(&self, path: &str, args: OpDelete) -> Result<RpDelete> { + self.runtime.block_on(self.inner.delete(path, args)) + } + + fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingPager)> { + self.runtime.block_on(async { + let (rp, pager) = self.inner.list(path, args).await?; + let blocking_pager = Self::BlockingPager::new(self.runtime.clone(), pager); + Ok((rp, blocking_pager)) + }) + } +} + +pub struct BlockingReader<R> { + runtime: Arc<runtime::Runtime>, + inner: R, +} + +impl<R> BlockingReader<R> { + fn new(runtime: Arc<runtime::Runtime>, inner: R) -> Self { + Self { runtime, inner } + } +} + +// impl<R: oio::Read> oio::Read for BlockingReader<R> { Review Comment: Let's remove not needed code. ########## core/src/layers/blocking.rs: ########## @@ -0,0 +1,262 @@ +// 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 async_trait::async_trait; +use bytes; +use bytes::Bytes; +use tokio::runtime; + +use crate::raw::oio::ReadExt; +use crate::raw::*; +use crate::*; + +/// Add blocking API support for every operations. +/// +/// # Blocking API +/// +/// - This layer is auto-added to the operator if it's accessor doesn't support blocking APIs. +/// +/// Tracking issue: #2678 +#[derive(Debug, Clone)] +pub struct BlockingLayer { + runtime: Arc<runtime::Runtime>, +} + +impl Default for BlockingLayer { + fn default() -> Self { + let runtime = runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + Self { + runtime: Arc::new(runtime), + } + } +} + +impl<A: Accessor> Layer<A> for BlockingLayer { + type LayeredAccessor = BlockingAccessor<A>; + + fn layer(&self, inner: A) -> Self::LayeredAccessor { + BlockingAccessor { + inner: inner, + runtime: self.runtime.clone(), + } + } +} + +#[derive(Clone, Debug)] +pub struct BlockingAccessor<A: Accessor> { + inner: A, + + runtime: Arc<runtime::Runtime>, Review Comment: Maybe we should implement drop by ourselves. ########## core/src/layers/blocking.rs: ########## @@ -0,0 +1,262 @@ +// 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 async_trait::async_trait; +use bytes; +use bytes::Bytes; +use tokio::runtime; + +use crate::raw::oio::ReadExt; +use crate::raw::*; +use crate::*; + +/// Add blocking API support for every operations. +/// +/// # Blocking API +/// +/// - This layer is auto-added to the operator if it's accessor doesn't support blocking APIs. +/// +/// Tracking issue: #2678 +#[derive(Debug, Clone)] +pub struct BlockingLayer { + runtime: Arc<runtime::Runtime>, +} + +impl Default for BlockingLayer { + fn default() -> Self { + let runtime = runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + Self { + runtime: Arc::new(runtime), + } + } +} + +impl<A: Accessor> Layer<A> for BlockingLayer { + type LayeredAccessor = BlockingAccessor<A>; + + fn layer(&self, inner: A) -> Self::LayeredAccessor { + BlockingAccessor { + inner: inner, + runtime: self.runtime.clone(), + } + } +} + +#[derive(Clone, Debug)] +pub struct BlockingAccessor<A: Accessor> { + inner: A, + + runtime: Arc<runtime::Runtime>, +} + +#[async_trait] +impl<A: Accessor> LayeredAccessor for BlockingAccessor<A> { + type Inner = A; + type Reader = A::Reader; + type BlockingReader = BlockingReader<A::Reader>; + type Writer = A::Writer; + type BlockingWriter = BlockingWriter<A::Writer>; + type Appender = A::Appender; + type Pager = A::Pager; + type BlockingPager = BlockingPager<A::Pager>; + + fn inner(&self) -> &Self::Inner { + &self.inner + } + + fn metadata(&self) -> AccessorInfo { + let mut info = self.inner.info(); + let capability = info.capability_mut(); + capability.blocking = true; + capability.read_can_next = true; + capability.read_can_seek = true; + info + } + + async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> { + self.inner.create_dir(path, args).await + } + + async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> { + self.inner.read(path, args).await + } + + async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> { + self.inner.write(path, args).await + } + + async fn append(&self, path: &str, args: OpAppend) -> Result<(RpAppend, Self::Appender)> { + self.inner.append(path, args).await + } + + async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> { + self.inner.copy(from, to, args).await + } + + async fn rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> { + self.inner.rename(from, to, args).await + } + + async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> { + self.inner.stat(path, args).await + } + + async fn delete(&self, path: &str, args: OpDelete) -> Result<RpDelete> { + self.inner.delete(path, args).await + } + + async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Pager)> { + self.inner.list(path, args).await + } + + async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> { + self.inner.presign(path, args).await + } + + async fn batch(&self, args: OpBatch) -> Result<RpBatch> { + self.inner.batch(args).await + } + + fn blocking_create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> { + self.runtime.block_on(self.inner.create_dir(path, args)) + } + + fn blocking_read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::BlockingReader)> { + self.runtime.block_on(async { + let (rp, reader) = self.inner.read(path, args).await?; + let blocking_reader = BlockingReader::new(self.runtime.clone(), reader); + + Ok((rp, blocking_reader)) + }) + } + + fn blocking_write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::BlockingWriter)> { + self.runtime.block_on(async { + let (rp, writer) = self.inner.write(path, args).await?; + let blocking_writer = Self::BlockingWriter::new(self.runtime.clone(), writer); + Ok((rp, blocking_writer)) + }) + } + + fn blocking_copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> { + self.runtime.block_on(self.inner.copy(from, to, args)) + } + + fn blocking_rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> { + self.runtime.block_on(self.inner.rename(from, to, args)) + } + + fn blocking_stat(&self, path: &str, args: OpStat) -> Result<RpStat> { + self.runtime.block_on(self.inner.stat(path, args)) + } + + fn blocking_delete(&self, path: &str, args: OpDelete) -> Result<RpDelete> { + self.runtime.block_on(self.inner.delete(path, args)) + } + + fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingPager)> { + self.runtime.block_on(async { + let (rp, pager) = self.inner.list(path, args).await?; + let blocking_pager = Self::BlockingPager::new(self.runtime.clone(), pager); + Ok((rp, blocking_pager)) + }) + } +} + +pub struct BlockingReader<R> { + runtime: Arc<runtime::Runtime>, + inner: R, +} + +impl<R> BlockingReader<R> { + fn new(runtime: Arc<runtime::Runtime>, inner: R) -> Self { + Self { runtime, inner } + } +} + +// impl<R: oio::Read> oio::Read for BlockingReader<R> { +// fn poll_read(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize>> { +// self.inner.poll_read(cx, buf) +// } + +// fn poll_seek(&mut self, cx: &mut Context<'_>, pos: io::SeekFrom) -> Poll<Result<u64>> { +// self.inner.poll_seek(cx, pos) +// } + +// fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes>>> { +// self.inner.poll_next(cx) +// } +// } + +impl<R: oio::Read + 'static> oio::BlockingRead for BlockingReader<R> { + fn read(&mut self, buf: &mut [u8]) -> Result<usize> { + self.runtime + .block_on(oio::ReadExt::read(&mut self.inner, buf)) + } + + fn seek(&mut self, pos: std::io::SeekFrom) -> Result<u64> { + self.runtime.block_on(self.inner.seek(pos)) + } + + fn next(&mut self) -> Option<Result<Bytes>> { + self.runtime.block_on(self.inner.next()) + } +} + +pub struct BlockingWriter<W> { Review Comment: We can implement a `BlockingWrapper` and implement different traits for it. ########## core/src/types/operator/builder.rs: ########## @@ -371,6 +371,11 @@ impl<A: Accessor> OperatorBuilder<A> { pub fn finish(self) -> Operator { let ob = self.layer(TypeEraseLayer); - Operator::from_inner(Arc::new(ob.accessor) as FusedAccessor) + let op = Operator::from_inner(Arc::new(ob.accessor) as FusedAccessor); + if !op.info().can_blocking() { + op.layer(BlockingLayer::default()) Review Comment: blocking layer should not be enabled by default since it will introduce cost for users don't want this. We can enable blocking layer in behavior tests directly for test. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
