MongoDB C++ Driver  legacy-1.1.2
dbclient_rs.h
Go to the documentation of this file.
1 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <boost/shared_ptr.hpp>
21 #include <utility>
22 
24 #include "mongo/client/export_macros.h"
25 #include "mongo/util/net/hostandport.h"
26 
27 namespace mongo {
28 
29 class ReplicaSetMonitor;
30 class TagSet;
31 struct ReadPreferenceSetting;
32 typedef boost::shared_ptr<ReplicaSetMonitor> ReplicaSetMonitorPtr;
33 
42 class MONGO_CLIENT_API DBClientReplicaSet : public DBClientBase {
43 public:
44  using DBClientBase::query;
47 
50  DBClientReplicaSet(const std::string& name,
51  const std::vector<HostAndPort>& servers,
52  double so_timeout = 0);
53  virtual ~DBClientReplicaSet();
54 
60  bool connect();
61 
69  virtual void logout(const std::string& dbname, BSONObj& info);
70 
71  // ----------- simple functions --------------
72 
74  virtual std::auto_ptr<DBClientCursor> query(const std::string& ns,
75  Query query,
76  int nToReturn = 0,
77  int nToSkip = 0,
78  const BSONObj* fieldsToReturn = 0,
79  int queryOptions = 0,
80  int batchSize = 0);
81 
83  virtual BSONObj findOne(const std::string& ns,
84  const Query& query,
85  const BSONObj* fieldsToReturn = 0,
86  int queryOptions = 0);
87 
88  virtual void insert(const std::string& ns,
89  BSONObj obj,
90  int flags = 0,
91  const WriteConcern* wc = NULL);
92 
93  virtual void insert(const std::string& ns,
94  const std::vector<BSONObj>& v,
95  int flags = 0,
96  const WriteConcern* wc = NULL);
97 
98  virtual void remove(const std::string& ns, Query obj, int flags, const WriteConcern* wc = NULL);
99 
100  virtual void update(
101  const std::string& ns, Query query, BSONObj obj, int flags, const WriteConcern* wc = NULL);
102 
103  virtual void killCursor(long long cursorID);
104 
105  // ---- access raw connections ----
106 
113  DBClientConnection& masterConn();
114 
122  DBClientConnection& slaveConn();
123 
124  // ---- callback pieces -------
125 
126  virtual void say(Message& toSend, bool isRetry = false, std::string* actualServer = 0);
127  virtual bool recv(Message& toRecv);
128  virtual void checkResponse(const char* data,
129  int nReturned,
130  bool* retry = NULL,
131  std::string* targetHost = NULL);
132 
133  /* this is the callback from our underlying connections to notify us that we got a "not master"
134  * error.
135  */
136  void isntMaster();
137 
138  /* this is used to indicate we got a "not master or secondary" error from a secondary.
139  */
140  void isntSecondary();
141 
142  // ----- status ------
143 
144  virtual bool isFailed() const {
145  return !_master || _master->isFailed();
146  }
147  bool isStillConnected();
148 
149  // ----- informational ----
150 
151  double getSoTimeout() const {
152  return _so_timeout;
153  }
154 
155  std::string toString() const {
156  return getServerAddress();
157  }
158 
159  std::string getServerAddress() const;
160 
161  virtual ConnectionString::ConnectionType type() const {
162  return ConnectionString::SET;
163  }
164  virtual bool lazySupported() const {
165  return true;
166  }
167 
168  // ---- low level ------
169 
170  virtual bool call(Message& toSend,
171  Message& response,
172  bool assertOk = true,
173  std::string* actualServer = 0);
174  virtual bool callRead(Message& toSend, Message& response) {
175  return checkMaster()->callRead(toSend, response);
176  }
177 
188  static bool MONGO_CLIENT_FUNC
189  isSecondaryQuery(const std::string& ns, const BSONObj& queryObj, int queryOptions);
190 
191  virtual void setRunCommandHook(DBClientWithCommands::RunCommandHookFunc func);
192  virtual void setPostRunCommandHook(DBClientWithCommands::PostRunCommandHookFunc func);
193 
198  virtual void reset();
199 
204  static void setAuthPooledSecondaryConn(bool setting);
205 
206  virtual int getMaxWireVersion() {
207  return checkMaster()->getMaxWireVersion();
208  }
209 
210  virtual int getMinWireVersion() {
211  return checkMaster()->getMinWireVersion();
212  }
213 
214 protected:
217  virtual void _auth(const BSONObj& params);
218 
219  virtual void sayPiggyBack(Message& toSend) {
220  checkMaster()->say(toSend);
221  }
222 
223 private:
231  std::auto_ptr<DBClientCursor> checkSlaveQueryResult(std::auto_ptr<DBClientCursor> result);
232 
233  DBClientConnection* checkMaster();
234 
247  DBClientConnection* selectNodeUsingTags(boost::shared_ptr<ReadPreferenceSetting> readPref);
248 
253  bool checkLastHost(const ReadPreferenceSetting* readPref);
254 
258  void invalidateLastSlaveOkCache();
259 
260  void _auth(DBClientConnection* conn);
261 
266  void logoutAll(DBClientConnection* conn);
267 
271  void resetMaster();
272 
276  void resetSlaveOkConn();
277 
282  static const size_t MAX_RETRY;
283 
284  // TODO: remove this when processes other than mongos uses the driver version.
285  static bool _authPooledSecondaryConn;
286 
287  // Throws a DBException if the monitor doesn't exist and there isn't a cached seed to use.
288  ReplicaSetMonitorPtr _getMonitor() const;
289 
290  std::string _setName;
291 
292  HostAndPort _masterHost;
293  boost::scoped_ptr<DBClientConnection> _master;
294 
295  // Last used host in a slaveOk query (can be a primary).
296  HostAndPort _lastSlaveOkHost;
297  // Last used connection in a slaveOk query (can be a primary).
298  // Connection can either be owned here or returned to the connection pool. Note that
299  // if connection is primary, it is owned by _master so it is incorrect to return
300  // it to the pool.
301  std::auto_ptr<DBClientConnection> _lastSlaveOkConn;
302  boost::shared_ptr<ReadPreferenceSetting> _lastReadPref;
303 
304  double _so_timeout;
305 
306  // we need to store so that when we connect to a new node on failure
307  // we can re-auth
308  // this could be a security issue, as the password is stored in memory
309  // not sure if/how we should handle
310  std::map<std::string, BSONObj> _auths; // dbName -> auth parameters
311 
312 protected:
316  class LazyState {
317  public:
318  LazyState() : _lastClient(NULL), _lastOp(-1), _secondaryQueryOk(false), _retries(0) {}
319  DBClientConnection* _lastClient;
320  int _lastOp;
321  bool _secondaryQueryOk;
322  int _retries;
323 
324  } _lazyState;
325 };
326 
330 class MONGO_CLIENT_API TagSet {
331 public:
337  TagSet();
338 
346  explicit TagSet(const BSONArray& tags) : _tags(tags) {}
347 
351  const BSONArray& getTagBSON() const {
352  return _tags;
353  }
354 
355  bool operator==(const TagSet& other) const {
356  return _tags == other._tags;
357  }
358 
359 private:
360  BSONArray _tags;
361 };
362 
363 struct MONGO_CLIENT_API ReadPreferenceSetting {
371  ReadPreferenceSetting(ReadPreference pref, const TagSet& tag) : pref(pref), tags(tag) {}
372 
373  inline bool equals(const ReadPreferenceSetting& other) const {
374  return pref == other.pref && tags == other.tags;
375  }
376 
377  BSONObj toBSON() const;
378 
379  const ReadPreference pref;
380  TagSet tags;
381 };
382 }
Definition: dbclient_rs.h:363
for storing (non-threadsafe) information between lazy calls
Definition: dbclient_rs.h:316
Definition: message.h:305
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
TagSet(const BSONArray &tags)
Creates a TagSet from a BSONArray of tags.
Definition: dbclient_rs.h:346
stdx::function< void(const BSONObj &, const std::string &)> PostRunCommandHookFunc
Similar to above, but for running a function on a command response after a command has been run...
Definition: dbclientinterface.h:1342
A basic connection to the database.
Definition: dbclientinterface.h:1716
Use this class to connect to a replica set of servers.
Definition: dbclient_rs.h:42
Core MongoDB C++ driver interfaces are defined here.
virtual void remove(const std::string &ns, Query q, bool justOne=0, const WriteConcern *wc=NULL)
remove matching objects from the database
Class to encapsulate client side "Write Concern" concept.
Definition: write_concern.h:35
Name of a process on the network.
Definition: hostandport.h:37
stdx::function< void(BSONObjBuilder *)> RunCommandHookFunc
A function type for runCommand hooking; the function takes a pointer to a BSONObjBuilder and returns ...
Definition: dbclientinterface.h:1332
const BSONArray & getTagBSON() const
Returns the BSONArray listing all tags that should be accepted.
Definition: dbclient_rs.h:351
virtual void update(const std::string &ns, Query query, BSONObj obj, bool upsert=false, bool multi=false, const WriteConcern *wc=NULL)
updates objects matching query
A simple object for representing the list of tags requested by a $readPreference. ...
Definition: dbclient_rs.h:330
Definition: bsonobj.h:581
virtual std::auto_ptr< DBClientCursor > query(const std::string &ns, Query query, int nToReturn=0, int nToSkip=0, const BSONObj *fieldsToReturn=0, int queryOptions=0, int batchSize=0)
send a query to the database.
Represents a Mongo query expression.
Definition: dbclientinterface.h:403
abstract class that implements the core db operations
Definition: dbclientinterface.h:1422
ReadPreferenceSetting(ReadPreference pref, const TagSet &tag)
pref the read preference mode.
Definition: dbclient_rs.h:371
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78