You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2025/04/12 05:47:01 UTC

(opendal) 01/01: docs(core): Add performance guide for concurrent write

This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch add-performance-guide
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit f6e47d396f3fc929422eaeb77aeb9f2524b3d551
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Sat Apr 12 13:46:38 2025 +0800

    docs(core): Add performance guide for concurrent write
    
    Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
 core/src/docs/mod.rs                          |  2 +
 core/src/docs/performance/concurrent_write.md | 99 +++++++++++++++++++++++++++
 core/src/docs/{ => performance}/mod.rs        | 32 +++------
 core/src/lib.rs                               |  6 ++
 4 files changed, 117 insertions(+), 22 deletions(-)

diff --git a/core/src/docs/mod.rs b/core/src/docs/mod.rs
index 5d39c17a3..88102e557 100644
--- a/core/src/docs/mod.rs
+++ b/core/src/docs/mod.rs
@@ -27,6 +27,8 @@ pub mod concepts;
 
 pub mod internals;
 
+pub mod performance;
+
 /// Changes log for all OpenDAL released versions.
 #[doc = include_str!("../../CHANGELOG.md")]
 #[cfg(not(doctest))]
diff --git a/core/src/docs/performance/concurrent_write.md b/core/src/docs/performance/concurrent_write.md
new file mode 100644
index 000000000..c995b54ab
--- /dev/null
+++ b/core/src/docs/performance/concurrent_write.md
@@ -0,0 +1,99 @@
+# Concurrent Write
+
+OpenDAL writes data sequentially by default.
+
+```rust
+# use opendal::Operator;
+# async fn test() {
+let w = op.writer("test.txt").await?;
+w.write(data1).await?;
+w.write(data2).await?;
+w.close().await?;
+# }
+```
+
+Most of the time, this can't maximize write performance due to limitations on a single connection. We can perform concurrent writes to improve performance.
+
+```rust
+# use opendal::Operator;
+# async fn test() {
+let w = op
+    .writer_with("test.txt")
+    .concurrent(8)
+    .await?;
+
+w.write(data1).await?;
+w.write(data2).await?;
+w.close().await?;
+# }
+```
+
+After setting `concurrent`, OpenDAL will attempt to write the specified file concurrently. The maximum level of concurrency is determined by the `concurrent` parameter. By default, it is set to 1, indicating a sequential write operation.
+
+Under the hood, OpenDAL maintains a task queue to manage concurrent writes. It spawns asynchronous tasks in the background using the [`Executor`][crate::Executor] and tracks the status of each task. The task queue is flushed when the writer is closed, allowing data to be written concurrently without blocking the main thread.
+
+Take our example here, the write of `data1` will not block the write of `data2`. The two writes will be executed concurrently.
+
+The underlying implementation of concurrent writes may vary depending on the backend. For instance, the `s3` backend leverages the S3 Multipart Uploads API to handle concurrent writes, while the `azblob` backend utilizes the Block API for the same purpose.
+
+## Tuning
+
+There are two parameters that can be tuned to optimize concurrent writes:
+
+- `concurrent`: This parameter controls the maximum number of concurrent writes. Increasing this value can improve performance, but it may also increase resource usage. The default value is 1.
+- `chunk`: This parameter specifies the size of each chunk of data to be written. A larger chunk size can improve performance, but it may also increase memory usage. The default value is vary for different storage services.
+
+### `concurrent`
+
+The most important thing to understand is that `concurrent` is not a strict limit. It represents the maximum number of concurrent writes that OpenDAL will attempt to perform. The actual number of concurrent writes may be lower, depending on the input data throughput.
+
+For example, if you set `concurrent` to 8, OpenDAL will attempt to perform up to 8 concurrent writes. However, if the input data throughput is low, it might only carry out 2 or 3 concurrent writes at a time, as there isn't enough data to keep all 8 writes active.
+
+The best value for `concurrent` depends on the specific use case and the underlying storage service. In general, a higher value can lead to better performance, but it may also increase resource usage. It's recommended to start with a lower value like `2` or `4` and gradually increase it while monitoring performance and resource usage.
+
+### `chunk`
+
+The `chunk` parameter specifies the size of each chunk of data to be written. A larger chunk size can improve performance, but it may also increase memory usage. The default value is vary for different storage services. 
+
+For example, s3 is using `5MiB` as the default chunk size. It's also the minimum chunk size for s3. If you set a smaller chunk size, OpenDAL will automatically adjust it to `5MiB`.
+
+The best value for `chunk` depends on the specific use case and the underlying storage service. For most object storage services, a chunk size of `8MiB` or larger is recommended. However, if you're working with smaller files or have limited memory resources, you may want to use a smaller chunk size.
+
+Please note that if you input small chunks of data, OpenDAL will attempt to merge them into a larger chunk before writing. This helps avoid the overhead of writing numerous small chunks, which can negatively affect performance.
+
+## Usage
+
+To upload a large in-memory chunk concurrently:
+
+```rust
+# use opendal::Operator;
+# async fn test() {
+let data = vec![0; 10 * 1024 * 1024]; // 10MiB
+let _ = op.write_with("test.txt", data).concurrent(4).await?;
+# }
+```
+
+`concurrent` and `chunk` also works in [`into_sink`][crate::Writer::into_sink], [`into_bytes_sink`][crate::Writer::into_bytes_sink] and [`into_futures_async_write`][crate::Writer::into_futures_async_write]:
+
+```rust
+use std::io;
+
+use bytes::Bytes;
+use futures::SinkExt;
+use opendal::{Buffer, Operator};
+use opendal::Result;
+
+async fn test(op: Operator) -> io::Result<()> {
+    let mut w = op
+        .writer_with("hello.txt")
+        .concurrent(8)
+        .chunk(256)
+        .await?
+        .into_sink();
+    let bs = "Hello, World!".as_bytes();
+    w.send(Buffer::from(bs)).await?;
+    w.close().await?;
+
+    Ok(())
+}
+```
diff --git a/core/src/docs/mod.rs b/core/src/docs/performance/mod.rs
similarity index 60%
copy from core/src/docs/mod.rs
copy to core/src/docs/performance/mod.rs
index 5d39c17a3..25f405df3 100644
--- a/core/src/docs/mod.rs
+++ b/core/src/docs/performance/mod.rs
@@ -15,27 +15,15 @@
 // specific language governing permissions and limitations
 // under the License.
 
