MongoDB  2.7.0
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/function.hpp>
19 #include <boost/shared_ptr.hpp>
20 #include <string>
21 #include <set>
22 
23 #include "mongo/base/disallow_copying.h"
24 #include "mongo/base/string_data.h"
25 #include "mongo/util/net/hostandport.h"
26 
27 namespace mongo {
28  class ReplicaSetMonitor;
29  class TagSet;
30  struct ReadPreferenceSetting;
31  typedef shared_ptr<ReplicaSetMonitor> ReplicaSetMonitorPtr;
32 
37  class MONGO_CLIENT_API ReplicaSetMonitor {
38  MONGO_DISALLOW_COPYING(ReplicaSetMonitor);
39  public:
40  class Refresher;
41 
42  typedef boost::function<void(const std::string& setName,
43  const std::string& newConnectionString)>
44  ConfigChangeHook;
45 
51  ReplicaSetMonitor(StringData name, const std::set<HostAndPort>& seeds);
52 
60  HostAndPort getHostOrRefresh(const ReadPreferenceSetting& criteria);
61 
69  HostAndPort getMasterOrUassert();
70 
76  Refresher startOrContinueRefresh();
77 
84  void failedHost(const HostAndPort& host);
85 
90  bool isPrimary(const HostAndPort& host) const;
91 
96  bool isHostUp(const HostAndPort& host) const;
97 
102  int getConsecutiveFailedScans() const;
103 
107  std::string getName() const;
108 
113  std::string getServerAddress() const;
114 
118  bool contains(const HostAndPort& server) const;
119 
123  void appendInfo(BSONObjBuilder& b) const;
124 
128  static void createIfNeeded(const std::string& name , const std::set<HostAndPort>& servers);
129 
135  static ReplicaSetMonitorPtr get(const std::string& name, bool createFromSeed = false);
136 
140  static std::set<std::string> getAllTrackedSets();
141 
147  static void remove(const std::string& name, bool clearSeedCache = false);
148 
158  static void setConfigChangeHook(ConfigChangeHook hook);
159 
165  static void cleanup();
166 
173 
174  //
175  // internal types (defined in replica_set_monitor_internal.h)
176  //
177 
178  struct IsMasterReply;
179  struct ScanState;
180  struct SetState;
181  typedef boost::shared_ptr<ScanState> ScanStatePtr;
182  typedef boost::shared_ptr<SetState> SetStatePtr;
183 
184  //
185  // FOR TESTING ONLY
186  //
187 
191  explicit ReplicaSetMonitor(const SetStatePtr& initialState)
192  : _state(initialState)
193  {}
194 
201 
202  private:
203  const SetStatePtr _state; // never NULL
204  };
205 
217  class MONGO_CLIENT_API ReplicaSetMonitor::Refresher {
218  public:
226  return _refreshUntilMatches(&criteria);
227  };
228 
235  void refreshAll() { _refreshUntilMatches(NULL); }
236 
237  //
238  // Remaining methods are only for testing and internal use.
239  // Callers are responsible for holding SetState::mutex before calling any of these methods.
240  //
241 
247  explicit Refresher(const SetStatePtr& setState);
248 
249  struct NextStep {
250  enum StepKind {
251  CONTACT_HOST,
254  };
255 
256  explicit NextStep(StepKind step, const HostAndPort& host=HostAndPort())
257  : step(step)
258  , host(host)
259  {}
260 
261  StepKind step;
262  HostAndPort host;
263  };
264 
271  NextStep getNextStep();
272 
277  void receivedIsMaster(const HostAndPort& from, int64_t latencyMicros, const BSONObj& reply);
278 
282  void failedHost(const HostAndPort& host);
283 
287  bool startedNewScan() const { return _startedNewScan; }
288 
292  static ScanStatePtr startNewScan(const SetState* set);
293 
294  private:
300  void receivedIsMasterFromMaster(const IsMasterReply& reply);
301 
307  void receivedIsMasterBeforeFoundMaster(const IsMasterReply& reply);
308 
314  HostAndPort _refreshUntilMatches(const ReadPreferenceSetting* criteria);
315 
316  // Both pointers are never NULL
317  SetStatePtr _set;
318  ScanStatePtr _scan; // May differ from _set->currentScan if a new scan has started.
319  bool _startedNewScan;
320  };
321 }
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:52
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:225
Contact the returned host.
Definition: replica_set_monitor.h:252
StepKind
Definition: replica_set_monitor.h:250
Definition: dbclient_rs.h:290
Wait on condition variable and try again.
Definition: replica_set_monitor.h:253
Holds state about a replica set and provides a means to refresh the local view.
Definition: replica_set_monitor.h:37
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:172
static bool useDeterministicHostSelection
Defaults to false, meaning that if multiple hosts meet a criteria we pick one at random.
Definition: replica_set_monitor.h:200
ReplicaSetMonitor(const SetStatePtr &initialState)
Allows tests to set initial conditions and introspect the current state.
Definition: replica_set_monitor.h:191
Definition: replica_set_monitor_internal.h:38
bool startedNewScan() const
True if this Refresher started a new full scan rather than joining an existing one.
Definition: replica_set_monitor.h:287
helper for manipulating host:port connection endpoints.
Definition: hostandport.h:31
Definition: replica_set_monitor.h:249
Definition: replica_set_monitor_internal.h:67
void refreshAll()
Refresh all hosts.
Definition: replica_set_monitor.h:235
Definition: replica_set_monitor_internal.h:173