[SERVER-9620] Index can be made multikey if a prefix of it is a shard key (splitVectors fail) Created: 08/May/13  Updated: 27/Oct/15  Resolved: 12/Feb/14

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: 2.2.4, 2.4.3
Fix Version/s: 2.6.0-rc0

Type: Bug Priority: Major - P3
Reporter: Thomas Rueckstiess Assignee: Randolph Tan
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Duplicate
is duplicated by SERVER-6095 make multikey indices useful for shar... Closed
Related
related to DOCS-4922 From 2.6 after a collection has been ... Closed
Operating System: ALL
Participants:

 Description   

The index used for a shard key cannot be a multikey.

Hence inserts into a sharded collection that would turn the relevant index into a multi-key index are prevented, but only if the index is identical with the shard key:

tried to insert object with no valid shard key for { a: 1.0, b: 1.0 } : { _id: ObjectId('518a5fe33c631895e210858a'), a: 1.0, b: [ 1.0, 2.0, 3.0 ] }

If the shard key is instead only a prefix of the index, we do not prevent such inserts for other fields. This causes the index to turn into a multikey index and splitVectors will fail.

Steps to reproduce
  1. Set up sharded cluster
  2. Connect to mongos, insert a document into a collection:

    > db.docs.insert({a:1, b:2, c:3, d:4})
    

  3. Create index on all 4 fields:

    > db.docs.ensureIndex({a:1, b:1, c:1, d:1})
    

  4. Enable sharding and shard collection on a prefix of the index:

    > sh.enableSharding('test')
    > sh.shardCollection('test.docs', {a:1, b:1})
    

  5. Now insert a document that contains an array for c:

    > db.docs.insert({a:5, b:6, c:[1, 2, 3], d:7})
    

  6. Switch to mongod and run a splitVector command

    > db.adminCommand({splitVector: 'test.docs', keyPattern: {a:1, b:1}, maxChunkSize: 1})
    {
    	"errmsg" : "couldn't find index over splitting key { a: 1.0, b: 1.0 }",
    	"ok" : 0
    }
    

Proposed behavior

Any insert turning the index into a multikey index should fail with an error, as it does when the index pattern is identical to the shard key.



 Comments   
Comment by Githook User [ 12/Feb/14 ]

Author:

{u'username': u'renctan', u'name': u'Randolph Tan', u'email': u'randolph@10gen.com'}

Message: SERVER-9620 Index can be made multikey if a prefix of it is a shard key

Fix test to replace empty remove
Branch: master
https://github.com/mongodb/mongo/commit/a210230220941b0a3b699ba0dce874ac137c66ae

Comment by Randolph Tan [ 12/Feb/14 ]

Note to docs:

Sharded collections can now have multikey indexes after it has been sharded. However, this is not encouraged as queries that use the multikey indexes will always be slower than normal indexes. Therefore it is highly recommended to have a dedicated' index for the shard key that is not a multikey type if the user wants to use a multikey index on another field.

You still cannot shard a collection if it has multikey indexes.

Comment by Githook User [ 12/Feb/14 ]

Author:

{u'username': u'renctan', u'name': u'Randolph Tan', u'email': u'randolph@10gen.com'}

Message: SERVER-9620 Index can be made multikey if a prefix of it is a shard key

Allowed sharded collections to have multikey indexes based on the
invariant that shard key values can never be arrays.
Branch: master
https://github.com/mongodb/mongo/commit/5e0365665f7244a9382b9bf078c890dce1cd5c03

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