From 19949447eca9b03db4b0772dd18452b24df939c7 Mon Sep 17 00:00:00 2001 From: GatewayJ <835269233@qq.com> Date: Wed, 25 Nov 2026 23:15:27 +0800 Subject: [PATCH] feat(phase-1): expose native S3 RDMA transports as a Rust library --- .github/scripts/build-cuobject-client.sh | 1 + README.md | 2 +- crates/s3/src/client.rs | 25 ++++++++- docs/ADR/0001-object-data-transport.md | 9 +++ docs/rdma-build.md | 34 +++++++++++- nixl/Cargo.lock | 24 +++++--- nixl/Cargo.toml | 17 ++---- nixl/rdma-client/Cargo.toml | 24 ++++++++ nixl/{ => rdma-client}/src/buffer.rs | 24 ++++++-- nixl/rdma-client/src/lib.rs | 29 ++++++++++ nixl/{ => rdma-client}/src/nvidia.rs | 0 nixl/{ => rdma-client}/src/pool.rs | 7 +++ nixl/{ => rdma-client}/src/transport.rs | 35 +++++++++--- .../src/transport/download.rs | 0 nixl/{ => rdma-client}/src/transport/tests.rs | 43 ++++++++++++--- .../{ => rdma-client}/src/transport/upload.rs | 0 nixl/{ => rdma-client}/src/wire.rs | 18 +++--- .../{ => rdma-client}/src/wire/native_test.rs | 1 + nixl/rdma-client/tests/provider.rs | 55 +++++++++++++++++++ nixl/src/main.rs | 17 +++--- 20 files changed, 304 insertions(+), 61 deletions(-) create mode 100644 nixl/rdma-client/Cargo.toml rename nixl/{ => rdma-client}/src/buffer.rs (76%) create mode 100644 nixl/rdma-client/src/lib.rs rename nixl/{ => rdma-client}/src/nvidia.rs (100%) rename nixl/{ => rdma-client}/src/pool.rs (96%) rename nixl/{ => rdma-client}/src/transport.rs (83%) rename nixl/{ => rdma-client}/src/transport/download.rs (100%) rename nixl/{ => rdma-client}/src/transport/tests.rs (93%) rename nixl/{ => rdma-client}/src/transport/upload.rs (100%) rename nixl/{ => rdma-client}/src/wire.rs (86%) rename nixl/{ => rdma-client}/src/wire/native_test.rs (95%) create mode 100644 nixl/rdma-client/tests/provider.rs diff --git a/.github/scripts/build-cuobject-client.sh b/.github/scripts/build-cuobject-client.sh index a7b1cbc..d792f17 100755 --- a/.github/scripts/build-cuobject-client.sh +++ b/.github/scripts/build-cuobject-client.sh @@ -29,3 +29,4 @@ export LIBRARY_PATH="$CUOBJCLIENT_ROOT/lib${LIBRARY_PATH:+:$LIBRARY_PATH}" export NIXL_PLUGIN_DIR="$NIXL_PREFIX/lib/plugins" ./scripts/build-rdma.sh --features cuobject cargo test --locked --manifest-path nixl/Cargo.toml --features cuobject +cargo test --locked --manifest-path nixl/Cargo.toml -p rustfs-rdma-client --no-default-features --features cuobject diff --git a/README.md b/README.md index ae11879..2ba6391 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ cd cli cargo build --release ``` -To use RustFS native NIXL/UCX for object transfers, build the [Linux RDMA client](docs/rdma-build.md#rustfs-native-nixlucx). +For native NIXL/UCX and NVIDIA cuObject transfers, [build the Linux CLI](docs/rdma-build.md#build-the-native-client) or [use the RDMA client in a Rust application](docs/rdma-build.md#embed-the-rust-client). ## Quick Start diff --git a/crates/s3/src/client.rs b/crates/s3/src/client.rs index 7158789..cb60f7e 100644 --- a/crates/s3/src/client.rs +++ b/crates/s3/src/client.rs @@ -1806,7 +1806,26 @@ impl Intercept for CustomHeaderInterceptor { impl S3Client { /// Create a new S3 client from an alias configuration pub async fn new(alias: Alias) -> Result { - let request_headers = global_request_headers(); + let mode = rc_core::global_data_transport_mode(); + let factory = data_transport::installed_factory(); + Self::build(alias, global_request_headers(), mode, factory.as_deref()).await + } + + /// Create a client with an explicit transport policy, independent of CLI defaults. + pub async fn new_with_transport_factory( + alias: Alias, + mode: rc_core::DataTransportMode, + factory: &dyn data_transport::ObjectDataTransportFactory, + ) -> Result { + Self::build(alias, Vec::new(), mode, Some(factory)).await + } + + async fn build( + alias: Alias, + request_headers: Vec, + mode: rc_core::DataTransportMode, + factory: Option<&dyn data_transport::ObjectDataTransportFactory>, + ) -> Result { for header in &request_headers { header.validate_transport_name()?; } @@ -1900,11 +1919,11 @@ impl S3Client { alias, session_token, request_headers, - data_transport_mode: rc_core::global_data_transport_mode(), + data_transport_mode: mode, data_transport: None, }; if client.data_transport_mode != rc_core::DataTransportMode::Http - && let Some(factory) = data_transport::installed_factory() + && let Some(factory) = factory { client.data_transport = Some(factory.create(Arc::new(client.clone()))?); } diff --git a/docs/ADR/0001-object-data-transport.md b/docs/ADR/0001-object-data-transport.md index 8f8480b..fd1e95c 100644 --- a/docs/ADR/0001-object-data-transport.md +++ b/docs/ADR/0001-object-data-transport.md @@ -16,6 +16,15 @@ checksum verification and receipt handling. The common launcher preserves command dispatch, output, exit codes, and the existing worker-thread configuration. +The separate native workspace contains the reusable `rustfs-rdma-client` +library. It implements the provider ports from `rc-s3`; only the launcher +depends on `rustfs-cli`. Applications use `RdmaProvider::client` with an +explicit alias and transfer mode. This constructor bypasses the CLI provider +registry and CLI request headers. Reusing a provider shares endpoint capability +probes across its clients. Each client creates its window pools on demand. +The library keeps registered buffers, token ownership and wire fields private. +Cargo features select the NIXL and NVIDIA native dependencies independently. + The NIXL backend supplies agent metadata and a registered buffer description. The NVIDIA backend uses `rustfs-cuobject-client` for registered windows and transfer tokens. Its dependency `rustfs-cuobject-client-sys` builds the C++ bridge and links the NVIDIA client SDK. The window can be reused after a verified receipt or proof that DMA never started. An uncertain transfer retains both resources diff --git a/docs/rdma-build.md b/docs/rdma-build.md index 0f161b8..08a587e 100644 --- a/docs/rdma-build.md +++ b/docs/rdma-build.md @@ -34,7 +34,7 @@ to the same executable, install the NVIDIA cuObject **client** SDK, cuFile development libraries, libibverbs development headers and CUDA headers for the target architecture. Set `CUOBJCLIENT_ROOT` to the SDK prefix and `CUDA_HOME` to the CUDA toolkit. -The `rustfs-cuobject-client-sys` crate builds the C++17 adapter against `cuobjclient.h` and links the NVIDIA SDK. The CLI accesses registered windows and tokens through `rustfs-cuobject-client`. +The `rustfs-cuobject-client-sys` crate builds the C++17 adapter against `cuobjclient.h` and links the NVIDIA SDK. The RDMA provider accesses registered windows and tokens through `rustfs-cuobject-client`. The SDK's `libcuobjclient`, `libcufile` and RDMA plugin must be available at runtime. Set `CUOBJ_ENV_PATH_JSON` to the NVIDIA client configuration that selects the application RDMA device. @@ -122,3 +122,35 @@ c++ -std=c++17 -fPIC -shared -I"$CUOBJCLIENT_ROOT/include" -I"$CUDA_HOME/include LD_PRELOAD=/tmp/rc-test-failed-deregistration.so cargo test --manifest-path nixl/Cargo.toml \ -p rustfs-cuobject-client --features client --test registration_budget -- --ignored ``` + +## Embed the Rust client + +Add the SDK from the CLI repository to your application's `Cargo.toml`. +Choose `nixl`, `cuobject`, or both. Each feature requires +the corresponding native libraries described above. The default is `nixl`. + +```toml +[dependencies] +rustfs-rdma-client = { git = "https://github.com/rustfs/cli", default-features = false, features = ["cuobject"] } +``` + +For a local checkout, replace the `git` key with `path = "../cli/nixl/rdma-client"`. + +The following function accepts the endpoint configuration and object key from +the application. Run it on a Tokio runtime. Reuse a provider for clients that +should share endpoint capability probes, and reuse each client across transfers. + +```rust +use rustfs_rdma_client::{Alias, DataTransportMode, ObjectStore, RdmaBackend, RdmaProvider, RemotePath, Result}; + +async fn download(alias: Alias, path: &RemotePath) -> Result> { + let provider = RdmaProvider::new(RdmaBackend::CuObject)?; + let client = provider.client(alias, DataTransportMode::Rdma).await?; + client.get_object(path).await +} +``` + +`RdmaProvider::new` returns `Error::UnsupportedFeature` for a backend +omitted from the build. Native memory registration happens on demand during +transfer. Use `DataTransportMode::Auto` to permit safe HTTP fallback. +Both backends transfer through SDK-managed host memory. diff --git a/nixl/Cargo.lock b/nixl/Cargo.lock index b0c88c3..e825587 100644 --- a/nixl/Cargo.lock +++ b/nixl/Cargo.lock @@ -2996,16 +2996,9 @@ dependencies = [ name = "rustfs-cli-nixl" version = "0.1.0" dependencies = [ - "async-trait", - "base64 0.22.1", - "getrandom 0.4.3", - "http", - "rc-core", "rc-s3", "rustfs-cli", - "rustfs-cuobject-client", - "rustfs-nixl", - "tokio", + "rustfs-rdma-client", ] [[package]] @@ -3038,6 +3031,21 @@ dependencies = [ "tracing", ] +[[package]] +name = "rustfs-rdma-client" +version = "0.1.0" +dependencies = [ + "async-trait", + "base64 0.22.1", + "getrandom 0.4.3", + "http", + "rc-core", + "rc-s3", + "rustfs-cuobject-client", + "rustfs-nixl", + "tokio", +] + [[package]] name = "rusticata-macros" version = "4.1.0" diff --git a/nixl/Cargo.toml b/nixl/Cargo.toml index 5dce38e..d834620 100644 --- a/nixl/Cargo.toml +++ b/nixl/Cargo.toml @@ -8,24 +8,19 @@ publish = false [workspace] resolver = "3" -members = ["cuobject-client", "cuobject-client-sys"] -default-members = ["."] +members = ["rdma-client", "cuobject-client", "cuobject-client-sys"] +default-members = [".", "rdma-client"] [[bin]] name = "rc" path = "src/main.rs" [features] -cuobject = ["dep:rustfs-cuobject-client"] +default = ["nixl"] +nixl = ["rustfs-rdma-client/nixl"] +cuobject = ["rustfs-rdma-client/cuobject"] [dependencies] -rustfs-cuobject-client = { path = "cuobject-client", features = ["client"], optional = true } +rustfs-rdma-client = { path = "rdma-client", default-features = false } rustfs-cli = { path = "../crates/cli" } -rc-core = { path = "../crates/core" } rc-s3 = { path = "../crates/s3" } -rustfs-nixl = { git = "https://github.com/rustfs/rustfs", rev = "b86624520c99de06d9e36b971520a7071ca54b4c", features = ["sys"] } -async-trait = "0.1" -base64 = "0.22" -getrandom = "0.4" -http = "1" -tokio = { version = "1", features = ["io-util", "sync", "time"] } diff --git a/nixl/rdma-client/Cargo.toml b/nixl/rdma-client/Cargo.toml new file mode 100644 index 0000000..a03ff35 --- /dev/null +++ b/nixl/rdma-client/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "rustfs-rdma-client" +version = "0.1.0" +edition = "2024" +rust-version = "1.99.0" +license = "MIT OR Apache-2.0" +description = "Native S3 object transfers over NIXL/UCX and NVIDIA cuObject" +publish = false + +[features] +default = ["nixl"] +nixl = ["dep:rustfs-nixl", "dep:base64", "dep:getrandom"] +cuobject = ["dep:rustfs-cuobject-client"] + +[dependencies] +rustfs-cuobject-client = { path = "../cuobject-client", features = ["client"], optional = true } +rc-core = { path = "../../crates/core" } +rc-s3 = { path = "../../crates/s3" } +rustfs-nixl = { git = "https://github.com/rustfs/rustfs", rev = "b86624520c99de06d9e36b971520a7071ca54b4c", features = ["sys"], optional = true } +async-trait = "0.1" +base64 = { version = "0.22", optional = true } +getrandom = { version = "0.4", optional = true } +http = "1" +tokio = { version = "1", features = ["io-util", "macros", "rt", "sync", "time"] } diff --git a/nixl/src/buffer.rs b/nixl/rdma-client/src/buffer.rs similarity index 76% rename from nixl/src/buffer.rs rename to nixl/rdma-client/src/buffer.rs index c7cb1fe..4ce7edd 100644 --- a/nixl/src/buffer.rs +++ b/nixl/rdma-client/src/buffer.rs @@ -1,11 +1,15 @@ //! Backend registrations used by the shared object transfer pipeline. +#[cfg(feature = "nixl")] use base64::{Engine, engine::general_purpose::STANDARD}; +#[cfg(feature = "nixl")] use http::HeaderValue; use rc_core::{Error, RdmaBackend, Result}; +#[cfg(feature = "nixl")] use rustfs_nixl::{NixlConfig, NixlServer, StagingChunk}; pub(super) enum Buffer { + #[cfg(feature = "nixl")] Nixl { chunk: StagingChunk, agent: HeaderValue, @@ -21,14 +25,20 @@ pub(super) fn register( count: usize, _budget: &crate::pool::Budget, ) -> Result> { - if backend == RdmaBackend::CuObject { + match backend { #[cfg(feature = "cuobject")] - return crate::nvidia::register(size, count, _budget); - #[cfg(not(feature = "cuobject"))] - return Err(Error::UnsupportedFeature( - "The cuObject backend is not installed".into(), - )); + RdmaBackend::CuObject => crate::nvidia::register(size, count, _budget), + #[cfg(feature = "nixl")] + RdmaBackend::Nixl => register_nixl(size, count), + #[cfg(not(all(feature = "cuobject", feature = "nixl")))] + _ => Err(Error::UnsupportedFeature(format!( + "RDMA backend {backend:?} is not enabled" + ))), } +} + +#[cfg(feature = "nixl")] +fn register_nixl(size: usize, count: usize) -> Result> { let id = getrandom::u64() .map_err(|_| Error::General("Could not generate a NIXL agent ID".into()))?; let name = format!("rc-{}-{id:016x}", std::process::id()); @@ -58,6 +68,7 @@ pub(super) fn register( .collect() } +#[cfg(feature = "nixl")] fn nixl_error(error: rustfs_nixl::NixlError) -> Error { Error::General(format!("NIXL/UCX: {error}")) } @@ -65,6 +76,7 @@ fn nixl_error(error: rustfs_nixl::NixlError) -> Error { impl Buffer { pub(super) fn writable(&mut self) -> Result<&mut [u8]> { match self { + #[cfg(feature = "nixl")] Self::Nixl { chunk, .. } => chunk.spare_mut().map_err(nixl_error), #[cfg(feature = "cuobject")] Self::CuObject(buffer) => buffer.writable(), diff --git a/nixl/rdma-client/src/lib.rs b/nixl/rdma-client/src/lib.rs new file mode 100644 index 0000000..5956b26 --- /dev/null +++ b/nixl/rdma-client/src/lib.rs @@ -0,0 +1,29 @@ +//! Linux S3 object transfers using NIXL/UCX or NVIDIA cuObject host memory. +//! +//! Select a backend when creating a provider and a transfer policy per client. +//! The S3 client handles credentials, TLS, signing and multipart publication. +//! +//! ```no_run +//! use rustfs_rdma_client::{Alias, DataTransportMode, ObjectStore, RdmaBackend, RdmaProvider, RemotePath, Result}; +//! +//! async fn download(alias: Alias, backend: RdmaBackend, path: &RemotePath) -> Result> { +//! let provider = RdmaProvider::new(backend)?; +//! let client = provider.client(alias, DataTransportMode::Rdma).await?; +//! client.get_object(path).await +//! } +//! ``` +#![forbid(unsafe_code)] + +#[cfg(not(any(feature = "nixl", feature = "cuobject")))] +compile_error!("Enable the nixl or cuobject feature to select a native transport"); + +mod buffer; +#[cfg(feature = "cuobject")] +mod nvidia; +mod pool; +mod transport; +mod wire; + +pub use rc_core::{Alias, DataTransportMode, Error, ObjectStore, RdmaBackend, RemotePath, Result}; +pub use rc_s3::S3Client; +pub use transport::RdmaProvider; diff --git a/nixl/src/nvidia.rs b/nixl/rdma-client/src/nvidia.rs similarity index 100% rename from nixl/src/nvidia.rs rename to nixl/rdma-client/src/nvidia.rs diff --git a/nixl/src/pool.rs b/nixl/rdma-client/src/pool.rs similarity index 96% rename from nixl/src/pool.rs rename to nixl/rdma-client/src/pool.rs index cf55852..c0da6a5 100644 --- a/nixl/src/pool.rs +++ b/nixl/rdma-client/src/pool.rs @@ -11,6 +11,10 @@ pub(crate) const WINDOW: usize = rc_s3::multipart::DEFAULT_PART_SIZE as usize; const SMALL_WINDOW: usize = 1024 * 1024; const WINDOWS_PER_POOL: usize = 2; static AGENTS: Semaphore = Semaphore::const_new(8); +// Several tests intentionally quarantine registrations for the life of the process. +// Run independent native scenarios one at a time against the shared registration budget. +#[cfg(test)] +pub(crate) static NATIVE_TEST_LOCK: AsyncMutex<()> = AsyncMutex::const_new(()); // Keep the exposed registration and its budget, so the retired pool can free its idle buffers. pub(super) type Budget = Arc>; static QUARANTINE: Mutex> = Mutex::new(Vec::new()); @@ -139,6 +143,7 @@ impl Window { let mut exposed = Exposed(Some(self)); let buffer = exposed.window_mut().buffer.as_mut().expect("owned window"); let result = match buffer { + #[cfg(feature = "nixl")] Buffer::Nixl { chunk, agent, @@ -208,6 +213,7 @@ mod tests { assert_eq!(window_size(u64::MAX), WINDOW); } + #[cfg(feature = "nixl")] #[tokio::test] async fn uncertain_windows_wake_waiters_and_cannot_be_reissued() { check_window_lifetime(RdmaBackend::Nixl).await; @@ -242,6 +248,7 @@ mod tests { } async fn check_window_lifetime(backend: RdmaBackend) { + let _guard = NATIVE_TEST_LOCK.lock().await; let pools = Pools::new(backend); let first = pools.reserve(32).await.unwrap(); let second = pools.reserve(32).await.unwrap(); diff --git a/nixl/src/transport.rs b/nixl/rdma-client/src/transport.rs similarity index 83% rename from nixl/src/transport.rs rename to nixl/rdma-client/src/transport.rs index 8f25893..ce297a5 100644 --- a/nixl/src/transport.rs +++ b/nixl/rdma-client/src/transport.rs @@ -3,11 +3,11 @@ mod download; mod upload; +use crate::wire::RDMA_REPLY_HEADER; use async_trait::async_trait; use http::{HeaderMap, HeaderValue, Method, StatusCode}; use rc_core::{DataTransferState, Error, ObjectInfo, RemotePath, Result}; use rc_s3::data_transport::*; -use rustfs_nixl::RDMA_REPLY_HEADER; use std::sync::Arc; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; @@ -20,17 +20,38 @@ use std::sync::Mutex; use std::sync::atomic::{AtomicU8, Ordering}; use tokio::sync::{Semaphore, SemaphorePermit}; -pub(crate) struct Factory { +/// A native transport factory for one backend. Reuse it across clients to share +/// endpoint capability probes. Each client owns its registered window pools. +pub struct RdmaProvider { backend: RdmaBackend, endpoints: Mutex>>, } -impl Factory { - pub(crate) fn new(backend: RdmaBackend) -> Self { - Self { +impl RdmaProvider { + /// Select a compiled backend. Registration is deferred until data transfer. + pub fn new(backend: RdmaBackend) -> Result { + let enabled = match backend { + RdmaBackend::Nixl => cfg!(feature = "nixl"), + RdmaBackend::CuObject => cfg!(feature = "cuobject"), + }; + if !enabled { + return Err(Error::UnsupportedFeature(format!( + "RDMA backend {backend:?} is not enabled" + ))); + } + Ok(Self { backend, endpoints: Mutex::default(), - } + }) + } + + /// Build an S3 client using this provider and the caller's transfer mode. + pub async fn client( + &self, + alias: rc_core::Alias, + mode: rc_core::DataTransportMode, + ) -> Result { + rc_s3::S3Client::new_with_transport_factory(alias, mode, self).await } fn support(&self, endpoint: Option<&str>) -> Arc { @@ -109,7 +130,7 @@ impl Capability { } } -impl ObjectDataTransportFactory for Factory { +impl ObjectDataTransportFactory for RdmaProvider { fn create(&self, control: Arc) -> Result> { Ok(Arc::new(Transport { support: self.support(control.endpoint()), diff --git a/nixl/src/transport/download.rs b/nixl/rdma-client/src/transport/download.rs similarity index 100% rename from nixl/src/transport/download.rs rename to nixl/rdma-client/src/transport/download.rs diff --git a/nixl/src/transport/tests.rs b/nixl/rdma-client/src/transport/tests.rs similarity index 93% rename from nixl/src/transport/tests.rs rename to nixl/rdma-client/src/transport/tests.rs index 9b42f5b..80ed821 100644 --- a/nixl/src/transport/tests.rs +++ b/nixl/rdma-client/src/transport/tests.rs @@ -1,11 +1,14 @@ use super::*; +#[cfg(feature = "nixl")] use std::sync::Mutex; +#[cfg(feature = "nixl")] struct VersionedObject { queries: Mutex>>, get_version: &'static str, } +#[cfg(feature = "nixl")] #[async_trait] impl ControlExecutor for VersionedObject { async fn execute_headers(&self, request: ControlRequest) -> Result { @@ -28,15 +31,18 @@ impl ControlExecutor for VersionedObject { } } +#[cfg(feature = "nixl")] async fn download( version_id: Option<&str>, get_version: &'static str, ) -> (DataTransportResult, Arc) { + let _guard = crate::pool::NATIVE_TEST_LOCK.lock().await; let endpoint = Arc::new(VersionedObject { queries: Mutex::default(), get_version, }); - let transport = Factory::new(RdmaBackend::Nixl) + let transport = RdmaProvider::new(RdmaBackend::Nixl) + .unwrap() .create(endpoint.clone()) .unwrap(); let path = RemotePath::new("test", "bucket", "key"); @@ -59,6 +65,7 @@ async fn download( (result, endpoint) } +#[cfg(feature = "nixl")] #[tokio::test] async fn current_and_explicit_version_keep_the_callers_selector() { for selector in [None, Some("v1")] { @@ -75,6 +82,7 @@ async fn current_and_explicit_version_keep_the_callers_selector() { } } +#[cfg(feature = "nixl")] #[tokio::test] async fn replacement_with_the_same_etag_is_detected_before_writing() { let (result, _) = download(None, "v2").await; @@ -100,7 +108,13 @@ async fn a_delete_marker_keeps_its_version_and_exit_code() { } } - let transport = Factory::new(RdmaBackend::Nixl) + let backend = if cfg!(feature = "nixl") { + RdmaBackend::Nixl + } else { + RdmaBackend::CuObject + }; + let transport = RdmaProvider::new(backend) + .unwrap() .create(Arc::new(DeleteMarker)) .unwrap(); let path = RemotePath::new("test", "bucket", "key"); @@ -128,8 +142,10 @@ async fn a_delete_marker_keeps_its_version_and_exit_code() { } } +#[cfg(feature = "nixl")] #[tokio::test] async fn only_proven_conditional_rejections_reuse_the_registered_window() { + let _guard = crate::pool::NATIVE_TEST_LOCK.lock().await; struct Peer(Mutex>); #[async_trait] impl ControlExecutor for Peer { @@ -171,7 +187,8 @@ async fn only_proven_conditional_rejections_reuse_the_registered_window() { } } let peer = Arc::new(Peer(Mutex::default())); - let transport = Factory::new(RdmaBackend::Nixl) + let transport = RdmaProvider::new(RdmaBackend::Nixl) + .unwrap() .create(peer.clone()) .unwrap(); let path = RemotePath::new("test", "bucket", "key"); @@ -200,6 +217,7 @@ async fn only_proven_conditional_rejections_reuse_the_registered_window() { #[tokio::test] #[ignore = "requires an NVIDIA RDMA device and an authenticated RustFS object"] async fn rustfs_declines_a_stale_conditional_get_before_dma() { + let _guard = crate::pool::NATIVE_TEST_LOCK.lock().await; let env = |key| { std::env::var(key) .expect("configure the test endpoint, credentials, CA and existing bucket") @@ -236,8 +254,10 @@ async fn rustfs_declines_a_stale_conditional_get_before_dma() { } } +#[cfg(feature = "nixl")] #[tokio::test] async fn large_get_submits_both_ranges_and_publishes_in_order() { + let _guard = crate::pool::NATIVE_TEST_LOCK.lock().await; use rustfs_nixl::{ NIXL_AGENT_HEADER, NIXL_BUFFER_HEADER, NIXL_METADATA_HEADER, NixlConfig, NixlServer, RdmaToken, @@ -341,7 +361,8 @@ async fn large_get_submits_both_ranges_and_publishes_in_order() { second_written: tokio::sync::Notify::new(), wrong_checksum, }); - let transport = Factory::new(RdmaBackend::Nixl) + let transport = RdmaProvider::new(RdmaBackend::Nixl) + .unwrap() .create(ranges.clone()) .unwrap(); let mut progress = Vec::new(); @@ -374,8 +395,10 @@ async fn large_get_submits_both_ranges_and_publishes_in_order() { } } +#[cfg(feature = "nixl")] #[tokio::test] async fn one_declined_range_cannot_hide_an_unconfirmed_sibling() { + let _guard = crate::pool::NATIVE_TEST_LOCK.lock().await; use std::sync::atomic::{AtomicUsize, Ordering}; struct Endpoint(AtomicUsize); #[async_trait] @@ -406,7 +429,8 @@ async fn one_declined_range_cannot_hide_an_unconfirmed_sibling() { } } let endpoint = Arc::new(Endpoint(AtomicUsize::new(0))); - let transport = Factory::new(RdmaBackend::Nixl) + let transport = RdmaProvider::new(RdmaBackend::Nixl) + .unwrap() .create(endpoint.clone()) .unwrap(); let result = transport @@ -428,8 +452,10 @@ async fn one_declined_range_cannot_hide_an_unconfirmed_sibling() { ); } +#[cfg(feature = "nixl")] #[tokio::test] async fn ordinary_s3_is_probed_once_per_endpoint_backend_and_operation() { + let _guard = crate::pool::NATIVE_TEST_LOCK.lock().await; use std::sync::atomic::{AtomicUsize, Ordering}; struct HttpOnly(AtomicUsize); #[async_trait] @@ -451,7 +477,7 @@ async fn ordinary_s3_is_probed_once_per_endpoint_backend_and_operation() { }) } } - let factory = Factory::new(RdmaBackend::Nixl); + let factory = RdmaProvider::new(RdmaBackend::Nixl).unwrap(); let endpoint = Arc::new(HttpOnly(AtomicUsize::new(0))); let mut readers = tokio::task::JoinSet::new(); for _ in 0..20 { @@ -491,7 +517,10 @@ async fn ordinary_s3_is_probed_once_per_endpoint_backend_and_operation() { assert_eq!(endpoint.0.load(Ordering::Relaxed), 2); for fresh in [ factory.support(Some("https://other.example")), - Factory::new(RdmaBackend::CuObject).support(endpoint.endpoint()), + #[cfg(feature = "cuobject")] + RdmaProvider::new(RdmaBackend::CuObject) + .unwrap() + .support(endpoint.endpoint()), ] { assert!(fresh.get.enter().await.unwrap().is_some()); } diff --git a/nixl/src/transport/upload.rs b/nixl/rdma-client/src/transport/upload.rs similarity index 100% rename from nixl/src/transport/upload.rs rename to nixl/rdma-client/src/transport/upload.rs diff --git a/nixl/src/wire.rs b/nixl/rdma-client/src/wire.rs similarity index 86% rename from nixl/src/wire.rs rename to nixl/rdma-client/src/wire.rs index 44ad99e..5138a57 100644 --- a/nixl/src/wire.rs +++ b/nixl/rdma-client/src/wire.rs @@ -1,12 +1,15 @@ -//! RustFS S3-over-NIXL request and response fields. +//! RustFS S3-over-RDMA request and response fields. -use http::{HeaderMap, HeaderValue, Method, StatusCode}; +#[cfg(feature = "nixl")] +use http::HeaderValue; +use http::{HeaderMap, Method, StatusCode}; use rc_core::{Error, RemotePath, Result}; use rc_s3::data_transport::ControlRequest; -use rustfs_nixl::{ - NIXL_AGENT_HEADER, NIXL_BUFFER_HEADER, NIXL_METADATA_HEADER, RDMA_BYTES_TRANSFERRED_HEADER, - RDMA_REPLY_HEADER, -}; +#[cfg(feature = "nixl")] +use rustfs_nixl::{NIXL_AGENT_HEADER, NIXL_BUFFER_HEADER, NIXL_METADATA_HEADER}; + +pub(crate) const RDMA_REPLY_HEADER: &str = "x-amz-rdma-reply"; +const RDMA_BYTES_TRANSFERRED_HEADER: &str = "x-amz-rdma-bytes-transferred"; pub(crate) fn request( method: Method, @@ -22,6 +25,7 @@ pub(crate) fn request( } } +#[cfg(feature = "nixl")] pub(crate) fn bind( headers: &mut HeaderMap, agent: &HeaderValue, @@ -93,5 +97,5 @@ mod tests { } } -#[cfg(test)] +#[cfg(all(test, feature = "nixl"))] mod native_test; diff --git a/nixl/src/wire/native_test.rs b/nixl/rdma-client/src/wire/native_test.rs similarity index 95% rename from nixl/src/wire/native_test.rs rename to nixl/rdma-client/src/wire/native_test.rs index b3f5dc6..114cc90 100644 --- a/nixl/src/wire/native_test.rs +++ b/nixl/rdma-client/src/wire/native_test.rs @@ -4,6 +4,7 @@ use rustfs_nixl::{NixlConfig, NixlServer, RdmaToken}; #[tokio::test] async fn rustfs_reads_and_writes_the_advertised_host_window() { + let _guard = crate::pool::NATIVE_TEST_LOCK.lock().await; let client = Pools::default(); let peer = NixlServer::new(&NixlConfig { agent_name: "rc-interop-peer".into(), diff --git a/nixl/rdma-client/tests/provider.rs b/nixl/rdma-client/tests/provider.rs new file mode 100644 index 0000000..c13824a --- /dev/null +++ b/nixl/rdma-client/tests/provider.rs @@ -0,0 +1,55 @@ +//! Exercise the SDK through its public API in a separate test executable. +use rc_s3::data_transport::{ + ControlExecutor, ObjectDataTransport, ObjectDataTransportFactory, install_rdma_backend, +}; +use rustfs_rdma_client::{ + Alias, DataTransportMode, Error, ObjectStore, RdmaBackend, RdmaProvider, RemotePath, Result, +}; +use std::sync::Arc; + +#[test] +fn selecting_a_backend_requires_its_feature() { + for (backend, enabled) in [ + (RdmaBackend::Nixl, cfg!(feature = "nixl")), + (RdmaBackend::CuObject, cfg!(feature = "cuobject")), + ] { + match RdmaProvider::new(backend) { + Ok(_) => assert!(enabled), + Err(Error::UnsupportedFeature(_)) => assert!(!enabled), + Err(error) => panic!("unexpected provider error: {error}"), + } + } +} + +#[tokio::test] +async fn sdk_clients_use_the_explicit_provider_even_with_a_global_factory() { + struct FailingFactory; + impl ObjectDataTransportFactory for FailingFactory { + fn create(&self, _: Arc) -> Result> { + Err(Error::General("global factory selected".into())) + } + } + install_rdma_backend(RdmaBackend::Nixl, Arc::new(FailingFactory)).unwrap(); + rc_core::set_global_data_transport_mode(DataTransportMode::Rdma); + let mut alias = Alias::new("test", "https://127.0.0.1:1", "test", "test"); + assert!( + matches!(rc_s3::S3Client::new(alias.clone()).await, Err(Error::General(message)) if message == "global factory selected") + ); + let backend = if cfg!(feature = "nixl") { + RdmaBackend::Nixl + } else { + RdmaBackend::CuObject + }; + alias.anonymous = true; + let client = RdmaProvider::new(backend) + .unwrap() + .client(alias, DataTransportMode::Rdma) + .await + .unwrap(); + let path = RemotePath::new("test", "bucket", "object"); + let error = client.get_object(&path).await.unwrap_err(); + assert!( + matches!(error, Error::DataTransfer { source: Some(source), .. } + if matches!(*source, Error::RequestRejected(_))) + ); +} diff --git a/nixl/src/main.rs b/nixl/src/main.rs index aa80de3..45055d9 100644 --- a/nixl/src/main.rs +++ b/nixl/src/main.rs @@ -1,24 +1,21 @@ //! rc with native S3-over-RDMA providers. #![forbid(unsafe_code)] -mod buffer; -#[cfg(feature = "cuobject")] -mod nvidia; -mod pool; -mod transport; -mod wire; - -use rc_core::RdmaBackend; +use rustfs_rdma_client::{RdmaBackend, RdmaProvider}; +use std::sync::Arc; fn register(backend: RdmaBackend) { - let factory = std::sync::Arc::new(transport::Factory::new(backend)); - if let Err(error) = rc_s3::data_transport::install_rdma_backend(backend, factory) { + let result = RdmaProvider::new(backend).and_then(|provider| { + rc_s3::data_transport::install_rdma_backend(backend, Arc::new(provider)) + }); + if let Err(error) = result { eprintln!("{error}"); std::process::exit(error.exit_code()); } } fn main() { + #[cfg(feature = "nixl")] register(RdmaBackend::Nixl); #[cfg(feature = "cuobject")] register(RdmaBackend::CuObject);