[SERVER-11493] Use a specific error code for duplicate key error when sharded Created: 30/Oct/13  Updated: 10/Dec/14  Resolved: 11/Dec/13

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Bernie Hackett Assignee: Randolph Tan
Resolution: Duplicate Votes: 7
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by JAVA-1103 CommandResult.getException not detect... Closed
Duplicate
duplicates SERVER-10519 Mongos support for new write operations Closed
Related
related to PYTHON-592 OperationFailure is raised instead of... Closed
is related to SERVER-10818 Implement compatibility to older servers Closed
Operating System: ALL
Participants:

 Description   

Most drivers raise a specific DuplicateKeyError exception for "duplicate key" errors. They do this by checking error returns for specific error codes.

mongos raises error code 16460 for a range of insert errors, including duplicate key error. That error should be handled separately, using a specific code (ErrorCodes::DuplicateKey is appropriate).

The sharding code in question:

https://github.com/mongodb/mongo/blob/master/src/mongo/s/strategy_shard.cpp#L704-L726



 Comments   
Comment by Michael Korbakov [ 18/Dec/13 ]

Can we expect to see this fix back ported to 2.4 branch?

Comment by Randolph Tan [ 11/Dec/13 ]

This is already fixed in master:

using write commands:

mongos> db.user.insert({ _id: 1 })
Insert WriteResult({
	"ok" : 0,
	"code" : 11000,
	"errmsg" : "E11000 duplicate key error index: test.user.$_id_  dup key: { : 1.0 }",
	"n" : 0
})
mongos> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
mongos> db.adminCommand({ enableSharding: 'test' })
{ "ok" : 1 }
mongos> db.adminCommand({ shardCollection: 'test.user', key: { x: 1 } })
{ "collectionsharded" : "test.user", "ok" : 1 }
mongos> db.user.insert({ _id: 1, x: 1 })
Insert WriteResult({ "ok" : 1, "n" : 1 })
mongos> db.user.insert({ _id: 1, x: 1 })
Insert WriteResult({
	"ok" : 0,
	"code" : 11000,
	"errmsg" : "E11000 duplicate key error index: test.user.$_id_  dup key: { : 1.0 }",
	"n" : 0
})

using old style writes:

mongos> db.user.insert({ _id: 1, x: 1 })
mongos> db.runCommand({ getLastError: 1 })
{
	"singleShard" : "localhost:30001",
	"err" : "E11000 duplicate key error index: test.user.$_id_  dup key: { : 1.0 }",
	"code" : 11000,
	"n" : 0,
	"connectionId" : 5,
	"syncMillis" : 0,
	"writtenTo" : null,
	"err" : "E11000 duplicate key error index: test.user.$_id_  dup key: { : 1.0 }",
	"ok" : 1
}

Comment by Eliot Horowitz (Inactive) [ 10/Dec/13 ]

Should make sure this is correct in write commands.

Comment by Eliot Horowitz (Inactive) [ 10/Dec/13 ]

Michael, I don't think so.
Can you open a new ticket describing what you're seeing as it seems different.

Comment by Michael Korbakov [ 10/Dec/13 ]

Is it possible that this error renders continue_on_error option of insert useless? Our testing shows that inserting bulk of documents with some of them violating unique index fails to insert non-violating ones. It only started with upgrade to MongoDB 2.4, everything worked fine with 2.2.

Generated at Thu Feb 08 03:25:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.