summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcos José Grillo Ramirez <marcos.grillo@mongodb.com>2022-09-26 13:46:29 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-09-26 14:19:29 +0000
commitcec86253e4200704cbd6d4d98e80384a2d57824a (patch)
tree5cc2bb4f5871c70815efd1091f676a09fbf5a8ac
parent67a20e1aae8124adfcc4f0c3d448d98c49f74c69 (diff)
downloadmongo-cec86253e4200704cbd6d4d98e80384a2d57824a.tar.gz
SERVER-67897 Use remove_shard_util.js in tests that need to reliably remove a shard
-rw-r--r--jstests/sharding/addshard2.js23
-rw-r--r--jstests/sharding/addshard5.js4
-rw-r--r--jstests/sharding/auth_add_shard.js11
-rw-r--r--jstests/sharding/balancing_sessions_collection_legacy.js17
-rw-r--r--jstests/sharding/configsvr_metadata_commands_require_majority_write_concern.js5
-rw-r--r--jstests/sharding/conversion_of_replica_set_to_sharded_cluster.js12
-rw-r--r--jstests/sharding/convert_to_and_from_sharded.js8
-rw-r--r--jstests/sharding/cwwc_conflict_add_shard.js29
-rw-r--r--jstests/sharding/data_size_aware_balancing_sessions_collection.js17
-rw-r--r--jstests/sharding/libs/mongos_api_params_util.js11
-rw-r--r--jstests/sharding/listshards.js24
-rw-r--r--jstests/sharding/merge_with_drop_shard.js14
-rw-r--r--jstests/sharding/move_chunk_remove_shard.js17
-rw-r--r--jstests/sharding/move_jumbo_chunk.js20
-rw-r--r--jstests/sharding/query/current_op_with_drop_shard.js10
-rw-r--r--jstests/sharding/remove2.js21
-rw-r--r--jstests/sharding/remove3.js4
-rw-r--r--jstests/sharding/remove_shard_near_doc_size_limit.js25
-rw-r--r--jstests/sharding/remove_shard_with_zones.js5
-rw-r--r--jstests/sharding/set_cluster_parameter.js12
-rw-r--r--jstests/sharding/set_user_write_block_mode.js19
-rw-r--r--jstests/sharding/shard_removal_triggers_catalog_cache_invalidation.js15
22 files changed, 67 insertions, 256 deletions
diff --git a/jstests/sharding/addshard2.js b/jstests/sharding/addshard2.js
index 28ffaf05e6f..3bc524550a5 100644
--- a/jstests/sharding/addshard2.js
+++ b/jstests/sharding/addshard2.js
@@ -3,6 +3,8 @@
* valid and invalid hosts, shardName matching or not matching a setName, etc).
*/
(function() {
+'use strict';
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -44,17 +46,6 @@ const assertAddShardFailed = function(res, shardName) {
}
};
-const removeShardWithName = function(shardName) {
- let res = st.s.adminCommand({removeShard: shardName});
- assert.commandWorked(res);
- assert.eq('started', res.state);
- assert.soon(function() {
- res = st.s.adminCommand({removeShard: shardName});
- assert.commandWorked(res);
- return ('completed' === res.state);
- }, "removeShard never completed for shard " + shardName);
-};
-
const st = new ShardingTest({
shards: 0,
mongos: 1,
@@ -81,7 +72,7 @@ rst1.initiate();
addShardRes = st.s.adminCommand({addShard: rst1.getURL()});
assertAddShardSucceeded(addShardRes);
assert.eq(rst1.name, addShardRes.shardAdded);
-removeShardWithName(addShardRes.shardAdded);
+removeShard(st, addShardRes.shardAdded);
rst1.stopSet();
jsTest.log(
@@ -91,7 +82,7 @@ rst2.startSet({shardsvr: ''});
rst2.initiate();
addShardRes = st.s.adminCommand({addShard: rst2.getURL(), name: rst2.name});
assertAddShardSucceeded(addShardRes, rst2.name);
-removeShardWithName(addShardRes.shardAdded);
+removeShard(st, addShardRes.shardAdded);
rst2.stopSet();
let rst3 = new ReplSetTest({nodes: 1});
@@ -102,7 +93,7 @@ jsTest.log(
"Adding a replica set with a specified shardName that differs from the set's name should succeed.");
addShardRes = st.s.adminCommand({addShard: rst3.getURL(), name: "differentShardName"});
assertAddShardSucceeded(addShardRes, "differentShardName");
-removeShardWithName(addShardRes.shardAdded);
+removeShard(st, addShardRes.shardAdded);
jsTest.log("Adding a replica with a specified shardName of 'config' should fail.");
addShardRes = st.s.adminCommand({addShard: rst3.getURL(), name: "config"});
@@ -145,7 +136,7 @@ jsTest.log(
"Adding a replica set whose setName is config with a non-'config' shardName should succeed");
addShardRes = st.s.adminCommand({addShard: rst4.getURL(), name: "nonConfig"});
assertAddShardSucceeded(addShardRes, "nonConfig");
-removeShardWithName(addShardRes.shardAdded);
+removeShard(st, addShardRes.shardAdded);
rst4.stopSet();
@@ -173,7 +164,7 @@ assert.neq(null, rst5.getPrimary().getDB('test').foo.findOne());
assert.commandWorked(st.s.getDB('test').runCommand({dropDatabase: 1}));
-removeShardWithName(addShardRes.shardAdded);
+removeShard(st, addShardRes.shardAdded);
rst5.stopSet();
diff --git a/jstests/sharding/addshard5.js b/jstests/sharding/addshard5.js
index c02ababc3ce..0f826aab738 100644
--- a/jstests/sharding/addshard5.js
+++ b/jstests/sharding/addshard5.js
@@ -2,6 +2,7 @@
// migrations
(function() {
'use strict';
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -31,8 +32,7 @@ assert.commandWorked(mongos.adminCommand(
{moveChunk: coll + '', find: {_id: 0}, to: st.shard0.shardName, _waitForDelete: true}));
// Drop and re-add shard with the same name but a new host.
-assert.commandWorked(mongos.adminCommand({removeShard: st.shard1.shardName}));
-assert.commandWorked(mongos.adminCommand({removeShard: st.shard1.shardName}));
+removeShard(st, st.shard1.shardName);
let shard2 = new ReplSetTest({nodes: 2, nodeOptions: {shardsvr: ""}});
shard2.startSet();
diff --git a/jstests/sharding/auth_add_shard.js b/jstests/sharding/auth_add_shard.js
index 1aab96e9f0a..0c7faf01426 100644
--- a/jstests/sharding/auth_add_shard.js
+++ b/jstests/sharding/auth_add_shard.js
@@ -3,6 +3,7 @@
// up a sharded system, then adds/removes a shard.
(function() {
'use strict';
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -90,15 +91,7 @@ st.startBalancer();
//--------------- Test 3 --------------------
// now drain the shard
-assert.commandWorked(admin.runCommand({removeShard: rst.getURL()}));
-
-// give it some time to drain
-assert.soon(function() {
- var result = admin.runCommand({removeShard: rst.getURL()});
- printjson(result);
-
- return result.ok && result.state == "completed";
-}, "failed to drain shard completely", 5 * 60 * 1000);
+removeShard(st, rst.getURL());
// create user directly on new shard to allow direct reads from config.migrationCoordinators
rst.getPrimary()
diff --git a/jstests/sharding/balancing_sessions_collection_legacy.js b/jstests/sharding/balancing_sessions_collection_legacy.js
index 230235a7a47..9c9e9a8a7ed 100644
--- a/jstests/sharding/balancing_sessions_collection_legacy.js
+++ b/jstests/sharding/balancing_sessions_collection_legacy.js
@@ -8,6 +8,7 @@
load("jstests/libs/feature_flag_util.js");
load("jstests/sharding/libs/find_chunks_util.js");
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -56,21 +57,7 @@ function addShardToCluster() {
* decrements numShards.
*/
function removeShardFromCluster(shardName) {
- assert.commandWorked(st.s.adminCommand({removeShard: shardName}));
- assert.soon(function() {
- const res = st.s.adminCommand({removeShard: shardName});
- if (!res.ok && res.code === ErrorCodes.ShardNotFound) {
- // If the config server primary steps down right after removing the config.shards doc
- // for the shard but before responding with "state": "completed", the mongos would retry
- // the _configsvrRemoveShard command against the new config server primary, which would
- // not find the removed shard in its ShardRegistry if it has done a ShardRegistry reload
- // after the config.shards doc for the shard was removed. This would cause the command
- // to fail with ShardNotFound.
- return true;
- }
- assert.commandWorked(res);
- return ("completed" == res.state);
- }, "failed to remove shard " + shardName, kBalancerTimeoutMS);
+ removeShard(st, shardName);
numShards--;
}
diff --git a/jstests/sharding/configsvr_metadata_commands_require_majority_write_concern.js b/jstests/sharding/configsvr_metadata_commands_require_majority_write_concern.js
index c1d34c4f0e0..032f34d81dc 100644
--- a/jstests/sharding/configsvr_metadata_commands_require_majority_write_concern.js
+++ b/jstests/sharding/configsvr_metadata_commands_require_majority_write_concern.js
@@ -6,6 +6,7 @@
*/
(function() {
'use strict';
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -72,9 +73,7 @@ const cleanupFuncs = {
}
assert.commandWorked(res);
assert.eq('started', res.state);
- res = st.s.adminCommand({removeShard: newShardName});
- assert.commandWorked(res);
- assert.eq('completed', res.state);
+ removeShard(st, newShardName);
},
};
diff --git a/jstests/sharding/conversion_of_replica_set_to_sharded_cluster.js b/jstests/sharding/conversion_of_replica_set_to_sharded_cluster.js
index 15a0ab42bf5..6c5933c381c 100644
--- a/jstests/sharding/conversion_of_replica_set_to_sharded_cluster.js
+++ b/jstests/sharding/conversion_of_replica_set_to_sharded_cluster.js
@@ -12,6 +12,7 @@
'use strict';
load('jstests/replsets/rslib.js');
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -259,16 +260,7 @@ let assertAddShardSucceeded = function(res, shardName) {
"newly added shard " + res.shardAdded + " not found in config.shards");
};
-var removeShardWithName = function(st, shardName) {
- var res = st.s.adminCommand({removeShard: shardName});
- assert.commandWorked(res);
- assert.eq('started', res.state);
- assert.soon(function() {
- res = st.s.adminCommand({removeShard: shardName});
- assert.commandWorked(res);
- return ('completed' === res.state);
- }, "removeShard never completed for shard " + shardName);
-};
+let removeShardWithName = removeShard;
let checkCRUDCommands = function(testDB) {
for (let command in CRUDCommands) {
diff --git a/jstests/sharding/convert_to_and_from_sharded.js b/jstests/sharding/convert_to_and_from_sharded.js
index a4a3cd10ef4..340580b5dbf 100644
--- a/jstests/sharding/convert_to_and_from_sharded.js
+++ b/jstests/sharding/convert_to_and_from_sharded.js
@@ -5,6 +5,7 @@
*/
(function() {
"use strict";
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -96,15 +97,10 @@ for (x = 0; x < 2; x++) {
checkBasicCRUD(st.s.getDB('test').unsharded);
checkBasicCRUD(st.s.getDB('test').sharded);
-assert.commandWorked(st.s.adminCommand({removeShard: 'toRemoveLater'}));
-
// Start the balancer to start draining the chunks.
st.startBalancer();
-assert.soon(function() {
- var res = st.s.adminCommand({removeShard: 'toRemoveLater'});
- return res.state == 'completed';
-});
+removeShard(st, 'toRemoveLater');
newShard.stopSet();
diff --git a/jstests/sharding/cwwc_conflict_add_shard.js b/jstests/sharding/cwwc_conflict_add_shard.js
index bdabce70097..f17829967a4 100644
--- a/jstests/sharding/cwwc_conflict_add_shard.js
+++ b/jstests/sharding/cwwc_conflict_add_shard.js
@@ -10,6 +10,7 @@
(function() {
"use strict";
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -52,29 +53,6 @@ function convertRSToShard() {
jsTest.log("Coversion RS -> Shard took: " + (new Date() - start));
}
-function removeShardAndWait() {
- let start = new Date();
- jsTestLog("Removing the shard from the cluster should succeed.");
- const removeShardCmd = {removeShard: shardServer.getURL()};
- const res = st.s.adminCommand(removeShardCmd);
-
- assert.commandWorked(res);
- assert(res.state === "started");
-
- assert.soon(function() {
- let res = st.s.adminCommand(removeShardCmd);
- if (res.state === "completed") {
- return true;
- } else {
- jsTestLog("Still waiting for shard removal to complete:");
- printjson(res);
- return false;
- }
- });
-
- jsTestLog("Shard removal completed. took: " + (new Date() - start));
-}
-
function testAddShard(cwwcOnShard, cwwcOnCluster, shouldSucceed, fixCWWCOnShard) {
jsTestLog("Running addShard test with CWWCOnShard: " + tojson(cwwcOnShard) +
" and CWWCOnCluster: " + tojson(cwwcOnCluster));
@@ -123,7 +101,10 @@ function testAddShard(cwwcOnShard, cwwcOnCluster, shouldSucceed, fixCWWCOnShard)
assert.commandWorked(admin.runCommand({addshard: shardServer.getURL()}));
// Cleanup.
- removeShardAndWait();
+ let start = new Date();
+ jsTestLog("Removing the shard from the cluster should succeed.");
+ removeShard(st, shardServer.getURL());
+ jsTestLog("Shard removal completed. took: " + (new Date() - start));
convertShardToRS();
}
diff --git a/jstests/sharding/data_size_aware_balancing_sessions_collection.js b/jstests/sharding/data_size_aware_balancing_sessions_collection.js
index 9513c44aac2..00357d4403a 100644
--- a/jstests/sharding/data_size_aware_balancing_sessions_collection.js
+++ b/jstests/sharding/data_size_aware_balancing_sessions_collection.js
@@ -12,6 +12,7 @@
load("jstests/libs/feature_flag_util.js");
load("jstests/sharding/libs/find_chunks_util.js");
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -63,21 +64,7 @@ function addShardsToCluster(shardsToAdd) {
* decrements numShards.
*/
function removeShardFromCluster(shardName) {
- assert.commandWorked(st.s.adminCommand({removeShard: shardName}));
- assert.soon(function() {
- const res = st.s.adminCommand({removeShard: shardName});
- if (!res.ok && res.code === ErrorCodes.ShardNotFound) {
- // If the config server primary steps down right after removing the config.shards doc
- // for the shard but before responding with "state": "completed", the mongos would retry
- // the _configsvrRemoveShard command against the new config server primary, which would
- // not find the removed shard in its ShardRegistry if it has done a ShardRegistry reload
- // after the config.shards doc for the shard was removed. This would cause the command
- // to fail with ShardNotFound.
- return true;
- }
- assert.commandWorked(res);
- return ("completed" == res.state);
- }, "failed to remove shard " + shardName, kBalancerTimeoutMS);
+ removeShard(st, shardName, kBalancerTimeoutMS);
numShards--;
}
diff --git a/jstests/sharding/libs/mongos_api_params_util.js b/jstests/sharding/libs/mongos_api_params_util.js
index 158fe2e3ac0..c920509c36d 100644
--- a/jstests/sharding/libs/mongos_api_params_util.js
+++ b/jstests/sharding/libs/mongos_api_params_util.js
@@ -8,6 +8,7 @@ let MongosAPIParametersUtil = (function() {
load('jstests/replsets/rslib.js');
load('jstests/sharding/libs/last_lts_mongos_commands.js');
+ load('jstests/sharding/libs/remove_shard_util.js');
load('jstests/sharding/libs/sharded_transactions_helpers.js');
load('jstests/libs/auto_retry_transaction_in_sharding.js');
@@ -76,15 +77,7 @@ let MongosAPIParametersUtil = (function() {
function awaitRemoveShard(shardName) {
assert.commandWorked(st.startBalancer());
st.awaitBalancerRound();
- assert.soon(() => {
- const res = st.s.adminCommand({removeShard: shardName});
- jsTestLog(`removeShard result: ${tojson(res)}`);
- if (!res.ok && res.code === ErrorCodes.ShardNotFound) {
- return true;
- }
-
- return 'completed' === res.state;
- }, "removeShard never completed for shard " + shardName, 10 * 60 * 1000, 1000);
+ removeShard(st, shardName);
assert.commandWorked(st.stopBalancer());
}
diff --git a/jstests/sharding/listshards.js b/jstests/sharding/listshards.js
index c1c6c5703de..b11a6e13831 100644
--- a/jstests/sharding/listshards.js
+++ b/jstests/sharding/listshards.js
@@ -3,6 +3,7 @@
//
(function() {
'use strict';
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -20,10 +21,10 @@ const checkShardName = function(shardName, shardsArray) {
return found;
};
-const shardTest =
+const st =
new ShardingTest({name: 'listShardsTest', shards: 1, mongos: 1, other: {useHostname: true}});
-const mongos = shardTest.s0;
+const mongos = st.s0;
let res = mongos.adminCommand('listShards');
assert.commandWorked(res, 'listShards command failed');
let shardsArray = res.shards;
@@ -34,7 +35,7 @@ const rs1 =
new ReplSetTest({name: 'repl', nodes: 1, useHostName: true, nodeOptions: {shardsvr: ""}});
rs1.startSet();
rs1.initiate();
-res = shardTest.admin.runCommand({addShard: rs1.getURL()});
+res = st.admin.runCommand({addShard: rs1.getURL()});
assert.commandWorked(res, 'addShard command failed');
res = mongos.adminCommand('listShards');
assert.commandWorked(res, 'listShards command failed');
@@ -44,20 +45,7 @@ assert(checkShardName('repl', shardsArray),
'listShards command didn\'t return replica set shard: ' + tojson(shardsArray));
// remove 'repl' shard
-assert.soon(function() {
- var res = shardTest.admin.runCommand({removeShard: 'repl'});
- if (!res.ok && res.code === ErrorCodes.ShardNotFound) {
- // If the config server primary steps down right after removing the config.shards doc
- // for the shard but before responding with "state": "completed", the mongos would retry
- // the _configsvrRemoveShard command against the new config server primary, which would
- // not find the removed shard in its ShardRegistry if it has done a ShardRegistry reload
- // after the config.shards doc for the shard was removed. This would cause the command
- // to fail with ShardNotFound.
- return true;
- }
- assert.commandWorked(res, 'removeShard command failed');
- return res.state === 'completed';
-}, 'failed to remove the replica set shard');
+removeShard(st, 'repl');
res = mongos.adminCommand('listShards');
assert.commandWorked(res, 'listShards command failed');
@@ -67,5 +55,5 @@ assert(!checkShardName('repl', shardsArray),
'listShards command returned removed replica set shard: ' + tojson(shardsArray));
rs1.stopSet();
-shardTest.stop();
+st.stop();
})();
diff --git a/jstests/sharding/merge_with_drop_shard.js b/jstests/sharding/merge_with_drop_shard.js
index bc0b18715ce..41c5dafb105 100644
--- a/jstests/sharding/merge_with_drop_shard.js
+++ b/jstests/sharding/merge_with_drop_shard.js
@@ -4,6 +4,7 @@
'use strict';
load("jstests/aggregation/extras/merge_helpers.js"); // For withEachMergeMode.
+load('jstests/sharding/libs/remove_shard_util.js'); // For removeShard.
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -26,17 +27,10 @@ function setAggHang(mode) {
{configureFailPoint: "hangWhileBuildingDocumentSourceMergeBatch", mode: mode}));
}
-function removeShard(shard) {
+function removeShardAndRefreshRouter(shard) {
// We need the balancer to drain all the chunks out of the shard that is being removed.
assert.commandWorked(st.startBalancer());
- var res = st.s.adminCommand({removeShard: shard.shardName});
- assert.commandWorked(res);
- assert.eq('started', res.state);
- assert.soon(function() {
- res = st.s.adminCommand({removeShard: shard.shardName});
- assert.commandWorked(res);
- return ('completed' === res.state);
- }, "removeShard never completed for shard " + shard.shardName);
+ removeShard(st, shard.shardName);
// Drop the test database on the removed shard so it does not interfere with addShard later.
assert.commandWorked(shard.getDB(mongosDB.getName()).dropDatabase());
@@ -103,7 +97,7 @@ function runMergeWithMode(
});
if (dropShard) {
- removeShard(st.shard0);
+ removeShardAndRefreshRouter(st.shard0);
} else {
addShard(st.rs0.getURL());
}
diff --git a/jstests/sharding/move_chunk_remove_shard.js b/jstests/sharding/move_chunk_remove_shard.js
index 352d18c7109..6cafa8a4a3c 100644
--- a/jstests/sharding/move_chunk_remove_shard.js
+++ b/jstests/sharding/move_chunk_remove_shard.js
@@ -10,7 +10,8 @@
(function() {
"use strict";
-load('./jstests/libs/chunk_manipulation_util.js');
+load('jstests/libs/chunk_manipulation_util.js');
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -47,19 +48,7 @@ let joinMoveChunk = moveChunkParallel(staticMongod,
waitForMoveChunkStep(st.shard0, moveChunkStepNames.reachedSteadyState);
-assert.soon(function() {
- let res = assert.commandWorked(st.s.adminCommand({removeShard: st.shard1.shardName}));
- if (!res.ok && res.code === ErrorCodes.ShardNotFound) {
- // If the config server primary steps down right after removing the config.shards doc
- // for the shard but before responding with "state": "completed", the mongos would retry
- // the _configsvrRemoveShard command against the new config server primary, which would
- // not find the removed shard in its ShardRegistry if it has done a ShardRegistry reload
- // after the config.shards doc for the shard was removed. This would cause the command
- // to fail with ShardNotFound.
- return true;
- }
- return res.state == 'completed';
-});
+removeShard(st, st.shard1.shardName);
unpauseMoveChunkAtStep(st.shard0, moveChunkStepNames.reachedSteadyState);
diff --git a/jstests/sharding/move_jumbo_chunk.js b/jstests/sharding/move_jumbo_chunk.js
index b9239396132..9e3d75a9504 100644
--- a/jstests/sharding/move_jumbo_chunk.js
+++ b/jstests/sharding/move_jumbo_chunk.js
@@ -9,7 +9,8 @@
(function() {
'use strict';
-load("jstests/sharding/libs/find_chunks_util.js");
+load('jstests/sharding/libs/find_chunks_util.js');
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -145,22 +146,7 @@ st.startBalancer();
// Now remove the shard that the jumbo chunk is on and make sure the chunk moves back to the other
// shard.
-let res = st.s.adminCommand({removeShard: st.shard1.shardName});
-assert.commandWorked(res);
-assert.soon(function() {
- res = st.s.adminCommand({removeShard: st.shard1.shardName});
- if (!res.ok && res.code === ErrorCodes.ShardNotFound) {
- // If the config server primary steps down right after removing the config.shards doc
- // for the shard but before responding with "state": "completed", the mongos would retry
- // the _configsvrRemoveShard command against the new config server primary, which would
- // not find the removed shard in its ShardRegistry if it has done a ShardRegistry reload
- // after the config.shards doc for the shard was removed. This would cause the command
- // to fail with ShardNotFound.
- return true;
- }
- assert.commandWorked(res);
- return ("completed" == res.state);
-}, "failed to remove shard");
+removeShard(st, st.shard1.shardName);
let jumboChunk = findChunksUtil.findOneChunkByNs(
st.getDB('config'), 'test.foo', {min: {$lte: {x: 0}}, max: {$gt: {x: 0}}});
diff --git a/jstests/sharding/query/current_op_with_drop_shard.js b/jstests/sharding/query/current_op_with_drop_shard.js
index e654b27733e..f92945fe32a 100644
--- a/jstests/sharding/query/current_op_with_drop_shard.js
+++ b/jstests/sharding/query/current_op_with_drop_shard.js
@@ -1,6 +1,7 @@
// Tests that currentOp is resilient to drop shard.
(function() {
'use strict';
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -15,14 +16,7 @@ st.startBalancer();
const mongosDB = st.s.getDB(jsTestName());
const shardName = st.shard0.shardName;
-var res = st.s.adminCommand({removeShard: shardName});
-assert.commandWorked(res);
-assert.eq('started', res.state);
-assert.soon(function() {
- res = st.s.adminCommand({removeShard: shardName});
- assert.commandWorked(res);
- return ('completed' === res.state);
-}, "removeShard never completed for shard " + shardName);
+removeShard(st, shardName);
assert.commandWorked(mongosDB.currentOp());
diff --git a/jstests/sharding/remove2.js b/jstests/sharding/remove2.js
index 6b9597d05ad..d7a37ec780a 100644
--- a/jstests/sharding/remove2.js
+++ b/jstests/sharding/remove2.js
@@ -14,6 +14,7 @@ TestData.skipCheckingUUIDsConsistentAcrossCluster = true;
'use strict';
load("jstests/replsets/rslib.js");
load("jstests/sharding/libs/find_chunks_util.js");
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -73,7 +74,7 @@ function setupInitialData(st, coll) {
st.printShardingStatus();
}
-function removeShard(st, coll, replTest) {
+function removeShardAndCleanup(st, coll, replTest) {
jsTest.log("Moving chunk from shard1 to shard0");
assert.commandWorked(st.moveChunk(coll.getFullName(), {i: 6}, st.shard0.shardName));
@@ -85,15 +86,7 @@ function removeShard(st, coll, replTest) {
st.s0.getDB('config'), coll.getFullName(), {shard: st.shard1.shardName}));
jsTest.log("Removing shard with name: " + replTest.name);
- var res = st.s.adminCommand({removeShard: replTest.name});
- assert.commandWorked(res);
- assert.eq('started', res.state);
- assert.soon(function() {
- jsTest.log("Removing shard in assert soon: " + replTest.name);
- res = st.s.adminCommand({removeShard: replTest.name});
- assert.commandWorked(res);
- return ('completed' === res.state);
- }, "failed to remove shard: " + tojson(res));
+ removeShard(st, replTest.name);
// Drop the database so the shard can be re-added.
assert.commandWorked(replTest.getPrimary().getDB(coll.getDB().getName()).dropDatabase());
@@ -126,14 +119,14 @@ setupInitialData(st, coll);
jsTestLog("Test basic removal and re-addition of shard without shutting down.");
let rst1 = st.rs1;
-removeShard(st, coll, rst1);
+removeShardAndCleanup(st, coll, rst1);
addShard(st, coll, rst1);
jsTestLog("Test basic removal and re-addition of shard with shutting down the replica set.");
const originalSeed = seedString(rst1);
-removeShard(st, coll, rst1);
+removeShardAndCleanup(st, coll, rst1);
rst1.stopSet();
// Await that both mongos and rs0 remove RSM for removed replicaset
@@ -149,7 +142,7 @@ addShard(st, coll, rst1);
jsTestLog(
"Test removal and re-addition of shard with an identical replica set name and different port.");
-removeShard(st, coll, rst1);
+removeShardAndCleanup(st, coll, rst1);
rst1.stopSet();
// Await that both mongos and rs0 remove RSM for removed replicaset
@@ -172,7 +165,7 @@ assert.eq(1, st.s.getDB('test2').foo.find().itcount());
// Have to take out rst2 and put rst1 back into the set so that it can clean up.
jsTestLog("Resetting the sharding test to its initial state to allow the test to shut down.");
assert.commandWorked(st.admin.runCommand({movePrimary: 'test2', to: st.rs0.name}));
-removeShard(st, coll, rst2);
+removeShardAndCleanup(st, coll, rst2);
rst2.stopSet();
// Await that both mongos and rs0 remove RSM for removed replicaset
diff --git a/jstests/sharding/remove3.js b/jstests/sharding/remove3.js
index ad1f0cd22b6..b095503fde3 100644
--- a/jstests/sharding/remove3.js
+++ b/jstests/sharding/remove3.js
@@ -1,6 +1,7 @@
// Validates the remove/drain shard functionality when there is data on the shard being removed
(function() {
'use strict';
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -37,8 +38,7 @@ assert.commandWorked(st.s0.adminCommand(
{moveChunk: 'TestDB.Coll', find: {_id: 1}, to: st.shard0.shardName, _waitForDelete: true}));
// Remove shard must succeed now
-removeRes = assert.commandWorked(st.s0.adminCommand({removeShard: st.shard1.shardName}));
-assert.eq('completed', removeRes.state);
+removeShard(st, st.shard1.shardName);
// Make sure both mongos instance refresh their metadata and do not reference the missing shard
assert.eq(2, st.s0.getDB('TestDB').Coll.find({}).toArray().length);
diff --git a/jstests/sharding/remove_shard_near_doc_size_limit.js b/jstests/sharding/remove_shard_near_doc_size_limit.js
index f1c32c37f72..648d936ff34 100644
--- a/jstests/sharding/remove_shard_near_doc_size_limit.js
+++ b/jstests/sharding/remove_shard_near_doc_size_limit.js
@@ -12,6 +12,7 @@
(function() {
'use strict';
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -43,28 +44,6 @@ assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {x: 1}}));
assert.commandWorked(st.s.adminCommand({split: ns, middle: {x: 0}}));
assert.commandWorked(st.s.adminCommand({moveChunk: ns, find: {x: 1}, to: st.shard1.shardName}));
-function removeShardAndWait(shardName) {
- const removeShardCmd = {removeShard: shardName};
- const res = st.s.adminCommand(removeShardCmd);
-
- assert.commandWorked(res);
- assert(res.state === "started");
-
- assert.soon(function() {
- let res = st.s.adminCommand(removeShardCmd);
- if (res.state === "completed") {
- return true;
- } else {
- jsTest.log("Still waiting for shard removal to complete:");
- printjson(res);
- assert.commandWorked(st.s.adminCommand({clearJumboFlag: ns, find: {"x": 1}}));
- return false;
- }
- });
-
- jsTest.log("Shard removal complete.");
-}
-
function assertDocsExist(shardKeys, numDocs, payloadSize) {
shardKeys.forEach(key => {
for (let i = 0; i < numDocs; i++) {
@@ -94,7 +73,7 @@ assert.commandWorked(st.s.getDB("config").settings.update(
{_id: "balancer"}, {$set: {attemptToBalanceJumboChunks: true}}, true));
st.startBalancer();
-removeShardAndWait(st.shard1.shardName);
+removeShard(st, st.shard1.shardName);
assertDocsExist(shardKeys, numDocs, bigDocSize);
st.stop();
diff --git a/jstests/sharding/remove_shard_with_zones.js b/jstests/sharding/remove_shard_with_zones.js
index bb5ed329848..2e8d4ebed2f 100644
--- a/jstests/sharding/remove_shard_with_zones.js
+++ b/jstests/sharding/remove_shard_with_zones.js
@@ -4,6 +4,7 @@
*/
(function() {
'use strict';
+load('jstests/sharding/libs/remove_shard_util.js');
// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
@@ -24,7 +25,7 @@ assert.commandWorked(st.s.adminCommand({addShardToZone: st.shard1.shardName, zon
assert.commandWorked(st.s.adminCommand({addShardToZone: st.shard2.shardName, zone: "zoneC"}));
// Can remove the only shard for a zone if that zone does not have any chunk ranges.
-assert.commandWorked(st.s.adminCommand({removeShard: st.shard1.shardName}));
+removeShard(st, st.shard1.shardName);
// Cannot remove the only shard for a zone if that zone has chunk ranges associated with it.
assert.commandWorked(
@@ -34,7 +35,7 @@ assert.commandFailedWithCode(st.s.adminCommand({removeShard: st.shard2.shardName
// Can remove a shard is if it is not the only shard for any zone.
assert.commandWorked(st.s.adminCommand({addShardToZone: st.shard0.shardName, zone: "zoneC"}));
-assert.commandWorked(st.s.adminCommand({removeShard: st.shard2.shardName}));
+removeShard(st, st.shard2.shardName);
st.stop();
})();
diff --git a/jstests/sharding/set_cluster_parameter.js b/jstests/sharding/set_cluster_parameter.js
index 130be911d35..e2229f224fd 100644
--- a/jstests/sharding/set_cluster_parameter.js
+++ b/jstests/sharding/set_cluster_parameter.js
@@ -13,6 +13,7 @@
'use strict';
load('jstests/libs/fail_point_util.js');
+load('jstests/sharding/libs/remove_shard_util.js');
const clusterParameter1Value = {
intData: 42
@@ -185,10 +186,7 @@ const checkClusterParameters =
st.configRS.getPrimary(),
newShard.getPrimary());
- assert.soon(() => {
- let res = st.s.adminCommand({removeShard: newShardName});
- return res.state == 'completed';
- });
+ removeShard(st, newShardName);
newShard.stopSet();
@@ -290,11 +288,7 @@ const checkClusterParameters =
}));
// Well behaved test, remove shard and stop the set.
- assert.soon(() => {
- let res = assert.commandWorked(st2.s.adminCommand({removeShard: newShard3Name}));
-
- return 'completed' === res.state;
- });
+ removeShard(st2, newShard3Name);
newShard3.stopSet();
}
diff --git a/jstests/sharding/set_user_write_block_mode.js b/jstests/sharding/set_user_write_block_mode.js
index 9fa4979712c..21108e95ccb 100644
--- a/jstests/sharding/set_user_write_block_mode.js
+++ b/jstests/sharding/set_user_write_block_mode.js
@@ -13,22 +13,7 @@
load("jstests/libs/fail_point_util.js");
load('jstests/libs/parallel_shell_helpers.js');
-
-function removeShard(shardName) {
- assert.soon(function() {
- let res = assert.commandWorked(st.s.adminCommand({removeShard: shardName}));
- if (!res.ok && res.code === ErrorCodes.ShardNotFound) {
- // If the config server primary steps down right after removing the config.shards doc
- // for the shard but before responding with "state": "completed", the mongos would retry
- // the _configsvrRemoveShard command against the new config server primary, which would
- // not find the removed shard in its ShardRegistry if it has done a ShardRegistry reload
- // after the config.shards doc for the shard was removed. This would cause the command
- // to fail with ShardNotFound.
- return true;
- }
- return res.state == 'completed';
- });
-}
+load('jstests/sharding/libs/remove_shard_util.js');
const st = new ShardingTest({shards: 2});
@@ -74,7 +59,7 @@ newShard.initiate();
// shard.
assert.commandWorked(st.s.getDB(newShardDB).dropDatabase());
assert.commandWorked(st.s.adminCommand({setUserWriteBlockMode: 1, global: true}));
- removeShard(newShardName);
+ removeShard(st, newShardName);
// Disable write blocking while 'newShard' is not part of the cluster.
assert.commandWorked(st.s.adminCommand({setUserWriteBlockMode: 1, global: false}));
diff --git a/jstests/sharding/shard_removal_triggers_catalog_cache_invalidation.js b/jstests/sharding/shard_removal_triggers_catalog_cache_invalidation.js
index 5da1f92d3b8..3fe72624b03 100644
--- a/jstests/sharding/shard_removal_triggers_catalog_cache_invalidation.js
+++ b/jstests/sharding/shard_removal_triggers_catalog_cache_invalidation.js
@@ -4,6 +4,7 @@
*/
(function() {
'use strict';
+load('jstests/sharding/libs/remove_shard_util.js');
// Checking UUID consistency involves talking to shards, but this test shuts down shards.
TestData.skipCheckingUUIDsConsistentAcrossCluster = true;
@@ -63,19 +64,7 @@ const dbName = 'TestDB';
st.startBalancer();
// Remove shard0.
- assert.soon(() => {
- const removeRes = st.s0.adminCommand({removeShard: st.shard0.shardName});
- if (!removeRes.ok && removeRes.code === ErrorCodes.ShardNotFound) {
- // If the config server primary steps down right after removing the config.shards doc
- // for the shard but before responding with "state": "completed", the mongos would retry
- // the _configsvrRemoveShard command against the new config server primary, which would
- // not find the removed shard in its ShardRegistry if it has done a ShardRegistry reload
- // after the config.shards doc for the shard was removed. This would cause the command
- // to fail with ShardNotFound.
- return true;
- }
- return removeRes.state === 'completed';
- });
+ removeShard(st, st.shard0.shardName);
// Stop the replica set so that future requests to this shard will be unsuccessful.
st.rs0.stopSet();