[SERVER-52634] Transactional bulkWrite error returns only 1 error Created: 05/Nov/20  Updated: 27/Oct/23  Resolved: 09/Nov/20

Status: Closed
Project: Core Server
Component/s: Replication, Write Ops
Affects Version/s: 4.2.3, 4.4.1, 4.2.10
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Dmitry Kizilov Assignee: Edwin Zhou
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:
  1. start session, open transaction
  2. in this session write documents with several duplicated keys
  3. expected: write errors about all duplicates
  4. actual: 1 write error

    @Test
    public void transactionalCase() {
        ClientSession session = dbFactory.getSession(ClientSessionOptions.builder().build());
 
        Document document1 = Document.parse("{_id:1, value: 1}");
        Document document2 = Document.parse("{_id:2, value: 2}");
        Document document3 = Document.parse("{_id:1, value: 3}");
        Document document4 = Document.parse("{_id:1, value: 4}");
 
        session.startTransaction();
        try {
            collection.insertMany(session, List.of(document1, document2, document3, document4),
                    new InsertManyOptions().ordered(false));
            Assert.fail("insertMany should fail");
//            session.commitTransaction();
        } catch (MongoBulkWriteException e) {
            session.abortTransaction();
            Assert.assertEquals(e.getWriteErrors().size(), 2, "Expected to have 2 dup key errors:"); // fails here!
        } finally {
            session.close();
        }
    }

 

Participants:

 Description   

Problem pretty same as in SERVER-38583

There is only one BulkWriteError with dup key after inserting duplicated data within a transaction, even if data contain many of them.

Problem reproduces with Mongo version 4.2.3, 4.2.10, 4.4.1. No info regarding another versions.

 



 Comments   
Comment by Oscar Calderon [ 21/Jan/22 ]

Agreed that it is intended (according to the documentation), but it is not useful. Why not returning all the errors instead of a single one? I would like to have a list of errors to be able to process them later in a correct way, instead of retrying just to find out the second error, and so on.

Comment by Edwin Zhou [ 09/Nov/20 ]

Hi dmitry.kiziloff@gmail.com,

Thank you for your ticket submission. My investigation leads me to believe that the behavior you described, i.e. only a single document is returned in the BulkWriteError, is intended. This is further detailed in the Error Handling inside Transactions section of the db.collection.bulkWrite() docs:

Inside a transaction, the first error in a bulk write causes the entire bulk write to fail and aborts the transaction, even if the bulk write is unordered.

This tells me that as soon as a duplicate error is reached inside of a transaction, there are no further attempts to insert additional documents, preventing further discovery of duplicates.

Best,

Edwin

Generated at Thu Feb 08 05:28:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.