summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard_collection_basic.js
diff options
context:
space:
mode:
authorclang-format-7.0.1 <adam.martin@10gen.com>2019-07-26 18:20:35 -0400
committerADAM David Alan Martin <adam.martin@10gen.com>2019-07-27 11:02:23 -0400
commit134a4083953270e8a11430395357fb70a29047ad (patch)
treedd428e1230e31d92b20b393dfdc17ffe7fa79cb6 /jstests/sharding/shard_collection_basic.js
parent1e46b5049003f427047e723ea5fab15b5a9253ca (diff)
downloadmongo-134a4083953270e8a11430395357fb70a29047ad.tar.gz
SERVER-41772 Apply clang-format 7.0.1 to the codebase
Diffstat (limited to 'jstests/sharding/shard_collection_basic.js')
-rw-r--r--jstests/sharding/shard_collection_basic.js579
1 files changed, 285 insertions, 294 deletions
diff --git a/jstests/sharding/shard_collection_basic.js b/jstests/sharding/shard_collection_basic.js
index f12d4c8482b..f417cdc4165 100644
--- a/jstests/sharding/shard_collection_basic.js
+++ b/jstests/sharding/shard_collection_basic.js
@@ -3,356 +3,347 @@
//
(function() {
- 'use strict';
+'use strict';
- var st = new ShardingTest({mongos: 1, shards: 2});
- var kDbName = 'db';
- var mongos = st.s0;
+var st = new ShardingTest({mongos: 1, shards: 2});
+var kDbName = 'db';
+var mongos = st.s0;
- function testAndClenaupWithKeyNoIndexFailed(keyDoc) {
- assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
-
- var ns = kDbName + '.foo';
- assert.commandFailed(mongos.adminCommand({shardCollection: ns, key: keyDoc}));
-
- assert.eq(mongos.getDB('config').collections.count({_id: ns, dropped: false}), 0);
- assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
- }
+function testAndClenaupWithKeyNoIndexFailed(keyDoc) {
+ assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
- function testAndClenaupWithKeyOK(keyDoc) {
- assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
- assert.commandWorked(mongos.getDB(kDbName).foo.createIndex(keyDoc));
+ var ns = kDbName + '.foo';
+ assert.commandFailed(mongos.adminCommand({shardCollection: ns, key: keyDoc}));
- var ns = kDbName + '.foo';
- assert.eq(mongos.getDB('config').collections.count({_id: ns, dropped: false}), 0);
+ assert.eq(mongos.getDB('config').collections.count({_id: ns, dropped: false}), 0);
+ assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
+}
- assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: keyDoc}));
+function testAndClenaupWithKeyOK(keyDoc) {
+ assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
+ assert.commandWorked(mongos.getDB(kDbName).foo.createIndex(keyDoc));
- assert.eq(mongos.getDB('config').collections.count({_id: ns, dropped: false}), 1);
- assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
- }
+ var ns = kDbName + '.foo';
+ assert.eq(mongos.getDB('config').collections.count({_id: ns, dropped: false}), 0);
- function testAndClenaupWithKeyNoIndexOK(keyDoc) {
- assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
+ assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: keyDoc}));
- var ns = kDbName + '.foo';
- assert.eq(mongos.getDB('config').collections.count({_id: ns, dropped: false}), 0);
+ assert.eq(mongos.getDB('config').collections.count({_id: ns, dropped: false}), 1);
+ assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
+}
- assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: keyDoc}));
+function testAndClenaupWithKeyNoIndexOK(keyDoc) {
+ assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
- assert.eq(mongos.getDB('config').collections.count({_id: ns, dropped: false}), 1);
- assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
- }
+ var ns = kDbName + '.foo';
+ assert.eq(mongos.getDB('config').collections.count({_id: ns, dropped: false}), 0);
- function getIndexSpecByName(coll, indexName) {
- var indexes = coll.getIndexes().filter(function(spec) {
- return spec.name === indexName;
- });
- assert.eq(1, indexes.length, 'index "' + indexName + '" not found"');
- return indexes[0];
- }
+ assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: keyDoc}));
- // Fail if db is not sharded.
- assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
+ assert.eq(mongos.getDB('config').collections.count({_id: ns, dropped: false}), 1);
+ assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
+}
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
+function getIndexSpecByName(coll, indexName) {
+ var indexes = coll.getIndexes().filter(function(spec) {
+ return spec.name === indexName;
+ });
+ assert.eq(1, indexes.length, 'index "' + indexName + '" not found"');
+ return indexes[0];
+}
- // Fail if db is not sharding enabled.
- assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
+// Fail if db is not sharded.
+assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
- assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
- // Verify wrong arguments errors.
- assert.commandFailed(mongos.adminCommand({shardCollection: 'foo', key: {_id: 1}}));
+// Fail if db is not sharding enabled.
+assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
- assert.commandFailed(mongos.adminCommand({shardCollection: 'foo', key: "aaa"}));
+assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
- // shardCollection may only be run against admin database.
- assert.commandFailed(
- mongos.getDB('test').runCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
+// Verify wrong arguments errors.
+assert.commandFailed(mongos.adminCommand({shardCollection: 'foo', key: {_id: 1}}));
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
- // Can't shard if key is not specified.
- assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo'}));
+assert.commandFailed(mongos.adminCommand({shardCollection: 'foo', key: "aaa"}));
- assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {}}));
+// shardCollection may only be run against admin database.
+assert.commandFailed(
+ mongos.getDB('test').runCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
- // Verify key format
- assert.commandFailed(
- mongos.adminCommand({shardCollection: kDbName + '.foo', key: {aKey: "hahahashed"}}));
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
+// Can't shard if key is not specified.
+assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo'}));
- // Shard key cannot contain embedded objects.
- assert.commandFailed(
- mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: {a: 1}}}));
- assert.commandFailed(
- mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: {'a.b': 1}}}));
+assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {}}));
- // Shard key can contain dotted path to embedded element.
- assert.commandWorked(mongos.adminCommand(
- {shardCollection: kDbName + '.shard_key_dotted_path', key: {'_id.a': 1}}));
+// Verify key format
+assert.commandFailed(
+ mongos.adminCommand({shardCollection: kDbName + '.foo', key: {aKey: "hahahashed"}}));
- //
- // Test shardCollection's idempotency
- //
+// Shard key cannot contain embedded objects.
+assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: {a: 1}}}));
+assert.commandFailed(
+ mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: {'a.b': 1}}}));
- // Succeed if a collection is already sharded with the same options.
- assert.commandWorked(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
- assert.commandWorked(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
- // Specifying the simple collation or not specifying a collation should be equivalent, because
- // if no collation is specified, the collection default collation is used.
- assert.commandWorked(mongos.adminCommand(
- {shardCollection: kDbName + '.foo', key: {_id: 1}, collation: {locale: 'simple'}}));
+// Shard key can contain dotted path to embedded element.
+assert.commandWorked(
+ mongos.adminCommand({shardCollection: kDbName + '.shard_key_dotted_path', key: {'_id.a': 1}}));
- // Fail if the collection is already sharded with different options.
- // different shard key
- assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {x: 1}}));
- // different 'unique'
- assert.commandFailed(
- mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}, unique: true}));
+//
+// Test shardCollection's idempotency
+//
- assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
+// Succeed if a collection is already sharded with the same options.
+assert.commandWorked(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
+assert.commandWorked(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}}));
+// Specifying the simple collation or not specifying a collation should be equivalent, because
+// if no collation is specified, the collection default collation is used.
+assert.commandWorked(mongos.adminCommand(
+ {shardCollection: kDbName + '.foo', key: {_id: 1}, collation: {locale: 'simple'}}));
- // Shard empty collections no index required.
- testAndClenaupWithKeyNoIndexOK({_id: 1});
- testAndClenaupWithKeyNoIndexOK({_id: 'hashed'});
+// Fail if the collection is already sharded with different options.
+// different shard key
+assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {x: 1}}));
+// different 'unique'
+assert.commandFailed(
+ mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}, unique: true}));
- // Shard by a plain key.
- testAndClenaupWithKeyNoIndexOK({a: 1});
+assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
- // Cant shard collection with data and no index on the shard key.
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
- testAndClenaupWithKeyNoIndexFailed({a: 1});
+// Shard empty collections no index required.
+testAndClenaupWithKeyNoIndexOK({_id: 1});
+testAndClenaupWithKeyNoIndexOK({_id: 'hashed'});
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
- testAndClenaupWithKeyOK({a: 1});
+// Shard by a plain key.
+testAndClenaupWithKeyNoIndexOK({a: 1});
- // Shard by a hashed key.
- testAndClenaupWithKeyNoIndexOK({a: 'hashed'});
+// Cant shard collection with data and no index on the shard key.
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
+testAndClenaupWithKeyNoIndexFailed({a: 1});
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
- testAndClenaupWithKeyNoIndexFailed({a: 'hashed'});
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
+testAndClenaupWithKeyOK({a: 1});
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
- testAndClenaupWithKeyOK({a: 'hashed'});
+// Shard by a hashed key.
+testAndClenaupWithKeyNoIndexOK({a: 'hashed'});
- // Shard by a compound key.
- testAndClenaupWithKeyNoIndexOK({x: 1, y: 1});
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
+testAndClenaupWithKeyNoIndexFailed({a: 'hashed'});
- assert.writeOK(mongos.getDB(kDbName).foo.insert({x: 1, y: 1}));
- testAndClenaupWithKeyNoIndexFailed({x: 1, y: 1});
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
+testAndClenaupWithKeyOK({a: 'hashed'});
- assert.writeOK(mongos.getDB(kDbName).foo.insert({x: 1, y: 1}));
- testAndClenaupWithKeyOK({x: 1, y: 1});
+// Shard by a compound key.
+testAndClenaupWithKeyNoIndexOK({x: 1, y: 1});
- testAndClenaupWithKeyNoIndexFailed({x: 'hashed', y: 1});
- testAndClenaupWithKeyNoIndexFailed({x: 'hashed', y: 'hashed'});
+assert.writeOK(mongos.getDB(kDbName).foo.insert({x: 1, y: 1}));
+testAndClenaupWithKeyNoIndexFailed({x: 1, y: 1});
- // Shard by a key component.
- testAndClenaupWithKeyOK({'z.x': 1});
- testAndClenaupWithKeyOK({'z.x': 'hashed'});
+assert.writeOK(mongos.getDB(kDbName).foo.insert({x: 1, y: 1}));
+testAndClenaupWithKeyOK({x: 1, y: 1});
- // Can't shard by a multikey.
- assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({a: 1}));
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: [1, 2, 3, 4, 5], b: 1}));
- testAndClenaupWithKeyNoIndexFailed({a: 1});
+testAndClenaupWithKeyNoIndexFailed({x: 'hashed', y: 1});
+testAndClenaupWithKeyNoIndexFailed({x: 'hashed', y: 'hashed'});
- assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({a: 1, b: 1}));
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: [1, 2, 3, 4, 5], b: 1}));
- testAndClenaupWithKeyNoIndexFailed({a: 1, b: 1});
+// Shard by a key component.
+testAndClenaupWithKeyOK({'z.x': 1});
+testAndClenaupWithKeyOK({'z.x': 'hashed'});
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
- testAndClenaupWithKeyNoIndexFailed({a: 'hashed'});
+// Can't shard by a multikey.
+assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({a: 1}));
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: [1, 2, 3, 4, 5], b: 1}));
+testAndClenaupWithKeyNoIndexFailed({a: 1});
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
- testAndClenaupWithKeyOK({a: 'hashed'});
+assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({a: 1, b: 1}));
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: [1, 2, 3, 4, 5], b: 1}));
+testAndClenaupWithKeyNoIndexFailed({a: 1, b: 1});
- // Cant shard by a parallel arrays.
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: [1, 2, 3, 4, 5], b: [1, 2, 3, 4, 5]}));
- testAndClenaupWithKeyNoIndexFailed({a: 1, b: 1});
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
+testAndClenaupWithKeyNoIndexFailed({a: 'hashed'});
- assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 1, b: 1}));
+testAndClenaupWithKeyOK({a: 'hashed'});
- // Can't shard on unique hashed key.
- assert.commandFailed(mongos.adminCommand(
- {shardCollection: kDbName + '.foo', key: {aKey: "hashed"}, unique: true}));
+// Cant shard by a parallel arrays.
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: [1, 2, 3, 4, 5], b: [1, 2, 3, 4, 5]}));
+testAndClenaupWithKeyNoIndexFailed({a: 1, b: 1});
- // If shardCollection has unique:true it must have a unique index.
- assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({aKey: 1}));
+assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
- assert.commandFailed(
- mongos.adminCommand({shardCollection: kDbName + '.foo', key: {aKey: 1}, unique: true}));
+// Can't shard on unique hashed key.
+assert.commandFailed(
+ mongos.adminCommand({shardCollection: kDbName + '.foo', key: {aKey: "hashed"}, unique: true}));
- //
- // Session-related tests
- //
+// If shardCollection has unique:true it must have a unique index.
+assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({aKey: 1}));
- assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
- assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
+assert.commandFailed(
+ mongos.adminCommand({shardCollection: kDbName + '.foo', key: {aKey: 1}, unique: true}));
- // shardCollection can be called under a session.
- const sessionDb = mongos.startSession().getDatabase(kDbName);
- assert.commandWorked(
- sessionDb.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 'hashed'}}));
- sessionDb.getSession().endSession();
-
- assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
-
- //
- // Collation-related tests
- //
+//
+// Session-related tests
+//
- assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
- assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
+assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
+assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
- // shardCollection should fail when the 'collation' option is not a nested object.
- assert.commandFailed(
- mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}, collation: true}));
-
- // shardCollection should fail when the 'collation' option cannot be parsed.
- assert.commandFailed(mongos.adminCommand(
- {shardCollection: kDbName + '.foo', key: {_id: 1}, collation: {locale: 'unknown'}}));
-
- // shardCollection should fail when the 'collation' option is valid but is not the simple
- // collation.
- assert.commandFailed(mongos.adminCommand(
- {shardCollection: kDbName + '.foo', key: {_id: 1}, collation: {locale: 'en_US'}}));
-
- // shardCollection should succeed when the 'collation' option specifies the simple collation.
- assert.commandWorked(mongos.adminCommand(
- {shardCollection: kDbName + '.foo', key: {_id: 1}, collation: {locale: 'simple'}}));
-
- // shardCollection should fail when it does not specify the 'collation' option but the
- // collection has a non-simple default collation.
- mongos.getDB(kDbName).foo.drop();
- assert.commandWorked(
- mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}}));
- assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {a: 1}}));
-
- // shardCollection should fail for the key pattern {_id: 1} if the collection has a non-simple
- // default collation.
- mongos.getDB(kDbName).foo.drop();
- assert.commandWorked(
- mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}}));
- assert.commandFailed(mongos.adminCommand(
- {shardCollection: kDbName + '.foo', key: {_id: 1}, collation: {locale: 'simple'}}));
-
- // shardCollection should fail for the key pattern {a: 1} if there is already an index 'a_1',
- // but it has a non-simple collation.
- mongos.getDB(kDbName).foo.drop();
- assert.commandWorked(
- mongos.getDB(kDbName).foo.createIndex({a: 1}, {collation: {locale: 'en_US'}}));
- assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {a: 1}}));
-
- // shardCollection should succeed for the key pattern {a: 1} and collation {locale: 'simple'} if
- // there is no index 'a_1', but there is a non-simple collection default collation.
- mongos.getDB(kDbName).foo.drop();
- assert.commandWorked(
- mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}}));
- assert.commandWorked(mongos.adminCommand(
- {shardCollection: kDbName + '.foo', key: {a: 1}, collation: {locale: 'simple'}}));
- var indexSpec = getIndexSpecByName(mongos.getDB(kDbName).foo, 'a_1');
- assert(!indexSpec.hasOwnProperty('collation'));
-
- // shardCollection should succeed for the key pattern {a: 1} if there are two indexes on {a: 1}
- // and one has the simple collation.
- mongos.getDB(kDbName).foo.drop();
- assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({a: 1}, {name: "a_1_simple"}));
- assert.commandWorked(mongos.getDB(kDbName).foo.createIndex(
- {a: 1}, {collation: {locale: 'en_US'}, name: "a_1_en_US"}));
- assert.commandWorked(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {a: 1}}));
-
- // shardCollection should fail on a non-empty collection when the only index available with the
- // shard key as a prefix has a non-simple collation.
- mongos.getDB(kDbName).foo.drop();
- assert.commandWorked(
- mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}}));
- assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 'foo'}));
- // This index will inherit the collection's default collation.
- assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({a: 1}));
- assert.commandFailed(mongos.adminCommand(
- {shardCollection: kDbName + '.foo', key: {a: 1}, collation: {locale: 'simple'}}));
-
- // shardCollection should succeed on an empty collection with a non-simple default collation.
- mongos.getDB(kDbName).foo.drop();
- assert.commandWorked(
- mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}}));
- assert.commandWorked(mongos.adminCommand(
- {shardCollection: kDbName + '.foo', key: {a: 1}, collation: {locale: 'simple'}}));
-
- // shardCollection should succeed on an empty collection with no default collation.
- mongos.getDB(kDbName).foo.drop();
- assert.commandWorked(mongos.getDB(kDbName).createCollection('foo'));
- assert.commandWorked(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {a: 1}}));
+// shardCollection can be called under a session.
+const sessionDb = mongos.startSession().getDatabase(kDbName);
+assert.commandWorked(
+ sessionDb.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 'hashed'}}));
+sessionDb.getSession().endSession();
- assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
+assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
- //
- // Tests for the shell helper sh.shardCollection().
- //
+//
+// Collation-related tests
+//
- db = mongos.getDB(kDbName);
- assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
+assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
+assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
+
+// shardCollection should fail when the 'collation' option is not a nested object.
+assert.commandFailed(
+ mongos.adminCommand({shardCollection: kDbName + '.foo', key: {_id: 1}, collation: true}));
+
+// shardCollection should fail when the 'collation' option cannot be parsed.
+assert.commandFailed(mongos.adminCommand(
+ {shardCollection: kDbName + '.foo', key: {_id: 1}, collation: {locale: 'unknown'}}));
+
+// shardCollection should fail when the 'collation' option is valid but is not the simple
+// collation.
+assert.commandFailed(mongos.adminCommand(
+ {shardCollection: kDbName + '.foo', key: {_id: 1}, collation: {locale: 'en_US'}}));
+
+// shardCollection should succeed when the 'collation' option specifies the simple collation.
+assert.commandWorked(mongos.adminCommand(
+ {shardCollection: kDbName + '.foo', key: {_id: 1}, collation: {locale: 'simple'}}));
+
+// shardCollection should fail when it does not specify the 'collation' option but the
+// collection has a non-simple default collation.
+mongos.getDB(kDbName).foo.drop();
+assert.commandWorked(mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}}));
+assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {a: 1}}));
+
+// shardCollection should fail for the key pattern {_id: 1} if the collection has a non-simple
+// default collation.
+mongos.getDB(kDbName).foo.drop();
+assert.commandWorked(mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}}));
+assert.commandFailed(mongos.adminCommand(
+ {shardCollection: kDbName + '.foo', key: {_id: 1}, collation: {locale: 'simple'}}));
+
+// shardCollection should fail for the key pattern {a: 1} if there is already an index 'a_1',
+// but it has a non-simple collation.
+mongos.getDB(kDbName).foo.drop();
+assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({a: 1}, {collation: {locale: 'en_US'}}));
+assert.commandFailed(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {a: 1}}));
+
+// shardCollection should succeed for the key pattern {a: 1} and collation {locale: 'simple'} if
+// there is no index 'a_1', but there is a non-simple collection default collation.
+mongos.getDB(kDbName).foo.drop();
+assert.commandWorked(mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}}));
+assert.commandWorked(mongos.adminCommand(
+ {shardCollection: kDbName + '.foo', key: {a: 1}, collation: {locale: 'simple'}}));
+var indexSpec = getIndexSpecByName(mongos.getDB(kDbName).foo, 'a_1');
+assert(!indexSpec.hasOwnProperty('collation'));
+
+// shardCollection should succeed for the key pattern {a: 1} if there are two indexes on {a: 1}
+// and one has the simple collation.
+mongos.getDB(kDbName).foo.drop();
+assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({a: 1}, {name: "a_1_simple"}));
+assert.commandWorked(mongos.getDB(kDbName).foo.createIndex(
+ {a: 1}, {collation: {locale: 'en_US'}, name: "a_1_en_US"}));
+assert.commandWorked(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {a: 1}}));
+
+// shardCollection should fail on a non-empty collection when the only index available with the
+// shard key as a prefix has a non-simple collation.
+mongos.getDB(kDbName).foo.drop();
+assert.commandWorked(mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}}));
+assert.writeOK(mongos.getDB(kDbName).foo.insert({a: 'foo'}));
+// This index will inherit the collection's default collation.
+assert.commandWorked(mongos.getDB(kDbName).foo.createIndex({a: 1}));
+assert.commandFailed(mongos.adminCommand(
+ {shardCollection: kDbName + '.foo', key: {a: 1}, collation: {locale: 'simple'}}));
+
+// shardCollection should succeed on an empty collection with a non-simple default collation.
+mongos.getDB(kDbName).foo.drop();
+assert.commandWorked(mongos.getDB(kDbName).createCollection('foo', {collation: {locale: 'en_US'}}));
+assert.commandWorked(mongos.adminCommand(
+ {shardCollection: kDbName + '.foo', key: {a: 1}, collation: {locale: 'simple'}}));
+
+// shardCollection should succeed on an empty collection with no default collation.
+mongos.getDB(kDbName).foo.drop();
+assert.commandWorked(mongos.getDB(kDbName).createCollection('foo'));
+assert.commandWorked(mongos.adminCommand({shardCollection: kDbName + '.foo', key: {a: 1}}));
+
+assert.commandWorked(mongos.getDB(kDbName).dropDatabase());
- // 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();
+//
+// 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();
})();