Skip to content

Commit

Permalink
refactor(core)!: Return Arc<AccessInfo> for metadata (#4883)
Browse files Browse the repository at this point in the history
* refactor: `Operator` add a field to store metadata and avoid calling metadata

Signed-off-by: Lzzzt <[email protected]>

* refactor: change the signature of `Access::info`

Signed-off-by: Lzzzt <[email protected]>

refactor: change the signature of `Access::info` and `AccessDyn::info`

Signed-off-by: Lzzzt <[email protected]>

refactor: change the signature of `Access::info` and `AccessDyn::info`

Signed-off-by: Lzzzt <[email protected]>

revert: revert

Signed-off-by: Lzzzt <[email protected]>

revert: revert

Signed-off-by: Lzzzt <[email protected]>

Update store.rs

refactor: change the signature of `Access::info` and `AccessDyn::info`

Signed-off-by: Lzzzt <[email protected]>

---------

Signed-off-by: Lzzzt <[email protected]>
  • Loading branch information
Lzzzzzt authored Jul 12, 2024
1 parent 700cee1 commit 9ef494d
Show file tree
Hide file tree
Showing 59 changed files with 146 additions and 124 deletions.
4 changes: 2 additions & 2 deletions core/src/docs/internals/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
//! ..Default::default()
//! });
//!
//! am
//! am.into()
//! }
//! }
//! ```
Expand Down Expand Up @@ -302,7 +302,7 @@
//! ..Default::default()
//! });
//!
//! am
//! am.into()
//! }
//!
//! async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
Expand Down
8 changes: 5 additions & 3 deletions core/src/layers/blocking.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 std::sync::Arc;

use tokio::runtime::Handle;

use crate::raw::*;
Expand Down Expand Up @@ -178,10 +180,10 @@ impl<A: Access> LayeredAccess for BlockingAccessor<A> {
&self.inner
}

fn metadata(&self) -> AccessorInfo {
let mut meta = self.inner.info();
fn metadata(&self) -> Arc<AccessorInfo> {
let mut meta = self.inner.info().as_ref().clone();
meta.full_capability_mut().blocking = true;
meta
meta.into()
}

async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
Expand Down
13 changes: 7 additions & 6 deletions core/src/layers/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<A: Access> Layer<A> for CompleteLayer {

/// Provide complete wrapper for backend.
pub struct CompleteAccessor<A: Access> {
meta: AccessorInfo,
meta: Arc<AccessorInfo>,
inner: Arc<A>,
}

Expand Down Expand Up @@ -380,13 +380,14 @@ impl<A: Access> LayeredAccess for CompleteAccessor<A> {
&self.inner
}

fn metadata(&self) -> AccessorInfo {
let mut meta = self.meta.clone();
// Todo: May move the logic to the implement of Layer::layer of CompleteAccessor<A>
fn metadata(&self) -> Arc<AccessorInfo> {
let mut meta = (*self.meta).clone();
let cap = meta.full_capability_mut();
if cap.list && cap.write_can_empty {
cap.create_dir = true;
}
meta
meta.into()
}

async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
Expand Down Expand Up @@ -730,11 +731,11 @@ mod tests {
type BlockingWriter = oio::BlockingWriter;
type BlockingLister = oio::BlockingLister;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut info = AccessorInfo::default();
info.set_native_capability(self.capability);

info
info.into()
}

async fn create_dir(&self, _: &str, _: OpCreateDir) -> Result<RpCreateDir> {
Expand Down
5 changes: 3 additions & 2 deletions core/src/layers/error_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use std::fmt::Debug;
use std::fmt::Formatter;
use std::sync::Arc;

use futures::TryFutureExt;

Expand Down Expand Up @@ -56,7 +57,7 @@ impl<A: Access> Layer<A> for ErrorContextLayer {

/// Provide error context wrapper for backend.
pub struct ErrorContextAccessor<A: Access> {
meta: AccessorInfo,
meta: Arc<AccessorInfo>,
inner: A,
}

Expand All @@ -79,7 +80,7 @@ impl<A: Access> LayeredAccess for ErrorContextAccessor<A> {
&self.inner
}

fn metadata(&self) -> AccessorInfo {
fn metadata(&self) -> Arc<AccessorInfo> {
self.meta.clone()
}

Expand Down
7 changes: 4 additions & 3 deletions core/src/layers/immutable_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use std::collections::HashSet;
use std::fmt::Debug;
use std::sync::Arc;
use std::vec::IntoIter;

use crate::raw::*;
Expand Down Expand Up @@ -145,14 +146,14 @@ impl<A: Access> LayeredAccess for ImmutableIndexAccessor<A> {
}

/// Add list capabilities for underlying storage services.
fn metadata(&self) -> AccessorInfo {
let mut meta = self.inner.info();
fn metadata(&self) -> Arc<AccessorInfo> {
let mut meta = (*self.inner.info()).clone();

let cap = meta.full_capability_mut();
cap.list = true;
cap.list_with_recursive = true;

meta
meta.into()
}

async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
Expand Down
3 changes: 2 additions & 1 deletion core/src/layers/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use std::fmt::Debug;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use std::sync::Arc;

use bytes::Buf;
use futures::FutureExt;
Expand Down Expand Up @@ -221,7 +222,7 @@ impl<A: Access> LayeredAccess for LoggingAccessor<A> {
&self.inner
}

fn metadata(&self) -> AccessorInfo {
fn metadata(&self) -> Arc<AccessorInfo> {
debug!(
target: LOGGING_TARGET,
"service={} operation={} -> started",
Expand Down
2 changes: 1 addition & 1 deletion core/src/layers/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ impl<A: Access> LayeredAccess for MetricsAccessor<A> {
&self.inner
}

fn metadata(&self) -> AccessorInfo {
fn metadata(&self) -> Arc<AccessorInfo> {
self.handle.requests_total_metadata.increment(1);

let start = Instant::now();
Expand Down
3 changes: 2 additions & 1 deletion core/src/layers/minitrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use std::fmt::Debug;
use std::future::Future;
use std::sync::Arc;

use futures::FutureExt;
use minitrace::prelude::*;
Expand Down Expand Up @@ -139,7 +140,7 @@ impl<A: Access> LayeredAccess for MinitraceAccessor<A> {
}

#[trace]
fn metadata(&self) -> AccessorInfo {
fn metadata(&self) -> Arc<AccessorInfo> {
self.inner.info()
}

Expand Down
3 changes: 2 additions & 1 deletion core/src/layers/oteltrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

use std::future::Future;
use std::sync::Arc;

use futures::FutureExt;
use opentelemetry::global;
Expand Down Expand Up @@ -75,7 +76,7 @@ impl<A: Access> LayeredAccess for OtelTraceAccessor<A> {
&self.inner
}

fn metadata(&self) -> AccessorInfo {
fn metadata(&self) -> Arc<AccessorInfo> {
let tracer = global::tracer("opendal");
tracer.in_span("metadata", |_cx| self.inner.info())
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/layers/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ mod tests {
type BlockingWriter = ();
type BlockingLister = ();

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut am = AccessorInfo::default();
am.set_native_capability(Capability {
read: true,
Expand All @@ -813,7 +813,7 @@ mod tests {
..Default::default()
});

am
am.into()
}

async fn stat(&self, _: &str, _: OpStat) -> Result<RpStat> {
Expand Down
4 changes: 2 additions & 2 deletions core/src/layers/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,15 @@ mod tests {
type BlockingWriter = ();
type BlockingLister = ();

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut am = AccessorInfo::default();
am.set_native_capability(Capability {
read: true,
delete: true,
..Default::default()
});

am
am.into()
}

/// This function will build a reader that always return pending.
Expand Down
3 changes: 2 additions & 1 deletion core/src/layers/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use std::fmt::Debug;
use std::future::Future;
use std::sync::Arc;

use futures::FutureExt;
use tracing::Span;
Expand Down Expand Up @@ -140,7 +141,7 @@ impl<A: Access> LayeredAccess for TracingAccessor<A> {
}

#[tracing::instrument(level = "debug")]
fn metadata(&self) -> AccessorInfo {
fn metadata(&self) -> Arc<AccessorInfo> {
self.inner.info()
}

Expand Down
13 changes: 7 additions & 6 deletions core/src/raw/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub trait Access: Send + Sync + Debug + Unpin + 'static {
///
/// - scheme: declare the scheme of backend.
/// - capabilities: declare the capabilities of current backend.
fn info(&self) -> AccessorInfo;
fn info(&self) -> Arc<AccessorInfo>;

/// Invoke the `create` operation on the specified path
///
Expand Down Expand Up @@ -399,7 +399,7 @@ pub trait Access: Send + Sync + Debug + Unpin + 'static {
/// `Box<dyn AccessDyn>`.
pub trait AccessDyn: Send + Sync + Debug + Unpin {
/// Dyn version of [`Accessor::info`]
fn info_dyn(&self) -> AccessorInfo;
fn info_dyn(&self) -> Arc<AccessorInfo>;
/// Dyn version of [`Accessor::create_dir`]
fn create_dir_dyn<'a>(
&'a self,
Expand Down Expand Up @@ -484,7 +484,7 @@ where
BlockingLister = oio::BlockingLister,
>,
{
fn info_dyn(&self) -> AccessorInfo {
fn info_dyn(&self) -> Arc<AccessorInfo> {
self.info()
}

Expand Down Expand Up @@ -607,7 +607,7 @@ impl Access for dyn AccessDyn {
type Lister = oio::Lister;
type BlockingLister = oio::BlockingLister;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
self.info_dyn()
}

Expand Down Expand Up @@ -693,14 +693,15 @@ impl Access for () {
type BlockingWriter = ();
type BlockingLister = ();

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
AccessorInfo {
scheme: Scheme::Custom("dummy"),
root: "".to_string(),
name: "dummy".to_string(),
native_capability: Capability::default(),
full_capability: Capability::default(),
}
.into()
}
}

Expand All @@ -717,7 +718,7 @@ impl<T: Access + ?Sized> Access for Arc<T> {
type BlockingWriter = T::BlockingWriter;
type BlockingLister = T::BlockingLister;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
self.as_ref().info()
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/raw/adapters/kv/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<S: Adapter> Access for Backend<S> {
type Lister = HierarchyLister<KvLister>;
type BlockingLister = HierarchyLister<KvLister>;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut am: AccessorInfo = self.kv.metadata().into();
am.set_root(&self.root);

Expand All @@ -84,7 +84,7 @@ impl<S: Adapter> Access for Backend<S> {

am.set_native_capability(cap);

am
am.into()
}

async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
Expand Down
4 changes: 2 additions & 2 deletions core/src/raw/adapters/typed_kv/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<S: Adapter> Access for Backend<S> {
type Lister = HierarchyLister<KvLister>;
type BlockingLister = HierarchyLister<KvLister>;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let kv_info = self.kv.info();
let mut am: AccessorInfo = AccessorInfo::default();
am.set_root(&self.root);
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<S: Adapter> Access for Backend<S> {

am.set_native_capability(cap);

am
am.into()
}

async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
Expand Down
9 changes: 5 additions & 4 deletions core/src/raw/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

use std::fmt::Debug;
use std::sync::Arc;

use futures::Future;

Expand Down Expand Up @@ -139,7 +140,7 @@ pub trait LayeredAccess: Send + Sync + Debug + Unpin + 'static {

fn inner(&self) -> &Self::Inner;

fn metadata(&self) -> AccessorInfo {
fn metadata(&self) -> Arc<AccessorInfo> {
self.inner().info()
}

Expand Down Expand Up @@ -246,7 +247,7 @@ impl<L: LayeredAccess> Access for L {
type Lister = L::Lister;
type BlockingLister = L::BlockingLister;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
(self as &L).metadata()
}

Expand Down Expand Up @@ -358,10 +359,10 @@ mod tests {
type Lister = ();
type BlockingLister = ();

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut am = AccessorInfo::default();
am.set_scheme(Scheme::Custom("test"));
am
am.into()
}

async fn delete(&self, _: &str, _: OpDelete) -> Result<RpDelete> {
Expand Down
Loading

0 comments on commit 9ef494d

Please sign in to comment.