Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-79914

CollectionRoutingInfoTargeter::targetInsert can incorrectly route a document missing _id

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.3.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • Sharding EMEA
    • Fully Compatible
    • ALL
    • Sharding EMEA 2023-10-02, Sharding EMEA 2023-10-16, Sharding EMEA 2023-10-30, CAR Team 2023-11-13, CAR Team 2023-11-27, CAR Team 2023-12-11, CAR Team 2023-12-25
    • 2

      This is because the logic in ShardKeyPattern::extractShardKeyFromDoc will fill in BSON null for any value in the shard key pattern that is not found in the document.

      This can cause a problem if a document to insert is passed to this method that does not already have an _id, and _id is in the shard key pattern. The document will then be routed to a shard based on an _id of BSON null. But once the document arrives on the shard, the shard will insert it, and give it a new _id, since it lacks one. And once it has a new _id, its value for the shard key has changed, and it may not be on the correct shard given its shard key.

      This happens to not be a problem right now for the insert command, because mongos always adds _id to incoming insert command requests. But we noticed this problem with the new bulkWrite command where we weren't generating _id on mongos.

      We will update the bulkWrite command to follow the same logic as insert to ensure there is always an _id, but it might be good to add some safeguard(s) around this. For example, maybe this method should error if _id is in the shard key pattern but not in the document.

            allison.easton@mongodb.com Allison Easton
            kaitlin.mahar@mongodb.com Kaitlin Mahar
            0 Vote for this issue
            5 Start watching this issue