diff options
author | Eliot Horowitz <eliot@10gen.com> | 2010-07-13 00:56:08 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2010-07-13 00:56:08 -0400 |
commit | 453398b8b2d28b2d84f7c62c19fa68a56f9e2af6 (patch) | |
tree | 4308044b03ad4173e6b97e1199f472869bba7666 /client | |
parent | 69251fae55f0eb264bc3115570b16f7a104fa831 (diff) | |
download | mongo-453398b8b2d28b2d84f7c62c19fa68a56f9e2af6.tar.gz |
distlock pings, get rid of ourHostname
Diffstat (limited to 'client')
-rw-r--r-- | client/distlock.cpp | 61 | ||||
-rw-r--r-- | client/distlock.h | 21 |
2 files changed, 61 insertions, 21 deletions
diff --git a/client/distlock.cpp b/client/distlock.cpp index f15dcd47fbd..32f4697ecd6 100644 --- a/client/distlock.cpp +++ b/client/distlock.cpp @@ -20,6 +20,65 @@ #include "distlock.h" namespace mongo { + + ThreadLocalValue<string> distLockIds(""); + + string getDistLockId(){ + string s = distLockIds.get(); + if ( s.empty() ){ + stringstream ss; + ss << getHostNameCached() << ":" << time(0) << ":" << rand(); + s = ss.str(); + distLockIds.set( s ); + } + return s; + } + + void distLockPingThread( ConnectionString addr ){ + while(1){ + try { + ScopedDbConnection conn( addr ); + conn->update( "config.lockpings" , + BSON( "_id" << getDistLockId() ) , + BSON( "$set" << BSON( "ping" << DATENOW ) ) , + true ); + conn.done(); + } + catch ( std::exception& e ){ + log( LL_WARNING ) << "couldn't ping: " << e.what() << endl; + } + sleepsecs(30); + } + } + + + class DistributedLockPinger { + public: + DistributedLockPinger() + : _mutex( "DistributedLockPinger" ){ + } + + void got( const ConnectionString& conn ){ + string s = conn.toString(); + scoped_lock lk( _mutex ); + if ( _seen.count( s ) > 0 ) + return; + boost::thread t( boost::bind( &distLockPingThread , conn ) ); + _seen.insert( s ); + } + + set<string> _seen; + mongo::mutex _mutex; + + } distLockPinger; + + DistributedLock::DistributedLock( const ConnectionString& conn , const string& name ) + : _conn(conn),_name(name){ + _id = BSON( "_id" << name ); + _ns = "config.locks"; + distLockPinger.got( conn ); + } + bool DistributedLock::lock_try( string why , BSONObj * other ){ // recursive @@ -55,7 +114,7 @@ namespace mongo { bool gotLock = false; BSONObj now; - BSONObj whatIWant = BSON( "$set" << BSON( "state" << 1 << "who" << myid() << + BSONObj whatIWant = BSON( "$set" << BSON( "state" << 1 << "who" << getDistLockId() << "when" << DATENOW << "why" << why << "ts" << ts ) ); try { conn->update( _ns , queryBuilder.obj() , whatIWant ); diff --git a/client/distlock.h b/client/distlock.h index df0c88ba499..d936a7ec181 100644 --- a/client/distlock.h +++ b/client/distlock.h @@ -28,16 +28,10 @@ namespace mongo { - extern string ourHostname; - class DistributedLock { public: - DistributedLock( const ConnectionString& conn , const string& name ) - : _conn(conn),_name(name),_myid(""){ - _id = BSON( "_id" << name ); - _ns = "config.locks"; - } + DistributedLock( const ConnectionString& conn , const string& name ); int getState(){ return _state.get(); @@ -50,18 +44,6 @@ namespace mongo { bool lock_try( string why , BSONObj * other = 0 ); void unlock(); - string myid(){ - string s = _myid.get(); - if ( s.empty() ){ - stringstream ss; - ss << ourHostname << ":" << time(0) << ":" << rand(); - s = ss.str(); - _myid.set( s ); - } - - return s; - } - private: ConnectionString _conn; string _ns; @@ -69,7 +51,6 @@ namespace mongo { BSONObj _id; ThreadLocalValue<int> _state; - ThreadLocalValue<string> _myid; }; class dist_lock_try { |