Xuanwo opened a new pull request, #8961:
URL: https://github.com/apache/opendal/pull/8961
# Which issue does this PR close?
None.
# Rationale for this change
`core/services/mongodb` had three verified correctness and configuration
bugs:
1. **Duplicate-document resurrection and concurrent upsert E11000 failure**:
- `MongodbCore::delete` used `delete_one`, so if a collection without a
unique index accumulated duplicate documents for the same key (for example from
concurrent upserts), deleting the key only removed one document and left stale
copies that resurfaced on subsequent `read` / `stat` calls.
- When a collection *does* have a unique index on `key_field`, concurrent
`update_one(...).upsert(true)` writes to a new key can fail with MongoDB
`E11000` (`DuplicateKey`, SERVER-14322). Previously `set` returned this as a
non-temporary `ErrorKind::Unexpected`.
2. **Broken nested (`a.b`) and invalid (`$`-prefixed / empty-segment) field
paths**:
- `MongodbCore::set` (`$set`) and `MongodbCore::get_length`
(`$binarySize`) interpret dotted `value_field` paths as nested document paths,
but `MongodbCore::get` called `doc.get_binary_generic(&self.value_field)` on
the top-level document and failed with `NotPresent` on every read when
`value_field` was dotted (e.g., `payload.data`).
- `$`-prefixed or malformed field paths (empty segments, NUL bytes) were
accepted at build time, causing `$binarySize: "$$..."` to evaluate as an
aggregation system variable or fail at runtime.
3. **Dropped URI credentials, overwritten `root` option, and missing config
validation**:
- `MongodbConfig::from_uri` dropped `uri.username()` and `uri.password()`
when constructing `connection_string` and unconditionally overwrote an explicit
`root` option when the URI path contained a third segment.
- `MongodbBuilder::build` accepted empty `connection_string`, `database`,
`collection`, `key_field`, and `value_field` values and invalid connection
strings without returning `ErrorKind::ConfigInvalid`.
# What changes are included in this PR?
- `core/services/mongodb/src/core.rs`:
- Switch `MongodbCore::delete` from `delete_one` to `delete_many` so all
duplicate documents for a deleted key are removed.
- Retry `update_one(...).upsert(true)` once in `MongodbCore::set` on
MongoDB `E11000` (`DuplicateKey`) errors and mark a second consecutive `E11000`
as temporary; mark transient MongoDB driver errors
(`TransientTransactionError`, `RetryableWriteError`, I/O, pool-cleared,
server-selection, DNS errors) as temporary in `parse_mongodb_error`, and map
client configuration errors (`InvalidArgument` / `InvalidTlsConfig`) to
`ErrorKind::ConfigInvalid`.
- Add `get_binary_field` to traverse dot-separated nested `Document`
segments in `MongodbCore::get`, matching `$set` and `$binarySize` semantics.
- Add unit tests for nested binary extraction and config-error mapping.
- `core/services/mongodb/src/backend.rs`:
- Validate `connection_string` (non-empty and parsed via
`mongodb::options::ConnectionString::parse`), non-empty `database` and
`collection`, and valid `key_field` / `value_field` paths (rejecting empty
strings, NUL bytes, empty dot segments, and `$`-prefixed segments) in
`MongodbBuilder::build` with `ErrorKind::ConfigInvalid`.
- Add unit tests covering valid flat/nested fields and invalid/empty
configurations.
- `core/services/mongodb/src/config.rs`:
- Preserve percent-encoded `uri.username()` / `uri.password()` in
`MongodbConfig::from_uri` and preserve explicit `root` options via
`entry("root").or_insert_with(...)`.
- Add unit tests covering credential preservation/percent-encoding and
explicit option precedence.
- `core/services/mongodb/src/docs.md`:
- Document creating a unique index on `key_field` to prevent duplicate
documents under concurrent writes.
# Are there any user-facing changes?
- `MongodbBuilder::build` now returns `ErrorKind::ConfigInvalid` for empty
`connection_string`, `database`, or `collection`, malformed
`connection_string`, and invalid `key_field` / `value_field` paths (empty, NUL,
empty dot segments, or `$`-prefixed segments).
- Dotted `value_field` paths (e.g., `payload.data`) now work for `read` in
addition to `write` and `stat`.
- `MongodbConfig::from_uri` preserves URI credentials (percent-encoded) and
respects explicit `root` options over URI path segments.
# Breaking changes
# AI Usage Statement
- Harness: pi
- Model: Gemini 4 Neon
- Effort: high
- Role: Verified all three findings against `core/services/mongodb`,
implemented the fixes, documentation note, and unit tests, and verified the
changes against both unit tests and local MongoDB containers (behavior tests
with default and nested `value_field`, duplicate-document `delete_many`,
concurrent unique-index upserts, and authenticated `from_uri`).
--
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]