Package ganeti :: Module locking :: Class LockSet
[hide private]
[frames] | no frames]

Class LockSet

source code

Implements a set of locks.

This abstraction implements a set of shared locks for the same resource type, distinguished by name. The user can lock a subset of the resources and the LockSet will take care of acquiring the locks always in the same order, thus preventing deadlock.

All the locks needed in the same set must be acquired together, though.

Instance Methods [hide private]
 
__init__(self, members, name, monitor=None)
Constructs a new LockSet.
source code
 
_GetLockName(self, mname)
Returns the name for a member lock.
source code
 
_get_lock(self)
Returns the lockset-internal lock.
source code
 
_get_lockdict(self)
Returns the lockset-internal lock dictionary.
source code
 
is_owned(self)
Is the current thread a current level owner?
source code
bool
check_owned(self, names, shared=-1)
Check if locks are owned in a specific mode.
source code
boolean
owning_all(self)
Checks whether current thread owns internal lock.
source code
 
_add_owned(self, name=None)
Note the current thread owns the given lock
source code
 
_del_owned(self, name=None)
Note the current thread owns the given lock
source code
 
list_owned(self)
Get the set of resource names owned by the current thread
source code
 
_release_and_delete_owned(self)
Release and delete all resources owned by the current thread
source code
 
__names(self)
Return the current set of names.
source code
 
_names(self)
Return a copy of the current set of elements.
source code
 
acquire(self, names, timeout=None, shared=0, priority=None, opportunistic=False, test_notify=None)
Acquire a set of resource locks.
source code
 
__acquire_inner(self, names, mode, shared, priority, timeout_fn, test_notify)
Inner logic for acquiring a number of locks.
source code
 
downgrade(self, names=None)
Downgrade a set of resource locks from exclusive to shared mode.
source code
 
release(self, names=None)
Release a set of resource locks, at the same level.
source code
 
add(self, names, acquired=0, shared=0)
Add a new set of elements to the set
source code
 
remove(self, names)
Remove elements from the lock set.
source code
Instance Variables [hide private]
string name
the name of the lockset
Method Details [hide private]

__init__(self, members, name, monitor=None)
(Constructor)

source code 

Constructs a new LockSet.

Parameters:
  • members (list of strings) - initial members of the set
  • monitor (LockMonitor) - Lock monitor with which to register member locks

_get_lockdict(self)

source code 

Returns the lockset-internal lock dictionary.

Accessing this structure is only safe in single-thread usage or when the lockset-internal lock is held.

is_owned(self)

source code 

Is the current thread a current level owner?

Note: Use check_owned to check if a specific lock is held

check_owned(self, names, shared=-1)

source code 

Check if locks are owned in a specific mode.

Parameters:
  • names (sequence or string) - Lock names (or a single lock name)
  • shared - See SharedLock.is_owned
Returns: bool

Note: Use is_owned to check if the current thread holds any lock and list_owned to get the names of all owned locks

owning_all(self)

source code 

Checks whether current thread owns internal lock.

Holding the internal lock is equivalent with holding all locks in the set (the opposite does not necessarily hold as it can not be easily determined). add and remove require the internal lock.

Returns: boolean

__names(self)

source code 

Return the current set of names.

Only call this function while holding __lock and don't iterate on the result after releasing the lock.

_names(self)

source code 

Return a copy of the current set of elements.

Used only for debugging purposes.

acquire(self, names, timeout=None, shared=0, priority=None, opportunistic=False, test_notify=None)

source code 

Acquire a set of resource locks.

Parameters:
  • names (list of strings (or string)) - the names of the locks which shall be acquired (special lock names, or instance/node names)
  • shared (integer (0/1) used as a boolean) - whether to acquire in shared mode; by default an exclusive lock will be acquired
  • timeout (float or None) - Maximum time to acquire all locks; for opportunistic acquisitions, a timeout can only be given when names is None, in which case it is exclusively used for acquiring the LockSet-internal lock; opportunistic acquisitions don't use a timeout for acquiring individual locks
  • priority (integer) - Priority for acquiring locks
  • opportunistic (boolean) - Acquire locks opportunistically; use the return value to determine which locks were actually acquired
  • test_notify (callable or None) - Special callback function for unittesting
Returns:
Set of all locks successfully acquired or None in case of timeout
Raises:
  • errors.LockError - when any lock we try to acquire has been deleted before we succeed. In this case none of the locks requested will be acquired.

Note: When acquiring locks opportunistically, any number of locks might actually be acquired, even zero.

__acquire_inner(self, names, mode, shared, priority, timeout_fn, test_notify)

source code 

Inner logic for acquiring a number of locks.

Acquisition modes:

  • _LS_ACQUIRE_ALL: names contains names of all locks in set, but deleted locks can be ignored as the whole set is being acquired with its internal lock held
  • _LS_ACQUIRE_EXACT: The names listed in names must be acquired; timeouts and deleted locks are fatal
  • _LS_ACQUIRE_OPPORTUNISTIC: names lists names of locks (potentially all within the set) which should be acquired opportunistically, that is failures are ignored
Parameters:
  • names - Names of the locks to be acquired
  • mode - Lock acquisition mode (one of _LS_ACQUIRE_MODES)
  • shared - Whether to acquire in shared mode
  • timeout_fn - Function returning remaining timeout (None for opportunistic acquisitions)
  • priority - Priority for acquiring locks
  • test_notify - Special callback function for unittesting

downgrade(self, names=None)

source code 

Downgrade a set of resource locks from exclusive to shared mode.

The locks must have been acquired in exclusive mode.

release(self, names=None)

source code 

Release a set of resource locks, at the same level.

You must have acquired the locks, either in shared or in exclusive mode, before releasing them.

Parameters:
  • names (list of strings, or None) - the names of the locks which shall be released (defaults to all the locks acquired at that level).

add(self, names, acquired=0, shared=0)

source code 

Add a new set of elements to the set

Parameters:
  • names (list of strings) - names of the new elements to add
  • acquired (integer (0/1) used as a boolean) - pre-acquire the new resource?
  • shared (integer (0/1) used as a boolean) - is the pre-acquisition shared?

remove(self, names)

source code 

Remove elements from the lock set.

You can either not hold anything in the lockset or already hold a superset of the elements you want to delete, exclusively.

Parameters:
  • names (list of strings) - names of the resource to remove.
Returns:
a list of locks which we removed; the list is always equal to the names list if we were holding all the locks exclusively