diff options
author | Greg Studer <greg@10gen.com> | 2013-03-20 12:54:47 -0400 |
---|---|---|
committer | Greg Studer <greg@10gen.com> | 2013-03-20 13:03:36 -0400 |
commit | 74323d671a216c8c87fcb295ed743f830d5212ee (patch) | |
tree | 6f00d8be5bf9e8864d47dfc1c287dac0197f5bfd | |
parent | 861863ac17feb7ae598f640919a1fc8ba71ce421 (diff) | |
download | mongo-74323d671a216c8c87fcb295ed743f830d5212ee.tar.gz |
SERVER-9022 allow releasing sharded conn back to pool after read operation
-rw-r--r-- | src/mongo/db/dbcommands_generic.cpp | 14 | ||||
-rw-r--r-- | src/mongo/s/server.cpp | 6 | ||||
-rw-r--r-- | src/mongo/s/shard.h | 12 | ||||
-rw-r--r-- | src/mongo/s/shardconnection.cpp | 20 |
4 files changed, 49 insertions, 3 deletions
diff --git a/src/mongo/db/dbcommands_generic.cpp b/src/mongo/db/dbcommands_generic.cpp index ff128dbd1ff..0c842328993 100644 --- a/src/mongo/db/dbcommands_generic.cpp +++ b/src/mongo/db/dbcommands_generic.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 "; diff --git a/src/mongo/s/server.cpp b/src/mongo/s/server.cpp index e7a963078c1..065069d5a3a 100644 --- a/src/mongo/s/server.cpp +++ b/src/mongo/s/server.cpp @@ -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; diff --git a/src/mongo/s/shard.h b/src/mongo/s/shard.h index ccb76a923a0..e2d5b2ab980 100644 --- a/src/mongo/s/shard.h +++ b/src/mongo/s/shard.h @@ -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(); diff --git a/src/mongo/s/shardconnection.cpp b/src/mongo/s/shardconnection.cpp index 325ad5f3776..0c884b7f764 100644 --- a/src/mongo/s/shardconnection.cpp +++ b/src/mongo/s/shardconnection.cpp @@ -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() ) { |