00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 #include "../pch.h"
00021 #include "dbclient.h"
00022
00023 namespace mongo {
00024
00025 class ReplicaSetMonitor;
00026 typedef shared_ptr<ReplicaSetMonitor> ReplicaSetMonitorPtr;
00027
00035 class ReplicaSetMonitor {
00036 public:
00037
00038 typedef boost::function1<void,const ReplicaSetMonitor*> ConfigChangeHook;
00039
00043 static ReplicaSetMonitorPtr get( const string& name , const vector<HostAndPort>& servers );
00044
00049 static void checkAll();
00050
00057 static void setConfigChangeHook( ConfigChangeHook hook );
00058
00059 ~ReplicaSetMonitor();
00060
00062 HostAndPort getMaster();
00063
00067 void notifyFailure( const HostAndPort& server );
00068
00070 HostAndPort getSlave( const HostAndPort& prev );
00071
00073 HostAndPort getSlave();
00074
00075
00079 void notifySlaveFailure( const HostAndPort& server );
00080
00084 void check();
00085
00086 string getName() const { return _name; }
00087
00088 string getServerAddress() const;
00089
00090 bool contains( const string& server ) const;
00091
00092 private:
00099 ReplicaSetMonitor( const string& name , const vector<HostAndPort>& servers );
00100
00101 void _check();
00102
00107 void _checkStatus(DBClientConnection *conn);
00108
00115 void _checkHosts(const BSONObj& hostList, bool& changed);
00116
00124 bool _checkConnection( DBClientConnection * c , string& maybePrimary , bool verbose );
00125
00126 int _find( const string& server ) const ;
00127 int _find( const HostAndPort& server ) const ;
00128
00129 mutable mongo::mutex _lock;
00130 mutable mongo::mutex _checkConnectionLock;
00131
00132 string _name;
00133 struct Node {
00134 Node( const HostAndPort& a , DBClientConnection* c ) : addr( a ) , conn(c) , ok(true) {}
00135 HostAndPort addr;
00136 DBClientConnection* conn;
00137
00138
00139
00140
00141 bool ok;
00142 };
00143
00147 vector<Node> _nodes;
00148
00149 int _master;
00150
00151
00152 static mongo::mutex _setsLock;
00153 static map<string,ReplicaSetMonitorPtr> _sets;
00154
00155 static ConfigChangeHook _hook;
00156 };
00157
00166 class DBClientReplicaSet : public DBClientBase {
00167
00168 public:
00170 DBClientReplicaSet( const string& name , const vector<HostAndPort>& servers );
00171 virtual ~DBClientReplicaSet();
00172
00178 bool connect();
00179
00182 virtual bool auth(const string &dbname, const string &username, const string &pwd, string& errmsg, bool digestPassword = true );
00183
00184
00185
00187 virtual auto_ptr<DBClientCursor> query(const string &ns, Query query, int nToReturn = 0, int nToSkip = 0,
00188 const BSONObj *fieldsToReturn = 0, int queryOptions = 0 , int batchSize = 0 );
00189
00191 virtual BSONObj findOne(const string &ns, const Query& query, const BSONObj *fieldsToReturn = 0, int queryOptions = 0);
00192
00193 virtual void insert( const string &ns , BSONObj obj );
00194
00197 virtual void insert( const string &ns, const vector< BSONObj >& v );
00198
00199 virtual void remove( const string &ns , Query obj , bool justOne = 0 );
00200
00201 virtual void update( const string &ns , Query query , BSONObj obj , bool upsert = 0 , bool multi = 0 );
00202
00203 virtual void killCursor( long long cursorID );
00204
00205
00206
00207 DBClientConnection& masterConn();
00208 DBClientConnection& slaveConn();
00209
00210
00211
00212 virtual void checkResponse( const char *data, int nReturned ) { checkMaster()->checkResponse( data , nReturned ); }
00213
00214
00215
00216 void isntMaster();
00217
00218
00219
00220 virtual bool isFailed() const { return ! _master || _master->isFailed(); }
00221
00222
00223
00224 string toString() { return getServerAddress(); }
00225
00226 string getServerAddress() const { return _monitor->getServerAddress(); }
00227
00228 virtual ConnectionString::ConnectionType type() const { return ConnectionString::SET; }
00229
00230
00231
00232 virtual bool call( Message &toSend, Message &response, bool assertOk=true , string * actualServer = 0 );
00233 virtual void say( Message &toSend ) { checkMaster()->say( toSend ); }
00234 virtual bool callRead( Message& toSend , Message& response ) { return checkMaster()->callRead( toSend , response ); }
00235
00236
00237 protected:
00238 virtual void sayPiggyBack( Message &toSend ) { checkMaster()->say( toSend ); }
00239
00240 private:
00241
00242 DBClientConnection * checkMaster();
00243 DBClientConnection * checkSlave();
00244
00245 void _auth( DBClientConnection * conn );
00246
00247 ReplicaSetMonitorPtr _monitor;
00248
00249 HostAndPort _masterHost;
00250 scoped_ptr<DBClientConnection> _master;
00251
00252 HostAndPort _slaveHost;
00253 scoped_ptr<DBClientConnection> _slave;
00254
00259 struct AuthInfo {
00260 AuthInfo( string d , string u , string p , bool di )
00261 : dbname( d ) , username( u ) , pwd( p ) , digestPassword( di ) {}
00262 string dbname;
00263 string username;
00264 string pwd;
00265 bool digestPassword;
00266 };
00267
00268
00269
00270
00271
00272 list<AuthInfo> _auths;
00273 };
00274
00275
00276 }