Xuanwo opened a new issue, #9249:
URL: https://github.com/apache/opendal/issues/9249

   > [!NOTE]
   > This issue was drafted with AI assistance (GPT-6.2-Astra for the initial 
read-only audit, Gemini 4 Neon for verification and write-up) under Xuanwo's 
direction. Every code reference below was verified against `origin/main` 
(`60526321f`).
   
   ## Component & Version
   
   - Commit: `60526321f` (`main`)
   - Component: `opendal-core` (`Writer`, `WriteState`, `WriteContext`)
   
   ## Problem
   
   `Writer::new` wraps `WriteContext` in an `Arc` and stores two handles to 
it—one on `Writer` and an `Option<Arc<WriteContext>>` on `WriteState`:
   
   - [`core/core/src/types/write/writer.rs` (`Writer` & 
`Writer::new`)](https://github.com/apache/opendal/blob/60526321f/core/core/src/types/write/writer.rs#L101-L123)
   - [`core/core/src/types/context/write.rs` (`WriteState`, `create`, and 
`#[cfg(test)] 
new`)](https://github.com/apache/opendal/blob/60526321f/core/core/src/types/context/write.rs#L148-L208)
   
   After creating the underlying `oio::Writer`, `WriteState` only uses `ctx` in 
`WriteState::reader()` for `copy_from` (introduced in #8140):
   
   - [`core/core/src/types/context/write.rs` 
(`WriteState::reader`)](https://github.com/apache/opendal/blob/60526321f/core/core/src/types/context/write.rs#L306-L320)
   
   This creates unnecessary ownership coupling and maintenance cost:
   
   1. Because the test constructor sets `ctx: None`, the production `copy_from` 
path relies on `.expect("write context must exist outside tests")`.
   2. Byte-only adapters created by `Writer::into_sink`, 
`Writer::into_futures_async_write`, and `Writer::into_bytes_sink` never call 
`copy_from`, yet continue retaining the `Arc<WriteContext>` inside `WriteState`.
   3. Outside unit tests, `WriteContext` is not shared anywhere beyond `Writer` 
and `WriteState`.
   
   ## Proposed Simplification
   
   - Have `Writer` be the sole owner of `WriteContext` (dropping the `Arc` 
wrapper on `Writer` as well if no other runtime owner needs it).
   - Pass `&WriteContext` into `WriteState::create(&ctx)` and 
`WriteState::copy_from_options(&self.ctx, ...)` on demand, keeping 
`pending_copy` continuation state inside `WriteState` so cancel/retry resumes 
from the accepted offset.
   - Remove `WriteState::ctx: Option<Arc<WriteContext>>` and the 
`.expect("write context must exist outside tests")` invariant check.
   
   ## Impact & Verification
   
   - No public API or observable behavior changes.
   - Existing `copy_from` retry, cancellation-resume, and streaming fallback 
tests in 
[`core/core/src/types/context/write.rs`](https://github.com/apache/opendal/blob/60526321f/core/core/src/types/context/write.rs#L1022-L1160)
 and 
[`core/core/src/types/write/writer.rs`](https://github.com/apache/opendal/blob/60526321f/core/core/src/types/write/writer.rs#L259-L299)
 cover the affected paths.
   


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