Skip to content

Commit

Permalink
SERVER-12252 Fix mongod log output for write commands. Fix too big msg.
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Kangas <[email protected]>
  • Loading branch information
visualzhou authored and kangas committed Feb 14, 2014
1 parent 6dd40e5 commit a6867c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/mongo/db/commands/write_commands/write_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "mongo/db/commands/write_commands/write_commands.h"

#include "mongo/base/init.h"
#include "mongo/bson/mutable/document.h"
#include "mongo/bson/mutable/element.h"
#include "mongo/db/commands/write_commands/batch_executor.h"
#include "mongo/db/commands/write_commands/write_commands_common.h"
#include "mongo/db/curop.h"
Expand Down Expand Up @@ -60,6 +62,22 @@ namespace mongo {
Command( name ), _writeType( writeType ) {
}

void WriteCmd::redactTooLongLog( mutablebson::Document* cmdObj, const StringData& fieldName ) {
namespace mmb = mutablebson;
mmb::Element root = cmdObj->root();
mmb::Element field = root.findFirstChildNamed( fieldName );

// If the cmdObj is too large, it will be a "too big" message given by CachedBSONObj.get()
if ( !field.ok() ) {
return;
}

// Redact the log if there are more than one documents or operations.
if ( field.countChildren() > 1 ) {
field.setValueInt( field.countChildren() );
}
}

// Write commands are fanned out in oplog as single writes.
bool WriteCmd::logTheOp() { return false; }

Expand Down Expand Up @@ -143,6 +161,10 @@ namespace mongo {
WriteCmd( "insert", BatchedCommandRequest::BatchType_Insert ) {
}

void CmdInsert::redactForLogging( mutablebson::Document* cmdObj ) {
redactTooLongLog( cmdObj, StringData( "documents", StringData::LiteralTag() ) );
}

void CmdInsert::help( stringstream& help ) const {
help << "insert documents";
}
Expand All @@ -151,6 +173,10 @@ namespace mongo {
WriteCmd( "update", BatchedCommandRequest::BatchType_Update ) {
}

void CmdUpdate::redactForLogging( mutablebson::Document* cmdObj ) {
redactTooLongLog( cmdObj, StringData( "updates", StringData::LiteralTag() ) );
}

void CmdUpdate::help( stringstream& help ) const {
help << "update documents";
}
Expand All @@ -159,6 +185,10 @@ namespace mongo {
WriteCmd( "delete", BatchedCommandRequest::BatchType_Delete ) {
}

void CmdDelete::redactForLogging( mutablebson::Document* cmdObj ) {
redactTooLongLog( cmdObj, StringData( "deletes", StringData::LiteralTag() ) );
}

void CmdDelete::help( stringstream& help ) const {
help << "delete documents";
}
Expand Down
6 changes: 6 additions & 0 deletions src/mongo/db/commands/write_commands/write_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ namespace mongo {
*/
WriteCmd( const StringData& name, BatchedCommandRequest::BatchType writeType );

// Full log of write command can be quite large.
static void redactTooLongLog( mutablebson::Document* cmdObj, const StringData& fieldName );

private:
virtual bool logTheOp();

Expand Down Expand Up @@ -86,6 +89,7 @@ namespace mongo {
MONGO_DISALLOW_COPYING(CmdInsert);
public:
CmdInsert();
void redactForLogging(mutablebson::Document* cmdObj);

private:
virtual void help(stringstream& help) const;
Expand All @@ -95,6 +99,7 @@ namespace mongo {
MONGO_DISALLOW_COPYING(CmdUpdate);
public:
CmdUpdate();
void redactForLogging(mutablebson::Document* cmdObj);

private:
virtual void help(stringstream& help) const;
Expand All @@ -104,6 +109,7 @@ namespace mongo {
MONGO_DISALLOW_COPYING(CmdDelete);
public:
CmdDelete();
void redactForLogging(mutablebson::Document* cmdObj);

private:
virtual void help(stringstream& help) const;
Expand Down

0 comments on commit a6867c6

Please sign in to comment.