Files
mongo/s/config.cpp

757 lines
24 KiB
C++
Raw Normal View History

2009-02-12 21:03:46 -05:00
// config.cpp
2008-10-13 17:58:51 -04:00
/**
* Copyright (C) 2008 10gen Inc.
2008-12-28 20:28:49 -05:00
*
2008-10-13 17:58:51 -04:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
2008-12-28 20:28:49 -05:00
*
2008-10-13 17:58:51 -04:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2008-12-28 20:28:49 -05:00
*
2008-10-13 17:58:51 -04:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
2009-02-03 13:30:28 -05:00
#include "../util/message.h"
#include "../util/stringutils.h"
2008-10-13 17:58:51 -04:00
#include "../util/unittest.h"
2008-10-31 19:17:54 -05:00
#include "../client/connpool.h"
2009-02-06 15:44:21 -05:00
#include "../client/model.h"
#include "../db/pdfile.h"
#include "../db/cmdline.h"
2009-02-12 21:09:06 -05:00
#include "chunk.h"
#include "config.h"
#include "grid.h"
#include "server.h"
2008-10-13 17:58:51 -04:00
2009-01-14 17:09:51 -05:00
namespace mongo {
int ConfigServer::VERSION = 3;
2010-04-19 16:55:16 -04:00
Shard Shard::EMPTY;
2011-01-04 00:40:41 -05:00
string ShardNS::shard = "config.shards";
string ShardNS::database = "config.databases";
string ShardNS::collection = "config.collections";
string ShardNS::chunk = "config.chunks";
2010-04-22 13:34:53 -04:00
string ShardNS::mongos = "config.mongos";
string ShardNS::settings = "config.settings";
BSONField<bool> ShardFields::draining("draining");
BSONField<long long> ShardFields::maxSize ("maxSize");
OID serverID;
2009-02-06 15:44:21 -05:00
/* --- DBConfig --- */
2008-10-19 17:46:53 -05:00
2011-01-04 00:40:41 -05:00
DBConfig::CollectionInfo::CollectionInfo( const BSONObj& in ) {
_dirty = false;
_dropped = in["dropped"].trueValue();
2011-06-06 15:38:40 -04:00
if ( in["key"].isABSONObj() ) {
_key = in["key"].Obj().getOwned();
_unqiue = in["unique"].trueValue();
shard( in["_id"].String() , _key , _unqiue );
}
_dirty = false;
}
2011-06-06 15:38:40 -04:00
2011-01-04 00:40:41 -05:00
void DBConfig::CollectionInfo::shard( const string& ns , const ShardKeyPattern& key , bool unique ) {
_cm.reset( new ChunkManager( ns , key , unique ) );
2011-06-06 15:38:40 -04:00
_key = key.key().getOwned();
_unqiue = unique;
_dirty = true;
_dropped = false;
}
2011-01-04 00:40:41 -05:00
void DBConfig::CollectionInfo::unshard() {
_cm.reset();
_dropped = true;
_dirty = true;
2011-06-06 15:38:40 -04:00
_key = BSONObj();
}
2011-01-04 00:40:41 -05:00
void DBConfig::CollectionInfo::save( const string& ns , DBClientBase* conn ) {
BSONObj key = BSON( "_id" << ns );
2011-01-04 00:40:41 -05:00
BSONObjBuilder val;
val.append( "_id" , ns );
val.appendDate( "lastmod" , time(0) );
val.appendBool( "dropped" , _dropped );
if ( _cm )
_cm->getInfo( val );
2011-01-04 00:40:41 -05:00
conn->update( ShardNS::collection , key , val.obj() , true );
2010-09-17 12:18:50 -04:00
string err = conn->getLastError();
uassert( 13473 , (string)"failed to save collection (" + ns + "): " + err , err.size() == 0 );
2010-09-17 12:18:50 -04:00
_dirty = false;
}
2011-01-04 00:40:41 -05:00
bool DBConfig::isSharded( const string& ns ) {
2010-04-27 17:22:17 -04:00
if ( ! _shardingEnabled )
return false;
scoped_lock lk( _lock );
return _isSharded( ns );
}
2011-01-04 00:40:41 -05:00
bool DBConfig::_isSharded( const string& ns ) {
2009-09-01 12:17:41 -04:00
if ( ! _shardingEnabled )
2009-02-06 15:44:21 -05:00
return false;
Collections::iterator i = _collections.find( ns );
if ( i == _collections.end() )
return false;
return i->second.isSharded();
2009-02-06 15:44:21 -05:00
}
2010-04-27 17:22:17 -04:00
2011-01-04 00:40:41 -05:00
const Shard& DBConfig::getShard( const string& ns ) {
2009-09-01 12:17:41 -04:00
if ( isSharded( ns ) )
2010-04-19 16:55:16 -04:00
return Shard::EMPTY;
2011-01-04 00:40:41 -05:00
2010-04-19 16:55:16 -04:00
uassert( 10178 , "no primary!" , _primary.ok() );
2009-02-06 15:44:21 -05:00
return _primary;
}
2011-01-04 00:40:41 -05:00
void DBConfig::enableSharding() {
if ( _shardingEnabled )
return;
2011-02-13 10:08:37 -05:00
assert( _name != "config" );
scoped_lock lk( _lock );
2011-01-04 00:40:41 -05:00
_shardingEnabled = true;
_save();
2009-02-19 12:55:01 -05:00
}
2011-01-04 00:40:41 -05:00
ChunkManagerPtr DBConfig::shardCollection( const string& ns , ShardKeyPattern fieldsAndOrder , bool unique ) {
uassert( 8042 , "db doesn't have sharding enabled" , _shardingEnabled );
uassert( 13648 , str::stream() << "can't shard collection because not all config servers are up" , configServer.allUp() );
2009-02-19 12:55:01 -05:00
{
scoped_lock lk( _lock );
CollectionInfo& ci = _collections[ns];
uassert( 8043 , "collection already sharded" , ! ci.isSharded() );
log() << "enable sharding on: " << ns << " with shard key: " << fieldsAndOrder << endl;
// From this point on, 'ns' is going to be treated as a sharded collection. We assume this is the first
// time it is seen by the sharded system and thus create the first chunk for the collection. All the remaining
// chunks will be created as a by-product of splitting.
ci.shard( ns , fieldsAndOrder , unique );
ChunkManagerPtr cm = ci.getCM();
uassert( 13449 , "collections already sharded" , (cm->numChunks() == 0) );
cm->createFirstChunk( getPrimary() );
_save();
}
2011-01-04 00:40:41 -05:00
try {
getChunkManager(ns, true)->maybeChunkCollection();
}
2011-01-04 00:40:41 -05:00
catch ( UserException& e ) {
// failure to chunk is not critical enough to abort the command (and undo the _save()'d configDB state)
log() << "couldn't chunk recently created collection: " << ns << " " << e << endl;
}
return getChunkManager(ns);
2009-02-19 12:55:01 -05:00
}
2009-02-06 15:44:21 -05:00
2011-01-04 00:40:41 -05:00
bool DBConfig::removeSharding( const string& ns ) {
if ( ! _shardingEnabled ) {
return false;
}
2011-01-04 00:40:41 -05:00
2010-04-27 17:22:17 -04:00
scoped_lock lk( _lock );
2011-01-04 00:40:41 -05:00
Collections::iterator i = _collections.find( ns );
2010-04-27 17:22:17 -04:00
if ( i == _collections.end() )
return false;
2011-01-04 00:40:41 -05:00
CollectionInfo& ci = _collections[ns];
if ( ! ci.isSharded() )
return false;
2011-01-04 00:40:41 -05:00
ci.unshard();
_save( false, true );
return true;
}
2011-01-04 00:40:41 -05:00
ChunkManagerPtr DBConfig::getChunkManager( const string& ns , bool shouldReload ) {
2011-06-06 15:38:40 -04:00
BSONObj key;
bool unique;
2011-06-06 15:38:40 -04:00
{
scoped_lock lk( _lock );
CollectionInfo& ci = _collections[ns];
bool earlyReload = ! ci.isSharded() && shouldReload;
if ( earlyReload ) {
// this is to catch cases where there this is a new sharded collection
_reload();
ci = _collections[ns];
}
massert( 10181 , (string)"not sharded:" + ns , ci.isSharded() || ci.wasDropped() );
assert( ci.wasDropped() || ! ci.key().isEmpty() );
if ( ! shouldReload || earlyReload )
return ci.getCM();
key = ci.key().copy();
unique = ci.unique();
}
assert( ! key.isEmpty() );
2011-06-06 15:38:40 -04:00
// we are not locked now, and want to load a new ChunkManager
auto_ptr<ChunkManager> temp( new ChunkManager( ns , key , unique ) );
if ( temp->numChunks() == 0 ) {
// maybe we're not sharded any more
reload(); // this is a full reload
return getChunkManager( ns , false );
}
scoped_lock lk( _lock );
CollectionInfo& ci = _collections[ns];
2011-06-06 15:38:40 -04:00
massert( 14822 , (string)"state changed in the middle: " + ns , ci.isSharded() || ci.wasDropped() );
ci.resetCM( temp.release() );
return ci.getCM();
}
2011-01-04 00:40:41 -05:00
void DBConfig::setPrimary( string s ) {
scoped_lock lk( _lock );
_primary.reset( s );
_save();
}
2011-01-04 00:40:41 -05:00
void DBConfig::serialize(BSONObjBuilder& to) {
to.append("_id", _name);
2009-09-01 12:17:41 -04:00
to.appendBool("partitioned", _shardingEnabled );
to.append("primary", _primary.getName() );
2009-02-06 15:44:21 -05:00
}
2011-01-04 00:40:41 -05:00
void DBConfig::unserialize(const BSONObj& from) {
2010-03-20 23:46:19 -04:00
log(1) << "DBConfig unserialize: " << _name << " " << from << endl;
assert( _name == from["_id"].String() );
2009-09-01 12:17:41 -04:00
_shardingEnabled = from.getBoolField("partitioned");
_primary.reset( from.getStringField("primary") );
2010-10-30 14:21:35 -04:00
// In the 1.5.x series, we used to have collection metadata nested in the database entry. The 1.6.x series
// had migration code that ported that info to where it belongs now: the 'collections' collection. We now
2011-01-04 00:40:41 -05:00
// just assert that we're not migrating from a 1.5.x directly into a 1.7.x without first converting.
2009-02-19 12:55:01 -05:00
BSONObj sharded = from.getObjectField( "sharded" );
2010-10-30 14:21:35 -04:00
if ( ! sharded.isEmpty() )
uasserted( 13509 , "can't migrate from 1.5.x release to the current one; need to upgrade to 1.6.x first");
2009-02-06 15:44:21 -05:00
}
2011-01-04 00:40:41 -05:00
bool DBConfig::load() {
scoped_lock lk( _lock );
return _load();
}
2011-01-04 00:40:41 -05:00
bool DBConfig::_load() {
ScopedDbConnection conn( configServer.modelServer(), 30.0 );
2011-01-04 00:40:41 -05:00
BSONObj o = conn->findOne( ShardNS::database , BSON( "_id" << _name ) );
2011-01-04 00:40:41 -05:00
if ( o.isEmpty() ) {
conn.done();
return false;
}
2011-01-04 00:40:41 -05:00
2010-10-30 14:21:35 -04:00
unserialize( o );
2011-01-04 00:40:41 -05:00
BSONObjBuilder b;
b.appendRegex( "_id" , (string)"^" + _name + "." );
auto_ptr<DBClientCursor> cursor = conn->query( ShardNS::collection ,b.obj() );
2010-08-02 14:44:53 -04:00
assert( cursor.get() );
2011-01-04 00:40:41 -05:00
while ( cursor->more() ) {
BSONObj o = cursor->next();
_collections[o["_id"].String()] = CollectionInfo( o );
}
2011-01-04 00:40:41 -05:00
conn.done();
return true;
}
void DBConfig::_save( bool db, bool coll ) {
ScopedDbConnection conn( configServer.modelServer(), 30.0 );
2011-01-04 00:40:41 -05:00
if( db ){
BSONObj n;
{
BSONObjBuilder b;
serialize(b);
n = b.obj();
}
conn->update( ShardNS::database , BSON( "_id" << _name ) , n , true );
string err = conn->getLastError();
uassert( 13396 , (string)"DBConfig save failed: " + err , err.size() == 0 );
}
2011-01-04 00:40:41 -05:00
if( coll ){
for ( Collections::iterator i=_collections.begin(); i!=_collections.end(); ++i ) {
if ( ! i->second.isDirty() )
continue;
i->second.save( i->first , conn.get() );
}
2011-01-04 00:40:41 -05:00
}
conn.done();
2009-02-06 15:44:21 -05:00
}
2011-01-04 00:40:41 -05:00
bool DBConfig::reload() {
scoped_lock lk( _lock );
return _reload();
}
2011-01-04 00:40:41 -05:00
bool DBConfig::_reload() {
// TODO: i don't think is 100% correct
return _load();
}
2011-01-04 00:40:41 -05:00
bool DBConfig::dropDatabase( string& errmsg ) {
2009-12-02 16:36:46 -05:00
/**
* 1) make sure everything is up
* 2) update config server
* 3) drop and reset sharded collections
* 4) drop and reset primary
* 5) drop everywhere to clean up loose ends
*/
2009-12-10 10:46:41 -05:00
log() << "DBConfig::dropDatabase: " << _name << endl;
configServer.logChange( "dropDatabase.start" , _name , BSONObj() );
2011-01-04 00:40:41 -05:00
2009-12-02 16:36:46 -05:00
// 1
2011-01-04 00:40:41 -05:00
if ( ! configServer.allUp( errmsg ) ) {
2009-12-02 16:36:46 -05:00
log(1) << "\t DBConfig::dropDatabase not all up" << endl;
return 0;
}
2011-01-04 00:40:41 -05:00
2009-12-02 16:36:46 -05:00
// 2
grid.removeDB( _name );
{
ScopedDbConnection conn( configServer.modelServer(), 30.0 );
conn->remove( ShardNS::database , BSON( "_id" << _name ) );
errmsg = conn->getLastError();
2011-01-04 00:40:41 -05:00
if ( ! errmsg.empty() ) {
log() << "could not drop '" << _name << "': " << errmsg << endl;
conn.done();
return false;
}
conn.done();
}
2011-01-04 00:40:41 -05:00
if ( ! configServer.allUp( errmsg ) ) {
2009-12-02 16:36:46 -05:00
log() << "error removing from config server even after checking!" << endl;
return 0;
}
log(1) << "\t removed entry from config server for: " << _name << endl;
2011-01-04 00:40:41 -05:00
set<Shard> allServers;
2009-12-02 16:36:46 -05:00
// 3
2011-01-04 00:40:41 -05:00
while ( true ) {
int num = 0;
2009-12-02 16:36:46 -05:00
if ( ! _dropShardedCollections( num , allServers , errmsg ) )
return 0;
log() << " DBConfig::dropDatabase: " << _name << " dropped sharded collections: " << num << endl;
if ( num == 0 )
break;
}
2011-01-04 00:40:41 -05:00
2009-12-02 16:36:46 -05:00
// 4
{
ScopedDbConnection conn( _primary, 30.0 );
2009-12-02 16:36:46 -05:00
BSONObj res;
2011-01-04 00:40:41 -05:00
if ( ! conn->dropDatabase( _name , &res ) ) {
2009-12-02 16:36:46 -05:00
errmsg = res.toString();
return 0;
}
conn.done();
}
2011-01-04 00:40:41 -05:00
2009-12-02 16:36:46 -05:00
// 5
2011-01-04 00:40:41 -05:00
for ( set<Shard>::iterator i=allServers.begin(); i!=allServers.end(); i++ ) {
ScopedDbConnection conn( *i, 30.0 );
2009-12-02 16:36:46 -05:00
BSONObj res;
2011-01-04 00:40:41 -05:00
if ( ! conn->dropDatabase( _name , &res ) ) {
2009-12-02 16:36:46 -05:00
errmsg = res.toString();
return 0;
}
2011-01-04 00:40:41 -05:00
conn.done();
2009-12-02 16:36:46 -05:00
}
2011-01-04 00:40:41 -05:00
2009-12-02 16:36:46 -05:00
log(1) << "\t dropped primary db for: " << _name << endl;
configServer.logChange( "dropDatabase" , _name , BSONObj() );
2009-12-02 16:36:46 -05:00
return true;
}
2011-01-04 00:40:41 -05:00
bool DBConfig::_dropShardedCollections( int& num, set<Shard>& allServers , string& errmsg ) {
2009-12-02 16:36:46 -05:00
num = 0;
set<string> seen;
2011-01-04 00:40:41 -05:00
while ( true ) {
Collections::iterator i = _collections.begin();
2011-01-04 00:40:41 -05:00
for ( ; i != _collections.end(); ++i ) {
if ( i->second.isSharded() )
break;
}
if ( i == _collections.end() )
2009-12-02 16:36:46 -05:00
break;
2011-01-04 00:40:41 -05:00
if ( seen.count( i->first ) ) {
2009-12-02 16:36:46 -05:00
errmsg = "seen a collection twice!";
return false;
}
seen.insert( i->first );
log(1) << "\t dropping sharded collection: " << i->first << endl;
i->second.getCM()->getAllShards( allServers );
i->second.getCM()->drop( i->second.getCM() );
uassert( 10176 , str::stream() << "shard state missing for " << i->first , removeSharding( i->first ) );
2009-12-02 16:36:46 -05:00
num++;
uassert( 10184 , "_dropShardedCollections too many collections - bailing" , num < 100000 );
2009-12-02 16:36:46 -05:00
log(2) << "\t\t dropped " << num << " so far" << endl;
}
2009-12-02 16:36:46 -05:00
return true;
}
2011-01-04 00:40:41 -05:00
void DBConfig::getAllShards(set<Shard>& shards) const {
2011-05-03 20:32:57 -04:00
scoped_lock lk( _lock );
shards.insert(getPrimary());
2011-01-04 00:40:41 -05:00
for (Collections::const_iterator it(_collections.begin()), end(_collections.end()); it != end; ++it) {
if (it->second.isSharded()) {
it->second.getCM()->getAllShards(shards);
} // TODO: handle collections on non-primary shard
}
}
2009-02-12 21:09:06 -05:00
/* --- ConfigServer ---- */
2011-01-04 00:40:41 -05:00
ConfigServer::ConfigServer() : DBConfig( "config" ) {
2009-09-01 12:17:41 -04:00
_shardingEnabled = false;
2009-02-12 21:09:06 -05:00
}
2011-01-04 00:40:41 -05:00
2009-02-12 21:09:06 -05:00
ConfigServer::~ConfigServer() {
}
2011-01-04 00:40:41 -05:00
bool ConfigServer::init( string s ) {
vector<string> configdbs;
2010-07-04 11:08:00 -04:00
splitStringDelim( s, &configdbs, ',' );
return init( configdbs );
}
2011-01-04 00:40:41 -05:00
bool ConfigServer::init( vector<string> configHosts ) {
uassert( 10187 , "need configdbs" , configHosts.size() );
2009-02-12 21:09:06 -05:00
string hn = getHostName();
if ( hn.empty() ) {
sleepsecs(5);
2009-08-07 15:37:50 -04:00
dbexit( EXIT_BADOPTIONS );
2009-02-12 21:09:06 -05:00
}
2011-01-04 00:40:41 -05:00
set<string> hosts;
2011-01-04 00:40:41 -05:00
for ( size_t i=0; i<configHosts.size(); i++ ) {
string host = configHosts[i];
hosts.insert( getHost( host , false ) );
configHosts[i] = getHost( host , true );
}
2011-01-04 00:40:41 -05:00
for ( set<string>::iterator i=hosts.begin(); i!=hosts.end(); i++ ) {
string host = *i;
bool ok = false;
2011-01-04 00:40:41 -05:00
for ( int x=10; x>0; x-- ) {
if ( ! hostbyname( host.c_str() ).empty() ) {
ok = true;
break;
2009-02-12 21:09:06 -05:00
}
log() << "can't resolve DNS for [" << host << "] sleeping and trying " << x << " more times" << endl;
sleepsecs( 10 );
2009-02-12 21:09:06 -05:00
}
if ( ! ok )
return false;
2009-02-12 21:09:06 -05:00
}
_config = configHosts;
2011-01-04 00:40:41 -05:00
2010-07-04 11:08:00 -04:00
string fullString;
joinStringDelim( configHosts, &fullString, ',' );
2011-02-14 14:16:18 -05:00
_primary.setAddress( ConnectionString( fullString , ConnectionString::SYNC ) );
2010-07-04 11:08:00 -04:00
log(1) << " config string : " << fullString << endl;
return true;
}
bool ConfigServer::checkConfigServersConsistent( string& errmsg , int tries ) const {
if ( tries <= 0 )
return false;
2011-01-04 00:40:41 -05:00
unsigned firstGood = 0;
int up = 0;
vector<BSONObj> res;
2011-01-04 00:40:41 -05:00
for ( unsigned i=0; i<_config.size(); i++ ) {
BSONObj x;
try {
ScopedDbConnection conn( _config[i], 30.0 );
if ( ! conn->simpleCommand( "config" , &x , "dbhash" ) )
x = BSONObj();
else {
x = x.getOwned();
if ( up == 0 )
firstGood = i;
up++;
}
conn.done();
}
catch ( SocketException& e ) {
warning() << " couldn't check on config server:" << _config[i] << " ok for now : " << e.toString() << endl;
}
res.push_back(x);
}
if ( _config.size() == 1 )
return true;
2011-01-04 00:40:41 -05:00
if ( up == 0 ) {
errmsg = "no config servers reachable";
return false;
}
2011-01-04 00:40:41 -05:00
if ( up == 1 ) {
log( LL_WARNING ) << "only 1 config server reachable, continuing" << endl;
return true;
}
BSONObj base = res[firstGood];
2011-01-04 00:40:41 -05:00
for ( unsigned i=firstGood+1; i<res.size(); i++ ) {
if ( res[i].isEmpty() )
continue;
string c1 = base.getFieldDotted( "collections.chunks" );
string c2 = res[i].getFieldDotted( "collections.chunks" );
2011-01-04 00:40:41 -05:00
string d1 = base.getFieldDotted( "collections.databases" );
string d2 = res[i].getFieldDotted( "collections.databases" );
if ( c1 == c2 && d1 == d2 )
continue;
2011-01-04 00:40:41 -05:00
stringstream ss;
ss << "config servers " << _config[firstGood] << " and " << _config[i] << " differ";
log( LL_WARNING ) << ss.str();
2011-01-04 00:40:41 -05:00
if ( tries <= 1 ) {
ss << "\n" << c1 << "\t" << c2 << "\n" << d1 << "\t" << d2;
errmsg = ss.str();
return false;
}
2011-01-04 00:40:41 -05:00
return checkConfigServersConsistent( errmsg , tries - 1 );
}
2011-01-04 00:40:41 -05:00
return true;
}
2011-01-04 00:40:41 -05:00
bool ConfigServer::ok( bool checkConsistency ) {
if ( ! _primary.ok() )
return false;
2011-01-04 00:40:41 -05:00
if ( checkConsistency ) {
2010-08-03 17:38:16 -04:00
string errmsg;
2011-01-04 00:40:41 -05:00
if ( ! checkConfigServersConsistent( errmsg ) ) {
2010-08-03 17:38:16 -04:00
log( LL_ERROR ) << "config servers not in sync! " << errmsg << endl;
return false;
}
}
2011-01-04 00:40:41 -05:00
2009-02-12 21:09:06 -05:00
return true;
}
2011-01-04 00:40:41 -05:00
bool ConfigServer::allUp() {
2009-12-02 16:36:46 -05:00
string errmsg;
return allUp( errmsg );
}
2011-01-04 00:40:41 -05:00
bool ConfigServer::allUp( string& errmsg ) {
try {
ScopedDbConnection conn( _primary, 30.0 );
conn->getLastError();
conn.done();
return true;
}
2011-01-04 00:40:41 -05:00
catch ( DBException& ) {
log() << "ConfigServer::allUp : " << _primary.toString() << " seems down!" << endl;
errmsg = _primary.toString() + " seems down";
return false;
}
2011-01-04 00:40:41 -05:00
}
2011-01-04 00:40:41 -05:00
int ConfigServer::dbConfigVersion() {
ScopedDbConnection conn( _primary, 30.0 );
int version = dbConfigVersion( conn.conn() );
conn.done();
return version;
}
2011-01-04 00:40:41 -05:00
int ConfigServer::dbConfigVersion( DBClientBase& conn ) {
auto_ptr<DBClientCursor> c = conn.query( "config.version" , BSONObj() );
int version = 0;
2011-01-04 00:40:41 -05:00
if ( c->more() ) {
BSONObj o = c->next();
version = o["version"].numberInt();
uassert( 10189 , "should only have 1 thing in config.version" , ! c->more() );
}
else {
2011-01-04 00:40:41 -05:00
if ( conn.count( ShardNS::shard ) || conn.count( ShardNS::database ) ) {
version = 1;
}
}
2011-01-04 00:40:41 -05:00
return version;
}
2011-01-04 00:40:41 -05:00
void ConfigServer::reloadSettings() {
set<string> got;
2011-01-04 00:40:41 -05:00
ScopedDbConnection conn( _primary, 30.0 );
2010-04-22 13:34:53 -04:00
auto_ptr<DBClientCursor> c = conn->query( ShardNS::settings , BSONObj() );
2010-08-02 14:44:53 -04:00
assert( c.get() );
2011-01-04 00:40:41 -05:00
while ( c->more() ) {
BSONObj o = c->next();
string name = o["_id"].valuestrsafe();
got.insert( name );
2011-01-04 00:40:41 -05:00
if ( name == "chunksize" ) {
log(1) << "MaxChunkSize: " << o["value"] << endl;
Chunk::MaxChunkSize = o["value"].numberInt() * 1024 * 1024;
}
2011-01-04 00:40:41 -05:00
else if ( name == "balancer" ) {
2010-04-22 13:34:53 -04:00
// ones we ignore here
}
else {
log() << "warning: unknown setting [" << name << "]" << endl;
}
}
2011-01-04 00:40:41 -05:00
if ( ! got.count( "chunksize" ) ) {
2010-04-22 13:34:53 -04:00
conn->insert( ShardNS::settings , BSON( "_id" << "chunksize" <<
"value" << (Chunk::MaxChunkSize / ( 1024 * 1024 ) ) ) );
}
2011-01-04 00:40:41 -05:00
2010-04-22 17:06:39 -04:00
// indexes
try {
conn->ensureIndex( ShardNS::chunk , BSON( "ns" << 1 << "min" << 1 ) , true );
conn->ensureIndex( ShardNS::chunk , BSON( "ns" << 1 << "shard" << 1 << "min" << 1 ) , true );
conn->ensureIndex( ShardNS::chunk , BSON( "ns" << 1 << "lastmod" << 1 ) , true );
conn->ensureIndex( ShardNS::shard , BSON( "host" << 1 ) , true );
}
2011-01-04 00:40:41 -05:00
catch ( std::exception& e ) {
log( LL_WARNING ) << "couldn't create indexes on config db: " << e.what() << endl;
}
conn.done();
}
2011-01-04 00:40:41 -05:00
string ConfigServer::getHost( string name , bool withPort ) {
if ( name.find( ":" ) != string::npos ) {
if ( withPort )
return name;
return name.substr( 0 , name.find( ":" ) );
}
2011-01-04 00:40:41 -05:00
if ( withPort ) {
stringstream ss;
ss << name << ":" << CmdLine::ConfigServerPort;
return ss.str();
}
2011-01-04 00:40:41 -05:00
return name;
}
2010-10-20 17:06:25 -04:00
/* must never throw */
2011-01-04 00:40:41 -05:00
void ConfigServer::logChange( const string& what , const string& ns , const BSONObj& detail ) {
2010-10-20 17:06:25 -04:00
string changeID;
2010-07-19 21:23:00 -04:00
2010-10-20 17:06:25 -04:00
try {
// get this entry's ID so we can use on the exception code path too
stringstream id;
static AtomicUInt num;
id << getHostNameCached() << "-" << terseCurrentTime() << "-" << num++;
changeID = id.str();
// send a copy of the message to the log in case it doesn't manage to reach config.changelog
Client* c = currentClient.get();
BSONObj msg = BSON( "_id" << changeID << "server" << getHostNameCached() << "clientAddr" << (c ? c->clientAddress(true) : "N/A")
2011-01-04 00:40:41 -05:00
<< "time" << DATENOW << "what" << what << "ns" << ns << "details" << detail );
log() << "about to log metadata event: " << msg << endl;
2010-10-20 17:06:25 -04:00
assert( _primary.ok() );
ScopedDbConnection conn( _primary, 30.0 );
2011-01-04 00:40:41 -05:00
2010-10-20 17:06:25 -04:00
static bool createdCapped = false;
2011-01-04 00:40:41 -05:00
if ( ! createdCapped ) {
2010-10-20 17:06:25 -04:00
try {
conn->createCollection( "config.changelog" , 1024 * 1024 * 10 , true );
}
2011-01-04 00:40:41 -05:00
catch ( UserException& e ) {
2010-10-20 17:06:25 -04:00
log(1) << "couldn't create changelog (like race condition): " << e << endl;
// don't care
}
createdCapped = true;
}
2011-01-04 00:40:41 -05:00
2010-10-20 17:06:25 -04:00
conn->insert( "config.changelog" , msg );
2010-04-20 17:07:10 -04:00
2010-10-20 17:06:25 -04:00
conn.done();
}
2010-10-20 17:06:25 -04:00
catch ( std::exception& e ) {
// if we got here, it means the config change is only in the log; it didn't make it to config.changelog
log() << "not logging config change: " << changeID << " " << e.what() << endl;
}
2010-04-20 17:07:10 -04:00
}
2011-01-04 00:40:41 -05:00
void ConfigServer::replicaSetChange( const ReplicaSetMonitor * monitor ) {
try {
ScopedDbConnection conn( configServer.getConnectionString(), 30.0 );
conn->update( ShardNS::shard , BSON( "_id" << monitor->getName() ) , BSON( "$set" << BSON( "host" << monitor->getServerAddress() ) ) );
conn.done();
}
2011-01-04 00:40:41 -05:00
catch ( DBException & ) {
error() << "RSChangeWatcher: could not update config db for set: " << monitor->getName() << " to: " << monitor->getServerAddress() << endl;
}
}
2011-01-04 00:40:41 -05:00
DBConfigPtr configServerPtr (new ConfigServer());
ConfigServer& configServer = dynamic_cast<ConfigServer&>(*configServerPtr);
2009-02-19 12:55:01 -05:00
2011-01-04 00:40:41 -05:00
}