MongoDB  2.7.0
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
mongo::ReplicaSetMonitor Class Reference

Holds state about a replica set and provides a means to refresh the local view. More...

#include <replica_set_monitor.h>

Inheritance diagram for mongo::ReplicaSetMonitor:

Classes

struct  IsMasterReply
 
struct  NextStep
 
struct  ScanState
 
struct  SetState
 

Public Types

typedef boost::function< void(const
std::string &setName, const
std::string
&newConnectionString)> 
ConfigChangeHook
 
typedef boost::shared_ptr
< ScanState
ScanStatePtr
 
typedef boost::shared_ptr
< SetState
SetStatePtr
 

Public Member Functions

 ReplicaSetMonitor (StringData name, const std::set< HostAndPort > &seeds)
 Initializes local state. More...
 
HostAndPort getHostOrRefresh (const ReadPreferenceSetting &criteria)
 Returns a host matching criteria or an empty HostAndPort if no host matches. More...
 
HostAndPort getMasterOrUassert ()
 Returns the host we think is the current master or uasserts. More...
 
Refresher startOrContinueRefresh ()
 Returns a refresher object that can be used to update our view of the set. More...
 
void failedHost (const HostAndPort &host)
 Notifies this Monitor that a host has failed and should be considered down. More...
 
bool isPrimary (const HostAndPort &host) const
 Returns true if this node is the master based ONLY on local data. More...
 
bool isHostUp (const HostAndPort &host) const
 Returns true if host is part of this set and is considered up (meaning it can accept queries).
 
int getConsecutiveFailedScans () const
 How may times in a row have we tried to refresh without successfully contacting any hosts who claim to be members of this set?
 
std::string getName () const
 The name of the set.
 
std::string getServerAddress () const
 Returns a string with the format name/server1,server2. More...
 
bool contains (const HostAndPort &server) const
 Is server part of this set? Uses only cached information.
 
void appendInfo (BSONObjBuilder &b) const
 Writes information about our cached view of the set to a BSONObjBuilder.
 
 ReplicaSetMonitor (const SetStatePtr &initialState)
 Allows tests to set initial conditions and introspect the current state.
 
HostAndPort refreshUntilMatches (const ReadPreferenceSetting &criteria)
 Contact hosts in the set to refresh our view, but stop once a host matches criteria. More...
 
void refreshAll ()
 Refresh all hosts. More...
 
 Refresher (const SetStatePtr &setState)
 Any passed-in pointers are shared with caller. More...
 
NextStep getNextStep ()
 Returns the next step to take. More...
 
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. More...
 
void failedHost (const HostAndPort &host)
 Call this if a host returned from getNextStep failed to reply to an isMaster call.
 
bool startedNewScan () const
 True if this Refresher started a new full scan rather than joining an existing one.
 

Static Public Member Functions

static void createIfNeeded (const std::string &name, const std::set< HostAndPort > &servers)
 Creates a new ReplicaSetMonitor, if it doesn't already exist.
 
static ReplicaSetMonitorPtr get (const std::string &name, bool createFromSeed=false)
 gets a cached Monitor per name. More...
 
static std::set< std::string > getAllTrackedSets ()
 Returns all the currently tracked replica set names.
 
static void remove (const std::string &name, bool clearSeedCache=false)
 Removes the ReplicaSetMonitor for the given set name from _sets, which will delete it. More...
 
static void setConfigChangeHook (ConfigChangeHook hook)
 Sets the hook to be called whenever the config of any replica set changes. More...
 
static void cleanup ()
 Permanently stops all monitoring on replica sets and clears all cached information as well. More...
 
static ScanStatePtr startNewScan (const SetState *set)
 Starts a new scan over the hosts in set.
 

Static Public Attributes

static int maxConsecutiveFailedChecks = 30
 If a ReplicaSetMonitor has been refreshed more than this many times in a row without finding any live nodes claiming to be in the set, the ReplicaSetMonitorWatcher will stop periodic background refreshes of this set.
 
static bool useDeterministicHostSelection = false
 Defaults to false, meaning that if multiple hosts meet a criteria we pick one at random. More...
 

Detailed Description

Holds state about a replica set and provides a means to refresh the local view.

Refreshes the local view of a replica set.

All methods perform the required synchronization to allow callers from multiple threads.

Use ReplicaSetMonitor::startOrContinueRefresh() to obtain a Refresher.

Multiple threads can refresh a single set without any additional synchronization, however they must each use their own Refresher object.

