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) {
114 std::set<HostAndPort> normalHosts;
119 int64_t latencyMicros;
130 , latencyMicros(unknownLatency) {
139 bool matches(
const ReadPreference& pref)
const;
154 bool matches(
const BSONObj& tag)
const;
162 static const int64_t unknownLatency;
167 int64_t latencyMicros;
170 typedef std::vector<Node> Nodes;
175 SetState(StringData name,
const std::set<HostAndPort>& seedNodes);
197 std::string getServerAddress()
const;
204 static ConfigChangeHook configChangeHook;
215 boost::condition_variable cv;
217 const std::string name;
218 int consecutiveFailedScans;
219 std::set<HostAndPort> seedNodes;
222 ScanStatePtr currentScan;
223 int64_t latencyThresholdMicros;
224 mutable PseudoRandom rand;
225 mutable int roundRobin;
232 ScanState() : foundUpMaster(
false), foundAnyUpNodes(
false) {}
238 template <
typename Container>
243 bool foundAnyUpNodes;
244 std::deque<HostAndPort> hostsToScan;
245 std::set<HostAndPort> possibleNodes;
246 std::set<HostAndPort> waitingFor;
247 std::set<HostAndPort> triedHosts;
250 typedef std::vector<IsMasterReply> UnconfirmedReplies;
251 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
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:122
Definition: replica_set_monitor_internal.h:229
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:127
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...