From 2775fa3e2b8a4a29abf9cc2480aa08033e491856 Mon Sep 17 00:00:00 2001 From: Mathias Stearn Date: Thu, 17 Mar 2011 18:22:03 -0400 Subject: [PATCH] Add shell helpers db.fsyncLock() and db.fsyncUnlock() and clean up messages SERVER-2789 --- db/dbcommands_admin.cpp | 2 +- db/instance.cpp | 7 +++++-- jstests/fsync.js | 12 +++++------- shell/db.js | 10 ++++++++++ shell/mongo_vstudio.cpp | 10 ++++++++++ 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/db/dbcommands_admin.cpp b/db/dbcommands_admin.cpp index c2ad5c32b7a..5ec4846a2b8 100644 --- a/db/dbcommands_admin.cpp +++ b/db/dbcommands_admin.cpp @@ -378,7 +378,7 @@ namespace mongo { } readlock lk(""); MemoryMappedFile::flushAll(true); - log() << "db is now locked for snapshotting, no writes allowed. use db.$cmd.sys.unlock.findOne() to unlock" << endl; + log() << "db is now locked for snapshotting, no writes allowed. use db.fsyncUnlock() to unlock" << endl; _ready = true; while( 1 ) { if( unlockRequested ) { diff --git a/db/instance.cpp b/db/instance.cpp index b648a38441e..d6f800c679c 100644 --- a/db/instance.cpp +++ b/db/instance.cpp @@ -111,7 +111,7 @@ namespace mongo { unsigned x = lockedForWriting; if( x ) { b.append("fsyncLock", x); - b.append("info", "use db.$cmd.sys.unlock.findOne() to terminate the fsync write/snapshot lock"); + b.append("info", "use db.fsyncUnlock() to terminate the fsync write/snapshot lock"); } } @@ -144,9 +144,12 @@ namespace mongo { void unlockFsync(const char *ns, Message& m, DbResponse &dbresponse) { BSONObj obj; - if( ! cc().isAdmin() || strncmp(ns, "admin.", 6) != 0 ) { + if ( ! cc().isAdmin() ) { // checks auth obj = fromjson("{\"err\":\"unauthorized\"}"); } + else if (strncmp(ns, "admin.", 6) != 0 ) { + obj = fromjson("{\"err\":\"unauthorized - this command must be run against the admin DB\"}"); + } else { if( lockedForWriting ) { log() << "command: unlock requested" << endl; diff --git a/jstests/fsync.js b/jstests/fsync.js index fccd623a2b3..bb31d986d9d 100644 --- a/jstests/fsync.js +++ b/jstests/fsync.js @@ -1,22 +1,20 @@ // test the lock/unlock snapshotting feature a bit -x=db.runCommand({fsync:1,lock:1}); +x=db.runCommand({fsync:1,lock:1}); // not on admin db assert(!x.ok,"D"); -d=db.getSisterDB("admin"); - -x=d.runCommand({fsync:1,lock:1}); +x=db.fsyncLock(); // uses admin automatically assert(x.ok,"C"); -y = d.currentOp(); +y = db.currentOp(); assert(y.fsyncLock,"B"); -z = d.$cmd.sys.unlock.findOne(); +z = db.fsyncUnlock(); // it will take some time to unlock, and unlock does not block and wait for that // doing a write will make us wait until db is writeable. db.jstests_fsync.insert({x:1}); -assert( d.currentOp().fsyncLock == null, "A" ); +assert( db.currentOp().fsyncLock == null, "A" ); diff --git a/shell/db.js b/shell/db.js index f5d2e0cb1df..49a7606fd4b 100644 --- a/shell/db.js +++ b/shell/db.js @@ -312,6 +312,8 @@ DB.prototype.help = function() { print("\tdb.stats()"); print("\tdb.version() current version of the server"); print("\tdb.getMongo().setSlaveOk() allow queries on a replication slave server"); + print("\tdb.fsyncLock() flush data to disk and lock server for backups"); + print("\tdb.fsyncUnock() unlocks server following a db.fsyncLock()"); return __magicNoPrint; } @@ -778,6 +780,14 @@ DB.prototype.printShardingStatus = function( verbose ){ printShardingStatus( this.getSiblingDB( "config" ) , verbose ); } +DB.prototype.fsyncLock = function() { + return db.adminCommand({fsync:1, lock:true}); +} + +DB.prototype.fsyncUnlock = function() { + return db.getSiblingDB("admin").$cmd.sys.unlock.findOne() +} + DB.autocomplete = function(obj){ var colls = obj.getCollectionNames(); var ret=[]; diff --git a/shell/mongo_vstudio.cpp b/shell/mongo_vstudio.cpp index 88fea1a4023..4c9543aa5c0 100644 --- a/shell/mongo_vstudio.cpp +++ b/shell/mongo_vstudio.cpp @@ -1796,6 +1796,8 @@ const StringData _jscode_raw_db = "print(\"\\tdb.stats()\");\n" "print(\"\\tdb.version() current version of the server\");\n" "print(\"\\tdb.getMongo().setSlaveOk() allow queries on a replication slave server\");\n" +"print(\"\\tdb.fsyncLock() flush data to disk and lock server for backups\");\n" +"print(\"\\tdb.fsyncUnock() unlocks server following a db.fsyncLock()\");\n" "\n" "return __magicNoPrint;\n" "}\n" @@ -2262,6 +2264,14 @@ const StringData _jscode_raw_db = "printShardingStatus( this.getSiblingDB( \"config\" ) , verbose );\n" "}\n" "\n" +"DB.prototype.fsyncLock = function() {\n" +"return db.adminCommand({fsync:1, lock:true});\n" +"}\n" +"\n" +"DB.prototype.fsyncUnlock = function() {\n" +"return db.getSiblingDB(\"admin\").$cmd.sys.unlock.findOne()\n" +"}\n" +"\n" "DB.autocomplete = function(obj){\n" "var colls = obj.getCollectionNames();\n" "var ret=[];\n"