MongoDB C++ Driver  legacy-1.0.6
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
replica_set_monitor.h
1 /* Copyright 2014 MongoDB Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <boost/shared_ptr.hpp>
19 #include <string>
20 #include <set>
21 
22 #include "mongo/base/disallow_copying.h"
23 #include "mongo/base/string_data.h"
24 #include "mongo/stdx/functional.h"
25 #include "mongo/util/net/hostandport.h"
26 
27 namespace mongo {
28 class BSONObj;
29 class ReplicaSetMonitor;
30 class TagSet;
31 struct ReadPreferenceSetting;
32 typedef boost::shared_ptr<ReplicaSetMonitor> ReplicaSetMonitorPtr;
33 
39  MONGO_DISALLOW_COPYING(ReplicaSetMonitor);
40 
41 public:
42  class Refresher;
43 
44  typedef stdx::function<void(const std::string& setName, const std::string& newConnectionString)>
45  ConfigChangeHook;
46 
52  ReplicaSetMonitor(StringData name, const std::set<HostAndPort>& seeds);
53 
62 
71 
78 
85  void failedHost(const HostAndPort& host);
86 
91  bool isPrimary(const HostAndPort& host) const;
92 
97  bool isHostUp(const HostAndPort& host) const;
98 
103  int getConsecutiveFailedScans() const;
104 
108  std::string getName() const;
109 
114  std::string getServerAddress() const;
115 
119  bool contains(const HostAndPort& server) const;
120 
124  void appendInfo(BSONObjBuilder& b) const;
125 
129  static void createIfNeeded(const std::string& name, const std::set<HostAndPort>& servers);
130 
136  static ReplicaSetMonitorPtr get(const std::string& name, bool createFromSeed = false);
137 
141  static std::set<std::string> getAllTrackedSets();
142 
148  static void remove(const std::string& name, bool clearSeedCache = false);
149 
159  static void setConfigChangeHook(ConfigChangeHook hook);
160 
167  static Status initialize();
168 
179  static Status shutdown(int gracePeriodMillis = 0);
180 
187 
188  //
189  // internal types (defined in replica_set_monitor_internal.h)
190  //
191 
192  struct IsMasterReply;
193  struct ScanState;
194  struct SetState;
195  typedef boost::shared_ptr<ScanState> ScanStatePtr;
196  typedef boost::shared_ptr<SetState> SetStatePtr;
197 
198  //
199  // FOR TESTING ONLY
200  //
201 
205  explicit ReplicaSetMonitor(const SetStatePtr& initialState) : _state(initialState) {}
206 
213 
214 private:
215  const SetStatePtr _state; // never NULL
216 };
217 
230 public:
238  return _refreshUntilMatches(&criteria);
239  };
240 
247  void refreshAll() {
248  _refreshUntilMatches(NULL);
249  }
250 
251  //
252  // Remaining methods are only for testing and internal use.
253  // Callers are responsible for holding SetState::mutex before calling any of these methods.
254  //
255 
261  explicit Refresher(const SetStatePtr& setState);
262 
263  struct NextStep {
264  enum StepKind {
265  CONTACT_HOST,
268  };
269 
270  explicit NextStep(StepKind step, const HostAndPort& host = HostAndPort())
271  : step(step), host(host) {}
272 
273  StepKind step;
274  HostAndPort host;
275  };
276 
283  NextStep getNextStep();
284 
289  void receivedIsMaster(const HostAndPort& from, int64_t latencyMicros, const BSONObj& reply);
290 
294  void failedHost(const HostAndPort& host);
295 
299  bool startedNewScan() const {
300  return _startedNewScan;
301  }
302 
306  static ScanStatePtr startNewScan(const SetState* set);
307 
308 private:
318  bool receivedIsMasterFromMaster(const IsMasterReply& reply);
319 
325  void receivedIsMasterBeforeFoundMaster(const IsMasterReply& reply);
326 
332  HostAndPort _refreshUntilMatches(const ReadPreferenceSetting* criteria);
333 
334  // Both pointers are never NULL
335  SetStatePtr _set;
336  ScanStatePtr _scan; // May differ from _set->currentScan if a new scan has started.
337  bool _startedNewScan;
338 };
339 }
Wait on condition variable and try again.
Definition: replica_set_monitor.h:267
Definition: dbclient_rs.h:355
Refresher(const SetStatePtr &setState)
Any passed-in pointers are shared with caller.
static void createIfNeeded(const std::string &name, const std::set< HostAndPort > &servers)
Creates a new ReplicaSetMonitor, if it doesn't already exist.
Definition: replica_set_monitor.h:263
void refreshAll()
Refresh all hosts.
Definition: replica_set_monitor.h:247
static int maxConsecutiveFailedChecks
If a ReplicaSetMonitor has been refreshed more than this many times in a row without finding any live...
Definition: replica_set_monitor.h:186
HostAndPort getMasterOrUassert()
Returns the host we think is the current master or uasserts.
ReplicaSetMonitor(const SetStatePtr &initialState)
Allows tests to set initial conditions and introspect the current state.
Definition: replica_set_monitor.h:205
static Status initialize()
Starts the ReplicaSetMonitorWatcher.
StepKind
Definition: replica_set_monitor.h:264
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
bool isHostUp(const HostAndPort &host) const
Returns true if host is part of this set and is considered up (meaning it can accept queries)...
std::string getServerAddress() const
Returns a string with the format name/server1,server2.
void failedHost(const HostAndPort &host)
Notifies this Monitor that a host has failed and should be considered down.
Definition: replica_set_monitor_internal.h:117
static bool useDeterministicHostSelection
Defaults to false, meaning that if multiple hosts meet a criteria we pick one at random.
Definition: replica_set_monitor.h:212
Contact the returned host.
Definition: replica_set_monitor.h:266
std::string getName() const
The name of the set.
int getConsecutiveFailedScans() const
How may times in a row have we tried to refresh without successfully contacting any hosts who claim t...
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:53
HostAndPort getHostOrRefresh(const ReadPreferenceSetting &criteria)
Returns a host matching criteria or an empty HostAndPort if no host matches.
Definition: replica_set_monitor_internal.h:223
static std::set< std::string > getAllTrackedSets()
Returns all the currently tracked replica set names.
bool startedNewScan() const
True if this Refresher started a new full scan rather than joining an existing one.
Definition: replica_set_monitor.h:299
bool contains(const HostAndPort &server) const
Is server part of this set? Uses only cached information.
Holds state about a replica set and provides a means to refresh the local view.
Definition: replica_set_monitor.h:38
bool isPrimary(const HostAndPort &host) const
Returns true if this node is the master based ONLY on local data.
Refresher startOrContinueRefresh()
Returns a refresher object that can be used to update our view of the set.
static ScanStatePtr startNewScan(const SetState *set)
Starts a new scan over the hosts in set.
Name of a process on the network.
Definition: hostandport.h:37
void receivedIsMaster(const HostAndPort &from, int64_t latencyMicros, const BSONObj &reply)
Call this if a host returned from getNextStep successfully replied to an isMaster call...
NextStep getNextStep()
Returns the next step to take.
void appendInfo(BSONObjBuilder &b) const
Writes information about our cached view of the set to a BSONObjBuilder.
static void setConfigChangeHook(ConfigChangeHook hook)
Sets the hook to be called whenever the config of any replica set changes.
ReplicaSetMonitor(StringData name, const std::set< HostAndPort > &seeds)
Initializes local state.
void failedHost(const HostAndPort &host)
Call this if a host returned from getNextStep failed to reply to an isMaster call.
Refreshes the local view of a replica set.
Definition: replica_set_monitor.h:229
static Status shutdown(int gracePeriodMillis=0)
Permanently stops all monitoring on replica sets and clears all cached information as well...
Definition: replica_set_monitor_internal.h:89
HostAndPort refreshUntilMatches(const ReadPreferenceSetting &criteria)
Contact hosts in the set to refresh our view, but stop once a host matches criteria.
Definition: replica_set_monitor.h:237
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
MONGO_CLIENT_API Status(MONGO_CLIENT_FUNC *saslClientAuthenticate)(DBClientWithCommands *client
Attempts to authenticate "client" using the SASL protocol.