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

219 lines
6.0 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"
2010-04-19 22:55:16 +02:00
#include "shard.h"
2008-10-13 23:58:51 +02:00
2009-01-14 23:09:51 +01:00
namespace mongo {
struct ShardNS {
static string database;
static string shard;
static string chunk;
2010-04-22 19:34:53 +02:00
static string mongos;
static string settings;
};
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
/**
2009-12-02 22:36:46 +01:00
* top level grid configuration for an entire database
* TODO: use shared_ptr for ChunkManager
2009-02-06 21:44:21 +01:00
*/
class DBConfig : public Model {
public:
DBConfig( string name = "" ) : _name( name ) , _primary("config","") , _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
2010-04-19 22:55:16 +02:00
* if the namespace is sharded, will return NULL
2009-02-06 21:44:21 +01:00
*/
2010-04-19 22:55:16 +02:00
const Shard& getShard( const string& ns );
2009-02-06 21:44:21 +01:00
2010-04-19 22:55:16 +02:00
const Shard& getPrimary(){
uassert( 8041 , (string)"no primary shard configured for db: " + _name , _primary.ok() );
2009-02-06 21:44:21 +01:00
return _primary;
}
2009-02-13 22:18:14 +01:00
void setPrimary( string s ){
_primary.reset( s );
2009-02-13 22:18:14 +01:00
}
bool reload();
2009-12-02 22:36:46 +01:00
bool dropDatabase( string& errmsg );
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-18 19:54:22 +01:00
2009-02-06 21:44:21 +01:00
protected:
2009-12-02 22:36:46 +01:00
2010-04-27 23:22:17 +02:00
/**
lockless
*/
bool _isSharded( const string& ns );
bool _dropShardedCollections( int& num, set<Shard>& allServers , string& errmsg );
bool doload();
/**
@return true if there was sharding info to remove
*/
bool removeSharding( const string& ns );
2009-02-06 21:44:21 +01:00
string _name; // e.g. "alleyinsider"
2010-04-19 22:55:16 +02:00
Shard _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
2010-04-27 23:22:17 +02:00
mongo::mutex _lock; // TODO: change to r/w lock ??
2009-02-08 23:55:33 +01:00
friend class Grid;
friend class ChunkManager;
};
2009-12-02 22:36:46 +01:00
/**
* stores meta-information about the grid
* TODO: used shard_ptr for DBConfig pointers
*/
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
2009-12-02 22:36:46 +01:00
/**
* removes db entry.
* on next getDBConfig call will fetch from db
*/
void removeDB( string db );
2009-02-08 23:55:33 +01:00
bool knowAboutShard( string name ) const;
2010-04-27 23:22:17 +02:00
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;
2010-04-27 23:22:17 +02:00
mongo::mutex _lock; // TODO: change to r/w lock ??
};
2009-02-13 03:09:06 +01:00
class ConfigServer : public DBConfig {
public:
ConfigServer();
~ConfigServer();
bool ok(){
// TODO: check can connect
2010-04-19 22:55:16 +02:00
return _primary.ok();
2009-02-13 03:09:06 +01:00
}
virtual string modelServer(){
2010-04-19 22:55:16 +02:00
uassert( 10190 , "ConfigServer not setup" , _primary.ok() );
return _primary.getConnString();
2009-02-13 03:09:06 +01:00
}
2009-02-13 03:09:06 +01:00
/**
call at startup, this will initiate connection to the grid db
*/
bool init( vector<string> configHosts );
bool allUp();
2009-12-02 22:36:46 +01:00
bool allUp( string& errmsg );
int dbConfigVersion();
int dbConfigVersion( DBClientBase& conn );
void reloadSettings();
/**
* @return 0 = ok, otherwise error #
*/
int checkConfigVersion( bool upgrade );
2010-04-20 23:07:10 +02:00
/**
* log a change to config.changes
* @param what e.g. "split" , "migrate"
* @param msg any more info
*/
void logChange( const string& what , const string& ns , const BSONObj& detail = BSONObj() );
static int VERSION;
private:
string getHost( string name , bool withPort );
};
2009-01-14 23:09:51 +01:00
} // namespace mongo