Skip to content

Commit c84488c

Browse files
authored
DRIVERS-2993 specify behavior of w:0 with MongoClient.bulkWrite (#1670)
* add CRUD prose test 15 * use unordered write in prose test 10 * use unordered write in existing `unacknowledged-client-bulkWrite.yml` * ban `w:0` with `verboseResults` / `ordered` * test `w:0` and `verboseResults` / `ordered`
1 parent c70785d commit c84488c

File tree

6 files changed

+150
-5
lines changed

6 files changed

+150
-5
lines changed

source/command-logging-and-monitoring/tests/monitoring/unacknowledged-client-bulkWrite.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/command-logging-and-monitoring/tests/monitoring/unacknowledged-client-bulkWrite.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ tests:
5050
namespace: *namespace
5151
filter: { _id: 3 }
5252
update: { $set: { x: 333 } }
53+
ordered: false
5354
expectResult:
5455
insertedCount:
5556
$$unsetOrMatches: 0
@@ -89,7 +90,7 @@ tests:
8990
command:
9091
bulkWrite: 1
9192
errorsOnly: true
92-
ordered: true
93+
ordered: false
9394
ops:
9495
- insert: 0
9596
document: { _id: 4, x: 44 }

source/crud/bulk-write.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,11 @@ returned. This field is optional and defaults to false on the server.
614614
value for `verboseResults`, drivers MUST define `errorsOnly` as the opposite of `verboseResults`. If the user did not
615615
specify a value for `verboseResults`, drivers MUST define `errorsOnly` as `true`.
616616

617+
Drivers MUST return a client-side error if `verboseResults` is true with an unacknowledged write concern containing the
618+
following message:
619+
620+
> Cannot request unacknowledged write concern and verbose results
621+
617622
### `ordered`
618623

619624
The `ordered` field defines whether writes should be executed in the order in which they were specified, and, if an
@@ -622,6 +627,11 @@ server. Drivers MUST explicitly define `ordered` as `true` in the `bulkWrite` co
622627
`BulkWriteOptions`. This is required to avoid inconsistencies between server and driver behavior if the server default
623628
changes in the future.
624629

630+
Drivers MUST return a client-side error if `ordered` is true (including when default is applied) with an unacknowledged
631+
write concern containing the following message:
632+
633+
> Cannot request unacknowledged write concern and ordered writes
634+
625635
### Size Limits
626636

627637
The server reports a `maxBsonObjectSize` in its `hello` response. This value defines the maximum size for documents that
@@ -925,6 +935,8 @@ batch-splitting to standardize implementations across drivers and simplify batch
925935

926936
## **Changelog**
927937

938+
- 2024-10-07: Error if `w:0` is used with `ordered=true` or `verboseResults=true`.
939+
928940
- 2024-10-01: Add sort option to `replaceOne` and `updateOne`.
929941

930942
- 2024-09-30: Define more options for modeling summary vs. verbose results.

source/crud/tests/README.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ InsertOne: {
397397

398398
Construct as list of write models (referred to as `models`) with the one `model`.
399399

400-
Call `MongoClient.bulkWrite` with `models` and `BulkWriteOptions.writeConcern` set to an unacknowledged write concern.
400+
Call `MongoClient.bulkWrite` with `models`. Pass `BulkWriteOptions` with `ordered` set to `false` and `writeConcern` set
401+
to an unacknowledged write concern.
401402

402403
Expect a client-side error due the size.
403404

@@ -415,7 +416,8 @@ ReplaceOne: {
415416

416417
Construct as list of write models (referred to as `models`) with the one `model`.
417418

418-
Call `MongoClient.bulkWrite` with `models` and `BulkWriteOptions.writeConcern` set to an unacknowledged write concern.
419+
Call `MongoClient.bulkWrite` with `models`. Pass `BulkWriteOptions` with `ordered` set to `false` and `writeConcern` set
420+
to an unacknowledged write concern.
419421

420422
Expect a client-side error due the size.
421423

@@ -693,3 +695,45 @@ maxTimeMS value of 2000ms for the `explain`.
693695

694696
Obtain the command started event for the explain. Confirm that the top-level explain command should has a `maxTimeMS`
695697
value of `2000`.
698+
699+
### 15. `MongoClient.bulkWrite` with unacknowledged write concern uses `w:0` for all batches
700+
701+
This test must only be run on 8.0+ servers. This test must be skipped on Atlas Serverless.
702+
703+
If testing with a sharded cluster, only connect to one mongos. This is intended to ensure the `countDocuments` operation
704+
uses the same connection as the `bulkWrite` to get the correct connection count. (See
705+
[DRIVERS-2921](https://jira.mongodb.org/browse/DRIVERS-2921)).
706+
707+
Construct a `MongoClient` (referred to as `client`) with
708+
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) enabled to observe
709+
CommandStartedEvents. Perform a `hello` command using `client` and record the `maxBsonObjectSize` and
710+
`maxMessageSizeBytes` values in the response.
711+
712+
Construct a `MongoCollection` (referred to as `coll`) for the collection "db.coll". Drop `coll`.
713+
714+
Use the `create` command to create "db.coll" to workaround [SERVER-95537](https://jira.mongodb.org/browse/SERVER-95537).
715+
716+
Construct the following write model (referred to as `model`):
717+
718+
```javascript
719+
InsertOne: {
720+
"namespace": "db.coll",
721+
"document": { "a": "b".repeat(maxBsonObjectSize - 500) }
722+
}
723+
```
724+
725+
Construct a list of write models (referred to as `models`) with `model` repeated
726+
`maxMessageSizeBytes / maxBsonObjectSize + 1` times.
727+
728+
Call `client.bulkWrite` with `models`. Pass `BulkWriteOptions` with `ordered` set to `false` and `writeConcern` set to
729+
an unacknowledged write concern. Assert no error occurred. Assert the result indicates the write was unacknowledged.
730+
731+
Assert that two CommandStartedEvents (referred to as `firstEvent` and `secondEvent`) were observed for the `bulkWrite`
732+
command. Assert that the length of `firstEvent.command.ops` is `maxMessageSizeBytes / maxBsonObjectSize`. Assert that
733+
the length of `secondEvent.command.ops` is 1. If the driver exposes `operationId`s in its CommandStartedEvents, assert
734+
that `firstEvent.operationId` is equal to `secondEvent.operationId`. Assert both commands include
735+
`writeConcern: {w: 0}`.
736+
737+
To force completion of the `w:0` writes, execute `coll.countDocuments` and expect the returned count is
738+
`maxMessageSizeBytes / maxBsonObjectSize + 1`. This is intended to avoid incomplete writes interfering with other tests
739+
that may use this collection.

source/crud/tests/unified/client-bulkWrite-errors.json

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/crud/tests/unified/client-bulkWrite-errors.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,32 @@ tests:
239239
verboseResults: true
240240
expectError:
241241
isClientError: true
242+
- description: "Requesting unacknowledged write with verboseResults is a client-side error"
243+
operations:
244+
- name: clientBulkWrite
245+
object: *client0
246+
arguments:
247+
models:
248+
- insertOne:
249+
namespace: *namespace
250+
document: { _id: 10 }
251+
verboseResults: true
252+
ordered: false
253+
writeConcern: { w: 0 }
254+
expectError:
255+
isClientError: true
256+
errorContains: "Cannot request unacknowledged write concern and verbose results"
257+
- description: "Requesting unacknowledged write with ordered is a client-side error"
258+
operations:
259+
- name: clientBulkWrite
260+
object: *client0
261+
arguments:
262+
models:
263+
- insertOne:
264+
namespace: *namespace
265+
document: { _id: 10 }
266+
# Omit `ordered` option. Defaults to true.
267+
writeConcern: { w: 0 }
268+
expectError:
269+
isClientError: true
270+
errorContains: "Cannot request unacknowledged write concern and ordered writes"

0 commit comments

Comments
 (0)