[SERVER-72620] $indexStats doesn't include the "shard" field when run on unsharded collections Created: 09/Jan/23  Updated: 29/Oct/23  Resolved: 29/Jan/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 4.4.18, 5.0.14, 6.0.4, 6.2.0-rc6
Fix Version/s: 4.4.19, 5.0.15, 6.3.0-rc0, 6.0.5

Type: Bug Priority: Major - P3
Reporter: Vishnu Kaushik Assignee: Tyler Brock
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
Problem/Incident
Related
related to SERVER-44915 Extend $indexStats output to include ... Closed
Assigned Teams:
Sharding NYC
Backwards Compatibility: Minor Change
Operating System: ALL
Backport Requested:
v6.2, v6.0, v5.0, v4.4
Participants:
Linked BF Score: 151

 Description   

$indexStats includes the "shard" field on version 4.2 when run on an unsharded collection (correct behavior). However on version 4.4 onwards the "shard" field is missing for unsharded collections even though it should be present.

See comments for more info.



 Comments   
Comment by Githook User [ 30/Jan/23 ]

Author:

{'name': 'Tyler Brock', 'email': 'tyler.brock@gmail.com', 'username': 'TylerBrock'}

Message: SERVER-72620 ensure shard added to $indexStats for unsharded collections
Branch: v4.4
https://github.com/mongodb/mongo/commit/942e2e9541f97448538e603c0fb14967d577e633

Comment by Githook User [ 30/Jan/23 ]

Author:

{'name': 'Tyler Brock', 'email': 'tyler.brock@gmail.com', 'username': 'TylerBrock'}

Message: SERVER-72620 ensure shard added to $indexStats for unsharded collections
Branch: v6.0
https://github.com/mongodb/mongo/commit/909bf6415431b6260dec1baedcd799c27626039c

Comment by Githook User [ 30/Jan/23 ]

Author:

{'name': 'Tyler Brock', 'email': 'tyler.brock@gmail.com', 'username': 'TylerBrock'}

Message: SERVER-72620 ensure shard added to $indexStats for unsharded collections
Branch: v5.0
https://github.com/mongodb/mongo/commit/58a2c8137cc2abfdf2bf274d2cc9aee645f9f519

Comment by Tyler Brock [ 29/Jan/23 ]

Fixes behavior to reflect original (documented) behavior of $indexStats to include the shard name in a sharded context even on unsharded collections.

Comment by Githook User [ 29/Jan/23 ]

Author:

{'name': 'Tyler Brock', 'email': 'tyler.brock@gmail.com', 'username': 'TylerBrock'}

Message: SERVER-72620 ensure shard added to $indexStats for unsharded collections
Branch: master
https://github.com/mongodb/mongo/commit/f6897efb24aea4d923b6646b2379fa64dd041480

Comment by Vishnu Kaushik [ 09/Jan/23 ]

tyler.brock@mongodb.com's comment:

It looks like getIndexStats() determines whether or not to include the shard name info based on the addShardName param which it gets from the expression context's fromMongos bool. So the question is what is setting the fromMongos bool...
I think this is because ClusterAggregate::runAggregate makes an ExpressionContext with fromMongos set to a default of false and switches on TargetingPolicy. When that targeting policy is:

  • kPassthrough -> sends the command to the primary shard without modifying default fromMongos
  • kAnyShard -> eventually calls into createCommandForMergingShard which sets fromMongos to true
    This seems reasonable since if the collection isn't sharded you know where the index lives -> the primary shard – the documentation can probably be clarified here.

...

On the other hand, the server ticket that added the code seems to indicate it should always be included however when run on a sharded cluster: SERVER-44915

...

"Output documents generated for indexes on sharding enabled node started with --shardsvr should contain the shardId of the shard that generated the document in a new shard field"

...

So it would seem that we've got a bug, nice spot!

Generated at Thu Feb 08 06:22:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.