0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +01:00
mongodb/s/config.cpp

376 lines
11 KiB
C++
Raw Normal View History

2009-02-13 03:03:46 +01:00
// config.cpp
2008-10-13 23:58:51 +02:00
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
2008-10-13 23:58:51 +02: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-29 02:28:49 +01:00
*
2008-10-13 23:58:51 +02: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-29 02:28:49 +01:00
*
2008-10-13 23:58:51 +02: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/>.
*/
#include "stdafx.h"
2009-02-03 19:30:28 +01:00
#include "../util/message.h"
2008-10-13 23:58:51 +02:00
#include "../util/unittest.h"
2008-11-01 01:17:54 +01:00
#include "../client/connpool.h"
2009-02-06 21:44:21 +01:00
#include "../client/model.h"
#include "../db/pdfile.h"
2009-02-13 03:09:06 +01:00
#include "server.h"
2009-02-13 03:03:46 +01:00
#include "config.h"
2009-02-19 18:55:01 +01:00
#include "shard.h"
2008-10-13 23:58:51 +02:00
2009-01-14 23:09:51 +01:00
namespace mongo {
2009-02-06 21:44:21 +01:00
/* --- DBConfig --- */
2008-10-20 00:46:53 +02:00
string DBConfig::modelServer() {
return configServer.modelServer();
}
2009-02-17 17:41:34 +01:00
2009-02-19 18:55:01 +01:00
bool DBConfig::sharded( const string& ns ){
2009-02-06 21:44:21 +01:00
if ( ! _partitioned )
return false;
return _sharded.find( ns ) != _sharded.end();
2009-02-06 21:44:21 +01:00
}
2009-02-19 18:55:01 +01:00
string DBConfig::getServer( const string& ns ){
2009-02-17 17:41:34 +01:00
if ( sharded( ns ) )
return "";
2009-02-06 21:44:21 +01:00
uassert( "no primary!" , _primary.size() );
2009-02-06 21:44:21 +01:00
return _primary;
}
2009-02-19 18:55:01 +01:00
void DBConfig::turnOnPartitioning(){
_partitioned = true;
}
ShardManager* DBConfig::turnOnSharding( const string& ns , ShardKeyPattern fieldsAndOrder ){
2009-02-19 18:55:01 +01:00
if ( ! _partitioned )
throw UserException( "not partitioned" );
ShardManager * info = _shards[ns];
2009-02-19 18:55:01 +01:00
if ( info )
return info;
if ( sharded( ns ) )
throw UserException( "already sharded" );
_sharded[ns] = fieldsAndOrder;
2009-02-19 18:55:01 +01:00
info = new ShardManager( this , ns , fieldsAndOrder );
2009-02-19 18:55:01 +01:00
_shards[ns] = info;
return info;
}
2009-02-06 21:44:21 +01:00
ShardManager* DBConfig::getShardManager( const string& ns ){
ShardManager* m = _shards[ns];
if ( m )
return m;
uassert( (string)"not sharded:" + ns , sharded( ns ) );
m = new ShardManager( this , ns , _sharded[ ns ] );
_shards[ns] = m;
return m;
}
2009-02-06 21:44:21 +01:00
void DBConfig::serialize(BSONObjBuilder& to){
to.append("name", _name);
to.appendBool("partitioned", _partitioned );
to.append("primary", _primary );
2009-02-19 18:55:01 +01:00
if ( _sharded.size() > 0 ){
BSONObjBuilder a;
2009-02-26 16:14:50 +01:00
for ( map<string,ShardKeyPattern>::reverse_iterator i=_sharded.rbegin(); i != _sharded.rend(); i++){
a.append( i->first.c_str() , i->second.key() );
2009-02-19 18:55:01 +01:00
}
to.appendArray( "sharded" , a.obj() );
}
2009-02-06 21:44:21 +01:00
}
2009-02-27 16:37:13 +01:00
void DBConfig::unserialize(const BSONObj& from){
2009-02-06 21:44:21 +01:00
_name = from.getStringField("name");
_partitioned = from.getBoolField("partitioned");
_primary = from.getStringField("primary");
2009-02-19 18:55:01 +01:00
_sharded.clear();
BSONObj sharded = from.getObjectField( "sharded" );
if ( ! sharded.isEmpty() ){
BSONObjIterator i(sharded);
while ( i.more() ){
BSONElement e = i.next();
if ( e.eoo() )
break;
uassert( "shared things have to be objects" , e.type() == Object );
_sharded[e.fieldName()] = e.embeddedObject();
2009-02-19 18:55:01 +01:00
}
}
2009-02-06 21:44:21 +01:00
}
2009-02-05 22:45:58 +01:00
void DBConfig::save( bool check ){
Model::save( check );
for ( map<string,ShardManager*>::iterator i=_shards.begin(); i != _shards.end(); i++)
i->second->save();
}
2009-02-06 21:44:21 +01:00
bool DBConfig::loadByName(const char *nm){
BSONObjBuilder b;
b.append("name", nm);
BSONObj q = b.done();
return load(q);
}
/* --- Grid --- */
2009-02-08 23:55:33 +01:00
string Grid::pickServerForNewDB(){
2009-02-09 19:34:40 +01:00
ScopedDbConnection conn( configServer.getPrimary() );
2009-02-08 23:55:33 +01:00
// TODO: this is temporary
vector<string> all;
auto_ptr<DBClientCursor> c = conn->query( "config.servers" , Query() );
while ( c->more() ){
BSONObj s = c->next();
all.push_back( s["host"].valuestrsafe() );
}
2009-02-09 19:34:40 +01:00
conn.done();
2009-02-14 15:42:18 +01:00
if ( all.size() == 0 )
return "";
return all[ rand() % all.size() ];
2009-02-08 23:55:33 +01:00
}
bool Grid::knowAboutServer( string name ) const{
ScopedDbConnection conn( configServer.getPrimary() );
BSONObj server = conn->findOne( "config.servers" , BSON( "host" << name ) );
conn.done();
return ! server.isEmpty();
}
DBConfig* Grid::getDBConfig( string database , bool create ){
2009-02-05 22:45:58 +01:00
{
string::size_type i = database.find( "." );
if ( i != string::npos )
database = database.substr( 0 , i );
}
if ( database == "config" )
2009-02-06 21:44:21 +01:00
return &configServer;
2009-02-05 22:45:58 +01:00
DBConfig*& cc = _databases[database];
if ( cc == 0 ){
cc = new DBConfig( database );
if ( ! cc->loadByName(database.c_str()) ){
if ( create ){
// note here that cc->primary == 0.
log() << "couldn't find database [" << database << "] in config db" << endl;
2009-02-14 15:42:18 +01:00
if ( database == "admin" )
cc->_primary = configServer.getPrimary();
else
cc->_primary = pickServerForNewDB();
if ( cc->_primary.size() ){
cc->save();
log() << "\t put [" << database << "] on: " << cc->_primary << endl;
}
else {
log() << "\t can't find a server" << endl;
cc = 0;
}
}
else {
cc = 0;
}
}
2008-10-20 00:46:53 +02:00
}
2009-02-05 22:45:58 +01:00
return cc;
2008-10-20 00:46:53 +02:00
}
2009-02-13 03:09:06 +01:00
/* --- ConfigServer ---- */
ConfigServer::ConfigServer() {
_partitioned = false;
_primary = "";
_name = "grid";
}
2009-02-05 22:45:58 +01:00
2009-02-13 03:09:06 +01:00
ConfigServer::~ConfigServer() {
}
bool ConfigServer::init( vector<string> configHosts , bool infer ){
string hn = getHostName();
if ( hn.empty() ) {
sleepsecs(5);
exit(16);
}
ourHostname = hn;
char buf[256];
strcpy(buf, hn.c_str());
if ( configHosts.empty() ) {
char *p = strchr(buf, '-');
if ( p )
p = strchr(p+1, '-');
if ( !p ) {
log() << "can't parse server's hostname, expect <city>-<locname>-n<nodenum>, got: " << buf << endl;
sleepsecs(5);
exit(17);
}
p[1] = 0;
}
string left, right; // with :port#
string hostLeft, hostRight;
if ( configHosts.empty() ) {
if ( ! infer ) {
out() << "--configdb or --infer required\n";
2009-02-13 03:09:06 +01:00
exit(7);
}
stringstream sl, sr;
sl << buf << "grid-l";
sr << buf << "grid-r";
hostLeft = sl.str();
hostRight = sr.str();
sl << ":" << Port;
sr << ":" << Port;
left = sl.str();
right = sr.str();
}
else {
hostLeft = getHost( configHosts[0] , false );
left = getHost( configHosts[0] , true );
2009-02-13 03:09:06 +01:00
if ( configHosts.size() > 1 ) {
hostRight = getHost( configHosts[1] , false );
right = getHost( configHosts[1] , true );
2009-02-13 03:09:06 +01:00
}
}
2009-02-13 03:09:06 +01:00
if ( !isdigit(left[0]) )
/* this loop is not really necessary, we we print out if we can't connect
but it gives much prettier error msg this way if the config is totally
wrong so worthwhile.
*/
while ( 1 ) {
2009-02-17 16:26:15 +01:00
if ( hostbyname(hostLeft.c_str()).empty() ) {
2009-02-13 03:09:06 +01:00
log() << "can't resolve DNS for " << hostLeft << ", sleeping and then trying again" << endl;
sleepsecs(15);
continue;
}
if ( !hostRight.empty() && hostbyname(hostRight.c_str()).empty() ) {
log() << "can't resolve DNS for " << hostRight << ", sleeping and then trying again" << endl;
sleepsecs(15);
continue;
}
break;
}
2009-02-17 16:26:15 +01:00
2009-02-13 03:09:06 +01:00
Nullstream& l = log();
l << "connecting to griddb ";
if ( !hostRight.empty() ) {
// connect in paired mode
l << "L:" << left << " R:" << right << "...";
l.flush();
_primary = left + "," + right;
}
else {
l << left << "...";
l.flush();
_primary = left;
}
return true;
}
2009-01-14 23:09:51 +01:00
string ConfigServer::getHost( string name , bool withPort ){
if ( name.find( ":" ) ){
if ( withPort )
return name;
return name.substr( 0 , name.find( ":" ) );
}
if ( withPort ){
stringstream ss;
ss << name << ":" << Port;
return ss.str();
}
return name;
}
2009-02-13 03:09:06 +01:00
ConfigServer configServer;
2009-02-06 21:44:21 +01:00
Grid grid;
2009-02-19 18:55:01 +01:00
class DBConfigUnitTest : public UnitTest {
public:
void testInOut( DBConfig& c , BSONObj o ){
c.unserialize( o );
BSONObjBuilder b;
c.serialize( b );
BSONObj out = b.obj();
if ( o.toString() == out.toString() )
return;
log() << "DBConfig serialization broken\n"
<< "in : " << o.toString() << "\n"
<< "out : " << out.toString()
<< endl;
assert(0);
}
void a(){
BSONObjBuilder b;
b << "name" << "abc";
b.appendBool( "partitioned" , true );
b << "primary" << "myserver";
DBConfig c;
testInOut( c , b.obj() );
}
void b(){
BSONObjBuilder b;
b << "name" << "abc";
b.appendBool( "partitioned" , true );
b << "primary" << "myserver";
BSONObjBuilder a;
a << "abc.foo" << BSON( "a" << 1 );
a << "abc.bar" << BSON( "b" << -1 );
2009-02-19 18:55:01 +01:00
b.appendArray( "sharded" , a.obj() );
DBConfig c;
testInOut( c , b.obj() );
assert( c.sharded( "abc.foo" ) );
assert( ! c.sharded( "abc.food" ) );
2009-02-19 18:55:01 +01:00
}
2009-02-19 18:55:01 +01:00
void run(){
a();
b();
}
} dbConfigUnitTest;
2009-02-05 22:45:58 +01:00
}