New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: Bulk execution with failure throws DuplicateKey instead of BulkWriteException #328

Closed
MustafaHaddara opened this Issue Mar 13, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@MustafaHaddara

MustafaHaddara commented Mar 13, 2018

Seems like a regression of #135

I'm doing this:

BulkWriteOperation builder = getCollection().initializeUnorderedBulkOperation();
for (Foo foo : myList) {
        DBObject insObj = entityToDBObj(for, involvedObjects);
        builder.insert(insObj);
    }

    try {
        builder.execute().isAcknowledged();
    } catch (BulkWriteException bwe) {
        // handle the BulkWriteException
    }
}

As in that issue, when I run this test against mongo, I hit the BulkWriteException and get the following stack trace:

com.mongodb.BulkWriteException: Bulk write operation error on server 127.0.0.1:27017. Write errors: [BulkWriteError{index=0, code=11000, message='E11000 duplicate key error collection: mydb.collectionName index: <indexName> dup key: { : 1, : 0 }', details={ }}, BulkWriteError{index=1, code=11000, message='E11000 duplicate key error collection: mydb.collectionName index: <indexName> dup key: { : 1, : 1 }', details={ }}, BulkWriteError{index=2, code=11000, message='E11000 duplicate key error collection: mydb.collectionName index: <indexName> dup key: { : 1, : 2 }', details={ }}, BulkWriteError{index=3, code=11000, message='E11000 duplicate key error collection: mydb.collectionName index: <indexName> dup key: { : 1, : 3 }', details={ }}, BulkWriteError{index=4, code=11000, message='E11000 duplicate key error collection: mydb.collectionName index: <indexName> dup key: { : 1, : 4 }', details={ }}, BulkWriteError{index=5, code=11000, message='E11000 duplicate key error collection: mydb.collectionName index: <indexName> dup key: { : 1, : 5 }', details={ }}, BulkWriteError{index=6, code=11000, message='E11000 duplicate key error collection: mydb.collectionName index: <indexName> dup key: { : 1, : 6 }', details={ }}, BulkWriteError{index=7, code=11000, message='E11000 duplicate key error collection: mydb.collectionName index: <indexName> dup key: { : 1, : 7 }', details={ }}, BulkWriteError{index=8, code=11000, message='E11000 duplicate key error collection: mydb.collectionName index: <indexName> dup key: { : 1, : 8 }', details={ }}, BulkWriteError{index=9, code=11000, message='E11000 duplicate key error collection: mydb.collectionName index: <indexName> dup key: { : 1, : 9 }', details={ }}]. 

    at com.mongodb.BulkWriteHelper.translateBulkWriteException(BulkWriteHelper.java:56)
    at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:2313)
    at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:2302)
    at com.mongodb.BulkWriteOperation.execute(BulkWriteOperation.java:121)

However, when I run against Fongo, I see a different exception entirely:

com.mongodb.DuplicateKeyException: Write failed with error code 11000 and error message 'E11000 duplicate key error index: ...'

	at com.mongodb.FongoDB.duplicateKeyException(FongoDB.java:470)
	at com.mongodb.FongoDB.duplicateKeyException(FongoDB.java:452)
	at com.mongodb.FongoDBCollection.addToIndexes(FongoDBCollection.java:1371)
	at com.mongodb.FongoDBCollection.putSizeCheck(FongoDBCollection.java:160)
	at com.mongodb.FongoDBCollection.insert(FongoDBCollection.java:126)
	at com.mongodb.DBCollection.insert(DBCollection.java:298)
	at com.mongodb.DBCollection.insert(DBCollection.java:264)
	at com.mongodb.DBCollection.insert(DBCollection.java:201)
	at com.mongodb.FongoDBCollection.executeBulkWriteOperation(FongoDBCollection.java:1212)
	at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:2302)
	at com.mongodb.BulkWriteOperation.execute(BulkWriteOperation.java:121)

The stack isn't identical to #135 but I'm chalking that up to different versions of mongo. It looks like the same error to me.

I'm using Fongo 2.1.0, mongo-java-driver 3.4.2, and morphia 0.108.

@camilla11

This comment has been minimized.

camilla11 commented Mar 26, 2018

The fix for #135 only fixed it in the case of collection.bulkWrite. If I execute() via the BulkWriteOperation class it fails.

See a failing test I created from your added test in fea1abc


  public void bulkWrite_duplicatedKey() {
    // 2. Ordered bulk operation - order is guaranteed
    // Given
    DBCollection collection = fongoRule.newCollection();


//    if (serverVersion().equals(Fongo.OLD_SERVER_VERSION)) {
//      exception.expect(DuplicateKeyException.class);
//    } else {
    exception.expect(MongoBulkWriteException.class);
//    }

    final BulkWriteOperation bulkWriteOperation = collection.initializeOrderedBulkOperation();
    bulkWriteOperation.insert((new BasicDBObject("_id", 1).append("date", "yesterday")));
    bulkWriteOperation.insert((new BasicDBObject("_id", 1).append("date", "yesterday")));
    final com.mongodb.BulkWriteResult bulkWriteResult = bulkWriteOperation.execute();

  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment