0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00
mongodb/s/config.h

169 lines
4.7 KiB
C
Raw Normal View History

2009-02-13 03:03:46 +01:00
// config.h
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/>.
*/
/* This file is things related to the "grid configuration":
- what machines make up the db component of our cloud
- where various ranges of things live
*/
#pragma once
2009-02-06 21:44:21 +01:00
#include "../db/namespace.h"
2008-10-20 00:46:53 +02:00
#include "../client/dbclient.h"
#include "../client/model.h"
2009-02-26 16:14:50 +01:00
#include "shardkey.h"
2008-10-13 23:58:51 +02:00
2009-01-14 23:09:51 +01:00
namespace mongo {
2009-02-08 23:55:33 +01:00
class Grid;
2009-02-13 03:09:06 +01:00
class ConfigServer;
2009-02-08 23:55:33 +01:00
2009-02-13 03:09:06 +01:00
extern ConfigServer configServer;
extern Grid grid;
2009-02-19 18:55:01 +01:00
class ChunkManager;
2009-09-03 22:48:34 +02:00
class CollectionInfo {
public:
CollectionInfo( ShardKeyPattern _key = BSONObj() , bool _unique = false ) :
key( _key ) , unique( _unique ){}
ShardKeyPattern key;
bool unique;
};
2009-02-06 21:44:21 +01:00
/**
top level grid configuration for an entire database
*/
class DBConfig : public Model {
public:
2009-09-01 18:17:41 +02:00
DBConfig( string name = "" ) : _name( name ) , _primary("") , _shardingEnabled(false){ }
string getName(){ return _name; };
2009-02-17 17:41:34 +01:00
/**
* @return if anything in this db is partitioned or not
*/
2009-09-01 18:17:41 +02:00
bool isShardingEnabled(){
return _shardingEnabled;
2009-02-17 17:41:34 +01:00
}
2009-09-01 18:17:41 +02:00
void enableSharding();
2009-09-03 22:48:34 +02:00
ChunkManager* shardCollection( const string& ns , ShardKeyPattern fieldsAndOrder , bool unique );
2009-02-06 21:44:21 +01:00
/**
* @return whether or not this partition is partitioned
*/
2009-09-01 18:17:41 +02:00
bool isSharded( const string& ns );
2009-02-06 21:44:21 +01:00
ChunkManager* getChunkManager( const string& ns , bool reload = false );
2009-02-06 21:44:21 +01:00
/**
* @return the correct for shard for the ns
2009-09-01 18:17:41 +02:00
* if the namespace is sharded, will return an empty string
2009-02-06 21:44:21 +01:00
*/
string getShard( const string& ns );
2009-02-06 21:44:21 +01:00
string getPrimary(){
if ( _primary.size() == 0 )
throw UserException( (string)"no primary shard configured for db: " + _name );
2009-02-06 21:44:21 +01:00
return _primary;
}
2009-02-13 22:18:14 +01:00
void setPrimary( string s ){
_primary = s;
}
2009-02-18 19:54:22 +01:00
virtual void save( bool check=true);
virtual string modelServer();
2009-02-18 19:54:22 +01:00
2009-02-06 21:44:21 +01:00
// model stuff
2009-02-08 23:55:33 +01:00
virtual const char * getNS(){ return "config.databases"; }
2009-02-06 21:44:21 +01:00
virtual void serialize(BSONObjBuilder& to);
2009-02-27 16:37:13 +01:00
virtual void unserialize(const BSONObj& from);
2009-02-06 21:44:21 +01:00
bool loadByName(const char *nm);
2009-02-18 19:54:22 +01:00
2009-02-06 21:44:21 +01:00
protected:
string _name; // e.g. "alleyinsider"
string _primary; // e.g. localhost , mongo.foo.com:9999
2009-09-01 18:17:41 +02:00
bool _shardingEnabled;
2009-02-19 18:55:01 +01:00
2009-09-03 22:48:34 +02:00
map<string,CollectionInfo> _sharded; // { "alleyinsider.blog.posts" : { ts : 1 } , ... ] - all ns that are sharded
map<string,ChunkManager*> _shards; // this will only have entries for things that have been looked at
2009-02-08 23:55:33 +01:00
friend class Grid;
};
class Grid {
public:
2009-02-05 22:45:58 +01:00
/**
gets the config the db.
will return an empty DBConfig if not in db already
*/
DBConfig * getDBConfig( string ns , bool create=true);
2009-02-08 23:55:33 +01:00
string pickShardForNewDB();
2009-02-08 23:55:33 +01:00
bool knowAboutShard( string name ) const;
2009-03-30 19:28:49 +02:00
unsigned long long getNextOpTime() const;
2009-02-05 22:45:58 +01:00
private:
map<string,DBConfig*> _databases;
};
2009-02-13 03:09:06 +01:00
class ConfigServer : public DBConfig {
public:
enum { Port = 27016 }; /* standard port # for a grid db */
ConfigServer();
~ConfigServer();
bool ok(){
// TODO: check can connect
return _primary.size() > 0;
}
virtual string modelServer(){
uassert( "ConfigServer not setup" , _primary.size() );
return _primary;
}
/**
call at startup, this will initiate connection to the grid db
*/
bool init( vector<string> configHosts , bool infer );
int dbConfigVersion();
int dbConfigVersion( DBClientBase& conn );
/**
* @return 0 = ok, otherwise error #
*/
int checkConfigVersion();
static int VERSION;
2009-01-14 23:09:51 +01:00
private:
string getHost( string name , bool withPort );
};
2009-01-14 23:09:51 +01:00
} // namespace mongo