All logic related to choosing the hosts to contact and updating the SetState based on replies lives in this class.

Constructor & Destructor Documentation

mongo::ReplicaSetMonitor::ReplicaSetMonitor ( StringData  name,
const std::set< HostAndPort > &  seeds 
)

Initializes local state.

seeds must not be empty.

Member Function Documentation

void mongo::ReplicaSetMonitor::cleanup ( )
static

Permanently stops all monitoring on replica sets and clears all cached information as well.

As a consequence, NEVER call this if you have other threads that have a DBClientReplicaSet instance.

void mongo::ReplicaSetMonitor::failedHost ( const HostAndPort host)

Notifies this Monitor that a host has failed and should be considered down.

Call this when you get a connection error. If you get an error while trying to refresh our view of a host, call Refresher::hostFailed() instead.

ReplicaSetMonitorPtr mongo::ReplicaSetMonitor::get ( const std::string &  name,
bool  createFromSeed = false 
)
static

gets a cached Monitor per name.

If the monitor is not found and createFromSeed is false, it will return none. If createFromSeed is true, it will try to look up the last known servers list for this set and will create a new monitor using that as the seed list.

HostAndPort mongo::ReplicaSetMonitor::getHostOrRefresh ( const ReadPreferenceSetting criteria)

Returns a host matching criteria or an empty HostAndPort if no host matches.

If no host matches initially, will then attempt to refresh our view of the set by contacting other hosts. May still return no result if no host matches following a refresh.

HostAndPort mongo::ReplicaSetMonitor::getMasterOrUassert ( )

Returns the host we think is the current master or uasserts.

This is a thin wrapper around getHostOrRefresh so this will also refresh our view if we don't think there is a master at first. The main difference is that this will uassert rather than returning an empty HostAndPort.

NextStep mongo::ReplicaSetMonitor::getNextStep ( )

Returns the next step to take.

By calling this, you promise to call receivedIsMaster or failedHost if the NextStep is CONTACT_HOST.

std::string mongo::ReplicaSetMonitor::getServerAddress ( ) const

Returns a string with the format name/server1,server2.

If name is empty, returns just comma-separated list of servers.

bool mongo::ReplicaSetMonitor::isPrimary ( const HostAndPort host) const

Returns true if this node is the master based ONLY on local data.

Be careful, return may be stale.

void mongo::ReplicaSetMonitor::receivedIsMaster ( const HostAndPort from,
int64_t  latencyMicros,
const BSONObj reply 
)

Call this if a host returned from getNextStep successfully replied to an isMaster call.

Negative latencyMicros are ignored.

void mongo::ReplicaSetMonitor::refreshAll ( )
inline

Refresh all hosts.

Equivalent to refreshUntilMatches with a criteria that never matches.

This is intended to be called periodically, possibly from a background thread.

mongo::ReplicaSetMonitor::Refresher ( const SetStatePtr &  setState)
explicit

Any passed-in pointers are shared with caller.

If no scan is in-progress, this function is responsible for setting up a new scan.

HostAndPort mongo::ReplicaSetMonitor::refreshUntilMatches ( const ReadPreferenceSetting criteria)
inline

Contact hosts in the set to refresh our view, but stop once a host matches criteria.

Returns the matching host or empty if none match after a refresh.

This is called by ReplicaSetMonitor::getHostWithRefresh()

void mongo::ReplicaSetMonitor::remove ( const std::string &  name,
bool  clearSeedCache = false 
)
static

Removes the ReplicaSetMonitor for the given set name from _sets, which will delete it.

If clearSeedCache is true, then the cached seed string for this Replica Set will be removed from _seedServers.

void mongo::ReplicaSetMonitor::setConfigChangeHook ( ConfigChangeHook  hook)
static

Sets the hook to be called whenever the config of any replica set changes.

Currently only 1 globally, so this asserts if one already exists.

The hook will be called from a fresh thread. It is responsible for initializing any thread-local state and ensuring that no exceptions escape.

The hook must not be changed while the program has multiple threads.

Refresher mongo::ReplicaSetMonitor::startOrContinueRefresh ( )

Returns a refresher object that can be used to update our view of the set.

If a refresh is currently in-progress, the returned Refresher will participate in the current refresh round.

Member Data Documentation

bool mongo::ReplicaSetMonitor::useDeterministicHostSelection = false
static

Defaults to false, meaning that if multiple hosts meet a criteria we pick one at random.

This is required by the replica set driver spec. Set this to true in tests that need host selection to be deterministic.


The documentation for this class was generated from the following files: