SERVER-35824 disallow long index namespaces under FCV 4.0

This commit is contained in:
Benety Goh
2018-07-13 09:43:31 -04:00
parent 576d836040
commit 2c19e062e4
4 changed files with 58 additions and 4 deletions

View File

@@ -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();
})();

View File

@@ -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',
],

View File

@@ -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)

View File

@@ -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);