diff options
Diffstat (limited to 'jstests/libs')
-rw-r--r-- | jstests/libs/override_methods/implicitly_shard_accessed_collections.js | 44 |
1 files changed, 12 insertions, 32 deletions
diff --git a/jstests/libs/override_methods/implicitly_shard_accessed_collections.js b/jstests/libs/override_methods/implicitly_shard_accessed_collections.js index a29f68b7323..7b5189c4b33 100644 --- a/jstests/libs/override_methods/implicitly_shard_accessed_collections.js +++ b/jstests/libs/override_methods/implicitly_shard_accessed_collections.js @@ -4,9 +4,6 @@ * * The DB.prototype.getCollection() function is called whenever an undefined property is accessed * on the db object. - * - * DBCollection.prototype.drop() function will re-shard any non-blacklisted collection that is - * dropped in a sharded cluster. */ (function() { @@ -15,7 +12,6 @@ // Save a reference to the original getCollection method in the IIFE's scope. // This scoping allows the original method to be called by the getCollection override below. var originalGetCollection = DB.prototype.getCollection; - var originalDBCollectionDrop = DBCollection.prototype.drop; // Blacklisted namespaces that should not be sharded. var blacklistedNamespaces = [ @@ -25,49 +21,33 @@ /\.system\./, ]; - function shardCollection(collection) { - var db = collection.getDB(); - var dbName = db.getName(); + DB.prototype.getCollection = function() { + var dbName = this.getName(); + var collection = originalGetCollection.apply(this, arguments); var fullName = collection.getFullName(); + // If the collection exists, there must have been a previous call to getCollection + // where we sharded the collection so there's no need to do it again. + if (collection.exists()) { + return collection; + } + for (var ns of blacklistedNamespaces) { if (fullName.match(ns)) { - return; + return collection; } } - var res = db.adminCommand({enableSharding: dbName}); + var res = this.adminCommand({enableSharding: dbName}); // enableSharding may only be called once for a database. if (res.code !== ErrorCodes.AlreadyInitialized) { assert.commandWorked(res, "enabling sharding on the '" + dbName + "' db failed"); } - res = db.adminCommand({shardCollection: fullName, key: {_id: 'hashed'}}); + res = this.adminCommand({shardCollection: fullName, key: {_id: 'hashed'}}); assert.commandWorked(res, "sharding '" + fullName + "' with a hashed _id key failed"); - } - - DB.prototype.getCollection = function() { - var collection = originalGetCollection.apply(this, arguments); - - // If the collection exists, there must have been a previous call to getCollection - // where we sharded the collection so there's no need to do it again. - if (collection.exists()) { - return collection; - } - - // Attempt to enable sharding on database and collection if not already done. - shardCollection(collection); return collection; }; - - DBCollection.prototype.drop = function() { - var dropResult = originalDBCollectionDrop.apply(this, arguments); - - // Attempt to enable sharding on database and collection if not already done. - shardCollection(this); - - return dropResult; - }; }()); |