diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2021-02-11 11:28:13 -0500 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-02-21 11:29:04 +0000 |
commit | d01febfefb28d3aab5305eb1dbcd6d047139c654 (patch) | |
tree | de0f45a2c56eb9d5dfb8751837aaba6766872d25 /jstests | |
parent | 5199abc5b9113e310a79d9ec29a5ac6b77ad5682 (diff) | |
download | mongo-d01febfefb28d3aab5305eb1dbcd6d047139c654.tar.gz |
SERVER-52812 Unify the implicit createDatabase/enableSharding flow
This change makes both enableSharding and the implicit createDatabase
from the routers to go through the same _configsvrCreateDatabase
command. This command has the sole responsibility now of creating (or
ensuring it is created) a database with the specified name, optional
primary flag and optional enableSharding field.
Diffstat (limited to 'jstests')
-rw-r--r-- | jstests/sharding/enable_sharding.js | 106 | ||||
-rw-r--r-- | jstests/sharding/enable_sharding_basic.js | 58 | ||||
-rw-r--r-- | jstests/sharding/enable_sharding_with_primary.js | 29 | ||||
-rw-r--r-- | jstests/sharding/shard_collection_basic.js | 12 | ||||
-rw-r--r-- | jstests/sharding/shard_collection_config_db.js (renamed from jstests/sharding/shard_config_db_collections.js) | 21 | ||||
-rw-r--r-- | jstests/sharding/shard_collection_existing_zones.js | 2 | ||||
-rw-r--r-- | jstests/sharding/shard_collection_verify_initial_chunks.js | 2 |
7 files changed, 121 insertions, 109 deletions
diff --git a/jstests/sharding/enable_sharding.js b/jstests/sharding/enable_sharding.js new file mode 100644 index 00000000000..0fe510083c6 --- /dev/null +++ b/jstests/sharding/enable_sharding.js @@ -0,0 +1,106 @@ +// +// Basic tests for enableSharding command. +// + +(function() { +'use strict'; + +var st = new ShardingTest({mongos: 2, shards: 2}); + +jsTest.log('enableSharding can run only against the admin database'); +{ + assert.commandFailedWithCode(st.s0.getDB('test').runCommand({enableSharding: 'db'}), + ErrorCodes.Unauthorized); +} + +jsTest.log('Cannot shard system databases except for the config db'); +{ + assert.commandWorked(st.s0.adminCommand({enableSharding: 'config'})); + assert.commandFailed(st.s0.adminCommand({enableSharding: 'local'})); + assert.commandFailed(st.s0.adminCommand({enableSharding: 'admin'})); +} + +jsTest.log('Cannot shard db with the name that just differ on case'); +{ + assert.commandWorked(st.s0.adminCommand({enableSharding: 'db'})); + assert.eq(st.s0.getDB('config').databases.findOne({_id: 'db'}).partitioned, true); + assert.commandFailedWithCode(st.s0.adminCommand({enableSharding: 'DB'}), + ErrorCodes.DatabaseDifferCase); +} + +jsTest.log('Cannot shard invalid db name'); +{ + assert.commandFailed(st.s0.adminCommand({enableSharding: 'a.b'})); + assert.commandFailed(st.s0.adminCommand({enableSharding: ''})); +} + +jsTest.log('Attempting to shard already sharded database returns success'); +{ + assert.commandWorked(st.s0.adminCommand({enableSharding: 'db'})); + assert.eq(st.s0.getDB('config').databases.findOne({_id: 'db'}).partitioned, true); +} + +jsTest.log('Verify config.databases metadata'); +{ + assert.commandWorked(st.s0.getDB('unsharded').foo.insert({aKey: "aValue"})); + assert.eq(st.s0.getDB('config').databases.findOne({_id: 'unsharded'}).partitioned, false); + assert.commandWorked(st.s0.adminCommand({enableSharding: 'unsharded'})); + assert.eq(st.s0.getDB('config').databases.findOne({_id: 'unsharded'}).partitioned, true); +} + +jsTest.log('Sharding a collection before enableSharding is called fails'); +{ + assert.commandFailed(st.s0.adminCommand({shardCollection: 'TestDB.TestColl', key: {_id: 1}})); + assert.commandFailed(st.s1.adminCommand({shardCollection: 'TestDB.TestColl', key: {_id: 1}})); + + assert.commandWorked(st.s0.getDB('TestDB').TestColl.insert({_id: 0})); + assert.commandWorked(st.s1.getDB('TestDB').TestColl.insert({_id: 1})); +} + +jsTest.log('Calling enableSharding on one mongos and shardCollection through another must work'); +{ + assert.commandWorked(st.s0.adminCommand({enableSharding: 'TestDB'})); + assert.commandWorked(st.s1.adminCommand({shardCollection: 'TestDB.TestColl', key: {_id: 1}})); + assert.commandWorked(st.s0.adminCommand({shardCollection: 'TestDB.TestColl', key: {_id: 1}})); +} + +jsTest.log('Cannot enable sharding on a database using a wrong shard name'); +{ + assert.commandFailed(st.s0.adminCommand( + {enableSharding: 'db2', primaryShard: st.shard1.shardName + '_unenxisting_name_postfix'})); +} + +jsTest.log('Enabling sharding on a database with a valid shard name must work'); +{ + assert.commandWorked( + st.s0.adminCommand({enableSharding: 'db_on_shard0', primaryShard: st.shard0.shardName})); + assert.commandWorked( + st.s0.adminCommand({enableSharding: 'db_on_shard1', primaryShard: st.shard1.shardName})); + assert.eq(st.s0.getDB('config').databases.findOne({_id: 'db_on_shard0'}).primary, + st.shard0.shardName); + assert.eq(st.s0.getDB('config').databases.findOne({_id: 'db_on_shard1'}).primary, + st.shard1.shardName); +} + +jsTest.log( + 'Enable sharding on a database already created with the correct primary shard name must work'); +{ + assert.commandWorked( + st.s0.adminCommand({enableSharding: 'db_on_shard0', primaryShard: st.shard0.shardName})); + assert.commandWorked( + st.s0.adminCommand({enableSharding: 'db_on_shard1', primaryShard: st.shard1.shardName})); +} + +jsTest.log( + 'Cannot enable sharding of a database already created with a different primary shard name'); +{ + assert.commandFailedWithCode( + st.s0.adminCommand({enableSharding: 'db_on_shard0', primaryShard: st.shard1.shardName}), + ErrorCodes.NamespaceExists); + assert.commandFailedWithCode( + st.s0.adminCommand({enableSharding: 'db_on_shard1', primaryShard: st.shard0.shardName}), + ErrorCodes.NamespaceExists); +} + +st.stop(); +})(); diff --git a/jstests/sharding/enable_sharding_basic.js b/jstests/sharding/enable_sharding_basic.js deleted file mode 100644 index 046b4f6e520..00000000000 --- a/jstests/sharding/enable_sharding_basic.js +++ /dev/null @@ -1,58 +0,0 @@ -// -// Basic tests for enableSharding command. -// - -(function() { -'use strict'; - -var st = new ShardingTest({mongos: 2, shards: 2}); - -// enableSharding can run only on mongos. -assert.commandFailedWithCode(st.rs0.getPrimary().getDB('admin').runCommand({enableSharding: 'db'}), - ErrorCodes.CommandNotFound); - -// enableSharding can run only against the admin database. -assert.commandFailedWithCode(st.s0.getDB('test').runCommand({enableSharding: 'db'}), - ErrorCodes.Unauthorized); - -// Can't shard 'local' database. -assert.commandFailed(st.s0.adminCommand({enableSharding: 'local'})); - -// Can't shard 'admin' database. -assert.commandFailed(st.s0.adminCommand({enableSharding: 'admin'})); - -// Can't shard db with the name that just differ on case. -assert.commandWorked(st.s0.adminCommand({enableSharding: 'db'})); -assert.eq(st.s0.getDB('config').databases.findOne({_id: 'db'}).partitioned, true); - -assert.commandFailedWithCode(st.s0.adminCommand({enableSharding: 'DB'}), - ErrorCodes.DatabaseDifferCase); - -// Can't shard invalid db name. -assert.commandFailed(st.s0.adminCommand({enableSharding: 'a.b'})); -assert.commandFailed(st.s0.adminCommand({enableSharding: ''})); - -// Attempting to shard already sharded database returns success. -assert.commandWorked(st.s0.adminCommand({enableSharding: 'db'})); -assert.eq(st.s0.getDB('config').databases.findOne({_id: 'db'}).partitioned, true); - -// Verify config.databases metadata. -assert.commandWorked(st.s0.getDB('unsharded').foo.insert({aKey: "aValue"})); -assert.eq(st.s0.getDB('config').databases.findOne({_id: 'unsharded'}).partitioned, false); -assert.commandWorked(st.s0.adminCommand({enableSharding: 'unsharded'})); -assert.eq(st.s0.getDB('config').databases.findOne({_id: 'unsharded'}).partitioned, true); - -// Sharding a collection before 'enableSharding' is called fails -assert.commandFailed(st.s0.adminCommand({shardCollection: 'TestDB.TestColl', key: {_id: 1}})); -assert.commandFailed(st.s1.adminCommand({shardCollection: 'TestDB.TestColl', key: {_id: 1}})); - -assert.commandWorked(st.s0.getDB('TestDB').TestColl.insert({_id: 0})); -assert.commandWorked(st.s1.getDB('TestDB').TestColl.insert({_id: 1})); - -// Calling 'enableSharding' on one mongos and 'shardCollection' through another must work -assert.commandWorked(st.s0.adminCommand({enableSharding: 'TestDB'})); -assert.commandWorked(st.s1.adminCommand({shardCollection: 'TestDB.TestColl', key: {_id: 1}})); -assert.commandWorked(st.s0.adminCommand({shardCollection: 'TestDB.TestColl', key: {_id: 1}})); - -st.stop(); -})(); diff --git a/jstests/sharding/enable_sharding_with_primary.js b/jstests/sharding/enable_sharding_with_primary.js deleted file mode 100644 index 1c42b07ac54..00000000000 --- a/jstests/sharding/enable_sharding_with_primary.js +++ /dev/null @@ -1,29 +0,0 @@ -// -// Enable sharding with custom primary shard tests -// - -(function() { -'use strict'; - -var st = new ShardingTest({mongos: 1, shards: 2}); - -// Can't enable sharding on a database using a wrong shard name -assert.commandFailed(st.s0.adminCommand( - {enableSharding: 'db2', primaryShard: st.shard1.shardName + '_unenxisting_name_postfix'})); - -// Enabling sharding on a database with a valid shard name must work -assert.commandWorked( - st.s0.adminCommand({enableSharding: 'db2', primaryShard: st.shard1.shardName})); -assert.eq(st.s0.getDB('config').databases.findOne({_id: 'db2'}).primary, st.shard1.shardName); - -// Enable sharding on a database already created with the correct primary shard name must work -assert.commandWorked( - st.s0.adminCommand({enableSharding: 'db2', primaryShard: st.shard1.shardName})); - -// Can't enable sharding of a database already created with a different primary shard name -assert.commandFailedWithCode( - st.s0.adminCommand({enableSharding: 'db2', primaryShard: st.shard0.shardName}), - ErrorCodes.NamespaceExists); - -st.stop(); -})();
\ No newline at end of file diff --git a/jstests/sharding/shard_collection_basic.js b/jstests/sharding/shard_collection_basic.js index 8341a84ca87..741f1469f60 100644 --- a/jstests/sharding/shard_collection_basic.js +++ b/jstests/sharding/shard_collection_basic.js @@ -1,15 +1,13 @@ -// -// Basic tests for shardCollection. -// - -load("jstests/sharding/libs/find_chunks_util.js"); - (function() { 'use strict'; -var st = new ShardingTest({mongos: 1, shards: 2}); +load("jstests/sharding/libs/find_chunks_util.js"); + +var st = new ShardingTest({shards: 2}); var kDbName = 'db'; var mongos = st.s0; +var config = st.s0.getDB('config'); +var admin = st.s0.getDB('admin'); function testAndClenaupWithKeyNoIndexFailed(keyDoc) { assert.commandWorked(mongos.adminCommand({enableSharding: kDbName})); diff --git a/jstests/sharding/shard_config_db_collections.js b/jstests/sharding/shard_collection_config_db.js index 8f8f324957b..e4787dc13a0 100644 --- a/jstests/sharding/shard_config_db_collections.js +++ b/jstests/sharding/shard_collection_config_db.js @@ -1,12 +1,12 @@ (function() { 'use strict'; -// Database-level tests -{ - var st = new ShardingTest({shards: 2}); - var config = st.s.getDB('config'); - var admin = st.s.getDB('admin'); +var st = new ShardingTest({shards: 2}); +var config = st.s.getDB('config'); +var admin = st.s.getDB('admin'); +jsTest.log('Cannot movePrimary on the config database'); +{ // At first, there should not be an entry for config assert.eq(0, config.databases.count({"_id": "config"})); @@ -19,24 +19,19 @@ // Test that you cannot set the primary shard for config (not even to 'config') assert.commandFailed(admin.runCommand({movePrimary: 'config', to: st.shard0.shardName})); assert.commandFailed(admin.runCommand({movePrimary: 'config', to: 'config'})); - - st.stop(); } -// Test that only system.sessions may be sharded. +jsTest.log('Only system.sessions may be sharded'); { - var st = new ShardingTest({shards: 2}); - var admin = st.s.getDB('admin'); - assert.commandWorked( admin.runCommand({shardCollection: "config.system.sessions", key: {_id: 1}})); assert.eq(0, st.s.getDB('config').chunks.count({"shard": "config"})); assert.commandFailed(admin.runCommand({shardCollection: "config.anythingelse", key: {_id: 1}})); - - st.stop(); } +st.stop(); + // Cannot shard things in config without shards. { var st = new ShardingTest({shards: 0}); diff --git a/jstests/sharding/shard_collection_existing_zones.js b/jstests/sharding/shard_collection_existing_zones.js index cc52b416bc2..b0c5b2a31a1 100644 --- a/jstests/sharding/shard_collection_existing_zones.js +++ b/jstests/sharding/shard_collection_existing_zones.js @@ -5,7 +5,7 @@ load("jstests/sharding/libs/find_chunks_util.js"); -var st = new ShardingTest({mongos: 1, shards: 3}); +var st = new ShardingTest({shards: 3}); var kDbName = 'test'; var kCollName = 'foo'; var ns = kDbName + '.' + kCollName; diff --git a/jstests/sharding/shard_collection_verify_initial_chunks.js b/jstests/sharding/shard_collection_verify_initial_chunks.js index 38e3d4d67a6..77d752ba164 100644 --- a/jstests/sharding/shard_collection_verify_initial_chunks.js +++ b/jstests/sharding/shard_collection_verify_initial_chunks.js @@ -5,7 +5,7 @@ (function() { 'use strict'; -let st = new ShardingTest({mongos: 1, shards: 2}); +let st = new ShardingTest({shards: 2}); let mongos = st.s0; let config = mongos.getDB("config"); |