diff --git a/s/d_logic.h b/s/d_logic.h index e27105635ae..2488b2ac9dc 100644 --- a/s/d_logic.h +++ b/s/d_logic.h @@ -47,7 +47,7 @@ namespace mongo { bool hasVersion( const string& ns ); bool hasVersion( const string& ns , ConfigVersion& version ); - ConfigVersion& getVersion( const string& ns ); // TODO: this is dangeroues + const ConfigVersion getVersion( const string& ns ) const; void setVersion( const string& ns , const ConfigVersion& version ); void appendInfo( BSONObjBuilder& b ); @@ -65,7 +65,7 @@ namespace mongo { string _shardHost; // protects state below - mongo::mutex _mutex; + mutable mongo::mutex _mutex; // map from a namespace into the highest ShardChunkVersion for that collection NSVersionMap _versions; @@ -88,7 +88,7 @@ namespace mongo { bool hasID() const { return _id.isSet(); } void setID( const OID& id ); - ConfigVersion& getVersion( const string& ns ); // TODO: this is dangeroues + const ConfigVersion getVersion( const string& ns ) const; void setVersion( const string& ns , const ConfigVersion& version ); static ShardedConnectionInfo* get( bool create ); diff --git a/s/d_state.cpp b/s/d_state.cpp index 7adcfe41c1a..628f00098ca 100644 --- a/s/d_state.cpp +++ b/s/d_state.cpp @@ -113,9 +113,15 @@ namespace mongo { return true; } - ConfigVersion& ShardingState::getVersion( const string& ns ){ + const ConfigVersion ShardingState::getVersion( const string& ns ) const { scoped_lock lk(_mutex); - return _versions[ns]; + + NSVersionMap::const_iterator it = _versions.find( ns ); + if ( it != _versions.end() ) { + return it->second; + } else { + return 0; + } } void ShardingState::setVersion( const string& ns , const ConfigVersion& version ){ @@ -273,8 +279,13 @@ namespace mongo { _tl.reset(); } - ConfigVersion& ShardedConnectionInfo::getVersion( const string& ns ){ - return _versions[ns]; + const ConfigVersion ShardedConnectionInfo::getVersion( const string& ns ) const { + NSVersionMap::const_iterator it = _versions.find( ns ); + if ( it != _versions.end() ) { + return it->second; + } else { + return 0; + } } void ShardedConnectionInfo::setVersion( const string& ns , const ConfigVersion& version ){ @@ -421,18 +432,18 @@ namespace mongo { return false; } - ConfigVersion& oldVersion = info->getVersion(ns); - ConfigVersion& globalVersion = shardingState.getVersion(ns); + const ConfigVersion oldVersion = info->getVersion(ns); + const ConfigVersion globalVersion = shardingState.getVersion(ns); if ( oldVersion > 0 && globalVersion == 0 ){ // this had been reset - oldVersion = 0; + info->setVersion( ns , 0 ); } if ( version == 0 && globalVersion == 0 ){ // this connection is cleaning itself - oldVersion = 0; - return 1; + info->setVersion( ns , 0 ); + return true; } if ( version == 0 && globalVersion > 0 ){ @@ -441,15 +452,15 @@ namespace mongo { result.appendTimestamp( "globalVersion" , globalVersion ); result.appendTimestamp( "oldVersion" , oldVersion ); errmsg = "dropping needs to be authoritative"; - return 0; + return false; } log() << "wiping data for: " << ns << endl; result.appendTimestamp( "beforeDrop" , globalVersion ); // only setting global version on purpose // need clients to re-find meta-data - globalVersion = 0; - oldVersion = 0; - return 1; + shardingState.setVersion( ns , 0 ); + info->setVersion( ns , 0 ); + return true; } if ( version < oldVersion ){ @@ -480,17 +491,18 @@ namespace mongo { return false; } + result.appendTimestamp( "oldVersion" , oldVersion ); + result.append( "ok" , 1 ); + + info->setVersion( ns , version ); + shardingState.setVersion( ns , version ); + { dbtemprelease unlock; shardingState.getChunkMatcher( ns ); } - result.appendTimestamp( "oldVersion" , oldVersion ); - oldVersion = version; - globalVersion = version; - - result.append( "ok" , 1 ); - return 1; + return true; } } setShardVersionCmd;