00001
00002
00019
00020
00021
00022
00023
00024 #pragma once
00025
00026 #include "../db/namespace.h"
00027 #include "../client/dbclient.h"
00028 #include "../client/model.h"
00029
00030 #include "chunk.h"
00031 #include "shard.h"
00032 #include "shardkey.h"
00033
00034 namespace mongo {
00035
00036 struct ShardNS {
00037 static string shard;
00038
00039 static string database;
00040 static string collection;
00041 static string chunk;
00042
00043 static string mongos;
00044 static string settings;
00045 };
00046
00050 struct ShardFields {
00051 static BSONField<bool> draining;
00052 static BSONField<long long> maxSize;
00053 };
00054
00055 class ConfigServer;
00056
00057 class DBConfig;
00058 typedef boost::shared_ptr<DBConfig> DBConfigPtr;
00059
00060 extern DBConfigPtr configServerPtr;
00061 extern ConfigServer& configServer;
00062
00066 class DBConfig {
00067
00068 struct CollectionInfo {
00069 CollectionInfo() {
00070 _dirty = false;
00071 _dropped = false;
00072 }
00073
00074 CollectionInfo( const BSONObj& in );
00075
00076 bool isSharded() const {
00077 return _cm.get();
00078 }
00079
00080 ChunkManagerPtr getCM() const {
00081 return _cm;
00082 }
00083
00084 void shard( const string& ns , const ShardKeyPattern& key , bool unique );
00085 void unshard();
00086
00087 bool isDirty() const { return _dirty; }
00088 bool wasDropped() const { return _dropped; }
00089
00090 void save( const string& ns , DBClientBase* conn );
00091
00092
00093 private:
00094 ChunkManagerPtr _cm;
00095 bool _dirty;
00096 bool _dropped;
00097 };
00098
00099 typedef map<string,CollectionInfo> Collections;
00100
00101 public:
00102
00103 DBConfig( string name )
00104 : _name( name ) ,
00105 _primary("config","") ,
00106 _shardingEnabled(false),
00107 _lock("DBConfig") {
00108 assert( name.size() );
00109 }
00110 virtual ~DBConfig() {}
00111
00112 string getName() { return _name; };
00113
00117 bool isShardingEnabled() {
00118 return _shardingEnabled;
00119 }
00120
00121 void enableSharding();
00122 ChunkManagerPtr shardCollection( const string& ns , ShardKeyPattern fieldsAndOrder , bool unique );
00123
00127 bool removeSharding( const string& ns );
00128
00132 bool isSharded( const string& ns );
00133
00134 ChunkManagerPtr getChunkManager( const string& ns , bool reload = false );
00135
00140 const Shard& getShard( const string& ns );
00141
00142 const Shard& getPrimary() const {
00143 uassert( 8041 , (string)"no primary shard configured for db: " + _name , _primary.ok() );
00144 return _primary;
00145 }
00146
00147 void setPrimary( string s );
00148
00149 bool load();
00150 bool reload();
00151
00152 bool dropDatabase( string& errmsg );
00153
00154
00155
00156
00157 void serialize(BSONObjBuilder& to);
00158
00159 void unserialize(const BSONObj& from);
00160
00161 void getAllShards(set<Shard>& shards) const;
00162
00163 protected:
00164
00168 bool _isSharded( const string& ns );
00169
00170 bool _dropShardedCollections( int& num, set<Shard>& allServers , string& errmsg );
00171
00172 bool _load();
00173 bool _reload();
00174 void _save();
00175
00176 string _name;
00177 Shard _primary;
00178 bool _shardingEnabled;
00179
00180
00181
00182
00183 Collections _collections;
00184
00185 mongo::mutex _lock;
00186 };
00187
00188 class ConfigServer : public DBConfig {
00189 public:
00190
00191 ConfigServer();
00192 ~ConfigServer();
00193
00194 bool ok( bool checkConsistency = false );
00195
00196 virtual string modelServer() {
00197 uassert( 10190 , "ConfigServer not setup" , _primary.ok() );
00198 return _primary.getConnString();
00199 }
00200
00204 bool init( vector<string> configHosts );
00205
00206 bool init( string s );
00207
00208 bool allUp();
00209 bool allUp( string& errmsg );
00210
00211 int dbConfigVersion();
00212 int dbConfigVersion( DBClientBase& conn );
00213
00214 void reloadSettings();
00215
00219 int checkConfigVersion( bool upgrade );
00220
00230 void logChange( const string& what , const string& ns , const BSONObj& detail = BSONObj() );
00231
00232 ConnectionString getConnectionString() const {
00233 return ConnectionString( _primary.getConnString() , ConnectionString::SYNC );
00234 }
00235
00236 void replicaSetChange( const ReplicaSetMonitor * monitor );
00237
00238 static int VERSION;
00239
00240
00245 bool checkConfigServersConsistent( string& errmsg , int tries = 4 ) const;
00246
00247 private:
00248 string getHost( string name , bool withPort );
00249 vector<string> _config;
00250 };
00251
00252 }