summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntonio Fuschetto <antonio.fuschetto@mongodb.com>2022-02-07 14:46:12 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-02-07 15:22:08 +0000
commite4d6153687cdc21e22d736cb47932dbfefe8b645 (patch)
treefbb250976e865d3c852a444855cab295b22b285a
parent3088b78dd33839099d53e702960e404039f098fc (diff)
downloadmongo-e4d6153687cdc21e22d736cb47932dbfefe8b645.tar.gz
SERVER-62906 Add a check in the createCollection/shardCollection path verifying the collection name length
-rw-r--r--jstests/sharding/shard_collection_basic.js13
-rw-r--r--src/mongo/db/namespace_string.h5
-rw-r--r--src/mongo/db/s/config/configsvr_shard_collection_command.cpp5
-rw-r--r--src/mongo/s/catalog/type_chunk.cpp4
4 files changed, 27 insertions, 0 deletions
diff --git a/jstests/sharding/shard_collection_basic.js b/jstests/sharding/shard_collection_basic.js
index 445dab8a2e7..877df390af6 100644
--- a/jstests/sharding/shard_collection_basic.js
+++ b/jstests/sharding/shard_collection_basic.js
@@ -67,6 +67,19 @@ assert.commandFailed(mongos.adminCommand({shardCollection: 'foo', key: {_id: 1}}
assert.commandFailed(mongos.adminCommand({shardCollection: 'foo', key: "aaa"}));
+// Verify namespace length limit.
+const testDB = st.rs0.getPrimary().getDB(kDbName);
+const fcvDoc = testDB.adminCommand({getParameter: 1, featureCompatibilityVersion: 1});
+if (MongoRunner.compareBinVersions(fcvDoc.featureCompatibilityVersion.version, '4.4') >= 0) {
+ const longEnoughNs = kDbName + '.' +
+ 'x'.repeat(235 - kDbName.length - 1);
+ assert.commandWorked(mongos.adminCommand({shardCollection: longEnoughNs, key: {_id: 1}}));
+
+ const tooLongNs = longEnoughNs + 'x';
+ assert.commandFailedWithCode(mongos.adminCommand({shardCollection: tooLongNs, key: {_id: 1}}),
+ ErrorCodes.InvalidNamespace);
+}
+
// shardCollection may only be run against admin database.
assert.commandFailed(
mongos.getDB('test').runCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
diff --git a/src/mongo/db/namespace_string.h b/src/mongo/db/namespace_string.h
index 2d549b1d1f7..8a9b62844d7 100644
--- a/src/mongo/db/namespace_string.h
+++ b/src/mongo/db/namespace_string.h
@@ -51,6 +51,11 @@ public:
constexpr static size_t MaxNSCollectionLenFCV42 = 120U;
constexpr static size_t MaxNsCollectionLen = 255;
+ // The maximum namespace length of sharded collections is less than that of unsharded ones since
+ // the namespace of the cached chunks metadata, local to each shard, is composed by the
+ // namespace of the related sharded collection (i.e., config.cache.chunks.<ns>).
+ constexpr static size_t MaxNsShardedCollectionLen = 235; // 255 - len(ChunkType::ShardNSPrefix)
+
// Reserved system namespaces
// Namespace for the admin database
diff --git a/src/mongo/db/s/config/configsvr_shard_collection_command.cpp b/src/mongo/db/s/config/configsvr_shard_collection_command.cpp
index 68af0777512..2a16c5bd554 100644
--- a/src/mongo/db/s/config/configsvr_shard_collection_command.cpp
+++ b/src/mongo/db/s/config/configsvr_shard_collection_command.cpp
@@ -270,6 +270,11 @@ public:
str::stream() << "sharding not enabled for db " << nss.db(),
dbType.getSharded());
+ uassert(ErrorCodes::InvalidNamespace,
+ str::stream() << "Namespace too long. Namespace: " << nss
+ << " Max: " << NamespaceString::MaxNsShardedCollectionLen,
+ nss.size() <= NamespaceString::MaxNsShardedCollectionLen);
+
// Get variables required throughout this command.
auto proposedKey(request.getKey().getOwned());
diff --git a/src/mongo/s/catalog/type_chunk.cpp b/src/mongo/s/catalog/type_chunk.cpp
index 06cebc73e6e..3180078395c 100644
--- a/src/mongo/s/catalog/type_chunk.cpp
+++ b/src/mongo/s/catalog/type_chunk.cpp
@@ -44,6 +44,10 @@
namespace mongo {
const NamespaceString ChunkType::ConfigNS("config.chunks");
+
+// The final namespace of the cached chunks metadata is composed of the namespace of the related
+// sharded collection (i.e., config.cache.chunks.<ns>). As a result, the maximum namespace length of
+// sharded collections is reduced. See NamespaceString::MaxNsShardedCollectionLen.
const std::string ChunkType::ShardNSPrefix = "config.cache.chunks.";
const BSONField<OID> ChunkType::name("_id");