diff --git a/jstests/multiVersion/long_index_mixed_version_replset.js b/jstests/multiVersion/long_index_mixed_version_replset.js new file mode 100644 index 00000000000..b28ce5966c5 --- /dev/null +++ b/jstests/multiVersion/long_index_mixed_version_replset.js @@ -0,0 +1,47 @@ +/** + * Long index namespaces exceeding 127 characters are supported starting in 4.2. + * However, we should still disallow long index namespaces under FCV 4.0. + * TODO: remove this test in 4.4. + */ +(function() { + 'use strict'; + + TestData.replSetFeatureCompatibilityVersion = '4.0'; + const rst = new ReplSetTest({ + nodes: [ + {binVersion: 'latest'}, + {rsConfig: {priority: 0, votes: 0}}, + ] + }); + rst.startSet(); + rst.initiate(); + rst.restart(1, {binVersion: '4.0'}); + + const primary = rst.getPrimary(); + const mydb = primary.getDB('test'); + const coll = mydb.getCollection('long_index_name'); + + // Compute maximum index name length for this collection under FCV 4.0. + const maxNsLength = 127; + const maxIndexNameLength = maxNsLength - (coll.getFullName() + ".$").length; + jsTestLog('Max index name length under FCV 4.0 = ' + maxIndexNameLength); + + // Create an index with the longest name allowed for this collection. + assert.commandWorked(coll.createIndex({a: 1}, {name: 'a'.repeat(maxIndexNameLength)})); + + // If this command succeeds unexpectedly, it will cause an fassert on the 4.0 secondary which + // cannot handle long index namespaces, with a "CannotCreateIndex: ... index name ... too long" + // error message. + assert.commandFailedWithCode( + coll.createIndex({b: 1}, {name: 'b'.repeat(maxIndexNameLength + 1)}), + ErrorCodes.CannotCreateIndex); + + // The existing index on {x: 1} has an index name that is the longest supported under FCV 4.0 + // for the current collection name. + // Any attempt to rename this collection with a longer name must fail. Otherwise, the invalid + // index namespace will cause the 4.0 secondary to fassert. + assert.commandFailedWithCode(coll.renameCollection(coll.getName() + 'z'), + ErrorCodes.InvalidLength); + + rst.stopSet(); +})(); diff --git a/src/mongo/db/catalog/SConscript b/src/mongo/db/catalog/SConscript index 97f4402ed92..7973ae3cccc 100644 --- a/src/mongo/db/catalog/SConscript +++ b/src/mongo/db/catalog/SConscript @@ -287,6 +287,7 @@ env.Library( '$BUILD_DIR/mongo/db/repl/drop_pending_collection_reaper', '$BUILD_DIR/mongo/db/repl/oplog', '$BUILD_DIR/mongo/db/s/balancer', + '$BUILD_DIR/mongo/db/server_options_core', '$BUILD_DIR/mongo/db/service_context', '$BUILD_DIR/mongo/db/storage/key_string', '$BUILD_DIR/mongo/db/system_index', @@ -322,6 +323,7 @@ env.Library( '$BUILD_DIR/mongo/db/background', '$BUILD_DIR/mongo/db/db_raii', '$BUILD_DIR/mongo/db/query_exec', + '$BUILD_DIR/mongo/db/server_options_core', '$BUILD_DIR/mongo/db/views/views', '$BUILD_DIR/mongo/db/write_ops', ], diff --git a/src/mongo/db/catalog/index_catalog_impl.cpp b/src/mongo/db/catalog/index_catalog_impl.cpp index 22dad6daeac..8a3cbfc8f0f 100644 --- a/src/mongo/db/catalog/index_catalog_impl.cpp +++ b/src/mongo/db/catalog/index_catalog_impl.cpp @@ -620,8 +620,10 @@ Status IndexCatalogImpl::_isSpecOk(OperationContext* opCtx, const BSONObj& spec) // namespace length constraints as the ones in created by users. // Index names do not limit the maximum allowable length of the target namespace under FCV 4.2 // and above. - // TODO(SERVER-35824): Re-enable index namespace check under FCV 4.0. - const bool checkIndexNamespace = false; + const auto checkIndexNamespace = + serverGlobalParams.featureCompatibility.isVersionInitialized() && + serverGlobalParams.featureCompatibility.getVersion() != + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo42; if (checkIndexNamespace && !nss.isDropPendingNamespace()) { auto indexNamespace = IndexDescriptor::makeIndexNamespace(nss.ns(), name); if (indexNamespace.length() > NamespaceString::MaxNsLen) diff --git a/src/mongo/db/catalog/rename_collection.cpp b/src/mongo/db/catalog/rename_collection.cpp index fcd24ad99d2..deccc82ce9b 100644 --- a/src/mongo/db/catalog/rename_collection.cpp +++ b/src/mongo/db/catalog/rename_collection.cpp @@ -51,6 +51,7 @@ #include "mongo/db/repl/replication_coordinator.h" #include "mongo/db/s/collection_sharding_state.h" #include "mongo/db/s/database_sharding_state.h" +#include "mongo/db/server_options.h" #include "mongo/db/service_context.h" #include "mongo/util/log.h" #include "mongo/util/scopeguard.h" @@ -176,8 +177,10 @@ Status renameCollectionCommon(OperationContext* opCtx, // Ensure that collection name does not exceed maximum length. // Index names do not limit the maximum allowable length of the target namespace under // FCV 4.2 and above. - // TODO(SERVER-35824): Re-enable index namespace check under FCV 4.0. - const bool checkIndexNamespace = false; + const auto checkIndexNamespace = + serverGlobalParams.featureCompatibility.isVersionInitialized() && + serverGlobalParams.featureCompatibility.getVersion() != + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo42; std::string::size_type longestIndexNameLength = checkIndexNamespace ? sourceColl->getIndexCatalog()->getLongestIndexNameLength(opCtx) : 0; auto status = target.checkLengthForRename(longestIndexNameLength);