diff options
-rw-r--r-- | jstests/sharding/shard_collection_basic.js | 68 | ||||
-rw-r--r-- | src/mongo/shell/utils_sh.js | 10 |
2 files changed, 76 insertions, 2 deletions
diff --git a/jstests/sharding/shard_collection_basic.js b/jstests/sharding/shard_collection_basic.js index 3d6fc1cc358..a17336eb99e 100644 --- a/jstests/sharding/shard_collection_basic.js +++ b/jstests/sharding/shard_collection_basic.js @@ -267,6 +267,74 @@ assert.commandWorked(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {a: 1}})); mongos.getDB(kDbName).dropDatabase(); + assert.commandWorked(mongos.adminCommand({setFeatureCompatibilityVersion: "3.4"})); + + // + // Tests for the shell helper sh.shardCollection(). + // + + db = mongos.getDB(kDbName); + assert.commandWorked(mongos.adminCommand({enableSharding: kDbName})); + + // shardCollection() propagates the shard key and the correct defaults. + mongos.getDB(kDbName).foo.drop(); + assert.commandWorked(mongos.getDB(kDbName).createCollection('foo')); + assert.commandWorked(sh.shardCollection(kDbName + '.foo', {a: 1})); + indexSpec = getIndexSpecByName(mongos.getDB(kDbName).foo, 'a_1'); + assert(!indexSpec.hasOwnProperty('unique'), tojson(indexSpec)); + assert(!indexSpec.hasOwnProperty('collation'), tojson(indexSpec)); + + // shardCollection() propagates the value for 'unique'. + mongos.getDB(kDbName).foo.drop(); + assert.commandWorked(mongos.getDB(kDbName).createCollection('foo')); + assert.commandWorked(sh.shardCollection(kDbName + '.foo', {a: 1}, true)); + indexSpec = getIndexSpecByName(mongos.getDB(kDbName).foo, 'a_1'); + assert(indexSpec.hasOwnProperty('unique'), tojson(indexSpec)); + assert.eq(indexSpec.unique, true, tojson(indexSpec)); + + mongos.getDB(kDbName).foo.drop(); + assert.commandWorked(mongos.getDB(kDbName).createCollection('foo')); + assert.commandWorked(sh.shardCollection(kDbName + '.foo', {a: 1}, false)); + indexSpec = getIndexSpecByName(mongos.getDB(kDbName).foo, 'a_1'); + assert(!indexSpec.hasOwnProperty('unique'), tojson(indexSpec)); + + // shardCollections() 'options' parameter must be an object. + mongos.getDB(kDbName).foo.drop(); + assert.commandWorked(mongos.getDB(kDbName).createCollection('foo')); + assert.throws(function() { + sh.shardCollection(kDbName + '.foo', {a: 1}, false, 'not an object'); + }); + + // shardCollection() propagates the value for 'collation'. + // Currently only the simple collation is supported. + mongos.getDB(kDbName).foo.drop(); + assert.commandWorked(mongos.getDB(kDbName).createCollection('foo')); + assert.commandFailed( + sh.shardCollection(kDbName + '.foo', {a: 1}, false, {collation: {locale: 'en_US'}})); + assert.commandWorked( + sh.shardCollection(kDbName + '.foo', {a: 1}, false, {collation: {locale: 'simple'}})); + indexSpec = getIndexSpecByName(mongos.getDB(kDbName).foo, 'a_1'); + assert(!indexSpec.hasOwnProperty('collation'), tojson(indexSpec)); + + mongos.getDB(kDbName).foo.drop(); + assert.commandWorked( + mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}})); + assert.commandFailed(sh.shardCollection(kDbName + '.foo', {a: 1})); + assert.commandFailed( + sh.shardCollection(kDbName + '.foo', {a: 1}, false, {collation: {locale: 'en_US'}})); + assert.commandWorked( + sh.shardCollection(kDbName + '.foo', {a: 1}, false, {collation: {locale: 'simple'}})); + indexSpec = getIndexSpecByName(mongos.getDB(kDbName).foo, 'a_1'); + assert(!indexSpec.hasOwnProperty('collation'), tojson(indexSpec)); + + // shardCollection() propagates the value for 'numInitialChunks'. + mongos.getDB(kDbName).foo.drop(); + assert.commandWorked(mongos.getDB(kDbName).createCollection('foo')); + assert.commandWorked( + sh.shardCollection(kDbName + '.foo', {a: "hashed"}, false, {numInitialChunks: 5})); + st.printShardingStatus(); + var numChunks = st.config.chunks.find({ns: kDbName + '.foo'}).count(); + assert.eq(numChunks, 5, "unexpected number of chunks"); st.stop(); diff --git a/src/mongo/shell/utils_sh.js b/src/mongo/shell/utils_sh.js index 010c8fe89fb..ef8ae2dfa36 100644 --- a/src/mongo/shell/utils_sh.js +++ b/src/mongo/shell/utils_sh.js @@ -69,7 +69,7 @@ sh.help = function() { print("\tsh.removeShardFromZone(shard,zone) removes the shard from zone"); print( "\tsh.removeRangeFromZone(fullName,min,max) removes the range of the given collection from any zone"); - print("\tsh.shardCollection(fullName,key,unique) shards the collection"); + print("\tsh.shardCollection(fullName,key,unique,options) shards the collection"); print( "\tsh.splitAt(fullName,middle) splits the chunk that middle is in at middle"); print( @@ -98,7 +98,7 @@ sh.enableSharding = function(dbname) { return sh._adminCommand({enableSharding: dbname}); }; -sh.shardCollection = function(fullName, key, unique) { +sh.shardCollection = function(fullName, key, unique, options) { sh._checkFullName(fullName); assert(key, "need a key"); assert(typeof(key) == "object", "key needs to be an object"); @@ -106,6 +106,12 @@ sh.shardCollection = function(fullName, key, unique) { var cmd = {shardCollection: fullName, key: key}; if (unique) cmd.unique = true; + if (options) { + if (typeof(options) !== "object") { + throw new Error("options must be an object"); + } + Object.extend(cmd, options); + } return sh._adminCommand(cmd); }; |