Skip to content

Commit

Permalink
SERVER-9022 allow releasing sharded conn back to pool after read oper…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
Greg Studer committed Mar 20, 2013
1 parent 861863a commit 74323d6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/mongo/db/dbcommands_generic.cpp
Expand Up @@ -41,6 +41,7 @@
#include "../util/version.h"
#include "../util/ramlog.h"
#include "repl/multicmd.h"
#include "mongo/s/shard.h"
#include "server.h"

namespace mongo {
Expand Down Expand Up @@ -165,6 +166,10 @@ namespace mongo {
if (all || cmdObj.hasElement("replIndexPrefetch")) {
result.append("replIndexPrefetch", fetchReplIndexPrefetchParam());
}
if (all || cmdObj.hasElement("releaseConnectionsAfterResponse")) {
result.append("releaseConnectionsAfterResponse",
ShardConnection::releaseConnectionsAfterResponse);
}
if ( before == result.len() ) {
errmsg = "no option found to get";
return false;
Expand Down Expand Up @@ -254,6 +259,15 @@ namespace mongo {
cmdObj["replMonitorMaxFailedChecks"].numberInt() );
s++;
}
if( cmdObj.hasElement( "releaseConnectionsAfterResponse" ) ) {
if ( s == 0 ) {
result.append( "was",
ShardConnection::releaseConnectionsAfterResponse );
}
ShardConnection::releaseConnectionsAfterResponse =
cmdObj["releaseConnectionsAfterResponse"].trueValue();
s++;
}

if( s == 0 && !found ) {
errmsg = "no option found to set, use help:true to see options ";
Expand Down
6 changes: 6 additions & 0 deletions src/mongo/s/server.cpp
Expand Up @@ -102,6 +102,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;
Expand Down
12 changes: 12 additions & 0 deletions src/mongo/s/shard.h
Expand Up @@ -284,6 +284,18 @@ namespace mongo {
/** checks all of my thread local connections for the version of this ns */
static void checkMyConnectionVersions( const string & ns );

/**
* Whether or not we should release all connections after an operation with
* a response.
*/
static bool releaseConnectionsAfterResponse;

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

private:
void _init();
void _finishInit();
Expand Down
20 changes: 17 additions & 3 deletions src/mongo/s/shardconnection.cpp
Expand Up @@ -116,7 +116,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;
Expand All @@ -132,9 +140,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 ) {
Expand Down Expand Up @@ -372,6 +380,12 @@ namespace mongo {
ClientConnections::threadInstance()->checkVersions( ns );
}

bool ShardConnection::releaseConnectionsAfterResponse( false );

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

ShardConnection::~ShardConnection() {
if ( _conn ) {
if ( ! _conn->isFailed() ) {
Expand Down

0 comments on commit 74323d6

Please sign in to comment.