[SERVER-14302] Equality queries on _id with projection may return no results on sharded collections Created: 19/Jun/14  Updated: 11/Jul/16  Resolved: 19/Jun/14

Status: Closed
Project: Core Server
Component/s: Querying, Sharding
Affects Version/s: 2.6.2
Fix Version/s: 2.6.3, 2.7.3

Type: Bug Priority: Critical - P2
Reporter: David Hows Assignee: J Rassi
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File idhack_find_projection.js    
Issue Links:
Depends
Related
related to SERVER-13337 Re-enable fast path for _id equality ... Closed
is related to SERVER-14304 Equality queries on _id with projecti... Closed
is related to SERVER-14320 CollectionMetadata::keyBelongsToMe() ... Closed
Operating System: ALL
Backport Completed:
Steps To Reproduce:

sh = new ShardingTest({name:"test", shards:2})
db = sh.getDB( "test" );
sh.adminCommand({enableSharding:"test"})
sh.adminCommand({shardCollection:"test.test", key: {foo:1}})
db.test.insert({_id:1, foo:1, bar:1})
res = db.test.find({_id:1},{bar:1}).itcount()
assert.eq(res, 1, "Was unable to find document")

Participants:

 Description   
Issue Status as of Jun 19, 2014

ISSUE SUMMARY
Queries on non-hashed sharded collections that include an equality predicate on the _id field and a projection to exclude the shard key erroneously return no results.

Example:

The following query includes an equality predicate on _id and contains a projection returns only the _id and bar fields. Suppose coll is a sharded collection with shard key {foo:1}. Since foo is not in the resulting document, the query is affected by this bug and won't return any results.

db.coll.find({_id:ObjectId("53a349440d3a24cdc742e1df"), baz:50}, {bar:1})

USER IMPACT
On non-hashed sharded collections, queries of this type return no results. Sharded collections on a hashed shard key are unaffected by this bug.

WORKAROUNDS
Add a hint of {_id:1} to these queries. Alternatively, it is safe to downgrade only the mongod shard servers to 2.6.1 to avoid this problem. For example:

db.coll.find({_id:ObjectId("53a349440d3a24cdc742e1df"), baz:50}, {bar:1}).hint({_id:1})

AFFECTED VERSIONS
MongoDB production release 2.6.2 is affected by this issue.

FIX VERSION
The fix is included in the 2.6.3 production release.

RESOLUTION DETAILS
On sharded connections, the IDHackRunner incorrectly applied the projection to the result document before passing it to the sharding filter (which removes orphan documents). The order of these steps has been reversed.

Original description

Sharded queries that include an equality predicate on _id and a projection that removes the shard key will return no results.

The cause of this issue is a regression introduced in 2.6.2 (SERVER-13337). On sharded connections, IDHackRunner incorrectly applies the projection to the result document before passing it to the sharding filter (which removes orphan documents); these steps need to happen in the reverse order. If the projected document being passed to the sharding filter doesn't include the shard key, then the document is concluded to be an orphan.

To work around this issue, add a hint of {_id: 1} to any affected query. An explicit hint disables use of the IDHackRunner.



 Comments   
Comment by Githook User [ 19/Jun/14 ]

Author:

{u'username': u'jrassi', u'name': u'Jason Rassi', u'email': u'rassi@10gen.com'}

Message: SERVER-14302 SERVER-14304 Fix IDHackRunner's use of sharding filter

(backport of 6fe6dee814326ac41f8d626c26bf32763ce73d71)
(cherry picked from commit 3d19c459fe80fb7aa3244174b61994f13f71c21f)
Branch: v2.6
https://github.com/mongodb/mongo/commit/ce54b89a5a015bb5c563f7f3cfb26f6af281cde2

Comment by Githook User [ 19/Jun/14 ]

Author:

{u'username': u'jrassi', u'name': u'Jason Rassi', u'email': u'rassi@10gen.com'}

Message: SERVER-14302 SERVER-14304 Fix IDHackRunner's use of sharding filter

(backport of 6fe6dee814326ac41f8d626c26bf32763ce73d71)
Branch: v2.6.3
https://github.com/mongodb/mongo/commit/3d19c459fe80fb7aa3244174b61994f13f71c21f

Comment by Githook User [ 19/Jun/14 ]

Author:

{u'username': u'jrassi', u'name': u'Jason Rassi', u'email': u'rassi@10gen.com'}

Message: SERVER-14302 SERVER-14304 Fix IDHackRunner's use of sharding filter
Branch: master
https://github.com/mongodb/mongo/commit/6fe6dee814326ac41f8d626c26bf32763ce73d71

Comment by Daniel Pasette (Inactive) [ 19/Jun/14 ]

Note, this issue does not exhibit with hashed shard keys.

Comment by David Hows [ 19/Jun/14 ]

Workaround is to .hint({_id:1})

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