MongoDB C++ Driver  legacy-1.1.2
Classes | 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>

Classes

struct  IsMasterReply
 
class  Refresher
 Refreshes the local view of a replica set. More...
 
struct  ScanState
 
struct  SetState
 

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.
 
bool isKnownToHaveGoodPrimary () const
 Returns true if the monitor knows a usable primary from it's interal view.
 
 ReplicaSetMonitor (const SetStatePtr &initialState)
 Allows tests to set initial conditions and introspect the current state.
 

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 Status initialize ()
 Starts the ReplicaSetMonitorWatcher. More...
 
static Status shutdown (int gracePeriodMillis=0)
 Permanently stops all monitoring on replica sets and clears all cached information as well. More...
 

Static Public Attributes

static int maxConsecutiveFailedChecks
 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
 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.

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

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::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.

static 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.

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.

static Status mongo::ReplicaSetMonitor::initialize ( )
static

Starts the ReplicaSetMonitorWatcher.

You shouldn't have to call this in production code as it is called in init.cpp. However, it may be useful to pair calls to initalize and cleanup in tests. The behavior of other methods in this class is undefined if initialize() has not been called.

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.

static 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.

static 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.

static Status mongo::ReplicaSetMonitor::shutdown ( int  gracePeriodMillis = 0)
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. After this is called, the behavior of other methods in this class is undefined until a subsequent call to initialize.

The gracePeriodMillis parameter determines the maximum amount of time to wait. A value of 0 (the default) indicates no timeout. If a nonzero timeout is specified, a return value of Status::OK() indicates that shutdown completed successfully in the allotted time.

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
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 file: