Skip to content

Commit

Permalink
RUST-2037 Fix logic for populating bulk write partial result (#1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelatkinson authored Oct 2, 2024
1 parent 17a90a9 commit a6f76b8
Show file tree
Hide file tree
Showing 9 changed files with 847 additions and 157 deletions.
21 changes: 19 additions & 2 deletions src/operation/bulk_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ where
.into()),
}
}

fn ordered(&self) -> bool {
self.options
.and_then(|options| options.ordered)
.unwrap_or(true)
}
}

/// A helper struct for tracking namespace information.
Expand Down Expand Up @@ -314,8 +320,19 @@ where
{
Ok(result)
} else {
// The partial result should only be populated if one or more operations succeeded.
if n_errors < self.n_attempted {
// The partial result should only be populated if the response indicates that at
// least one write succeeded.
let write_succeeded = if self.ordered() {
error
.write_errors
.iter()
.next()
.map(|(index, _)| *index != self.offset)
.unwrap_or(true)
} else {
n_errors < self.n_attempted
};
if write_succeeded {
error.partial_result = Some(result.into_partial_result());
}

Expand Down
34 changes: 25 additions & 9 deletions src/test/spec/json/crud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Using a 5.0+ server, create a collection with
}
```

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand Down Expand Up @@ -431,7 +431,7 @@ Repeat the following setup for each test case:
### Setup

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

Expand Down Expand Up @@ -677,3 +677,19 @@ InsertOne {

Execute `bulkWrite` on `client` with `model`. Assert that an error (referred to as `error`) is returned. Assert that
`error` is a client error containing the message: "bulkWrite does not currently support automatic encryption".

### 14. `explain` helpers allow users to specify `maxTimeMS`

Drivers that provide multiple APIs to specify explain should ensure this test is run at least once with each distinct
API. For example, the Node driver runs this test with option API (`collection.find({}, { explain: ... })`) and the
fluent API (`collection.find({}).explain(...)`).

Create a MongoClient with command monitoring enabled (referred to as `client`).

Create a collection, referred to as `collection`, with the namespace `explain-test.collection`.

Run an explained find on `collection`. The find will have the query predicate `{ name: 'john doe' }`. Specify a
maxTimeMS value of 2000ms for the `explain`.

Obtain the command started event for the explain. Confirm that the top-level explain command should has a `maxTimeMS`
value of `2000`.
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@
"x": 33
}
]
},
{
"collectionName": "coll1",
"databaseName": "db0",
"documents": []
}
],
"tests": [
Expand Down Expand Up @@ -159,22 +154,6 @@
}
]
}
],
"outcome": [
{
"collectionName": "coll1",
"databaseName": "db0",
"documents": [
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
]
},
{
Expand Down Expand Up @@ -250,22 +229,6 @@
}
]
}
],
"outcome": [
{
"collectionName": "coll1",
"databaseName": "db0",
"documents": [
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
]
},
{
Expand Down Expand Up @@ -344,22 +307,6 @@
}
]
}
],
"outcome": [
{
"collectionName": "coll1",
"databaseName": "db0",
"documents": [
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
]
},
{
Expand Down Expand Up @@ -438,22 +385,6 @@
}
]
}
],
"outcome": [
{
"collectionName": "coll1",
"databaseName": "db0",
"documents": [
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
]
}
]
Expand Down
12 changes: 0 additions & 12 deletions src/test/spec/json/crud/unified/aggregate-write-readPreference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ initialData:
- { _id: 1, x: 11 }
- { _id: 2, x: 22 }
- { _id: 3, x: 33 }
- collectionName: *collection1Name
databaseName: *database0Name
documents: []

tests:
- description: "Aggregate with $out includes read preference for 5.0+ server"
Expand All @@ -78,12 +75,6 @@ tests:
$readPreference: *readPreference
readConcern: *readConcern
writeConcern: *writeConcern
outcome: &outcome
- collectionName: *collection1Name
databaseName: *database0Name
documents:
- { _id: 2, x: 22 }
- { _id: 3, x: 33 }

- description: "Aggregate with $out omits read preference for pre-5.0 server"
runOnRequirements:
Expand All @@ -108,7 +99,6 @@ tests:
$readPreference: { $$exists: false }
readConcern: *readConcern
writeConcern: *writeConcern
outcome: *outcome

- description: "Aggregate with $merge includes read preference for 5.0+ server"
runOnRequirements:
Expand All @@ -131,7 +121,6 @@ tests:
$readPreference: *readPreference
readConcern: *readConcern
writeConcern: *writeConcern
outcome: *outcome

- description: "Aggregate with $merge omits read preference for pre-5.0 server"
runOnRequirements:
Expand All @@ -152,4 +141,3 @@ tests:
$readPreference: { $$exists: false }
readConcern: *readConcern
writeConcern: *writeConcern
outcome: *outcome
Loading

0 comments on commit a6f76b8

Please sign in to comment.