summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntonio Fuschetto <antonio.fuschetto@mongodb.com>2022-02-08 08:34:23 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-02-08 09:28:21 +0000
commit0491f281895741b178e668d98937c2f92f797d25 (patch)
tree377ebc295db9a0281a69a0cfa0e5867119bb3086
parentd7ecec38af5b1b63603519cc4d36fc0532d192e1 (diff)
downloadmongo-0491f281895741b178e668d98937c2f92f797d25.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 f417cdc4165..84a2ac9b5c9 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.2') >= 0) {
+ const longEnoughNs = kDbName + '.' +
+ 'x'.repeat(100 - 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 ae05ea7e097..e2718c36601 100644
--- a/src/mongo/db/namespace_string.h
+++ b/src/mongo/db/namespace_string.h
@@ -174,6 +174,11 @@ public:
// Maximum allowed length of fully qualified namespace name of any real collection.
// Does not include NUL so it can be directly compared to std::string lengths.
MaxNsCollectionLen = MaxNsLen - 7 /*strlen(".$extra")*/,
+
+ // 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>).
+ MaxNsShardedCollectionLen = 100 /* MaxNsCollectionLen - len(ChunkType::ShardNSPrefix) */,
};
/**
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 b63165e4517..f1f2f5e85fe 100644
--- a/src/mongo/db/s/config/configsvr_shard_collection_command.cpp
+++ b/src/mongo/db/s/config/configsvr_shard_collection_command.cpp
@@ -656,6 +656,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 547d985c387..72ed27b3884 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<std::string> ChunkType::name("_id");