Skip to content

Commit

Permalink
fix memory leak in ParallelCursor on config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Jul 29, 2010
1 parent fbd6ebc commit 0aecc0b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
12 changes: 10 additions & 2 deletions client/parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace mongo {
_batchSize = 2;

_done = false;
_didInit = false;
}

ClusteredCursor::ClusteredCursor( const string& ns , const BSONObj& q , int options , const BSONObj& fields ){
Expand All @@ -48,14 +49,23 @@ namespace mongo {
_batchSize = 0;

_done = false;
_didInit = false;
}

ClusteredCursor::~ClusteredCursor(){
_done = true; // just in case
}

void ClusteredCursor::init(){
if ( _didInit )
return;
_didInit = true;
_init();
}

auto_ptr<DBClientCursor> ClusteredCursor::query( const string& server , int num , BSONObj extra , int skipLeft ){
uassert( 10017 , "cursor already done" , ! _done );
assert( _didInit );

BSONObj q = _query;
if ( ! extra.isEmpty() ){
Expand Down Expand Up @@ -301,7 +311,6 @@ namespace mongo {
: ClusteredCursor( q ) , _servers( servers ){
_sortKey = sortKey.getOwned();
_needToSkip = q.ntoskip;
_init();
}

ParallelSortClusteredCursor::ParallelSortClusteredCursor( const set<ServerAndQuery>& servers , const string& ns ,
Expand All @@ -310,7 +319,6 @@ namespace mongo {
: ClusteredCursor( ns , q.obj , options , fields ) , _servers( servers ){
_sortKey = q.getSort().copy();
_needToSkip = 0;
_init();
}

void ParallelSortClusteredCursor::_init(){
Expand Down
14 changes: 12 additions & 2 deletions client/parallel.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ namespace mongo {
ClusteredCursor( QueryMessage& q );
ClusteredCursor( const string& ns , const BSONObj& q , int options=0 , const BSONObj& fields=BSONObj() );
virtual ~ClusteredCursor();


/** call before using */
void init();

virtual bool more() = 0;
virtual BSONObj next() = 0;

Expand All @@ -82,6 +85,9 @@ namespace mongo {
virtual BSONObj explain();

protected:

virtual void _init() = 0;

auto_ptr<DBClientCursor> query( const string& server , int num = 0 , BSONObj extraFilter = BSONObj() , int skipLeft = 0 );
BSONObj explain( const string& server , BSONObj extraFilter = BSONObj() );

Expand All @@ -95,6 +101,8 @@ namespace mongo {
BSONObj _fields;
int _batchSize;

bool _didInit;

bool _done;
};

Expand Down Expand Up @@ -190,6 +198,8 @@ namespace mongo {
private:
virtual void _explain( map< string,list<BSONObj> >& out );

void _init(){}

vector<ServerAndQuery> _servers;
unsigned _serverIndex;

Expand All @@ -212,7 +222,7 @@ namespace mongo {
virtual bool more();
virtual BSONObj next();
virtual string type() const { return "ParallelSort"; }
private:
protected:
void _init();

virtual void _explain( map< string,list<BSONObj> >& out );
Expand Down
2 changes: 1 addition & 1 deletion db/mr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ namespace mongo {

ParallelSortClusteredCursor cursor( servers , dbname + "." + shardedOutputCollection ,
Query().sort( sortKey ) );

cursor.init();

auto_ptr<Scope> s = globalScriptEngine->getPooledScope( dbname );
s->localConnect( dbname.c_str() );
Expand Down
3 changes: 2 additions & 1 deletion s/strategy_shard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ namespace mongo {
}

assert( cursor );

cursor->init();

try {
log(5) << " cursor type: " << cursor->type() << endl;
shardedCursorTypes.hit( cursor->type() );
Expand Down

0 comments on commit 0aecc0b

Please sign in to comment.