Permalink
Browse files

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

…ation
  • Loading branch information...
Greg Studer
Greg Studer committed Mar 20, 2013
1 parent 861863a commit 74323d671a216c8c87fcb295ed743f830d5212ee
Showing with 49 additions and 3 deletions.
  1. +14 −0 src/mongo/db/dbcommands_generic.cpp
  2. +6 −0 src/mongo/s/server.cpp
  3. +12 −0 src/mongo/s/shard.h
  4. +17 −3 src/mongo/s/shardconnection.cpp
@@ -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 {
@@ -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;
@@ -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 ";
@@ -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;
@@ -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();
@@ -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;
@@ -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 ) {
@@ -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() ) {

0 comments on commit 74323d6

Please sign in to comment.