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

Changing the shard key could lead to DuplicateKeyError on _id with orphan documents

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.1.11
    • Affects Version/s: None
    • Component/s: Sharding
    • None
    • Fully Compatible
    • ALL
    • Sharding 2019-04-22

      If we change a document's shard key such that the document will have to change shards, we could end up with a duplicate key error on _id due to an orphaned version of that document existing on that shard. Other legitimate DuplicateKeyErrors could occur (for example, if there's a unique index on the shard key), in which case we'll throw an ordinary DuplicateKeyError. This ticket only addresses _id conflicts.

      Consider the following scenario:
      1) A document x is migrated from shard A to shard B. Suppose the RangeDeleter does not run yet, and the orphaned document x remains on shard A.
      2) An update is issued to document x (residing on shard B) such that it requires moving that document back to shard A. The update operation is converted into a delete from shard B and an insert into shard A.
      3) The insert operation into shard A fails with a duplicate key error on _id, because the orphaned version of x still exists on shard A.

      We should make sure this case leads to an error message that's more meaningful to the user than DuplicateKeyError (something indicated it's related to orphaned documents), and perhaps with a link to documentation.

            Assignee:
            janna.golden@mongodb.com Janna Golden
            Reporter:
            matthew.saltz@mongodb.com Matthew Saltz (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: