diff options
Diffstat (limited to 's/grid.cpp')
-rw-r--r-- | s/grid.cpp | 52 |
1 files changed, 40 insertions, 12 deletions
@@ -119,12 +119,14 @@ namespace mongo { } bool Grid::addShard( string* name , const ConnectionString& servers , long long maxSize , string& errMsg ) { - // name can be NULL, so privide a dummy one here to avoid testing it elsewhere + // name can be NULL, so provide a dummy one here to avoid testing it elsewhere string nameInternal; if ( ! name ) { name = &nameInternal; } + ReplicaSetMonitorPtr rsMonitor; + // Check whether the host (or set) exists and run several sanity checks on this request. // There are two set of sanity checks: making sure adding this particular shard is consistent // with the replica set state (if it exists) and making sure this shards databases can be @@ -140,7 +142,7 @@ namespace mongo { errMsg = "can't use sync cluster as a shard. for replica set, have to use <setname>/<server1>,<server2>,..."; return false; } - + BSONObj resIsMongos; bool ok = newShardConn->runCommand( "admin" , BSON( "isdbgrid" << 1 ) , resIsMongos ); @@ -171,6 +173,13 @@ namespace mongo { newShardConn.done(); return false; } + if ( !commandSetName.empty() && setName.empty() ) { + ostringstream ss; + ss << "host did not return a set name, is the replica set still initializing? " << resIsMaster; + errMsg = ss.str(); + newShardConn.done(); + return false; + } // if the shard is part of replica set, make sure it is the right one if ( ! commandSetName.empty() && ( commandSetName != setName ) ) { @@ -197,6 +206,12 @@ namespace mongo { hostSet.insert( piter.next().String() ); // host:port } } + if ( resIsMaster["arbiters"].isABSONObj() ) { + BSONObjIterator piter( resIsMaster["arbiters"].Obj() ); + while ( piter.more() ) { + hostSet.insert( piter.next().String() ); // host:port + } + } vector<HostAndPort> hosts = servers.getServers(); for ( size_t i = 0 ; i < hosts.size() ; i++ ) { @@ -213,7 +228,8 @@ namespace mongo { } if ( ! foundAll ) { ostringstream ss; - ss << "host " << offendingHost << " does not belong to replica set as a non-passive member" << setName;; + ss << "in seed list " << servers.toString() << ", host " << offendingHost + << " does not belong to replica set " << setName; errMsg = ss.str(); newShardConn.done(); return false; @@ -250,6 +266,9 @@ namespace mongo { } } + if ( newShardConn->type() == ConnectionString::SET ) + rsMonitor = ReplicaSetMonitor::get( setName ); + newShardConn.done(); } catch ( DBException& e ) { @@ -281,7 +300,7 @@ namespace mongo { // build the ConfigDB shard document BSONObjBuilder b; b.append( "_id" , *name ); - b.append( "host" , servers.toString() ); + b.append( "host" , rsMonitor ? rsMonitor->getServerAddress() : servers.toString() ); if ( maxSize > 0 ) { b.append( ShardFields::maxSize.name() , maxSize ); } @@ -375,10 +394,7 @@ namespace mongo { // check the 'stopped' marker maker // if present, it is a simple bool BSONElement stoppedElem = balancerDoc["stopped"]; - if ( ! stoppedElem.eoo() && stoppedElem.isBoolean() ) { - return stoppedElem.boolean(); - } - return false; + return stoppedElem.trueValue(); } bool Grid::_inBalancingWindow( const BSONObj& balancerDoc , const boost::posix_time::ptime& now ) { @@ -392,24 +408,32 @@ namespace mongo { // check if both 'start' and 'stop' are present if ( ! windowElem.isABSONObj() ) { - log(1) << "'activeWindow' format is { start: \"hh:mm\" , stop: ... }" << balancerDoc << endl; + warning() << "'activeWindow' format is { start: \"hh:mm\" , stop: ... }" << balancerDoc << endl; return true; } BSONObj intervalDoc = windowElem.Obj(); const string start = intervalDoc["start"].str(); const string stop = intervalDoc["stop"].str(); if ( start.empty() || stop.empty() ) { - log(1) << "must specify both start and end of balancing window: " << intervalDoc << endl; + warning() << "must specify both start and end of balancing window: " << intervalDoc << endl; return true; } // check that both 'start' and 'stop' are valid time-of-day boost::posix_time::ptime startTime, stopTime; if ( ! toPointInTime( start , &startTime ) || ! toPointInTime( stop , &stopTime ) ) { - log(1) << "cannot parse active window (use hh:mm 24hs format): " << intervalDoc << endl; + warning() << "cannot parse active window (use hh:mm 24hs format): " << intervalDoc << endl; return true; } + if ( logLevel ) { + stringstream ss; + ss << " now: " << now + << " startTime: " << startTime + << " stopTime: " << stopTime; + log() << "_inBalancingWindow: " << ss.str() << endl; + } + // allow balancing if during the activeWindow // note that a window may be open during the night if ( stopTime > startTime ) { @@ -453,6 +477,10 @@ namespace mongo { class BalancingWindowUnitTest : public UnitTest { public: void run() { + + if ( ! cmdLine.isMongos() ) + return; + // T0 < T1 < now < T2 < T3 and Error const string T0 = "9:00"; const string T1 = "11:00"; @@ -485,7 +513,7 @@ namespace mongo { assert( Grid::_inBalancingWindow( w8 , now ) ); assert( Grid::_inBalancingWindow( w9 , now ) ); - log(1) << "BalancingWidowObjTest passed" << endl; + LOG(1) << "BalancingWidowObjTest passed" << endl; } } BalancingWindowObjTest; |