Permalink
Browse files

SERVER-6991 allow releasing sharded conn back to pool after read oper…

…ation
  • Loading branch information...
Greg Studer
Greg Studer committed Mar 14, 2013
1 parent d2d811e commit 706459a8af0b278609d70e7122595243df6aeee8
Showing with 40 additions and 3 deletions.
  1. +6 −0 src/mongo/s/server.cpp
  2. +8 −0 src/mongo/s/shard.h
  3. +26 −3 src/mongo/s/shardconnection.cpp
@@ -104,6 +104,12 @@ namespace mongo {
try {
r.init();
r.process();

// Release connections after non-write op
if ( ShardConnection::releaseConnectionsAfterResponse && r.expectResponse() ) {
LOG(2) << "release thread local connections back to pool" << endl;
ShardConnection::releaseMyConnections();
}
}
catch ( AssertionException & e ) {
LOG( e.isUserAssertion() ? 1 : 0 ) << "AssertionException while processing op type : " << m.operation() << " to : " << r.getns() << causedBy(e) << endl;
@@ -290,9 +290,17 @@ namespace mongo {
*/
bool runCommand( const string& db , const BSONObj& cmd , BSONObj& res );

static bool releaseConnectionsAfterResponse;

/** checks all of my thread local connections for the version of this ns */
static void checkMyConnectionVersions( const string & ns );

/**
* Returns all the current sharded connections to the pool.
* Note: This is *dangerous* if we have GLE state.
*/
static void releaseMyConnections();

/**
* Clears all connections in the sharded pool, including connections in the
* thread local storage pool of the current thread.
@@ -22,6 +22,7 @@

#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/server_parameters.h"
#include "mongo/s/config.h"
#include "mongo/s/request.h"
#include "mongo/s/shard.h"
@@ -131,7 +132,15 @@ namespace mongo {
// Stop tracking these client connections
activeClientConnections.remove( this );

// No longer need spinlock protection
releaseAll( true );
}

void releaseAll( bool fromDestructor = false ) {

// Don't need spinlock protection because if not in the destructor, we don't
// modify _hosts, and if in the destructor we are not accessible to external
// threads.

for ( HostMap::iterator i=_hosts.begin(); i!=_hosts.end(); ++i ) {
string addr = i->first;
Status* ss = i->second;
@@ -147,9 +156,9 @@ namespace mongo {
release( addr , ss->avail );
ss->avail = 0;
}
delete ss;
if ( fromDestructor ) delete ss;
}
_hosts.clear();
if ( fromDestructor ) _hosts.clear();
}

DBClientBase * get( const string& addr , const string& ns ) {
@@ -460,6 +469,20 @@ namespace mongo {
}
}

bool ShardConnection::releaseConnectionsAfterResponse( false );

ExportedServerParameter<bool> ReleaseConnectionsAfterResponse(
ServerParameterSet::getGlobal(),
"releaseConnectionsAfterResponse",
&ShardConnection::releaseConnectionsAfterResponse,
true,
true
);

void ShardConnection::releaseMyConnections() {
ClientConnections::threadInstance()->releaseAll();
}

void ShardConnection::clearPool() {
shardConnectionPool.clear();
ClientConnections::threadInstance()->clearPool();

0 comments on commit 706459a

Please sign in to comment.