summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSpencer T Brody <spencer@mongodb.com>2015-07-07 16:24:27 -0400
committerSpencer T Brody <spencer@mongodb.com>2015-07-08 11:09:45 -0400
commitf11138ed8f262e577bca330a655d39287a64d7e5 (patch)
tree01450cf7c1d2c7cc0ef1f30c695bc5fdd8d08aad
parentdd3c12c2e331cd423319278e751ba38b7c5437ef (diff)
downloadmongo-f11138ed8f262e577bca330a655d39287a64d7e5.tar.gz
SERVER-19297 Make shardCollection take distributed lock
-rw-r--r--jstests/sharding/sync_cluster_config/sync7.js12
-rw-r--r--src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp7
2 files changed, 13 insertions, 6 deletions
diff --git a/jstests/sharding/sync_cluster_config/sync7.js b/jstests/sharding/sync_cluster_config/sync7.js
index 4ba0812bbab..65ac5b48d1b 100644
--- a/jstests/sharding/sync_cluster_config/sync7.js
+++ b/jstests/sharding/sync_cluster_config/sync7.js
@@ -2,6 +2,12 @@
s = new ShardingTest( "moveDistLock", 3, 0, undefined, { sync : true } );
+// Enable sharding on DB and collection before skewing the clocks
+result = s.getDB("admin").runCommand( { enablesharding : "test1" } );
+result = s.getDB("test1").foo.ensureIndex( { a : 1 } );
+result = s.getDB("admin").runCommand( { shardcollection : "test1.foo", key : { a : 1 } } );
+print(" Collection Sharded! ")
+
s._configServers[0].getDB( "admin" ).runCommand( { _skewClockCommand : 1, skew : 15000 } )
s._configServers[1].getDB( "admin" ).runCommand( { _skewClockCommand : 1, skew : -32000 } )
@@ -32,12 +38,6 @@ printjson(result);
s.printShardingStatus();
assert.eq( result.ok, 0, "Move command should not have succeeded!" )
-// Enable sharding on DB and collection
-result = otherMongos.getDB("admin").runCommand( { enablesharding : "test1" } );
-result = otherMongos.getDB("test1").foo.ensureIndex( { a : 1 } );
-result = otherMongos.getDB("admin").runCommand( { shardcollection : "test1.foo", key : { a : 1 } } );
-print(" Collection Sharded! ")
-
// Make sure we can't split when our clock skew is so high
result = otherMongos.getDB( "admin" ).runCommand( { split : "test1.foo", find : { a : 2 } } );
printjson(result);
diff --git a/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp b/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp
index 57b02260411..dbf282006bd 100644
--- a/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp
+++ b/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp
@@ -322,6 +322,13 @@ Status CatalogManagerLegacy::shardCollection(OperationContext* txn,
bool unique,
vector<BSONObj>* initPoints,
set<ShardId>* initShardIds) {
+ // Lock the collection globally so that no other mongos can try to shard or drop the collection
+ // at the same time.
+ auto scopedDistLock = getDistLockManager()->lock(ns, "shardCollection");
+ if (!scopedDistLock.isOK()) {
+ return scopedDistLock.getStatus();
+ }
+
StatusWith<DatabaseType> status = getDatabase(nsToDatabase(ns));
if (!status.isOK()) {
return status.getStatus();