Skip to content

Commit

Permalink
fix replica set cursor cleaning through mongos SERVER-2505
Browse files Browse the repository at this point in the history
fix cursor cleaning in mongos generally by not doing lazy cleaning
  • Loading branch information
erh committed Feb 10, 2011
1 parent e0c8d9a commit 37b11e4
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
8 changes: 6 additions & 2 deletions client/dbclient.cpp
Expand Up @@ -902,11 +902,15 @@ namespace mongo {

Message m;
m.setData( dbKillCursors , b.buf() , b.len() );

sayPiggyBack( m );

if ( _lazyKillCursor )
sayPiggyBack( m );
else
say(m);
}

AtomicUInt DBClientConnection::_numConnections;
bool DBClientConnection::_lazyKillCursor = true;


bool serverAlive( const string &uri ) {
Expand Down
4 changes: 4 additions & 0 deletions client/dbclient.h
Expand Up @@ -900,6 +900,9 @@ namespace mongo {
static int getNumConnections() {
return _numConnections;
}

static void setLazyKillCursor( bool lazy ) { _lazyKillCursor = lazy; }
static bool getLazyKillCursor() { return _lazyKillCursor; }

protected:
friend class SyncClusterConnection;
Expand All @@ -924,6 +927,7 @@ namespace mongo {
bool _connect( string& errmsg );

static AtomicUInt _numConnections;
static bool _lazyKillCursor; // lazy means we piggy back kill cursors on next op
};

/** pings server to check if it's up
Expand Down
10 changes: 9 additions & 1 deletion client/dbclient_rs.cpp
Expand Up @@ -522,6 +522,10 @@ namespace mongo {
}

void DBClientReplicaSet::killCursor( long long cursorID ) {
// we should neve call killCursor on a replica set conncetion
// since we don't know which server it belongs to
// can't assume master because of slave ok
// and can have a cursor survive a master change
assert(0);
}

Expand All @@ -547,7 +551,11 @@ namespace mongo {
}
}
}
return checkMaster()->call( toSend , response , assertOk );

DBClientConnection* m = checkMaster();
if ( actualServer )
*actualServer = m->getServerAddress();
return m->call( toSend , response , assertOk );
}

}
3 changes: 3 additions & 0 deletions jstests/slowNightly/sharding_rs2.js
Expand Up @@ -95,6 +95,9 @@ assert.eq( 100 , ts.count() , "B4" )
assert.eq( 100 , ts.find().itcount() , "B5" )
assert.eq( 100 , ts.find().batchSize(5).itcount() , "B6" )

t.find().batchSize(3).next();
gc(); gc(); gc();

// --- sharded ----

assert.eq( 100 , db.foo.count() , "C1" )
Expand Down
8 changes: 8 additions & 0 deletions s/cursors.cpp
Expand Up @@ -163,12 +163,14 @@ namespace mongo {
}

void CursorCache::storeRef( const string& server , long long id ) {
LOG(_myLogLevel) << "CursorCache::storeRef server: " << server << " id: " << id << endl;
assert( id );
scoped_lock lk( _mutex );
_refs[id] = server;
}

string CursorCache::getRef( long long id ) const {
LOG(_myLogLevel) << "CursorCache::getRef id: " << id << endl;
assert( id );
scoped_lock lk( _mutex );
MapNormal::const_iterator i = _refs.find( id );
Expand Down Expand Up @@ -215,6 +217,8 @@ namespace mongo {
long long * cursors = (long long *)x;
for ( int i=0; i<n; i++ ) {
long long id = cursors[i];
LOG(_myLogLevel) << "CursorCache::gotKillCursors id: " << id << endl;

if ( ! id ) {
log( LL_WARNING ) << " got cursor id of 0 to kill" << endl;
continue;
Expand All @@ -239,6 +243,8 @@ namespace mongo {
_refs.erase( j );
}

LOG(_myLogLevel) << "CursorCache::found gotKillCursors id: " << id << " server: " << server << endl;

assert( server.size() );
ScopedDbConnection conn( server );
conn->killCursor( id );
Expand Down Expand Up @@ -269,6 +275,8 @@ namespace mongo {

CursorCache cursorCache;

int CursorCache::_myLogLevel = 3;

class CursorTimeoutTask : public task::Task {
public:
virtual string name() const { return "cursorTimeout"; }
Expand Down
2 changes: 2 additions & 0 deletions s/cursors.h
Expand Up @@ -98,6 +98,8 @@ namespace mongo {
MapNormal _refs;

long long _shardedTotal;

static int _myLogLevel;
};

extern CursorCache cursorCache;
Expand Down
6 changes: 5 additions & 1 deletion s/server.cpp
Expand Up @@ -276,12 +276,16 @@ int _main(int argc, char* argv[]) {
return 9;
}
}

// set some global state

pool.addHook( &shardingConnectionHook );
pool.setName( "mongos connectionpool" );

DBClientConnection::setLazyKillCursor( false );

ReplicaSetMonitor::setConfigChangeHook( boost::bind( &ConfigServer::replicaSetChange , &configServer , _1 ) );

if ( argc <= 1 ) {
usage( argv );
return 3;
Expand Down

0 comments on commit 37b11e4

Please sign in to comment.