00001
00002
00019 #pragma once
00020
00021 #include "../pch.h"
00022 #include "../client/connpool.h"
00023
00024 namespace mongo {
00025
00026 class ShardConnection;
00027 class ShardStatus;
00028
00029
00030
00031
00032
00033
00034 class Shard {
00035 public:
00036 Shard()
00037 : _name("") , _addr("") , _maxSize(0) , _isDraining( false ) {
00038 }
00039
00040 Shard( const string& name , const string& addr, long long maxSize = 0 , bool isDraining = false )
00041 : _name(name) , _addr( addr ) , _maxSize( maxSize ) , _isDraining( isDraining ) {
00042 _setAddr( addr );
00043 }
00044
00045 Shard( const string& ident ) {
00046 reset( ident );
00047 }
00048
00049 Shard( const Shard& other )
00050 : _name( other._name ) , _addr( other._addr ) , _cs( other._cs ) ,
00051 _maxSize( other._maxSize ) , _isDraining( other._isDraining ) , _rs( other._rs ) {
00052 }
00053
00054 Shard( const Shard* other )
00055 : _name( other->_name ) , _addr( other->_addr ), _cs( other->_cs ) ,
00056 _maxSize( other->_maxSize ) , _isDraining( other->_isDraining ) , _rs( other->_rs ) {
00057 }
00058
00059 static Shard make( const string& ident ) {
00060 Shard s;
00061 s.reset( ident );
00062 return s;
00063 }
00064
00068 void reset( const string& ident );
00069
00070 void setAddress( const ConnectionString& cs );
00071
00072 ConnectionString getAddress() const { return _cs; }
00073
00074 string getName() const {
00075 assert( _name.size() );
00076 return _name;
00077 }
00078
00079 string getConnString() const {
00080 assert( _addr.size() );
00081 return _addr;
00082 }
00083
00084 long long getMaxSize() const {
00085 return _maxSize;
00086 }
00087
00088 bool isDraining() const {
00089 return _isDraining;
00090 }
00091
00092 string toString() const {
00093 return _name + ":" + _addr;
00094 }
00095
00096 friend ostream& operator << (ostream& out, const Shard& s) {
00097 return (out << s.toString());
00098 }
00099
00100 bool operator==( const Shard& s ) const {
00101 bool n = _name == s._name;
00102 bool a = _addr == s._addr;
00103
00104 assert( n == a );
00105 return n;
00106 }
00107
00108 bool operator!=( const Shard& s ) const {
00109 bool n = _name == s._name;
00110 bool a = _addr == s._addr;
00111 return ! ( n && a );
00112 }
00113
00114
00115 bool operator==( const string& s ) const {
00116 return _name == s || _addr == s;
00117 }
00118
00119 bool operator!=( const string& s ) const {
00120 return _name != s && _addr != s;
00121 }
00122
00123 bool operator<(const Shard& o) const {
00124 return _name < o._name;
00125 }
00126
00127 bool ok() const {
00128 return _addr.size() > 0 && _addr.size() > 0;
00129 }
00130
00131 BSONObj runCommand( const string& db , const string& simple ) const {
00132 return runCommand( db , BSON( simple << 1 ) );
00133 }
00134 BSONObj runCommand( const string& db , const BSONObj& cmd ) const ;
00135
00136 ShardStatus getStatus() const ;
00137
00143 bool containsNode( const string& node ) const;
00144
00145 static void getAllShards( vector<Shard>& all );
00146 static void printShardInfo( ostream& out );
00147
00152 static Shard pick( const Shard& current = EMPTY );
00153
00154 static void reloadShardInfo();
00155
00156 static void removeShard( const string& name );
00157
00158 static bool isAShardNode( const string& ident );
00159
00160 static Shard EMPTY;
00161
00162 private:
00163
00164 void _rsInit();
00165 void _setAddr( const string& addr );
00166
00167 string _name;
00168 string _addr;
00169 ConnectionString _cs;
00170 long long _maxSize;
00171 bool _isDraining;
00172 ReplicaSetMonitorPtr _rs;
00173 };
00174
00175 class ShardStatus {
00176 public:
00177
00178 ShardStatus( const Shard& shard , const BSONObj& obj );
00179
00180 friend ostream& operator << (ostream& out, const ShardStatus& s) {
00181 out << s.toString();
00182 return out;
00183 }
00184
00185 string toString() const {
00186 stringstream ss;
00187 ss << "shard: " << _shard << " mapped: " << _mapped << " writeLock: " << _writeLock;
00188 return ss.str();
00189 }
00190
00191 bool operator<( const ShardStatus& other ) const {
00192 return _mapped < other._mapped;
00193 }
00194
00195 Shard shard() const {
00196 return _shard;
00197 }
00198
00199 long long mapped() const {
00200 return _mapped;
00201 }
00202
00203 bool hasOpsQueued() const {
00204 return _hasOpsQueued;
00205 }
00206
00207 private:
00208 Shard _shard;
00209 long long _mapped;
00210 bool _hasOpsQueued;
00211 double _writeLock;
00212 };
00213
00214 class ShardConnection : public AScopedConnection {
00215 public:
00216 ShardConnection( const Shard * s , const string& ns );
00217 ShardConnection( const Shard& s , const string& ns );
00218 ShardConnection( const string& addr , const string& ns );
00219
00220 ~ShardConnection();
00221
00222 void done();
00223 void kill();
00224
00225 DBClientBase& conn() {
00226 _finishInit();
00227 assert( _conn );
00228 return *_conn;
00229 }
00230
00231 DBClientBase* operator->() {
00232 _finishInit();
00233 assert( _conn );
00234 return _conn;
00235 }
00236
00237 DBClientBase* get() {
00238 _finishInit();
00239 assert( _conn );
00240 return _conn;
00241 }
00242
00243 string getHost() const {
00244 return _addr;
00245 }
00246
00247 bool setVersion() {
00248 _finishInit();
00249 return _setVersion;
00250 }
00251
00252 static void sync();
00253
00254 void donotCheckVersion() {
00255 _setVersion = false;
00256 _finishedInit = true;
00257 }
00258
00262 bool runCommand( const string& db , const BSONObj& cmd , BSONObj& res );
00263
00265 static void checkMyConnectionVersions( const string & ns );
00266
00267 private:
00268 void _init();
00269 void _finishInit();
00270
00271 bool _finishedInit;
00272
00273 string _addr;
00274 string _ns;
00275 DBClientBase* _conn;
00276 bool _setVersion;
00277 };
00278 }