MongoDB C++ Driver legacy-1.0.7
Loading...
Searching...
No Matches
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
27namespace mongo {
28
29class ReplicaSetMonitor;
30class TagSet;
31struct ReadPreferenceSetting;
32typedef boost::shared_ptr<ReplicaSetMonitor> ReplicaSetMonitorPtr;
33
42class MONGO_CLIENT_API DBClientReplicaSet : public DBClientBase {
43public:
44 using DBClientBase::query;
45 using DBClientBase::update;
46 using DBClientBase::remove;
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
114
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 }
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
206protected:
209 virtual void _auth(const BSONObj& params);
210
211 virtual void sayPiggyBack(Message& toSend) {
212 checkMaster()->say(toSend);
213 }
214
215private:
223 std::auto_ptr<DBClientCursor> checkSlaveQueryResult(std::auto_ptr<DBClientCursor> result);
224
225 DBClientConnection* checkMaster();
226
239 DBClientConnection* selectNodeUsingTags(boost::shared_ptr<ReadPreferenceSetting> readPref);
240
245 bool checkLastHost(const ReadPreferenceSetting* readPref);
246
250 void invalidateLastSlaveOkCache();
251
252 void _auth(DBClientConnection* conn);
253
258 void logoutAll(DBClientConnection* conn);
259
263 void resetMaster();
264
268 void resetSlaveOkConn();
269
274 static const size_t MAX_RETRY;
275
276 // TODO: remove this when processes other than mongos uses the driver version.
277 static bool _authPooledSecondaryConn;
278
279 // Throws a DBException if the monitor doesn't exist and there isn't a cached seed to use.
280 ReplicaSetMonitorPtr _getMonitor() const;
281
282 std::string _setName;
283
284 HostAndPort _masterHost;
285 boost::scoped_ptr<DBClientConnection> _master;
286
287 // Last used host in a slaveOk query (can be a primary).
288 HostAndPort _lastSlaveOkHost;
289 // Last used connection in a slaveOk query (can be a primary).
290 // Connection can either be owned here or returned to the connection pool. Note that
291 // if connection is primary, it is owned by _master so it is incorrect to return
292 // it to the pool.
293 std::auto_ptr<DBClientConnection> _lastSlaveOkConn;
294 boost::shared_ptr<ReadPreferenceSetting> _lastReadPref;
295
296 double _so_timeout;
297
298 // we need to store so that when we connect to a new node on failure
299 // we can re-auth
300 // this could be a security issue, as the password is stored in memory
301 // not sure if/how we should handle
302 std::map<std::string, BSONObj> _auths; // dbName -> auth parameters
303
304protected:
308 class LazyState {
309 public:
310 LazyState() : _lastClient(NULL), _lastOp(-1), _secondaryQueryOk(false), _retries(0) {}
311 DBClientConnection* _lastClient;
312 int _lastOp;
313 bool _secondaryQueryOk;
314 int _retries;
315
316 } _lazyState;
317};
318
322class MONGO_CLIENT_API TagSet {
323public:
330
338 explicit TagSet(const BSONArray& tags) : _tags(tags) {}
339
343 const BSONArray& getTagBSON() const {
344 return _tags;
345 }
346
347 bool operator==(const TagSet& other) const {
348 return _tags == other._tags;
349 }
350
351private:
352 BSONArray _tags;
353};
354
355struct MONGO_CLIENT_API ReadPreferenceSetting {
363 ReadPreferenceSetting(ReadPreference pref, const TagSet& tag) : pref(pref), tags(tag) {}
364
365 inline bool equals(const ReadPreferenceSetting& other) const {
366 return pref == other.pref && tags == other.tags;
367 }
368
369 BSONObj toBSON() const;
370
371 const ReadPreference pref;
372 TagSet tags;
373};
374}
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
abstract class that implements the core db operations
Definition dbclientinterface.h:1467
A basic connection to the database.
Definition dbclientinterface.h:1707
for storing (non-threadsafe) information between lazy calls
Definition dbclient_rs.h:308
Use this class to connect to a replica set of servers.
Definition dbclient_rs.h:42
bool connect()
Returns false if no member of the set were reachable.
static bool MONGO_CLIENT_FUNC isSecondaryQuery(const std::string &ns, const BSONObj &queryObj, int queryOptions)
Returns whether a query or command can be sent to secondaries based on the query object and options.
DBClientConnection & slaveConn()
WARNING: this method is very dangerous - this object can decide to free the returned master connectio...
static void setAuthPooledSecondaryConn(bool setting)
@bool setting if true, DBClientReplicaSet connections will make sure that secondary connections are a...
DBClientReplicaSet(const std::string &name, const std::vector< HostAndPort > &servers, double so_timeout=0)
Call connect() after constructing.
virtual void logout(const std::string &dbname, BSONObj &info)
Logs out the connection for the given database.
DBClientConnection & masterConn()
WARNING: this method is very dangerous - this object can decide to free the returned master connectio...
virtual void insert(const std::string &ns, BSONObj obj, int flags=0, const WriteConcern *wc=NULL)
insert an object into the database
bool isStillConnected()
if not checked recently, checks whether the underlying socket/sockets are still valid
virtual void _auth(const BSONObj &params)
Authorize.
virtual bool call(Message &toSend, Message &response, bool assertOk=true, std::string *actualServer=0)
actualServer is set to the actual server where they call went if there was a choice (SlaveOk)
virtual void insert(const std::string &ns, const std::vector< BSONObj > &v, int flags=0, const WriteConcern *wc=NULL)
insert a vector of objects into the database
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)
throws userassertion "no master found"
virtual BSONObj findOne(const std::string &ns, const Query &query, const BSONObj *fieldsToReturn=0, int queryOptions=0)
throws userassertion "no master found"
virtual void reset()
Performs a "soft reset" by clearing all states relating to secondary nodes and returning secondary co...
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:1378
stdx::function< void(BSONObjBuilder *)> RunCommandHookFunc
A function type for runCommand hooking; the function takes a pointer to a BSONObjBuilder and returns ...
Definition dbclientinterface.h:1368
Definition message.h:305
Represents a Mongo query expression.
Definition dbclientinterface.h:403
A simple object for representing the list of tags requested by a $readPreference.
Definition dbclient_rs.h:322
const BSONArray & getTagBSON() const
Returns the BSONArray listing all tags that should be accepted.
Definition dbclient_rs.h:343
TagSet(const BSONArray &tags)
Creates a TagSet from a BSONArray of tags.
Definition dbclient_rs.h:338
TagSet()
Creates a TagSet that matches any nodes.
Class to encapsulate client side "Write Concern" concept.
Definition write_concern.h:35
Core MongoDB C++ driver interfaces are defined here.
the main MongoDB namespace
Definition bulk_operation_builder.h:24
Definition bsonobj.h:581
Name of a process on the network.
Definition hostandport.h:37
Definition dbclient_rs.h:355
ReadPreferenceSetting(ReadPreference pref, const TagSet &tag)
@parm pref the read preference mode.
Definition dbclient_rs.h:363