MongoDB  2.4.4-pre-
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
mongo::DistributedLock Class Reference

The distributed lock is a configdb backed way of synchronizing system-wide tasks. More...

#include <distlock.h>

Classes

class  LastPings
 
struct  PingData
 

Public Member Functions

 DistributedLock (const ConnectionString &conn, const string &name, unsigned long long lockTimeout=0, bool asProcess=false)
 The constructor does not connect to the configdb yet and constructing does not mean the lock was acquired. More...
 
bool lock_try (const string &why, bool reenter=false, BSONObj *other=0, double timeout=0.0)
 Attempts to acquire 'this' lock, checking if it could or should be stolen from the previous holder. More...
 
bool isLockHeld (double timeout, string *errMsg)
 Returns true if we currently believe we hold this lock and it was possible to confirm that, within 'timeout' seconds, if provided, with the config servers. More...
 
void unlock (BSONObj *oldLockPtr=NULL)
 Releases a previously taken lock.
 
Date_t getRemoteTime ()
 
bool isRemoteTimeSkewed ()
 
const string & getProcessId ()
 
const ConnectionStringgetRemoteConnection ()
 

Static Public Member Functions

static bool checkSkew (const ConnectionString &cluster, unsigned skewChecks=NUM_LOCK_SKEW_CHECKS, unsigned long long maxClockSkew=MAX_LOCK_CLOCK_SKEW, unsigned long long maxNetSkew=MAX_LOCK_NET_SKEW)
 Check the skew between a cluster of servers.
 
static Date_t remoteTime (const ConnectionString &cluster, unsigned long long maxNetSkew=MAX_LOCK_NET_SKEW)
 Get the remote time from a server or cluster. More...
 
static bool killPinger (DistributedLock &lock)
 

Public Attributes

const ConnectionString _conn
 
const string _name
 
const string _processId
 
const unsigned long long _lockTimeout
 
const unsigned long long _maxClockSkew
 
const unsigned long long _maxNetSkew
 
const unsigned long long _lockPing
 

Static Public Attributes

static LabeledLevel logLvl
 
static LastPings lastPings
 
static const string lockPingNS
 Namespace for lock pings.
 
static const string locksNS
 Namespace for locks.
 

Detailed Description

The distributed lock is a configdb backed way of synchronizing system-wide tasks.

A task must be identified by a unique name across the system (e.g., "balancer"). A lock is taken by writing a document in the configdb's locks collection with that name.

To be maintained, each taken lock needs to be revalidated ("pinged") within a pre-established amount of time. This class does this maintenance automatically once a DistributedLock object was constructed.

Constructor & Destructor Documentation

mongo::DistributedLock::DistributedLock ( const ConnectionString conn,
const string &  name,
unsigned long long  lockTimeout = 0,
bool  asProcess = false 
)

The constructor does not connect to the configdb yet and constructing does not mean the lock was acquired.

Create a new distributed lock, potentially with a custom sleep and takeover time.

Construction does trigger a lock "pinging" mechanism, though.

Parameters
connaddress of config(s) server(s)
nameidentifier for the lock
lockTimeouthow long can the log go "unpinged" before a new attempt to lock steals it (in minutes).
lockPinghow long to wait between lock pings
legacyuse legacy logic

If a custom sleep time is specified (time between pings)

Member Function Documentation

bool mongo::DistributedLock::isLockHeld ( double  timeout,
string *  errMsg 
)

Returns true if we currently believe we hold this lock and it was possible to confirm that, within 'timeout' seconds, if provided, with the config servers.

If the lock is not held or if we failed to contact the config servers within the timeout, returns false.

bool mongo::DistributedLock::lock_try ( const string &  why,
bool  reenter = false,
BSONObj other = 0,
double  timeout = 0.0 
)

Attempts to acquire 'this' lock, checking if it could or should be stolen from the previous holder.

Please consider using the dist_lock_try construct to acquire this lock in an exception safe way.

Parameters
whyhuman readable description of why the lock is being taken (used to log)
whetherthis is a lock re-entry or a new lock
otherconfigdb's lock document that is currently holding the lock, if lock is taken, or our own lock details if not
Returns
true if it managed to grab the lock
Date_t mongo::DistributedLock::remoteTime ( const ConnectionString cluster,
unsigned long long  maxNetSkew = MAX_LOCK_NET_SKEW 
)
static

Get the remote time from a server or cluster.

Returns the remote time as reported by the cluster or server.

The maximum difference between the reported time and the actual time on the remote server (at the completion of the function) is the maxNetSkew


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