summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mongo/db/database.cpp15
-rw-r--r--src/mongo/db/introspect.cpp32
-rw-r--r--src/mongo/db/introspect.h9
3 files changed, 40 insertions, 16 deletions
diff --git a/src/mongo/db/database.cpp b/src/mongo/db/database.cpp
index 480f0f38f68..bb7f5c7fcbf 100644
--- a/src/mongo/db/database.cpp
+++ b/src/mongo/db/database.cpp
@@ -20,6 +20,7 @@
#include "pdfile.h"
#include "database.h"
#include "instance.h"
+#include "introspect.h"
#include "clientcursor.h"
#include "databaseholder.h"
@@ -91,7 +92,7 @@ namespace mongo {
checkDuplicateUncasedNames(true);
// If already exists, open. Otherwise behave as if empty until
// there's a write, then open.
- if ( ! newDb || cmdLine.defaultProfile ) {
+ if (!newDb) {
namespaceIndex.init();
if( _openAllFiles )
openAllFiles();
@@ -378,15 +379,9 @@ namespace mongo {
verify( cc().database() == this );
- if ( ! namespaceIndex.details( profileName.c_str() ) ) {
- log() << "creating profile collection: " << profileName << endl;
- BSONObjBuilder spec;
- spec.appendBool( "capped", true );
- spec.append( "size", 1024*1024 );
- if ( ! userCreateNS( profileName.c_str(), spec.done(), errmsg , false /* we don't replica profile messages */ ) ) {
- return false;
- }
- }
+ if (!getOrCreateProfileCollection(this, true))
+ return false;
+
profile = newLevel;
return true;
}
diff --git a/src/mongo/db/introspect.cpp b/src/mongo/db/introspect.cpp
index e5db13bf384..c6c811c4f0b 100644
--- a/src/mongo/db/introspect.cpp
+++ b/src/mongo/db/introspect.cpp
@@ -70,19 +70,39 @@ namespace mongo {
}
// write: not replicated
- NamespaceDetails *d = db->namespaceIndex.details(ns);
- if( d ) {
+ // get or create the profiling collection
+ NamespaceDetails *details = getOrCreateProfileCollection(db);
+ if (details) {
int len = p.objsize();
- Record *r = theDataFileMgr.fast_oplog_insert(d, ns, len);
+ Record *r = theDataFileMgr.fast_oplog_insert(details, ns, len);
memcpy(getDur().writingPtr(r->data(), len), p.objdata(), len);
}
- else {
- static time_t last;
+ }
+
+ NamespaceDetails* getOrCreateProfileCollection(Database *db, bool force) {
+ fassert(16363, db);
+ const char* profileName = db->profileName.c_str();
+ NamespaceDetails* details = db->namespaceIndex.details(profileName);
+ if (!details && (cmdLine.defaultProfile || force)) {
+ // system.profile namespace doesn't exist; create it
+ log() << "creating profile collection: " << profileName << endl;
+ string errmsg;
+ if (!userCreateNS(db->profileName.c_str(),
+ BSON("capped" << true << "size" << 1024 * 1024), errmsg , false)) {
+ log() << "could not create ns " << db->profileName << ": " << errmsg << endl;
+ return NULL;
+ }
+ details = db->namespaceIndex.details(profileName);
+ }
+ if (!details) {
+ // failed to get or create profile collection
+ static time_t last = time(0) - 10; // warn the first time
if( time(0) > last+10 ) {
- log() << "profile: warning ns " << ns << " does not exist" << endl;
+ log() << "profile: warning ns " << profileName << " does not exist" << endl;
last = time(0);
}
}
+ return details;
}
} // namespace mongo
diff --git a/src/mongo/db/introspect.h b/src/mongo/db/introspect.h
index 209eeacab7c..c0dc425c692 100644
--- a/src/mongo/db/introspect.h
+++ b/src/mongo/db/introspect.h
@@ -31,4 +31,13 @@ namespace mongo {
void profile( const Client& c , CurOp& currentOp );
+ /**
+ * Get (or create) the profile collection
+ *
+ * @param db Database in which to create the profile collection
+ * @param force Always create the collection if it does not exist
+ * @return NamespaceDetails for the newly created collection, or NULL on error
+ **/
+ NamespaceDetails* getOrCreateProfileCollection(Database *db, bool force = false);
+
} // namespace mongo