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: make AsyncBacktraceLayer covers oio::Read and oio::Write #4789

Merged
merged 3 commits into from
Jun 23, 2024
Merged
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
104 changes: 92 additions & 12 deletions core/src/layers/async_backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.

use futures::FutureExt;

use crate::raw::*;
use crate::*;

Expand Down Expand Up @@ -58,25 +60,31 @@ pub struct AsyncBacktraceAccessor<A: Access> {

impl<A: Access> LayeredAccess for AsyncBacktraceAccessor<A> {
type Inner = A;
type Reader = A::Reader;
type BlockingReader = A::BlockingReader;
type Writer = A::Writer;
type BlockingWriter = A::BlockingWriter;
type Lister = A::Lister;
type BlockingLister = A::BlockingLister;
type Reader = AsyncBacktraceWrapper<A::Reader>;
type BlockingReader = AsyncBacktraceWrapper<A::BlockingReader>;
type Writer = AsyncBacktraceWrapper<A::Writer>;
type BlockingWriter = AsyncBacktraceWrapper<A::BlockingWriter>;
type Lister = AsyncBacktraceWrapper<A::Lister>;
type BlockingLister = AsyncBacktraceWrapper<A::BlockingLister>;

fn inner(&self) -> &Self::Inner {
&self.inner
}

#[async_backtrace::framed]
async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
self.inner.read(path, args).await
self.inner
.read(path, args)
.map(|v| v.map(|(rp, r)| (rp, AsyncBacktraceWrapper::new(r))))
.await
}

#[async_backtrace::framed]
async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
self.inner.write(path, args).await
self.inner
.write(path, args)
.map(|v| v.map(|(rp, r)| (rp, AsyncBacktraceWrapper::new(r))))
.await
}

#[async_backtrace::framed]
Expand All @@ -101,7 +109,10 @@ impl<A: Access> LayeredAccess for AsyncBacktraceAccessor<A> {

#[async_backtrace::framed]
async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
self.inner.list(path, args).await
self.inner
.list(path, args)
.map(|v| v.map(|(rp, r)| (rp, AsyncBacktraceWrapper::new(r))))
.await
}

#[async_backtrace::framed]
Expand All @@ -115,14 +126,83 @@ impl<A: Access> LayeredAccess for AsyncBacktraceAccessor<A> {
}

fn blocking_read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::BlockingReader)> {
self.inner.blocking_read(path, args)
self.inner
.blocking_read(path, args)
.map(|(rp, r)| (rp, AsyncBacktraceWrapper::new(r)))
}

fn blocking_write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::BlockingWriter)> {
self.inner.blocking_write(path, args)
self.inner
.blocking_write(path, args)
.map(|(rp, r)| (rp, AsyncBacktraceWrapper::new(r)))
}

fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingLister)> {
self.inner.blocking_list(path, args)
self.inner
.blocking_list(path, args)
.map(|(rp, r)| (rp, AsyncBacktraceWrapper::new(r)))
}
}

pub struct AsyncBacktraceWrapper<R> {
inner: R,
}

impl<R> AsyncBacktraceWrapper<R> {
fn new(inner: R) -> Self {
Self { inner }
}
}

impl<R: oio::Read> oio::Read for AsyncBacktraceWrapper<R> {
#[async_backtrace::framed]
async fn read(&mut self) -> Result<Buffer> {
self.inner.read().await
}
}

impl<R: oio::BlockingRead> oio::BlockingRead for AsyncBacktraceWrapper<R> {
fn read(&mut self) -> Result<Buffer> {
self.inner.read()
}
}

impl<R: oio::Write> oio::Write for AsyncBacktraceWrapper<R> {
#[async_backtrace::framed]
async fn write(&mut self, bs: Buffer) -> Result<usize> {
self.inner.write(bs).await
}

#[async_backtrace::framed]
async fn abort(&mut self) -> Result<()> {
self.inner.abort().await
}

#[async_backtrace::framed]
async fn close(&mut self) -> Result<()> {
self.inner.close().await
}
}

impl<R: oio::BlockingWrite> oio::BlockingWrite for AsyncBacktraceWrapper<R> {
fn write(&mut self, bs: Buffer) -> Result<usize> {
self.inner.write(bs)
}

fn close(&mut self) -> Result<()> {
self.inner.close()
}
}

impl<R: oio::List> oio::List for AsyncBacktraceWrapper<R> {
#[async_backtrace::framed]
async fn next(&mut self) -> Result<Option<oio::Entry>> {
self.inner.next().await
}
}

impl<R: oio::BlockingList> oio::BlockingList for AsyncBacktraceWrapper<R> {
fn next(&mut self) -> Result<Option<oio::Entry>> {
self.inner.next()
}
}
Loading