Revision c41eea6e lib/locking.py
b/lib/locking.py | ||
---|---|---|
104 | 104 |
def _is_owned(self, shared=-1): |
105 | 105 |
"""Is the current thread somehow owning the lock at this time? |
106 | 106 |
|
107 |
Args: |
|
108 |
shared: |
|
109 |
< 0: check for any type of ownership (default) |
|
110 |
0: check for exclusive ownership |
|
111 |
> 0: check for shared ownership |
|
107 |
@param shared: |
|
108 |
- < 0: check for any type of ownership (default) |
|
109 |
- 0: check for exclusive ownership |
|
110 |
- > 0: check for shared ownership |
|
112 | 111 |
|
113 | 112 |
""" |
114 | 113 |
self.__lock.acquire() |
... | ... | |
123 | 122 |
"""Wait on the given condition, and raise an exception if the current lock |
124 | 123 |
is declared deleted in the meantime. |
125 | 124 |
|
126 |
Args: |
|
127 |
c: condition to wait on |
|
125 |
@param c: the condition to wait on |
|
128 | 126 |
|
129 | 127 |
""" |
130 | 128 |
c.wait() |
... | ... | |
158 | 156 |
def acquire(self, blocking=1, shared=0): |
159 | 157 |
"""Acquire a shared lock. |
160 | 158 |
|
161 |
Args: |
|
162 |
shared: whether to acquire in shared mode. By default an exclusive lock |
|
163 |
will be acquired. |
|
164 |
blocking: whether to block while trying to acquire or to operate in |
|
165 |
try-lock mode. this locking mode is not supported yet. |
|
159 |
@param shared: whether to acquire in shared mode; by default an |
|
160 |
exclusive lock will be acquired |
|
161 |
@param blocking: whether to block while trying to acquire or to |
|
162 |
operate in try-lock mode (this locking mode is not supported yet) |
|
166 | 163 |
|
167 | 164 |
""" |
168 | 165 |
if not blocking: |
... | ... | |
268 | 265 |
acquired in exclusive mode if you don't already own it, then the lock |
269 | 266 |
will be put in a state where any future and pending acquire() fail. |
270 | 267 |
|
271 |
Args: |
|
272 |
blocking: whether to block while trying to acquire or to operate in |
|
273 |
try-lock mode. this locking mode is not supported yet unless |
|
274 |
you are already holding exclusively the lock. |
|
268 |
@param blocking: whether to block while trying to acquire or to |
|
269 |
operate in try-lock mode. this locking mode is not supported |
|
270 |
yet unless you are already holding exclusively the lock. |
|
275 | 271 |
|
276 | 272 |
""" |
277 | 273 |
self.__lock.acquire() |
... | ... | |
317 | 313 |
def __init__(self, members=None): |
318 | 314 |
"""Constructs a new LockSet. |
319 | 315 |
|
320 |
Args: |
|
321 |
members: initial members of the set |
|
316 |
@param members: initial members of the set |
|
322 | 317 |
|
323 | 318 |
""" |
324 | 319 |
# Used internally to guarantee coherency. |
... | ... | |
406 | 401 |
def acquire(self, names, blocking=1, shared=0): |
407 | 402 |
"""Acquire a set of resource locks. |
408 | 403 |
|
409 |
Args: |
|
410 |
names: the names of the locks which shall be acquired. |
|
411 |
(special lock names, or instance/node names) |
|
412 |
shared: whether to acquire in shared mode. By default an exclusive lock |
|
413 |
will be acquired. |
|
414 |
blocking: whether to block while trying to acquire or to operate in |
|
415 |
try-lock mode. this locking mode is not supported yet. |
|
404 |
@param names: the names of the locks which shall be acquired |
|
405 |
(special lock names, or instance/node names) |
|
406 |
@param shared: whether to acquire in shared mode; by default an |
|
407 |
exclusive lock will be acquired |
|
408 |
@param blocking: whether to block while trying to acquire or to |
|
409 |
operate in try-lock mode (this locking mode is not supported yet) |
|
416 | 410 |
|
417 |
Returns: |
|
418 |
True: when all the locks are successfully acquired |
|
411 |
@return: True when all the locks are successfully acquired |
|
419 | 412 |
|
420 |
Raises: |
|
421 |
errors.LockError: when any lock we try to acquire has been deleted |
|
422 |
before we succeed. In this case none of the locks requested will be |
|
423 |
acquired. |
|
413 |
@raise errors.LockError: when any lock we try to acquire has |
|
414 |
been deleted before we succeed. In this case none of the |
|
415 |
locks requested will be acquired. |
|
424 | 416 |
|
425 | 417 |
""" |
426 | 418 |
if not blocking: |
... | ... | |
520 | 512 |
You must have acquired the locks, either in shared or in exclusive mode, |
521 | 513 |
before releasing them. |
522 | 514 |
|
523 |
Args: |
|
524 |
names: the names of the locks which shall be released. |
|
525 |
(defaults to all the locks acquired at that level). |
|
515 |
@param names: the names of the locks which shall be released |
|
516 |
(defaults to all the locks acquired at that level). |
|
526 | 517 |
|
527 | 518 |
""" |
528 | 519 |
assert self._is_owned(), "release() on lock set while not owner" |
... | ... | |
554 | 545 |
def add(self, names, acquired=0, shared=0): |
555 | 546 |
"""Add a new set of elements to the set |
556 | 547 |
|
557 |
Args: |
|
558 |
names: names of the new elements to add |
|
559 |
acquired: pre-acquire the new resource? |
|
560 |
shared: is the pre-acquisition shared? |
|
548 |
@param names: names of the new elements to add |
|
549 |
@param acquired: pre-acquire the new resource? |
|
550 |
@param shared: is the pre-acquisition shared? |
|
561 | 551 |
|
562 | 552 |
""" |
563 | 553 |
# Check we don't already own locks at this level |
... | ... | |
616 | 606 |
You can either not hold anything in the lockset or already hold a superset |
617 | 607 |
of the elements you want to delete, exclusively. |
618 | 608 |
|
619 |
Args: |
|
620 |
names: names of the resource to remove. |
|
621 |
blocking: whether to block while trying to acquire or to operate in |
|
622 |
try-lock mode. this locking mode is not supported yet unless |
|
623 |
you are already holding exclusively the locks. |
|
609 |
@param names: names of the resource to remove. |
|
610 |
@param blocking: whether to block while trying to acquire or to |
|
611 |
operate in try-lock mode (this locking mode is not supported |
|
612 |
yet unless you are already holding exclusively the locks) |
|
624 | 613 |
|
625 |
Returns:
|
|
626 |
A list of lock which we removed. The list is always equal to the names
|
|
627 |
list if we were holding all the locks exclusively.
|
|
614 |
@return:: a list of locks which we removed; the list is always
|
|
615 |
equal to the names list if we were holding all the locks
|
|
616 |
exclusively
|
|
628 | 617 |
|
629 | 618 |
""" |
630 | 619 |
if not blocking and not self._is_owned(): |
... | ... | |
712 | 701 |
There should be only a GanetiLockManager object at any time, so this |
713 | 702 |
function raises an error if this is not the case. |
714 | 703 |
|
715 |
Args: |
|
716 |
nodes: list of node names |
|
717 |
instances: list of instance names |
|
704 |
@param nodes: list of node names |
|
705 |
@param instances: list of instance names |
|
718 | 706 |
|
719 | 707 |
""" |
720 |
assert self.__class__._instance is None, "double GanetiLockManager instance" |
|
708 |
assert self.__class__._instance is None, \ |
|
709 |
"double GanetiLockManager instance" |
|
710 |
|
|
721 | 711 |
self.__class__._instance = self |
722 | 712 |
|
723 | 713 |
# The keyring contains all the locks, at their level and in the correct |
... | ... | |
730 | 720 |
|
731 | 721 |
def _names(self, level): |
732 | 722 |
"""List the lock names at the given level. |
733 |
Used for debugging/testing purposes. |
|
734 | 723 |
|
735 |
Args: |
|
736 |
level: the level whose list of locks to get |
|
724 |
This can be used for debugging/testing purposes. |
|
725 |
|
|
726 |
@param level: the level whose list of locks to get |
|
737 | 727 |
|
738 | 728 |
""" |
739 | 729 |
assert level in LEVELS, "Invalid locking level %s" % level |
... | ... | |
770 | 760 |
return BGL in self.__keyring[LEVEL_CLUSTER]._list_owned() |
771 | 761 |
|
772 | 762 |
def _contains_BGL(self, level, names): |
773 |
"""Check if acting on the given level and set of names will change the |
|
774 |
status of the Big Ganeti Lock. |
|
763 |
"""Check if the level contains the BGL. |
|
764 |
|
|
765 |
Check if acting on the given level and set of names will change |
|
766 |
the status of the Big Ganeti Lock. |
|
775 | 767 |
|
776 | 768 |
""" |
777 | 769 |
return level == LEVEL_CLUSTER and (names is None or BGL in names) |
... | ... | |
779 | 771 |
def acquire(self, level, names, blocking=1, shared=0): |
780 | 772 |
"""Acquire a set of resource locks, at the same level. |
781 | 773 |
|
782 |
Args: |
|
783 |
level: the level at which the locks shall be acquired. |
|
784 |
It must be a memmber of LEVELS. |
|
785 |
names: the names of the locks which shall be acquired. |
|
786 |
(special lock names, or instance/node names) |
|
787 |
shared: whether to acquire in shared mode. By default an exclusive lock |
|
788 |
will be acquired. |
|
789 |
blocking: whether to block while trying to acquire or to operate in |
|
790 |
try-lock mode. this locking mode is not supported yet. |
|
774 |
@param level: the level at which the locks shall be acquired; |
|
775 |
it must be a memmber of LEVELS. |
|
776 |
@param names: the names of the locks which shall be acquired |
|
777 |
(special lock names, or instance/node names) |
|
778 |
@param shared: whether to acquire in shared mode; by default |
|
779 |
an exclusive lock will be acquired |
|
780 |
@param blocking: whether to block while trying to acquire or to |
|
781 |
operate in try-lock mode (this locking mode is not supported yet) |
|
791 | 782 |
|
792 | 783 |
""" |
793 | 784 |
assert level in LEVELS, "Invalid locking level %s" % level |
... | ... | |
812 | 803 |
def release(self, level, names=None): |
813 | 804 |
"""Release a set of resource locks, at the same level. |
814 | 805 |
|
815 |
You must have acquired the locks, either in shared or in exclusive mode,
|
|
816 |
before releasing them. |
|
806 |
You must have acquired the locks, either in shared or in exclusive |
|
807 |
mode, before releasing them.
|
|
817 | 808 |
|
818 |
Args: |
|
819 |
level: the level at which the locks shall be released. |
|
820 |
It must be a memmber of LEVELS. |
|
821 |
names: the names of the locks which shall be released. |
|
822 |
(defaults to all the locks acquired at that level). |
|
809 |
@param level: the level at which the locks shall be released; |
|
810 |
it must be a memmber of LEVELS |
|
811 |
@param names: the names of the locks which shall be released |
|
812 |
(defaults to all the locks acquired at that level) |
|
823 | 813 |
|
824 | 814 |
""" |
825 | 815 |
assert level in LEVELS, "Invalid locking level %s" % level |
... | ... | |
834 | 824 |
def add(self, level, names, acquired=0, shared=0): |
835 | 825 |
"""Add locks at the specified level. |
836 | 826 |
|
837 |
Args:
|
|
838 |
level: the level at which the locks shall be added.
|
|
839 |
It must be a memmber of LEVELS_MOD.
|
|
840 |
names: names of the locks to acquire
|
|
841 |
acquired: whether to acquire the newly added locks
|
|
842 |
shared: whether the acquisition will be shared |
|
827 |
@param level: the level at which the locks shall be added;
|
|
828 |
it must be a memmber of LEVELS_MOD.
|
|
829 |
@param names: names of the locks to acquire
|
|
830 |
@param acquired: whether to acquire the newly added locks
|
|
831 |
@param shared: whether the acquisition will be shared
|
|
832 |
|
|
843 | 833 |
""" |
844 | 834 |
assert level in LEVELS_MOD, "Invalid or immutable level %s" % level |
845 | 835 |
assert self._BGL_owned(), ("You must own the BGL before performing other" |
... | ... | |
851 | 841 |
def remove(self, level, names, blocking=1): |
852 | 842 |
"""Remove locks from the specified level. |
853 | 843 |
|
854 |
You must either already own the locks you are trying to remove exclusively
|
|
855 |
or not own any lock at an upper level. |
|
844 |
You must either already own the locks you are trying to remove |
|
845 |
exclusively or not own any lock at an upper level.
|
|
856 | 846 |
|
857 |
Args: |
|
858 |
level: the level at which the locks shall be removed. |
|
859 |
It must be a memmber of LEVELS_MOD. |
|
860 |
names: the names of the locks which shall be removed. |
|
861 |
(special lock names, or instance/node names) |
|
862 |
blocking: whether to block while trying to operate in try-lock mode. |
|
863 |
this locking mode is not supported yet. |
|
847 |
@param level: the level at which the locks shall be removed; |
|
848 |
it must be a member of LEVELS_MOD |
|
849 |
@param names: the names of the locks which shall be removed |
|
850 |
(special lock names, or instance/node names) |
|
851 |
@param blocking: whether to block while trying to operate in |
|
852 |
try-lock mode (this locking mode is not supported yet) |
|
864 | 853 |
|
865 | 854 |
""" |
866 | 855 |
assert level in LEVELS_MOD, "Invalid or immutable level %s" % level |
Also available in: Unified diff