-//! This module holds documentation for OpenDAL.
+//! OpenDAL Performance Guide
 //!
-//! It's highly recommended that you start by reading [`concepts`] first.
-
-#![allow(rustdoc::bare_urls)]
-
-pub mod comparisons;
-
-pub mod concepts;
-
-pub mod internals;
-
-/// Changes log for all OpenDAL released versions.
-#[doc = include_str!("../../CHANGELOG.md")]
-#[cfg(not(doctest))]
-pub mod changelog {}
-
-#[cfg(not(doctest))]
-pub mod rfcs;
+//! OpenDAL is designed in a zero-cost abstraction way, which means that
+//! users won't pay for the abstraction cost if they don't use it. But this
+//! can also means that users can't maximize the performance of OpenDAL
+//! if they don't know how to use it.
+//!
+//! This document will introduce some tips to improve the performance of
+//! OpenDAL.
 
-/// Upgrade and migrate procedures while OpenDAL meets breaking changes.
-#[doc = include_str!("upgrade.md")]
-#[cfg(not(doctest))]
-pub mod upgrade {}
+#[doc = include_str!("concurrent_write.md")]
+pub mod concurrent_write {}
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 98dde718c..1bce1d9e4 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -124,6 +124,12 @@
 //!     Ok(())
 //! }
 //! ```
+//!
+//! # Useful Links
+//!
+//! - [Concept][crate::docs::concepts]
+//! - [Internals][crate::docs::internals]
+//! - [Performance Guide][crate::docs::performance]
 
 // Make sure all our public APIs have docs.
 #![warn(missing_docs)]