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> > ConnectionMap;
54 class ConnectionGuard {
58 : _cache(cache), _host(host) {
60 _conn = _cache.getConnectionTo(_host);
64 _cache.returnConnection(_host, _conn);
67 boost::shared_ptr<DBClientConnection> _conn;
76 boost::shared_ptr<DBClientConnection> getConnectionTo(
const HostAndPort& host);
78 void returnConnection(
const HostAndPort& host, boost::shared_ptr<DBClientConnection> conn);
80 inline bool connectionOk(
const boost::shared_ptr<DBClientConnection>& conn) {
82 return conn && !conn->isFailed() && conn->isStillConnected();
85 ConnectionMap _cacheStorage;
86 boost::mutex _cacheLock;
92 : ok(
false), host(host), latencyMicros(latencyMicros) {
109 std::set<HostAndPort> normalHosts;
114 int64_t latencyMicros;
123 explicit Node(
const HostAndPort& host) : host(host), latencyMicros(unknownLatency) {
132 bool matches(
const ReadPreference& pref)
const;
147 bool matches(
const BSONObj& tag)
const;
155 static const int64_t unknownLatency;
160 int64_t latencyMicros;
163 typedef std::vector<Node> Nodes;
190 std::string getServerAddress()
const;
197 static ConfigChangeHook configChangeHook;
208 boost::condition_variable cv;
210 const std::string name;
211 int consecutiveFailedScans;
212 std::set<HostAndPort> seedNodes;
216 ScanStatePtr currentScan;
217 int64_t latencyThresholdMicros;
219 mutable int roundRobin;
227 ScanState() : foundUpMaster(
false), foundAnyUpNodes(
false) {}
233 template <
typename Container>
238 bool foundAnyUpNodes;
239 std::deque<HostAndPort> hostsToScan;
240 std::set<HostAndPort> possibleNodes;
241 std::set<HostAndPort> waitingFor;
242 std::set<HostAndPort> triedHosts;
245 typedef std::vector<IsMasterReply> UnconfirmedReplies;
246 UnconfirmedReplies unconfirmedReplies;
Definition: dbclient_rs.h:363
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.
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents...
Definition: string_data.h:43
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
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:117
Definition: replica_set_monitor_internal.h:223
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:37
Definition: replica_set_monitor_internal.h:122
void checkInvariants() const
Before unlocking, do DEV checkInvariants();.
Uses http://en.wikipedia.org/wiki/Xorshift.
Definition: random.h:27
Definition: replica_set_monitor_internal.h:89
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...