Skip to content

Commit a6f76b8

Browse files
RUST-2037 Fix logic for populating bulk write partial result (#1212)
1 parent 17a90a9 commit a6f76b8

9 files changed

+847
-157
lines changed

src/operation/bulk_write.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ where
136136
.into()),
137137
}
138138
}
139+
140+
fn ordered(&self) -> bool {
141+
self.options
142+
.and_then(|options| options.ordered)
143+
.unwrap_or(true)
144+
}
139145
}
140146

141147
/// A helper struct for tracking namespace information.
@@ -314,8 +320,19 @@ where
314320
{
315321
Ok(result)
316322
} else {
317-
// The partial result should only be populated if one or more operations succeeded.
318-
if n_errors < self.n_attempted {
323+
// The partial result should only be populated if the response indicates that at
324+
// least one write succeeded.
325+
let write_succeeded = if self.ordered() {
326+
error
327+
.write_errors
328+
.iter()
329+
.next()
330+
.map(|(index, _)| *index != self.offset)
331+
.unwrap_or(true)
332+
} else {
333+
n_errors < self.n_attempted
334+
};
335+
if write_succeeded {
319336
error.partial_result = Some(result.into_partial_result());
320337
}
321338

src/test/spec/json/crud/README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Using a 5.0+ server, create a collection with
5959
}
6060
```
6161

62-
Enable [command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.rst) to observe
62+
Enable [command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) to observe
6363
CommandSucceededEvents. Then, insert an invalid document (e.g. `{x: 1}`) and assert that a WriteError occurs, that its
6464
code is `121` (i.e. DocumentValidationFailure), and that its `details` property is accessible. Additionally, assert that
6565
a CommandSucceededEvent was observed and that the `writeErrors[0].errInfo` field in the response document matches the
@@ -73,7 +73,7 @@ Test that `MongoClient.bulkWrite` properly handles `writeModels` inputs containi
7373
This test must only be run on 8.0+ servers. This test must be skipped on Atlas Serverless.
7474

7575
Construct a `MongoClient` (referred to as `client`) with
76-
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.rst) enabled to observe
76+
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) enabled to observe
7777
CommandStartedEvents. Perform a `hello` command using `client` and record the `maxWriteBatchSize` value contained in the
7878
response. Then, construct the following write model (referred to as `model`):
7979

@@ -101,7 +101,7 @@ Test that `MongoClient.bulkWrite` properly handles a `writeModels` input which c
101101
This test must only be run on 8.0+ servers. This test must be skipped on Atlas Serverless.
102102

103103
Construct a `MongoClient` (referred to as `client`) with
104-
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.rst) enabled to observe
104+
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) enabled to observe
105105
CommandStartedEvents. Perform a `hello` command using `client` and record the following values from the response:
106106
`maxBsonObjectSize` and `maxMessageSizeBytes`. Then, construct the following document (referred to as `document`):
107107

@@ -140,7 +140,7 @@ Test that `MongoClient.bulkWrite` properly collects and reports `writeConcernErr
140140
This test must only be run on 8.0+ servers. This test must be skipped on Atlas Serverless.
141141

142142
Construct a `MongoClient` (referred to as `client`) with `retryWrites: false` configured and
143-
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.rst) enabled to observe
143+
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) enabled to observe
144144
CommandStartedEvents. Perform a `hello` command using `client` and record the `maxWriteBatchSize` value contained in the
145145
response. Then, configure the following fail point with `client`:
146146

@@ -185,7 +185,7 @@ Test that `MongoClient.bulkWrite` handles individual write errors across batches
185185
This test must only be run on 8.0+ servers. This test must be skipped on Atlas Serverless.
186186

187187
Construct a `MongoClient` (referred to as `client`) with
188-
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.rst) enabled to observe
188+
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) enabled to observe
189189
CommandStartedEvents. Perform a `hello` command using `client` and record the `maxWriteBatchSize` value contained in the
190190
response.
191191

