Xuanwo commented on code in PR #2006: URL: https://github.com/apache/incubator-opendal/pull/2006#discussion_r1168884492
########## core/Cargo.toml: ########## @@ -85,6 +86,8 @@ layers-prometheus = ["dep:prometheus"] layers-minitrace = ["dep:minitrace"] # Enable layers tracing support. layers-tracing = ["dep:tracing"] +# Enable layers madsim support +layers-madsim = ["dep:madsim"] Review Comment: Please place this layer before `layers-minitrace`. ########## core/src/layers/madsim.rs: ########## @@ -0,0 +1,318 @@ +// 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::{oio, Accessor, Layer, LayeredAccessor, RpList, RpRead, RpScan, RpWrite}; +use crate::{EntryMode, Metadata}; +use async_compat::CompatExt; +use async_trait::async_trait; +use bytes::Bytes; +use madsim::plugin::node; +use madsim::plugin::simulator; +use madsim::plugin::Simulator; +use madsim::rand::GlobalRng; +use madsim::task::NodeId; +use madsim::time::TimeHandle; +use madsim::Config; +use std::collections::HashMap; +use std::fmt::Debug; +use std::io::SeekFrom; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll}; + +#[derive(Debug, Copy, Clone)] +pub struct MadsimLayer { + service_node_id: NodeId, +} + +impl MadsimLayer { + pub fn new(service_node_id: NodeId) -> Self { Review Comment: It seems not a good idea to me that we need to acccept a `NodeId` from outside. Can we call `Handle::current().create_node().build()` inside `MadsimLayer::new()`? ########## core/src/layers/madsim.rs: ########## @@ -0,0 +1,318 @@ +// 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::{oio, Accessor, Layer, LayeredAccessor, RpList, RpRead, RpScan, RpWrite}; +use crate::{EntryMode, Metadata}; +use async_compat::CompatExt; +use async_trait::async_trait; +use bytes::Bytes; +use madsim::plugin::node; +use madsim::plugin::simulator; +use madsim::plugin::Simulator; +use madsim::rand::GlobalRng; +use madsim::task::NodeId; +use madsim::time::TimeHandle; +use madsim::Config; +use std::collections::HashMap; +use std::fmt::Debug; +use std::io::SeekFrom; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll}; + +#[derive(Debug, Copy, Clone)] +pub struct MadsimLayer { + service_node_id: NodeId, +} + +impl MadsimLayer { + pub fn new(service_node_id: NodeId) -> Self { + Self { service_node_id } + } +} + +impl<A: Accessor> Layer<A> for MadsimLayer { + type LayeredAccessor = MadsimAccessor; + + fn layer(&self, _inner: A) -> Self::LayeredAccessor { + let handle = simulator::<ServiceSim>().get_node(self.service_node_id); + MadsimAccessor { handle } + } +} + +#[derive(Debug)] +pub struct MadsimAccessor { + handle: ServiceNodeHandle, +} + +#[async_trait] +impl LayeredAccessor for MadsimAccessor { + type Inner = (); + type Reader = MadsimReader; Review Comment: I think we can wrap `MadsimReader<Inner::Reader>` so that we can inject errors to underlying readers. ########## core/src/layers/madsim.rs: ########## @@ -0,0 +1,318 @@ +// 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::{oio, Accessor, Layer, LayeredAccessor, RpList, RpRead, RpScan, RpWrite}; +use crate::{EntryMode, Metadata}; +use async_compat::CompatExt; +use async_trait::async_trait; +use bytes::Bytes; +use madsim::plugin::node; +use madsim::plugin::simulator; +use madsim::plugin::Simulator; +use madsim::rand::GlobalRng; +use madsim::task::NodeId; +use madsim::time::TimeHandle; +use madsim::Config; +use std::collections::HashMap; +use std::fmt::Debug; +use std::io::SeekFrom; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll}; + +#[derive(Debug, Copy, Clone)] +pub struct MadsimLayer { + service_node_id: NodeId, +} + +impl MadsimLayer { + pub fn new(service_node_id: NodeId) -> Self { + Self { service_node_id } + } +} + +impl<A: Accessor> Layer<A> for MadsimLayer { + type LayeredAccessor = MadsimAccessor; + + fn layer(&self, _inner: A) -> Self::LayeredAccessor { + let handle = simulator::<ServiceSim>().get_node(self.service_node_id); + MadsimAccessor { handle } + } +} + +#[derive(Debug)] +pub struct MadsimAccessor { + handle: ServiceNodeHandle, +} + +#[async_trait] +impl LayeredAccessor for MadsimAccessor { + type Inner = (); + type Reader = MadsimReader; + type BlockingReader = MadsimReader; + type Writer = MadsimWriter; + type BlockingWriter = MadsimWriter; + type Pager = MadsimPager; + type BlockingPager = MadsimPager; + + fn inner(&self) -> &Self::Inner { + &() + } + + async fn read(&self, path: &str, args: OpRead) -> crate::Result<(RpRead, Self::Reader)> { + let handle = self.handle.inner.lock().unwrap(); + let length = handle + .get(path) + .map(|(_, data)| data.len()) + .unwrap_or_default(); + Ok(( + RpRead::new(length as u64), + MadsimReader { + path: path.to_string(), + args, + handle: self.handle.clone(), + }, + )) + } + + async fn write(&self, path: &str, args: OpWrite) -> crate::Result<(RpWrite, Self::Writer)> { + Ok(( + RpWrite::new(), + MadsimWriter { + path: path.to_string(), + args, + handle: self.handle.clone(), + }, + )) + } + + async fn list(&self, path: &str, args: OpList) -> crate::Result<(RpList, Self::Pager)> { + todo!() Review Comment: Please forward to inner instead of `todo!()` ########## core/src/layers/mod.rs: ########## @@ -18,47 +18,67 @@ //! `Layer` is the mechanism to intercept operations. mod concurrent_limit; + pub use concurrent_limit::ConcurrentLimitLayer; mod immutable_index; + pub use immutable_index::ImmutableIndexLayer; mod logging; + pub use logging::LoggingLayer; #[cfg(feature = "layers-chaos")] mod chaos; + #[cfg(feature = "layers-chaos")] pub use chaos::ChaosLayer; #[cfg(feature = "layers-metrics")] mod metrics; + #[cfg(feature = "layers-metrics")] pub use self::metrics::MetricsLayer; #[cfg(feature = "layers-prometheus")] mod prometheus; + #[cfg(feature = "layers-prometheus")] pub use self::prometheus::PrometheusLayer; mod retry; + pub use self::retry::RetryLayer; #[cfg(feature = "layers-tracing")] mod tracing; + #[cfg(feature = "layers-tracing")] pub use self::tracing::TracingLayer; #[cfg(feature = "layers-minitrace")] mod minitrace; + #[cfg(feature = "layers-minitrace")] pub use self::minitrace::MinitraceLayer; mod type_eraser; + pub(crate) use type_eraser::TypeEraseLayer; mod error_context; + pub(crate) use error_context::ErrorContextLayer; mod complete; + pub(crate) use complete::CompleteLayer; + +#[cfg(feature = "layers-madsim")] +#[cfg(madsim)] Review Comment: We don't need this cfg. ########## core/src/layers/madsim.rs: ########## @@ -0,0 +1,318 @@ +// 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::{oio, Accessor, Layer, LayeredAccessor, RpList, RpRead, RpScan, RpWrite}; +use crate::{EntryMode, Metadata}; +use async_compat::CompatExt; +use async_trait::async_trait; +use bytes::Bytes; +use madsim::plugin::node; +use madsim::plugin::simulator; +use madsim::plugin::Simulator; +use madsim::rand::GlobalRng; +use madsim::task::NodeId; +use madsim::time::TimeHandle; +use madsim::Config; +use std::collections::HashMap; +use std::fmt::Debug; +use std::io::SeekFrom; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll}; + +#[derive(Debug, Copy, Clone)] +pub struct MadsimLayer { + service_node_id: NodeId, +} + +impl MadsimLayer { + pub fn new(service_node_id: NodeId) -> Self { + Self { service_node_id } + } +} + +impl<A: Accessor> Layer<A> for MadsimLayer { + type LayeredAccessor = MadsimAccessor; + + fn layer(&self, _inner: A) -> Self::LayeredAccessor { + let handle = simulator::<ServiceSim>().get_node(self.service_node_id); Review Comment: Do we need to support forward actions to underlying accessor? ########## core/src/layers/mod.rs: ########## @@ -18,47 +18,67 @@ //! `Layer` is the mechanism to intercept operations. mod concurrent_limit; + Review Comment: Please remove this line. -- 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]
