-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
Note: I'm creating this issue simply to record an edge case I encountered while implementing the retryable writes test runner (PHPLIB-307). While doing so, I was reminded that the original CRUD test runner ignores outcome.result.insertedIds assertions for bulkWrite and insertMany operations (see: 88b9f6b for PHPLIB-387).
Currently, InsertOne, InsertMany, and BulkWrite operations only return their corresponding result objects if Server::executeBulkWrite() is successful. Otherwise, a BulkWriteException is thrown, which will only include a WriteResult derived from libmongoc's reply document (examples in Bulk Write Operations), which does not report inserted IDs (CDRIVER-3093).
It may be possible for address this entirely in PHPLIB by examining both the insertedCount from the BulkWriteException's write result and any write errors to filter the array of inserted IDs that would otherwise be attached to PHPLIB's result object. This relies on inferring that libmongoc constructs its insert commands with the same document order as is passed into PHPLIB's own functions (and calls to BulkWrite::insert()). We need not worry about ordered=false for InsertMany and BulkWrite, as that option only determines how insert, update, and delete commands are assembled/executed with respect to one another (documents are still appended to insert commands in the order they are received).
Regarding reverse-engineering of an inserted IDs array, consider:
- If a BulkWriteException was thrown due to a socket error, the sequence of inserts was likely interrupted. The inserted count reported by libmongoc will not include documents in any insert command that failed to return. If the original bulk write included 10 documents evenly split over two commands and only the second command failed, we can simply trim the inserted ID array to the first five elements.
- If a BulkWriteException was thrown due to some write error, we would need to redact any inserted IDs corresponding to each write error index (i.e. WriteError::getIndex()) within the write result.
- If the BulkWriteException indicates a mix of write and socket errors, things are a bit more complicated. Assuming libmongoc still reports the incomplete write result, I believe we can still utilize the reported insertedCount and any reported write errors to filter the original array of inserted IDs.
That said, after obtaining a redacted inserted ID array, we would still need to create a PHPLIB-specific BulkWrite exception to proxy PHPC's original exception and attach the new inserted ID array.
- is related to
-
PHPC-1373 WriteResult is inaccessible if executeBulkWrite() encounters socket error
- Closed
-
CDRIVER-3093 Include inserted _Ids in insert_one() and insert() results
- Closed