summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Stearn <mathias@10gen.com>2015-09-28 15:03:14 -0400
committerMathias Stearn <redbeard0531@gmail.com>2015-09-30 15:47:23 -0400
commit59ae30a629db3ecb675037dc63e06cd4fcae4aaa (patch)
tree0c215ab3bab094c1e1383762091b912d4e53cd14
parentea2cc1388cf707512a04f4437def3aedd78c7211 (diff)
downloadmongo-59ae30a629db3ecb675037dc63e06cd4fcae4aaa.tar.gz
SERVER-20638 Reading profiling level shouldn't create databases that don't exist
Setting the profiling level will still create the database. (cherry picked from commit e5883822fd2fc777fd3e3e6b766f54e2caa8e594)
-rw-r--r--jstests/core/profile1.js4
-rw-r--r--jstests/core/profile_no_such_db.js38
-rw-r--r--jstests/slow2/sharding_jscore_passthrough.js4
-rw-r--r--src/mongo/db/dbcommands.cpp17
-rw-r--r--src/mongo/shell/db.js4
5 files changed, 54 insertions, 13 deletions
diff --git a/jstests/core/profile1.js b/jstests/core/profile1.js
index f3e13324b10..39c525eeedc 100644
--- a/jstests/core/profile1.js
+++ b/jstests/core/profile1.js
@@ -56,11 +56,11 @@ try {
// create a msg for later if there is a failure.
var msg = "";
- profileItems.forEach(function(d) {msg += "profile doc: " + d.ns + " " + d.op + " " + tojson(d.query ? d.query : d.command)});
+ profileItems.forEach(function(d) {msg += "profile doc: " + d.ns + " " + d.op + " " + tojson(d.query ? d.query : d.command) + "\n"});
msg += tojson(db.system.profile.stats());
// If these nunmbers don't match, it is possible the collection has rolled over (set to 32MB above in the hope this doesn't happen)
- assert.eq( 3, profileItems.length , "E2 -- " + msg );
+ assert.eq( 2, profileItems.length , "E2 -- " + msg );
/* Make sure we can't drop if profiling is still on */
assert.throws( function(z){ db.getCollection("system.profile").drop(); } )
diff --git a/jstests/core/profile_no_such_db.js b/jstests/core/profile_no_such_db.js
new file mode 100644
index 00000000000..e11d93ca66c
--- /dev/null
+++ b/jstests/core/profile_no_such_db.js
@@ -0,0 +1,38 @@
+// Test that reading the profiling level doesn't create databases, but setting it does.
+(function (db) {
+'use strict';
+
+function dbExists() {
+ return Array.contains(db.getMongo().getDBNames(), db.getName());
+}
+
+db = db.getSiblingDB('profile_no_such_db'); // Note: changes db argument not global var.
+assert.commandWorked(db.dropDatabase());
+assert(!dbExists());
+
+// Reading the profiling level shouldn't create the database.
+var defaultProfilingLevel = db.getProfilingLevel();
+assert(!dbExists());
+
+// This test assumes that the default profiling level hasn't been changed.
+assert.eq(defaultProfilingLevel, 0);
+
+[0,1,2].forEach(function(level) {
+ jsTest.log('Testing profiling level ' + level);
+
+ // Setting the profiling level creates the database.
+ // Note: in storage engines other than MMAPv1 setting the profiling level to 0 puts the database
+ // in a weird state where it exists internally, but doesn't show up in listDatabases, and won't
+ // exist if you restart the server.
+ var res = db.setProfilingLevel(level);
+ assert.eq(res.was, defaultProfilingLevel);
+ assert(dbExists() || level == 0);
+ assert.eq(db.getProfilingLevel(), level);
+
+ // Dropping the db reverts the profiling level to the default.
+ assert.commandWorked(db.dropDatabase());
+ assert.eq(db.getProfilingLevel(), defaultProfilingLevel);
+ assert(!dbExists());
+});
+
+}(db));
diff --git a/jstests/slow2/sharding_jscore_passthrough.js b/jstests/slow2/sharding_jscore_passthrough.js
index be1580b7a9a..404fbd829dc 100644
--- a/jstests/slow2/sharding_jscore_passthrough.js
+++ b/jstests/slow2/sharding_jscore_passthrough.js
@@ -69,7 +69,7 @@ files.forEach(function(x) {
'apitest_db|' +
'cursor6|' +
'copydb-auth|' +
- 'profile\\d*|' +
+ 'profile.*|' +
'dbhash|' +
'dbhash2|' +
'median|' +
@@ -91,7 +91,7 @@ files.forEach(function(x) {
'shellkillop|' +
'update4|' +
'update_setOnInsert|' +
- 'profile\\d*|' +
+ 'profile.*|' +
'max_time_ms|' + // Will be fixed when SERVER-2212 is resolved.
'fts_querylang|' + // Will be fixed when SERVER-9063 is resolved.
'fts_projection' +
diff --git a/src/mongo/db/dbcommands.cpp b/src/mongo/db/dbcommands.cpp
index d9749c57b7c..d59ddbfc132 100644
--- a/src/mongo/db/dbcommands.cpp
+++ b/src/mongo/db/dbcommands.cpp
@@ -342,20 +342,23 @@ public:
// Needs to be locked exclusively, because creates the system.profile collection
// in the local database.
ScopedTransaction transaction(txn, MODE_IX);
- Lock::DBLock dbXLock(txn->lockState(), dbname, MODE_X);
- Client::Context ctx(txn, dbname);
+ AutoGetDb ctx(txn, dbname, MODE_X);
+ Database* db = ctx.getDb();
BSONElement e = cmdObj.firstElement();
- result.append("was", ctx.db()->getProfilingLevel());
+ result.append("was", db ? db->getProfilingLevel() : serverGlobalParams.defaultProfile);
result.append("slowms", serverGlobalParams.slowMS);
int p = (int)e.number();
Status status = Status::OK();
- if (p == -1)
- status = Status::OK();
- else if (p >= 0 && p <= 2) {
- status = ctx.db()->setProfilingLevel(txn, p);
+ if (p >= 0 && p <= 2) {
+ if (!db) {
+ // When setting the profiling level, create the database if it didn't already exist.
+ // When just reading the profiling level, we do not create the database.
+ db = dbHolder().openDb(txn, dbname);
+ }
+ status = db->setProfilingLevel(txn, p);
}
const BSONElement slow = cmdObj["slowms"];
diff --git a/src/mongo/shell/db.js b/src/mongo/shell/db.js
index 2b5fec4b89d..cbe996d44d1 100644
--- a/src/mongo/shell/db.js
+++ b/src/mongo/shell/db.js
@@ -154,7 +154,7 @@ DB.prototype.createCollection = function(name, opt) {
* @return SOMETHING_FIXME or null on error
*/
DB.prototype.getProfilingLevel = function() {
- var res = this._dbCommand( { profile: -1 } );
+ var res = assert.commandWorked(this._dbCommand( { profile: -1 } ));
return res ? res.was : null;
}
@@ -426,7 +426,7 @@ DB.prototype.setProfilingLevel = function(level,slowms) {
var cmd = { profile: level };
if ( isNumber( slowms ) )
cmd["slowms"] = slowms;
- return this._dbCommand( cmd );
+ return assert.commandWorked(this._dbCommand( cmd ));
}
/**