24 #include <boost/thread/condition_variable.hpp>
30 #include "mongo/base/disallow_copying.h"
32 #include "mongo/client/replica_set_monitor.h"
34 #include "mongo/platform/cstdint.h"
35 #include "mongo/platform/random.h"
36 #include "mongo/platform/unordered_map.h"
37 #include "mongo/util/net/hostandport.h"
49 typedef unordered_map<HostAndPort, boost::shared_ptr<DBClientConnection> >
55 class ConnectionGuard {
64 _conn = _cache.getConnectionTo(_host);
68 _cache.returnConnection(_host, _conn);
71 boost::shared_ptr<DBClientConnection> _conn;
80 boost::shared_ptr<DBClientConnection> getConnectionTo(
const HostAndPort& host);
82 void returnConnection(
const HostAndPort& host, boost::shared_ptr<DBClientConnection> conn);
84 inline bool connectionOk(
const boost::shared_ptr<DBClientConnection>& conn) {
86 return conn && !conn->isFailed() && conn->isStillConnected();
89 ConnectionMap _cacheStorage;
90 boost::mutex _cacheLock;
98 , latencyMicros(latencyMicros) {
115 std::set<HostAndPort> normalHosts;
120 int64_t latencyMicros;
131 , latencyMicros(unknownLatency) {
140 bool matches(
const ReadPreference& pref)
const;
155 bool matches(
const BSONObj& tag)
const;
163 static const int64_t unknownLatency;
168 int64_t latencyMicros;
171 typedef std::vector<Node> Nodes;
176 SetState(StringData name,
const std::set<HostAndPort>& seedNodes);
198 std::string getServerAddress()
const;
205 static ConfigChangeHook configChangeHook;
216 boost::condition_variable cv;
218 const std::string name;
219 int consecutiveFailedScans;
220 std::set<HostAndPort> seedNodes;
224 ScanStatePtr currentScan;
225 int64_t latencyThresholdMicros;
226 mutable PseudoRandom rand;
227 mutable int roundRobin;
234 ScanState() : foundUpMaster(
false), foundAnyUpNodes(
false) {}
240 template <
typename Container>
245 bool foundAnyUpNodes;
246 std::deque<HostAndPort> hostsToScan;
247 std::set<HostAndPort> possibleNodes;
248 std::set<HostAndPort> waitingFor;
249 std::set<HostAndPort> triedHosts;
252 typedef std::vector<IsMasterReply> UnconfirmedReplies;
253 UnconfirmedReplies unconfirmedReplies;
Definition: dbclient_rs.h:316
Node * findNode(const HostAndPort &host)
Returns the Node with the given host, or NULL if no Node has that host.
void update(const IsMasterReply &reply)
Updates this Node based on information in reply.
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
Object ID type.
Definition: oid.h:60
Node * findOrCreateNode(const HostAndPort &host)
Returns the Node with the given host, or creates one if no Node has that host.
SetState(StringData name, const std::set< HostAndPort > &seedNodes)
seedNodes must not be empty
void parse(const BSONObj &obj)
Never throws.
Definition: replica_set_monitor_internal.h:123
Definition: replica_set_monitor_internal.h:231
Connect to a Replica Set, from C++.
HostAndPort getMatchingHost(const ReadPreferenceSetting &criteria) const
Returns a host matching criteria or an empty host if no known host matches.
Name of a process on the network.
Definition: hostandport.h:36
Definition: replica_set_monitor_internal.h:128
void checkInvariants() const
Before unlocking, do DEV checkInvariants();.
Definition: replica_set_monitor_internal.h:93
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
Definition: replica_set_monitor_internal.h:42
void enqueAllUntriedHosts(const Container &container, PseudoRandom &rand)
Adds all hosts in container that aren't in triedHosts to hostsToScan, then shuffles the queue...