[SERVER-36446] Creating a unique hashed index trips dassert in shard_key_pattern.cpp Created: 03/Aug/18  Updated: 29/Oct/23  Resolved: 09/Jul/19

Status: Closed
Project: Core Server
Component/s: Index Maintenance, Sharding
Affects Version/s: None
Fix Version/s: 4.3.1

Type: Task Priority: Major - P3
Reporter: Kyle Suarez Assignee: Blake Oler
Resolution: Fixed Votes: 0
Labels: sharding-wfbf-day
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Sharding 2019-07-15
Participants:

 Description   

This only affects debug builds, since the assertion is a dassert. In a sharded cluster, I have an existing sharded collection sharded on {"shardKey": "hashed"}:

mongos> sh.shardCollection("test.sharded", {shardKey: "hashed"})
{
        "collectionsharded" : "test.sharded",
        "collectionUUID" : UUID("872244a4-4045-444c-b014-61170c3bc6da"),
        "ok" : 1,
        "operationTime" : Timestamp(1533326578, 10),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1533326578, 10),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

This index creation trips the invariant failure:

mongos> db.sharded.createIndex({shardKey: "hashed", x: 1, y: 1}, {unique: true})
{
        "raw" : {
                "shardingtest-rs0/kimchi:20000" : {
                        "ok" : 0,
                        "errmsg" : "Connection was closed",
                        "code" : 6,
                        "codeName" : "HostUnreachable"
                },
                "shardingtest-rs1/kimchi:20001" : {
                        "ok" : 0,
                        "errmsg" : "Connection was closed",
                        "code" : 6,
                        "codeName" : "HostUnreachable"
                }
        },
        "code" : 6,
        "codeName" : "HostUnreachable",
        "ok" : 0,
        "errmsg" : "{ shardingtest-rs0/kimchi:20000: \"Connection was closed\", shardingtest-rs1/kimchi:20001: \"Connection was closed\" }",
        "operationTime" : Timestamp(1533326580, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1533326580, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

The invariant failure in question is

d20001| 2018-08-03T16:03:00.733-0400 F -        [conn21] Invariant failure !KeyPattern::isHashedKeyPattern(uniqueIndexPattern) src/mongo/s/shard_key_pattern.cpp 351

The failing assertion is here. It seems like we should be doing more validation beforehand before hitting the invariant, though.

shard_key_pattern.cpp

350
bool ShardKeyPattern::isUniqueIndexCompatible(const BSONObj& uniqueIndexPattern) const {
351
    dassert(!KeyPattern::isHashedKeyPattern(uniqueIndexPattern));
352
 
353
    if (!uniqueIndexPattern.isEmpty() && uniqueIndexPattern.firstElementFieldName() == kIdField) {
354
        return true;
355
    }
356
 
357
    return _keyPattern.toBSON().isFieldNamePrefixOf(uniqueIndexPattern);
358
}



 Comments   
Comment by Githook User [ 09/Jul/19 ]

Author:

{'name': 'Blake Oler', 'username': 'BlakeIsBlake', 'email': 'blake.oler@mongodb.com'}

Message: SERVER-36446 Remove unnecessary debug assert in shard_key_pattern.cpp
Branch: master
https://github.com/mongodb/mongo/commit/445f40a88bd4976c78e8a0269940b206f1517023

Comment by Kaloian Manassiev [ 24/May/19 ]

We should just get rid of this dassert, because hashed indexes cannot be non-unique and we are ignoring it anyways later in that function.

Comment by Kyle Suarez [ 03/Aug/18 ]

Editing the description, because I have a much simpler reproducer:

db.sharded.createIndex({y: "hashed"}, {unique: true})

Generated at Thu Feb 08 04:43:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.