Xuanwo commented on code in PR #2006:
URL: 
https://github.com/apache/incubator-opendal/pull/2006#discussion_r1169659557


##########
core/src/layers/madsim.rs:
##########
@@ -0,0 +1,420 @@
+// 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 crate::ops::{OpList, OpRead, OpScan, OpWrite};
+use crate::raw::oio::Entry;
+use crate::raw::AccessorInfo;
+use crate::raw::{oio, Accessor, Layer, LayeredAccessor, RpList, RpRead, 
RpScan, RpWrite};
+use crate::types::Error;
+use crate::types::ErrorKind;
+use async_trait::async_trait;
+use bytes::Bytes;
+use madsim::net::Endpoint;
+use madsim::net::Payload;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::io::Result;
+use std::io::SeekFrom;
+use std::net::SocketAddr;
+use std::sync::Arc;
+use std::sync::Mutex;
+use std::task::{Context, Poll};
+
+/// Add deterministic simulation for async operations, powered by 
[`madsim`](https://docs.rs/madsim/latest/madsim/).
+///
+/// # Note
+///
+/// - blocking operations are not supported, as 
[`madsim`](https://docs.rs/madsim/latest/madsim/) is async only.
+///
+///
+/// # Examples
+///
+/// ```
+/// use opendal::Operator;
+/// use opendal::services;
+/// use opendal::layers::MadsimLayer;
+/// use opendal::layers::MadsimServer;
+/// use madsim::{net::NetSim, runtime::Handle, time::sleep};
+/// use std::time::Duration;
+///
+/// #[cfg(madsim)]
+/// #[madsim::test]
+/// async fn deterministic_simulation_test(){
+///     let handle = Handle::current();
+///     let ip1 = "10.0.0.1".parse().unwrap();
+///     let ip2 = "10.0.0.2".parse().unwrap();
+///     let sim_server_socket = "10.0.0.1:2379".parse().unwrap();
+///     let server = handle.create_node().name("server").ip(ip1).build();
+///     let client = handle.create_node().name("client").ip(ip2).build();
+///
+///     server.spawn(async move {
+///          SimServer::serve(sim_server_socket).await.unwrap();
+///     });
+///     sleep(Duration::from_secs(1)).await;
+///
+///     let handle = client.spawn(async move {
+///     let mut builder = services::Fs::default();
+///     builder.root(".");
+///     let op = Operator::new(builder)
+///         .unwrap()
+///         .layer(MadsimLayer::new(sim_server_socket))
+///         .finish();
+///
+///          let path = "hello.txt";
+///          let data = "Hello, World!";
+///          op.write(path, data).await.unwrap();
+///          assert_eq!(data.as_bytes(), op.read(path).await.unwrap());
+///      });
+///      handle.await.unwrap();
+/// }
+/// ```
+/// To enable logging output, please set `RUSTFLAGS="--cfg madsim"`:
+/// ```shell
+/// RUSTFLAGS="--cfg madsim" cargo test
+/// ```
+#[derive(Debug, Copy, Clone)]
+pub struct MadsimLayer {
+    addr: SocketAddr,
+}
+
+impl MadsimLayer {
+    pub fn new(endpoint: &str) -> Self {
+        Self {
+            addr: endpoint.parse().unwrap(),
+        }
+    }
+}
+
+impl<A: Accessor> Layer<A> for MadsimLayer {
+    type LayeredAccessor = MadsimAccessor;
+
+    fn layer(&self, _: A) -> Self::LayeredAccessor {
+        MadsimAccessor { addr: self.addr }
+    }
+}
+
+#[derive(Debug)]
+pub struct MadsimAccessor {
+    addr: SocketAddr,
+}
+
+#[async_trait]
+impl LayeredAccessor for MadsimAccessor {
+    type Inner = ();
+    type Reader = MadsimReader;
+    type BlockingReader = ();
+    type Writer = MadsimWriter;
+    type BlockingWriter = ();
+    type Pager = MadsimPager;
+    type BlockingPager = ();
+
+    fn inner(&self) -> &Self::Inner {
+        &()
+    }
+
+    fn metadata(&self) -> AccessorInfo {
+        let mut info = AccessorInfo::default();
+        info.set_name("madsim");

Review Comment:
   Please also set 
[`AccessorCapability`](https://docs.rs/opendal/latest/opendal/raw/enum.AccessorCapability.html)
   
   Based on current implementation, we can set `Read | Write`.



##########
core/src/layers/madsim.rs:
##########
@@ -0,0 +1,420 @@
+// 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 crate::ops::{OpList, OpRead, OpScan, OpWrite};
+use crate::raw::oio::Entry;
+use crate::raw::AccessorInfo;
+use crate::raw::{oio, Accessor, Layer, LayeredAccessor, RpList, RpRead, 
RpScan, RpWrite};
+use crate::types::Error;
+use crate::types::ErrorKind;
+use async_trait::async_trait;
+use bytes::Bytes;
+use madsim::net::Endpoint;
+use madsim::net::Payload;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::io::Result;
+use std::io::SeekFrom;
+use std::net::SocketAddr;
+use std::sync::Arc;
+use std::sync::Mutex;
+use std::task::{Context, Poll};
+
+/// Add deterministic simulation for async operations, powered by 
[`madsim`](https://docs.rs/madsim/latest/madsim/).
+///
+/// # Note
+///
+/// - blocking operations are not supported, as 
[`madsim`](https://docs.rs/madsim/latest/madsim/) is async only.
+///
+///
+/// # Examples
+///
+/// ```
+/// use opendal::Operator;
+/// use opendal::services;
+/// use opendal::layers::MadsimLayer;
+/// use opendal::layers::MadsimServer;
+/// use madsim::{net::NetSim, runtime::Handle, time::sleep};
+/// use std::time::Duration;
+///
+/// #[cfg(madsim)]
+/// #[madsim::test]
+/// async fn deterministic_simulation_test(){
+///     let handle = Handle::current();
+///     let ip1 = "10.0.0.1".parse().unwrap();
+///     let ip2 = "10.0.0.2".parse().unwrap();
+///     let sim_server_socket = "10.0.0.1:2379".parse().unwrap();
+///     let server = handle.create_node().name("server").ip(ip1).build();
+///     let client = handle.create_node().name("client").ip(ip2).build();
+///
+///     server.spawn(async move {
+///          SimServer::serve(sim_server_socket).await.unwrap();
+///     });
+///     sleep(Duration::from_secs(1)).await;
+///
+///     let handle = client.spawn(async move {
+///     let mut builder = services::Fs::default();
+///     builder.root(".");
+///     let op = Operator::new(builder)
+///         .unwrap()
+///         .layer(MadsimLayer::new(sim_server_socket))
+///         .finish();
+///
+///          let path = "hello.txt";
+///          let data = "Hello, World!";
+///          op.write(path, data).await.unwrap();
+///          assert_eq!(data.as_bytes(), op.read(path).await.unwrap());
+///      });
+///      handle.await.unwrap();
+/// }
+/// ```
+/// To enable logging output, please set `RUSTFLAGS="--cfg madsim"`:
+/// ```shell
+/// RUSTFLAGS="--cfg madsim" cargo test
+/// ```
+#[derive(Debug, Copy, Clone)]
+pub struct MadsimLayer {
+    addr: SocketAddr,
+}
+
+impl MadsimLayer {
+    pub fn new(endpoint: &str) -> Self {
+        Self {
+            addr: endpoint.parse().unwrap(),
+        }
+    }
+}
+
+impl<A: Accessor> Layer<A> for MadsimLayer {
+    type LayeredAccessor = MadsimAccessor;
+
+    fn layer(&self, _: A) -> Self::LayeredAccessor {
+        MadsimAccessor { addr: self.addr }
+    }
+}
+
+#[derive(Debug)]
+pub struct MadsimAccessor {
+    addr: SocketAddr,
+}
+
+#[async_trait]
+impl LayeredAccessor for MadsimAccessor {
+    type Inner = ();
+    type Reader = MadsimReader;
+    type BlockingReader = ();
+    type Writer = MadsimWriter;
+    type BlockingWriter = ();
+    type Pager = MadsimPager;
+    type BlockingPager = ();
+
+    fn inner(&self) -> &Self::Inner {
+        &()
+    }
+
+    fn metadata(&self) -> AccessorInfo {
+        let mut info = AccessorInfo::default();
+        info.set_name("madsim");
+        info
+    }
+
+    async fn read(&self, path: &str, args: OpRead) -> crate::Result<(RpRead, 
Self::Reader)> {
+        let req = Request::Read(path.to_string(), args);
+        let ep = Endpoint::connect(self.addr)
+            .await
+            .expect("fail to connect to sim server");
+        let (tx, mut rx) = ep
+            .connect1(self.addr)
+            .await
+            .expect("fail to connect1 to sim server");
+        tx.send(Box::new(req))
+            .await
+            .expect("fail to send request to sim server");
+        let resp = rx
+            .recv()
+            .await
+            .expect("fail to recv response from sim server");
+        let resp = resp
+            .downcast::<ReadResponse>()
+            .expect("fail to downcast response to ReadResponse");
+        let content_length = resp.data.as_ref().map(|b| b.len()).unwrap_or(0);
+        Ok((
+            RpRead::new(content_length as u64),
+            MadsimReader { data: resp.data },
+        ))
+    }
+
+    async fn write(&self, path: &str, args: OpWrite) -> 
crate::Result<(RpWrite, Self::Writer)> {
+        Ok((
+            RpWrite::default(),
+            MadsimWriter {
+                path: path.to_string(),
+                args,
+                sim_server_socket: self.addr,
+            },
+        ))
+    }
+
+    async fn list(&self, path: &str, args: OpList) -> crate::Result<(RpList, 
Self::Pager)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will be supported in the future",
+        ))
+    }
+
+    async fn scan(&self, path: &str, args: OpScan) -> crate::Result<(RpScan, 
Self::Pager)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will be supported in the future",
+        ))
+    }
+
+    fn blocking_read(
+        &self,
+        path: &str,
+        args: OpRead,
+    ) -> crate::Result<(RpRead, Self::BlockingReader)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will not be supported in MadsimLayer",
+        ))
+    }
+
+    fn blocking_write(
+        &self,
+        path: &str,
+        args: OpWrite,
+    ) -> crate::Result<(RpWrite, Self::BlockingWriter)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will not be supported in MadsimLayer",
+        ))
+    }
+
+    fn blocking_list(
+        &self,
+        path: &str,
+        args: OpList,
+    ) -> crate::Result<(RpList, Self::BlockingPager)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will not be supported in MadsimLayer",
+        ))
+    }
+
+    fn blocking_scan(
+        &self,
+        path: &str,
+        args: OpScan,
+    ) -> crate::Result<(RpScan, Self::BlockingPager)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will not be supported in MadsimLayer",
+        ))
+    }
+}
+
+pub struct MadsimReader {
+    data: Option<Bytes>,
+}
+
+impl oio::Read for MadsimReader {
+    fn poll_read(&mut self, _cx: &mut Context<'_>, buf: &mut [u8]) -> 
Poll<crate::Result<usize>> {
+        if let Some(ref data) = self.data {
+            let len = data.len();
+            buf[..len].copy_from_slice(data);
+            Poll::Ready(Ok(len))
+        } else {
+            Poll::Ready(Ok(0))
+        }
+    }
+
+    fn poll_seek(&mut self, cx: &mut Context<'_>, pos: SeekFrom) -> 
Poll<crate::Result<u64>> {
+        Poll::Ready(Err(Error::new(
+            ErrorKind::Unsupported,
+            "will be supported in the future",
+        )))
+    }
+
+    fn poll_next(&mut self, cx: &mut Context<'_>) -> 
Poll<Option<crate::Result<Bytes>>> {
+        Poll::Ready(Some(Err(Error::new(
+            ErrorKind::Unsupported,
+            "will be supported in the future",
+        ))))
+    }
+}
+
+pub struct MadsimWriter {
+    path: String,
+    args: OpWrite,
+    sim_server_socket: SocketAddr,
+}
+
+#[async_trait]
+impl oio::Write for MadsimWriter {
+    async fn write(&mut self, bs: Bytes) -> crate::Result<()> {
+        let req = Request::Write(self.path.to_string(), bs);
+        let ep = Endpoint::connect(self.sim_server_socket)
+            .await
+            .expect("fail to connect to sim server");

Review Comment:
   It's better to implement `From<madsim::Error> for opendal::Error` so that we 
can use `ep.connect1(self.addr).await?` for better reading.



##########
core/src/layers/madsim.rs:
##########
@@ -0,0 +1,420 @@
+// 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 crate::ops::{OpList, OpRead, OpScan, OpWrite};
+use crate::raw::oio::Entry;
+use crate::raw::AccessorInfo;
+use crate::raw::{oio, Accessor, Layer, LayeredAccessor, RpList, RpRead, 
RpScan, RpWrite};
+use crate::types::Error;
+use crate::types::ErrorKind;
+use async_trait::async_trait;
+use bytes::Bytes;
+use madsim::net::Endpoint;
+use madsim::net::Payload;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::io::Result;
+use std::io::SeekFrom;
+use std::net::SocketAddr;
+use std::sync::Arc;
+use std::sync::Mutex;
+use std::task::{Context, Poll};
+
+/// Add deterministic simulation for async operations, powered by 
[`madsim`](https://docs.rs/madsim/latest/madsim/).
+///
+/// # Note
+///
+/// - blocking operations are not supported, as 
[`madsim`](https://docs.rs/madsim/latest/madsim/) is async only.
+///
+///
+/// # Examples
+///
+/// ```
+/// use opendal::Operator;
+/// use opendal::services;
+/// use opendal::layers::MadsimLayer;
+/// use opendal::layers::MadsimServer;
+/// use madsim::{net::NetSim, runtime::Handle, time::sleep};
+/// use std::time::Duration;
+///
+/// #[cfg(madsim)]
+/// #[madsim::test]
+/// async fn deterministic_simulation_test(){
+///     let handle = Handle::current();
+///     let ip1 = "10.0.0.1".parse().unwrap();
+///     let ip2 = "10.0.0.2".parse().unwrap();
+///     let sim_server_socket = "10.0.0.1:2379".parse().unwrap();
+///     let server = handle.create_node().name("server").ip(ip1).build();
+///     let client = handle.create_node().name("client").ip(ip2).build();
+///
+///     server.spawn(async move {
+///          SimServer::serve(sim_server_socket).await.unwrap();
+///     });
+///     sleep(Duration::from_secs(1)).await;
+///
+///     let handle = client.spawn(async move {
+///     let mut builder = services::Fs::default();
+///     builder.root(".");
+///     let op = Operator::new(builder)
+///         .unwrap()
+///         .layer(MadsimLayer::new(sim_server_socket))
+///         .finish();
+///
+///          let path = "hello.txt";
+///          let data = "Hello, World!";
+///          op.write(path, data).await.unwrap();
+///          assert_eq!(data.as_bytes(), op.read(path).await.unwrap());
+///      });
+///      handle.await.unwrap();
+/// }
+/// ```
+/// To enable logging output, please set `RUSTFLAGS="--cfg madsim"`:
+/// ```shell
+/// RUSTFLAGS="--cfg madsim" cargo test
+/// ```
+#[derive(Debug, Copy, Clone)]
+pub struct MadsimLayer {
+    addr: SocketAddr,
+}
+
+impl MadsimLayer {
+    pub fn new(endpoint: &str) -> Self {
+        Self {
+            addr: endpoint.parse().unwrap(),
+        }
+    }
+}
+
+impl<A: Accessor> Layer<A> for MadsimLayer {
+    type LayeredAccessor = MadsimAccessor;
+
+    fn layer(&self, _: A) -> Self::LayeredAccessor {
+        MadsimAccessor { addr: self.addr }
+    }
+}
+
+#[derive(Debug)]
+pub struct MadsimAccessor {
+    addr: SocketAddr,
+}
+
+#[async_trait]
+impl LayeredAccessor for MadsimAccessor {
+    type Inner = ();
+    type Reader = MadsimReader;
+    type BlockingReader = ();
+    type Writer = MadsimWriter;
+    type BlockingWriter = ();
+    type Pager = MadsimPager;
+    type BlockingPager = ();
+
+    fn inner(&self) -> &Self::Inner {
+        &()
+    }
+
+    fn metadata(&self) -> AccessorInfo {
+        let mut info = AccessorInfo::default();
+        info.set_name("madsim");
+        info
+    }
+
+    async fn read(&self, path: &str, args: OpRead) -> crate::Result<(RpRead, 
Self::Reader)> {
+        let req = Request::Read(path.to_string(), args);
+        let ep = Endpoint::connect(self.addr)
+            .await
+            .expect("fail to connect to sim server");
+        let (tx, mut rx) = ep
+            .connect1(self.addr)
+            .await
+            .expect("fail to connect1 to sim server");
+        tx.send(Box::new(req))
+            .await
+            .expect("fail to send request to sim server");
+        let resp = rx
+            .recv()
+            .await
+            .expect("fail to recv response from sim server");
+        let resp = resp
+            .downcast::<ReadResponse>()
+            .expect("fail to downcast response to ReadResponse");
+        let content_length = resp.data.as_ref().map(|b| b.len()).unwrap_or(0);
+        Ok((
+            RpRead::new(content_length as u64),
+            MadsimReader { data: resp.data },
+        ))
+    }
+
+    async fn write(&self, path: &str, args: OpWrite) -> 
crate::Result<(RpWrite, Self::Writer)> {
+        Ok((
+            RpWrite::default(),
+            MadsimWriter {
+                path: path.to_string(),
+                args,
+                sim_server_socket: self.addr,
+            },
+        ))
+    }
+
+    async fn list(&self, path: &str, args: OpList) -> crate::Result<(RpList, 
Self::Pager)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will be supported in the future",
+        ))
+    }
+
+    async fn scan(&self, path: &str, args: OpScan) -> crate::Result<(RpScan, 
Self::Pager)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will be supported in the future",
+        ))
+    }
+
+    fn blocking_read(
+        &self,
+        path: &str,
+        args: OpRead,
+    ) -> crate::Result<(RpRead, Self::BlockingReader)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will not be supported in MadsimLayer",
+        ))
+    }
+
+    fn blocking_write(
+        &self,
+        path: &str,
+        args: OpWrite,
+    ) -> crate::Result<(RpWrite, Self::BlockingWriter)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will not be supported in MadsimLayer",
+        ))
+    }
+
+    fn blocking_list(
+        &self,
+        path: &str,
+        args: OpList,
+    ) -> crate::Result<(RpList, Self::BlockingPager)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will not be supported in MadsimLayer",
+        ))
+    }
+
+    fn blocking_scan(
+        &self,
+        path: &str,
+        args: OpScan,
+    ) -> crate::Result<(RpScan, Self::BlockingPager)> {
+        Err(Error::new(
+            ErrorKind::Unsupported,
+            "will not be supported in MadsimLayer",
+        ))
+    }
+}
+
+pub struct MadsimReader {
+    data: Option<Bytes>,
+}
+
+impl oio::Read for MadsimReader {
+    fn poll_read(&mut self, _cx: &mut Context<'_>, buf: &mut [u8]) -> 
Poll<crate::Result<usize>> {
+        if let Some(ref data) = self.data {
+            let len = data.len();
+            buf[..len].copy_from_slice(data);
+            Poll::Ready(Ok(len))
+        } else {
+            Poll::Ready(Ok(0))
+        }
+    }
+
+    fn poll_seek(&mut self, cx: &mut Context<'_>, pos: SeekFrom) -> 
Poll<crate::Result<u64>> {
+        Poll::Ready(Err(Error::new(
+            ErrorKind::Unsupported,
+            "will be supported in the future",
+        )))
+    }
+
+    fn poll_next(&mut self, cx: &mut Context<'_>) -> 
Poll<Option<crate::Result<Bytes>>> {
+        Poll::Ready(Some(Err(Error::new(
+            ErrorKind::Unsupported,
+            "will be supported in the future",
+        ))))
+    }
+}
+
+pub struct MadsimWriter {
+    path: String,
+    args: OpWrite,
+    sim_server_socket: SocketAddr,

Review Comment:
   Please replace all `sim_server_socket` to `addr` for better reading.



-- 
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]

Reply via email to