@@ -240,7 +240,7 @@ Test that `MongoClient.bulkWrite` properly iterates the results cursor when `get
240240
This test must only be run on 8.0+ servers. This test must be skipped on Atlas Serverless.
241241

242242
Construct a `MongoClient` (referred to as `client`) with
243-
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.rst) enabled to observe
243+
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) enabled to observe
244244
CommandStartedEvents. Perform a `hello` command using `client` and record the `maxBsonObjectSize` value from the
245245
response.
246246

@@ -280,7 +280,7 @@ This test must only be run on 8.0+ servers. This test must be skipped on Atlas S
280280
against standalone servers.
281281

282282
Construct a `MongoClient` (referred to as `client`) with
283-
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.rst) enabled to observe
283+
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) enabled to observe
284284
CommandStartedEvents. Perform a `hello` command using `client` and record the `maxBsonObjectSize` value from the
285285
response.
286286

@@ -322,7 +322,7 @@ Test that `MongoClient.bulkWrite` properly handles a failure that occurs when at
322322
This test must only be run on 8.0+ servers. This test must be skipped on Atlas Serverless.
323323

324324
Construct a `MongoClient` (referred to as `client`) with
325-
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.rst) enabled to observe
325+
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) enabled to observe
326326
CommandStartedEvents. Perform a `hello` command using `client` and record the `maxBsonObjectSize` value from the
327327
response. Then, configure the following fail point with `client`:
328328

@@ -431,7 +431,7 @@ Repeat the following setup for each test case:
431431
### Setup
432432

433433
Construct a `MongoClient` (referred to as `client`) with
434-
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.rst) enabled to observe
434+
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) enabled to observe
435435
CommandStartedEvents. Perform a `hello` command using `client` and record the following values from the response:
436436
`maxBsonObjectSize` and `maxMessageSizeBytes`.
437437

@@ -677,3 +677,19 @@ InsertOne {
677677

678678
Execute `bulkWrite` on `client` with `model`. Assert that an error (referred to as `error`) is returned. Assert that
679679
`error` is a client error containing the message: "bulkWrite does not currently support automatic encryption".
680+
681+
### 14. `explain` helpers allow users to specify `maxTimeMS`
682+
683+
Drivers that provide multiple APIs to specify explain should ensure this test is run at least once with each distinct
684+
API. For example, the Node driver runs this test with option API (`collection.find({}, { explain: ... })`) and the
685+
fluent API (`collection.find({}).explain(...)`).
686+
687+
Create a MongoClient with command monitoring enabled (referred to as `client`).
688+
689+
Create a collection, referred to as `collection`, with the namespace `explain-test.collection`.
690+
691+
Run an explained find on `collection`. The find will have the query predicate `{ name: 'john doe' }`. Specify a
692+
maxTimeMS value of 2000ms for the `explain`.
693+
694+
Obtain the command started event for the explain. Confirm that the top-level explain command should has a `maxTimeMS`
695+
value of `2000`.

src/test/spec/json/crud/unified/aggregate-write-readPreference.json

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@
7878
"x": 33
7979
}
8080
]
81-
},
82-
{
83-
"collectionName": "coll1",
84-
"databaseName": "db0",
85-
"documents": []
8681
}
8782
],
8883
"tests": [
@@ -159,22 +154,6 @@
159154
}
160155
]
161156
}
162-
],
163-
"outcome": [
164-
{
165-
"collectionName": "coll1",
166-
"databaseName": "db0",
167-
"documents": [
168-
{
169-
"_id": 2,
170-
"x": 22
171-
},
172-
{
173-
"_id": 3,
174-
"x": 33
175-
}
176-
]
177-
}
178157
]
179158
},
180159
{
@@ -250,22 +229,6 @@
250229
}
251230
]
252231
}
253-
],
254-
"outcome": [
255-
{
256-
"collectionName": "coll1",
257-
"databaseName": "db0",
258-
"documents": [
259-
{
260-
"_id": 2,
261-
"x": 22
262-
},
263-
{
264-
"_id": 3,
265-
"x": 33
266-
}
267-
]
268-
}
269232
]
270233
},
271234
{
@@ -344,22 +307,6 @@
344307
}
345308
]
346309
}
347-
],
348-
"outcome": [
349-
{
350-
"collectionName": "coll1",
351-
"databaseName": "db0",
352-
"documents": [
353-
{
354-
"_id": 2,
355-
"x": 22
356-
},
357-
{
358-
"_id": 3,
359-
"x": 33
360-
}
361-
]
362-
}
363310
]
364311
},
365312
{
@@ -438,22 +385,6 @@
438385
}
439386
]
440387
}
441-
],
442-
"outcome": [
443-
{
444-
"collectionName": "coll1",
445-
"databaseName": "db0",
446-
"documents": [
447-
{
448-
"_id": 2,
449-
"x": 22
450-
},
451-
{
452-
"_id": 3,
453-
"x": 33
454-
}
455-
]
456-
}
457388
]
458389
}
459390
]

src/test/spec/json/crud/unified/aggregate-write-readPreference.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ initialData:
5151
- { _id: 1, x: 11 }
5252
- { _id: 2, x: 22 }
5353
- { _id: 3, x: 33 }
54-
- collectionName: *collection1Name
55-
databaseName: *database0Name
56-
documents: []
5754

5855
tests:
5956
- description: "Aggregate with $out includes read preference for 5.0+ server"
@@ -78,12 +75,6 @@ tests:
7875
$readPreference: *readPreference
7976
readConcern: *readConcern
8077
writeConcern: *writeConcern
81-
outcome: &outcome
82-
- collectionName: *collection1Name
83-
databaseName: *database0Name
84-
documents:
85-
- { _id: 2, x: 22 }
86-
- { _id: 3, x: 33 }
8778

8879
- description: "Aggregate with $out omits read preference for pre-5.0 server"
8980
runOnRequirements:
@@ -108,7 +99,6 @@ tests:
10899
$readPreference: { $$exists: false }
109100
readConcern: *readConcern
110101
writeConcern: *writeConcern
111-
outcome: *outcome
112102

113103
- description: "Aggregate with $merge includes read preference for 5.0+ server"
114104
runOnRequirements:
@@ -131,7 +121,6 @@ tests:
131121
$readPreference: *readPreference
132122
readConcern: *readConcern
133123
writeConcern: *writeConcern
134-
outcome: *outcome
135124

136125
- description: "Aggregate with $merge omits read preference for pre-5.0 server"
137126
runOnRequirements:
@@ -152,4 +141,3 @@ tests:
152141
$readPreference: { $$exists: false }
153142
readConcern: *readConcern
154143
writeConcern: *writeConcern
155-
outcome: *outcome

0 commit comments

Comments
 (0)