Permalink
Browse files

repl: only try to get a write lock for keepalive

  • Loading branch information...
erh committed Jun 5, 2010
1 parent efde5cf commit 3fa734901337e5f9515de056256d8a1d3ce880b8
Showing with 63 additions and 13 deletions.
  1. +45 −2 db/concurrency.h
  2. +17 −10 db/repl.cpp
  3. +1 −1 jstests/geo3.js
View
@@ -102,16 +102,25 @@ namespace mongo {
bool atLeastReadLocked() { return _state.get() != 0; }
void assertAtLeastReadLocked() { assert(atLeastReadLocked()); }
- void lock() {
+ bool _checkWriteLockAlready(){
//DEV cout << "LOCK" << endl;
DEV assert( haveClient() );
int s = _state.get();
if( s > 0 ) {
_state.set(s+1);
- return;
+ return true;
}
+
massert( 10293 , (string)"internal error: locks are not upgradeable: " + sayClientState() , s == 0 );
+
+ return false;
+ }
+
+ void lock() {
+ if ( _checkWriteLockAlready() )
+ return;
+
_state.set(1);
curopWaitingForLock( 1 );
@@ -120,6 +129,24 @@ namespace mongo {
_minfo.entered();
}
+
+ bool lock_try( int millis ) {
+ if ( _checkWriteLockAlready() )
+ return true;
+
+ curopWaitingForLock( 1 );
+ bool got = _m.lock_try( millis );
+ curopGotLock();
+
+ if ( got ){
+ _minfo.entered();
+ _state.set(1);
+ }
+
+ return got;
+ }
+
+
void unlock() {
//DEV cout << "UNLOCK" << endl;
int s = _state.get();
@@ -248,6 +275,22 @@ namespace mongo {
bool _got;
};
+ struct writelocktry {
+ writelocktry( const string&ns , int tryms ){
+ _got = dbMutex.lock_try( tryms );
+ }
+ ~writelocktry() {
+ if ( _got ){
+ dbunlocking_read();
+ dbMutex.unlock();
+ }
+ }
+ bool got() const { return _got; }
+ private:
+ bool _got;
+ };
+
+
struct readlocktryassert : public readlocktry {
readlocktryassert(const string& ns, int tryms) :
readlocktry(ns,tryms) {
View
@@ -1670,23 +1670,30 @@ namespace mongo {
static void replMasterThread() {
sleepsecs(4);
Client::initThread("replmaster");
+ int toSleep = 10;
while( 1 ) {
- {
- dblock lk;
- cc().getAuthenticationInfo()->authorize("admin");
- }
- sleepsecs(10);
+
+ sleepsecs( toSleep );
/* write a keep-alive like entry to the log. this will make things like
printReplicationStatus() and printSlaveReplicationStatus() stay up-to-date
even when things are idle.
*/
{
- writelock lk("");
- try {
- logKeepalive();
+ writelocktry lk("",1);
+ if ( lk.got() ){
+ toSleep = 10;
+
+ cc().getAuthenticationInfo()->authorize("admin");
+
+ try {
+ logKeepalive();
+ }
+ catch(...) {
+ log() << "caught exception in replMasterThread()" << endl;
+ }
}
- catch(...) {
- log() << "caught exception in replMasterThread()" << endl;
+ else {
+ toSleep = 1;
}
}
}
View
@@ -18,7 +18,7 @@ fast = db.runCommand( { geoNear : t.getName() , near : [ 50 , 50 ] , num : 10 }
slow = db.runCommand( { geoNear : t.getName() , near : [ 50 , 50 ] , num : 10 , start : "11" } );
-//printjson( slow.stats );
+printjson( slow.stats );
assert.lt( fast.stats.nscanned * 10 , slow.stats.nscanned , "A1" );
assert.lt( fast.stats.objectsLoaded , slow.stats.objectsLoaded , "A2" );

0 comments on commit 3fa7349

Please sign in to comment.