From 1a95c75d0ffa5dfb865ac40f8fafecd2732bc4ca Mon Sep 17 00:00:00 2001 From: Dwight Date: Fri, 5 Dec 2008 10:09:16 -0500 Subject: don't log to our oplog operations we are replicating from elsewhere --- db/cloner.cpp | 19 ++++++++++--------- db/commands.cpp | 2 +- db/commands.h | 6 +++++- db/dbcommands.cpp | 37 +++++++++++++++++++------------------ db/dbeval.cpp | 2 +- db/query.cpp | 8 ++++---- db/repl.cpp | 8 ++++---- dbgrid/dbgrid_commands.cpp | 6 +++--- 8 files changed, 47 insertions(+), 41 deletions(-) diff --git a/db/cloner.cpp b/db/cloner.cpp index f52e4e25aca..c63e116152b 100644 --- a/db/cloner.cpp +++ b/db/cloner.cpp @@ -30,7 +30,7 @@ extern int port; class Cloner: boost::noncopyable { DBClientConnection conn; - void copy(const char *from_ns, const char *to_ns, bool isindex = false); + void copy(const char *from_ns, const char *to_ns, bool isindex, bool logForRepl); public: Cloner() { } bool go(const char *masterHost, string& errmsg, const string& fromdb, bool logForRepl); @@ -73,7 +73,7 @@ BSONObj fixindex(BSONObj o) { /* copy the specified collection isindex - if true, this is system.indexes collection. */ -void Cloner::copy(const char *from_collection, const char *to_collection, bool isindex) { +void Cloner::copy(const char *from_collection, const char *to_collection, bool isindex, bool logForRepl) { auto_ptr c; { dbtemprelease r; @@ -101,7 +101,8 @@ void Cloner::copy(const char *from_collection, const char *to_collection, bool i } theDataFileMgr.insert(to_collection, (void*) js.objdata(), js.objsize()); - logOp("i", to_collection, js); + if( logForRepl ) + logOp("i", to_collection, js); } } @@ -171,13 +172,13 @@ bool Cloner::go(const char *masterHost, string& errmsg, const string& fromdb, bo string err; userCreateNS(to_name.c_str(), options, err, logForRepl); } - copy(from_name, to_name.c_str()); + copy(from_name, to_name.c_str(), false, logForRepl); } // now build the indexes string system_indexes_from = fromdb + ".system.indexes"; string system_indexes_to = todb + ".system.indexes"; - copy(system_indexes_from.c_str(), system_indexes_to.c_str(), true); + copy(system_indexes_from.c_str(), system_indexes_to.c_str(), true, logForRepl); return true; } @@ -195,14 +196,14 @@ class CmdClone : public Command { public: CmdClone() : Command("clone") { } - virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) { string from = cmdObj.getStringField("clone"); if( from.empty() ) return false; /* replication note: we must logOp() not the command, but the cloned data -- if the slave were to clone it would get a different point-in-time and not match. */ - return cloneFrom(from.c_str(), errmsg, database->name, true); + return cloneFrom(from.c_str(), errmsg, database->name, /*logForReplication=*/!fromRepl); } } cmdclone; @@ -214,7 +215,7 @@ public: CmdCopyDb() : Command("copydb") { } virtual bool adminOnly() { return true; } - virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) { string fromhost = cmdObj.getStringField("fromhost"); if( fromhost.empty() ) { /* copy from self */ @@ -229,7 +230,7 @@ public: return false; } setClient(todb.c_str()); - bool res = cloneFrom(fromhost.c_str(), errmsg, fromdb, true); + bool res = cloneFrom(fromhost.c_str(), errmsg, fromdb, /*logForReplication=*/!fromRepl); database = 0; return res; } diff --git a/db/commands.cpp b/db/commands.cpp index 8b4840ddc6b..73838b7ea50 100644 --- a/db/commands.cpp +++ b/db/commands.cpp @@ -57,7 +57,7 @@ bool runCommandAgainstRegistered(const char *ns, BSONObj& jsobj, BSONObjBuilder& errmsg = "access denied"; } else { - ok = c->run(ns, jsobj, errmsg, anObjBuilder); + ok = c->run(ns, jsobj, errmsg, anObjBuilder, false); } if( !ok ) anObjBuilder.append("errmsg", errmsg); diff --git a/db/commands.h b/db/commands.h index 88bf552fb48..761af145721 100644 --- a/db/commands.h +++ b/db/commands.h @@ -28,9 +28,13 @@ public: /* run the given command implement this... + + fromRepl - command is being invoked as part of replication syncing. In this situation you + normally do not want to log the command to the local oplog. + return value is true if succeeded. if false, set errmsg text. */ - virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) = 0; + virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) = 0; /* return true if only the admin ns has privileges to run this command. */ virtual bool adminOnly() { return false; } diff --git a/db/dbcommands.cpp b/db/dbcommands.cpp index d9d76b3689c..17f668441f4 100644 --- a/db/dbcommands.cpp +++ b/db/dbcommands.cpp @@ -169,7 +169,7 @@ string validateNS(const char *ns, NamespaceDetails *d) { class CmdGetOpTime : public Command { public: CmdGetOpTime() : Command("getoptime") { } - bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) { result.appendDate("optime", OpTime::now().asDate()); return true; } @@ -190,7 +190,7 @@ class CmdOpLogging : public Command { public: CmdOpLogging() : Command("opLogging") { } bool adminOnly() { return true; } - bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) { opLogging = (int) cmdObj.findElement("opLogging").number(); flushOpLog(); log() << "CMD: opLogging set to " << opLogging << endl; @@ -202,7 +202,7 @@ class CmdQueryTraceLevel : public Command { public: CmdQueryTraceLevel() : Command("queryTraceLevel") { } bool adminOnly() { return true; } - bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) { queryTraceLevel = (int) cmdObj.findElement(name.c_str()).number(); return true; } @@ -212,7 +212,7 @@ class Cmd : public Command { public: Cmd() : Command("traceAll") { } bool adminOnly() { return true; } - bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) { queryTraceLevel = otherTraceLevel = (int) cmdObj.findElement(name.c_str()).number(); return true; } @@ -220,14 +220,14 @@ public: extern map *commands; -// TODO make these all command objects -- legacy stuff here -// -// e.g. -// system.cmd$.find( { queryTraceLevel: 2 } ); -// -// returns true if ran a cmd -// -bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder &b, BSONObjBuilder& anObjBuilder) { +/* TODO make these all command objects -- legacy stuff here + + usage: + abc.$cmd.findOne( { ismaster:1 } ); + + returns true if ran a cmd +*/ +bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder &b, BSONObjBuilder& anObjBuilder, bool fromRepl) { const char *p = strchr(ns, '.'); if( !p ) return false; @@ -236,8 +236,6 @@ bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder & bool ok = false; bool valid = false; - //cout << jsobj.toString() << endl; - BSONElement e; e = jsobj.firstElement(); @@ -257,7 +255,7 @@ bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder & errmsg = "access denied"; } else { - ok = c->run(ns, jsobj, errmsg, anObjBuilder); + ok = c->run(ns, jsobj, errmsg, anObjBuilder, fromRepl); } if( !ok ) anObjBuilder.append("errmsg", errmsg); @@ -272,7 +270,8 @@ bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder & ok = false; } else { dropDatabase(ns); - logOp("c", ns, jsobj); + if( !fromRepl ) + logOp("c", ns, jsobj); ok = true; } } @@ -353,7 +352,8 @@ bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder & anObjBuilder.append("ns", nsToDrop.c_str()); ClientCursor::invalidate(nsToDrop.c_str()); dropNS(nsToDrop); - logOp("c", ns, jsobj); + if( !fromRepl ) + logOp("c", ns, jsobj); /* { BSONObjBuilder b; @@ -393,7 +393,8 @@ bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder & d->aboutToDeleteAnIndex(); ClientCursor::invalidate(toDeleteNs.c_str()); - logOp("c", ns, jsobj); + if( !fromRepl ) + logOp("c", ns, jsobj); // delete a specific index or all? if( f.type() == String ) { diff --git a/db/dbeval.cpp b/db/dbeval.cpp index 7b4f8663bdf..4391d54570d 100644 --- a/db/dbeval.cpp +++ b/db/dbeval.cpp @@ -97,7 +97,7 @@ bool dbEval(const char *ns, BSONObj& cmd, BSONObjBuilder& result, string& errmsg class CmdEval : public Command { public: CmdEval() : Command("$eval") { } - bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) { return dbEval(ns, cmdObj, result, errmsg); } } cmdeval; diff --git a/db/query.cpp b/db/query.cpp index a51775efbb1..e2009e6c8a1 100644 --- a/db/query.cpp +++ b/db/query.cpp @@ -492,11 +492,11 @@ int otherTraceLevel = 0; int initialExtentSize(int len); -bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder &b, BSONObjBuilder& anObjBuilder); +bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder &b, BSONObjBuilder& anObjBuilder, bool fromRepl); -bool runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder &b, BSONObjBuilder& anObjBuilder) { +bool runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder &b, BSONObjBuilder& anObjBuilder, bool fromRepl) { try { - return _runCommands(ns, jsobj, ss, b, anObjBuilder); + return _runCommands(ns, jsobj, ss, b, anObjBuilder, fromRepl); } catch( AssertionException e ) { if( !e.msg.empty() ) @@ -634,7 +634,7 @@ QueryResult* runQuery(Message& message, const char *ns, int ntoskip, int _ntoret b.skip(sizeof(QueryResult)); /* we assume you are using findOne() for running a cmd... */ - if( ntoreturn == 1 && runCommands(ns, jsobj, ss, b, cmdResBuf) ) { + if( ntoreturn == 1 && runCommands(ns, jsobj, ss, b, cmdResBuf, false) ) { n = 1; } else { diff --git a/db/repl.cpp b/db/repl.cpp index 0ae93f6ab45..5546f861840 100644 --- a/db/repl.cpp +++ b/db/repl.cpp @@ -38,7 +38,7 @@ extern boost::mutex dbMutex; auto_ptr findTableScan(const char *ns, BSONObj& order, bool *isSorted=0); int _updateObjects(const char *ns, BSONObj updateobj, BSONObj pattern, bool upsert, stringstream& ss, bool logOp=false); -bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder &b, BSONObjBuilder& anObjBuilder); +bool _runCommands(const char *ns, BSONObj& jsobj, stringstream& ss, BufBuilder &b, BSONObjBuilder& anObjBuilder, bool fromRepl); void ensureHaveIdIndex(const char *ns); /* "dead" means something really bad happened like replication falling completely out of sync. @@ -96,7 +96,7 @@ class CmdIsMaster : public Command { public: CmdIsMaster() : Command("ismaster") { } - virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) { if( allDead ) { result.append("ismaster", 0.0); if( replPair ) @@ -142,7 +142,7 @@ public: virtual bool adminOnly() { return true; } - virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) { if( replPair == 0 ) { problem() << "got negotiatemaster cmd but we are not in paired mode." << endl; errmsg = "not paired"; @@ -526,7 +526,7 @@ void ReplSource::sync_pullOpLog_applyOperation(BSONObj& op) { BufBuilder bb; BSONObjBuilder ob; assert( *opType == 'c' ); - _runCommands(ns, o, ss, bb, ob); + _runCommands(ns, o, ss, bb, ob, true); } } catch( UserAssertionException& e ) { diff --git a/dbgrid/dbgrid_commands.cpp b/dbgrid/dbgrid_commands.cpp index f3b34994cd9..684af43354f 100644 --- a/dbgrid/dbgrid_commands.cpp +++ b/dbgrid/dbgrid_commands.cpp @@ -43,7 +43,7 @@ namespace dbgrid_cmds { class NetStatCmd : public Command { public: NetStatCmd() : Command("netstat") { } - bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) { result.append("griddb", gridDatabase.toString()); result.append("isdbgrid", 1); return true; @@ -53,7 +53,7 @@ public: class IsDbGridCmd : public Command { public: IsDbGridCmd() : Command("isdbgrid") { } - bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) { result.append("isdbgrid", 1); result.append("hostname", ourHostname); return true; @@ -64,7 +64,7 @@ class CmdIsMaster : public Command { public: CmdIsMaster() : Command("ismaster") { } - virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result) { + virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) { result.append("ismaster", 0.0); result.append("msg", "isdbgrid"); return true; -- cgit v1.2.1