4 # Copyright (C) 2006, 2007 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 """Module implementing the Ganeti locking code."""
29 from ganeti import errors
30 from ganeti import utils
33 def ssynchronized(lock, shared=0):
34 """Shared Synchronization decorator.
36 Calls the function holding the given lock, either in exclusive or shared
37 mode. It requires the passed lock to be a SharedLock (or support its
42 def sync_function(*args, **kwargs):
43 lock.acquire(shared=shared)
45 return fn(*args, **kwargs)
52 class _SingleNotifyPipeConditionWaiter(object):
53 """Helper class for SingleNotifyPipeCondition
61 def __init__(self, poller, fd):
62 """Constructor for _SingleNotifyPipeConditionWaiter
64 @type poller: select.poll
65 @param poller: Poller object
67 @param fd: File descriptor to wait for
74 def __call__(self, timeout):
75 """Wait for something to happen on the pipe.
77 @type timeout: float or None
78 @param timeout: Timeout for waiting (can be None)
81 start_time = time.time()
82 remaining_time = timeout
84 while timeout is None or remaining_time > 0:
86 result = self._poller.poll(remaining_time)
87 except EnvironmentError, err:
88 if err.errno != errno.EINTR:
92 # Check whether we were notified
93 if result and result[0][0] == self._fd:
96 # Re-calculate timeout if necessary
97 if timeout is not None:
98 remaining_time = start_time + timeout - time.time()
101 class _BaseCondition(object):
102 """Base class containing common code for conditions.
104 Some of this code is taken from python's threading module.
113 def __init__(self, lock):
114 """Constructor for _BaseCondition.
116 @type lock: L{threading.Lock}
117 @param lock: condition base lock
120 object.__init__(self)
122 # Recursive locks are not supported
123 assert not hasattr(lock, "_acquire_restore")
124 assert not hasattr(lock, "_release_save")
128 # Export the lock's acquire() and release() methods
129 self.acquire = lock.acquire
130 self.release = lock.release
133 """Check whether lock is owned by current thread.
136 if self._lock.acquire(0):
142 def _check_owned(self):
143 """Raise an exception if the current thread doesn't own the lock.
146 if not self._is_owned():
147 raise RuntimeError("cannot work with un-aquired lock")
150 class SingleNotifyPipeCondition(_BaseCondition):
151 """Condition which can only be notified once.
153 This condition class uses pipes and poll, internally, to be able to wait for
154 notification with a timeout, without resorting to polling. It is almost
155 compatible with Python's threading.Condition, with the following differences:
156 - notifyAll can only be called once, and no wait can happen after that
157 - notify is not supported, only notifyAll
161 __slots__ = _BaseCondition.__slots__ + [
169 _waiter_class = _SingleNotifyPipeConditionWaiter
171 def __init__(self, lock):
172 """Constructor for SingleNotifyPipeCondition
175 _BaseCondition.__init__(self, lock)
177 self._notified = False
179 self._write_fd = None
182 def _check_unnotified(self):
184 raise RuntimeError("cannot use already notified condition")
187 """Cleanup open file descriptors, if any.
190 if self._read_fd is not None:
191 os.close(self._read_fd)
194 if self._write_fd is not None:
195 os.close(self._write_fd)
196 self._write_fd = None
199 def wait(self, timeout=None):
200 """Wait for a notification.
202 @type timeout: float or None
203 @param timeout: Waiting timeout (can be None)
207 self._check_unnotified()
211 if self._poller is None:
212 (self._read_fd, self._write_fd) = os.pipe()
213 self._poller = select.poll()
214 self._poller.register(self._read_fd, select.POLLHUP)
216 wait_fn = self._waiter_class(self._poller, self._read_fd)
219 # Wait for notification
226 if self._nwaiters == 0:
230 """Close the writing side of the pipe to notify all waiters.
234 self._check_unnotified()
235 self._notified = True
236 if self._write_fd is not None:
237 os.close(self._write_fd)
238 self._write_fd = None
241 class PipeCondition(_BaseCondition):
242 """Group-only non-polling condition with counters.
244 This condition class uses pipes and poll, internally, to be able to wait for
245 notification with a timeout, without resorting to polling. It is almost
246 compatible with Python's threading.Condition, but only supports notifyAll and
247 non-recursive locks. As an additional features it's able to report whether
248 there are any waiting threads.
251 __slots__ = _BaseCondition.__slots__ + [
256 _single_condition_class = SingleNotifyPipeCondition
258 def __init__(self, lock):
259 """Initializes this class.
262 _BaseCondition.__init__(self, lock)
264 self._single_condition = self._single_condition_class(self._lock)
266 def wait(self, timeout=None):
267 """Wait for a notification.
269 @type timeout: float or None
270 @param timeout: Waiting timeout (can be None)
275 # Keep local reference to the pipe. It could be replaced by another thread
276 # notifying while we're waiting.
277 my_condition = self._single_condition
279 assert self._nwaiters >= 0
282 my_condition.wait(timeout)
284 assert self._nwaiters > 0
288 """Notify all currently waiting threads.
292 self._single_condition.notifyAll()
293 self._single_condition = self._single_condition_class(self._lock)
295 def has_waiting(self):
296 """Returns whether there are active waiters.
301 return bool(self._nwaiters)
304 class _CountingCondition(object):
305 """Wrapper for Python's built-in threading.Condition class.
307 This wrapper keeps a count of active waiters. We can't access the internal
308 "__waiters" attribute of threading.Condition because it's not thread-safe.
316 def __init__(self, lock):
317 """Initializes this class.
320 object.__init__(self)
321 self._cond = threading.Condition(lock=lock)
325 """Notifies the condition.
328 return self._cond.notifyAll()
330 def wait(self, timeout=None):
331 """Waits for the condition to be notified.
333 @type timeout: float or None
334 @param timeout: Waiting timeout (can be None)
337 assert self._nwaiters >= 0
341 return self._cond.wait(timeout=timeout)
345 def has_waiting(self):
346 """Returns whether there are active waiters.
349 return bool(self._nwaiters)
352 class SharedLock(object):
353 """Implements a shared lock.
355 Multiple threads can acquire the lock in a shared way, calling
356 acquire_shared(). In order to acquire the lock in an exclusive way threads
357 can call acquire_exclusive().
359 The lock prevents starvation but does not guarantee that threads will acquire
360 the shared lock in the order they queued for it, just that they will
374 __condition_class = PipeCondition
377 """Construct a new SharedLock.
380 object.__init__(self)
383 self.__lock = threading.Lock()
385 # Queue containing waiting acquires
388 # Active and inactive conditions for shared locks
389 self.__active_shr_c = self.__condition_class(self.__lock)
390 self.__inactive_shr_c = self.__condition_class(self.__lock)
392 # Current lock holders
396 # is this lock in the deleted state?
397 self.__deleted = False
399 def __check_deleted(self):
400 """Raises an exception if the lock has been deleted.
404 raise errors.LockError("Deleted lock")
406 def __is_sharer(self):
407 """Is the current thread sharing the lock at this time?
410 return threading.currentThread() in self.__shr
412 def __is_exclusive(self):
413 """Is the current thread holding the lock exclusively at this time?
416 return threading.currentThread() == self.__exc
418 def __is_owned(self, shared=-1):
419 """Is the current thread somehow owning the lock at this time?
421 This is a private version of the function, which presumes you're holding
426 return self.__is_sharer() or self.__is_exclusive()
428 return self.__is_sharer()
430 return self.__is_exclusive()
432 def _is_owned(self, shared=-1):
433 """Is the current thread somehow owning the lock at this time?
436 - < 0: check for any type of ownership (default)
437 - 0: check for exclusive ownership
438 - > 0: check for shared ownership
441 self.__lock.acquire()
443 return self.__is_owned(shared=shared)
445 self.__lock.release()
447 def _count_pending(self):
448 """Returns the number of pending acquires.
453 self.__lock.acquire()
455 return len(self.__pending)
457 self.__lock.release()
459 def __do_acquire(self, shared):
460 """Actually acquire the lock.
464 self.__shr.add(threading.currentThread())
466 self.__exc = threading.currentThread()
468 def __can_acquire(self, shared):
469 """Determine whether lock can be acquired.
473 return self.__exc is None
475 return len(self.__shr) == 0 and self.__exc is None
477 def __is_on_top(self, cond):
478 """Checks whether the passed condition is on top of the queue.
480 The caller must make sure the queue isn't empty.
483 return self.__pending[0] == cond
485 def __acquire_unlocked(self, shared, timeout):
486 """Acquire a shared lock.
488 @param shared: whether to acquire in shared mode; by default an
489 exclusive lock will be acquired
490 @param timeout: maximum waiting time before giving up
493 self.__check_deleted()
495 # We cannot acquire the lock if we already have it
496 assert not self.__is_owned(), "double acquire() on a non-recursive lock"
498 # Check whether someone else holds the lock or there are pending acquires.
499 if not self.__pending and self.__can_acquire(shared):
500 # Apparently not, can acquire lock directly.
501 self.__do_acquire(shared)
505 wait_condition = self.__active_shr_c
507 # Check if we're not yet in the queue
508 if wait_condition not in self.__pending:
509 self.__pending.append(wait_condition)
511 wait_condition = self.__condition_class(self.__lock)
512 # Always add to queue
513 self.__pending.append(wait_condition)
516 # Wait until we become the topmost acquire in the queue or the timeout
518 while not (self.__is_on_top(wait_condition) and
519 self.__can_acquire(shared)):
520 # Wait for notification
521 wait_condition.wait(timeout)
522 self.__check_deleted()
524 # A lot of code assumes blocking acquires always succeed. Loop
525 # internally for that case.
526 if timeout is not None:
529 if self.__is_on_top(wait_condition) and self.__can_acquire(shared):
530 self.__do_acquire(shared)
533 # Remove condition from queue if there are no more waiters
534 if not wait_condition.has_waiting() and not self.__deleted:
535 self.__pending.remove(wait_condition)
539 def acquire(self, shared=0, timeout=None, test_notify=None):
540 """Acquire a shared lock.
543 @param shared: whether to acquire in shared mode; by default an
544 exclusive lock will be acquired
546 @param timeout: maximum waiting time before giving up
547 @type test_notify: callable or None
548 @param test_notify: Special callback function for unittesting
551 self.__lock.acquire()
553 # We already got the lock, notify now
554 if __debug__ and callable(test_notify):
557 return self.__acquire_unlocked(shared, timeout)
559 self.__lock.release()
562 """Release a Shared Lock.
564 You must have acquired the lock, either in shared or in exclusive mode,
565 before calling this function.
568 self.__lock.acquire()
570 assert self.__is_exclusive() or self.__is_sharer(), \
571 "Cannot release non-owned lock"
573 # Autodetect release type
574 if self.__is_exclusive():
577 self.__shr.remove(threading.currentThread())
579 # Notify topmost condition in queue
581 first_condition = self.__pending[0]
582 first_condition.notifyAll()
584 if first_condition == self.__active_shr_c:
585 self.__active_shr_c = self.__inactive_shr_c
586 self.__inactive_shr_c = first_condition
589 self.__lock.release()
591 def delete(self, timeout=None):
592 """Delete a Shared Lock.
594 This operation will declare the lock for removal. First the lock will be
595 acquired in exclusive mode if you don't already own it, then the lock
596 will be put in a state where any future and pending acquire() fail.
599 @param timeout: maximum waiting time before giving up
602 self.__lock.acquire()
604 assert not self.__is_sharer(), "Cannot delete() a lock while sharing it"
606 self.__check_deleted()
608 # The caller is allowed to hold the lock exclusively already.
609 acquired = self.__is_exclusive()
612 acquired = self.__acquire_unlocked(0, timeout)
614 assert self.__is_exclusive() and not self.__is_sharer(), \
615 "Lock wasn't acquired in exclusive mode"
618 self.__deleted = True
621 # Notify all acquires. They'll throw an error.
622 while self.__pending:
623 self.__pending.pop().notifyAll()
627 self.__lock.release()
630 # Whenever we want to acquire a full LockSet we pass None as the value
631 # to acquire. Hide this behind this nicely named constant.
636 """Implements a set of locks.
638 This abstraction implements a set of shared locks for the same resource type,
639 distinguished by name. The user can lock a subset of the resources and the
640 LockSet will take care of acquiring the locks always in the same order, thus
643 All the locks needed in the same set must be acquired together, though.
646 def __init__(self, members=None):
647 """Constructs a new LockSet.
649 @param members: initial members of the set
652 # Used internally to guarantee coherency.
653 self.__lock = SharedLock()
655 # The lockdict indexes the relationship name -> lock
656 # The order-of-locking is implied by the alphabetical order of names
659 if members is not None:
661 self.__lockdict[name] = SharedLock()
663 # The owner dict contains the set of locks each thread owns. For
664 # performance each thread can access its own key without a global lock on
665 # this structure. It is paramount though that *no* other type of access is
666 # done to this structure (eg. no looping over its keys). *_owner helper
667 # function are defined to guarantee access is correct, but in general never
668 # do anything different than __owners[threading.currentThread()], or there
673 """Is the current thread a current level owner?"""
674 return threading.currentThread() in self.__owners
676 def _add_owned(self, name=None):
677 """Note the current thread owns the given lock"""
679 if not self._is_owned():
680 self.__owners[threading.currentThread()] = set()
683 self.__owners[threading.currentThread()].add(name)
685 self.__owners[threading.currentThread()] = set([name])
687 def _del_owned(self, name=None):
688 """Note the current thread owns the given lock"""
691 self.__owners[threading.currentThread()].remove(name)
693 # Only remove the key if we don't hold the set-lock as well
694 if (not self.__lock._is_owned() and
695 not self.__owners[threading.currentThread()]):
696 del self.__owners[threading.currentThread()]
698 def _list_owned(self):
699 """Get the set of resource names owned by the current thread"""
701 return self.__owners[threading.currentThread()].copy()
706 """Return the current set of names.
708 Only call this function while holding __lock and don't iterate on the
709 result after releasing the lock.
712 return self.__lockdict.keys()
715 """Return a copy of the current set of elements.
717 Used only for debugging purposes.
720 # If we don't already own the set-level lock acquired
721 # we'll get it and note we need to release it later.
723 if not self.__lock._is_owned():
725 self.__lock.acquire(shared=1)
727 result = self.__names()
730 self.__lock.release()
733 def acquire(self, names, timeout=None, shared=0):
734 """Acquire a set of resource locks.
736 @param names: the names of the locks which shall be acquired
737 (special lock names, or instance/node names)
738 @param shared: whether to acquire in shared mode; by default an
739 exclusive lock will be acquired
741 @param timeout: Maximum time to acquire all locks
743 @return: True when all the locks are successfully acquired
745 @raise errors.LockError: when any lock we try to acquire has
746 been deleted before we succeed. In this case none of the
747 locks requested will be acquired.
750 if timeout is not None:
751 raise NotImplementedError
753 # Check we don't already own locks at this level
754 assert not self._is_owned(), "Cannot acquire locks in the same set twice"
757 # If no names are given acquire the whole set by not letting new names
758 # being added before we release, and getting the current list of names.
759 # Some of them may then be deleted later, but we'll cope with this.
761 # We'd like to acquire this lock in a shared way, as it's nice if
762 # everybody else can use the instances at the same time. If are acquiring
763 # them exclusively though they won't be able to do this anyway, though,
764 # so we'll get the list lock exclusively as well in order to be able to
765 # do add() on the set while owning it.
766 self.__lock.acquire(shared=shared)
768 # note we own the set-lock
770 names = self.__names()
772 # We shouldn't have problems adding the lock to the owners list, but
773 # if we did we'll try to release this lock and re-raise exception.
774 # Of course something is going to be really wrong, after this.
775 self.__lock.release()
779 # Support passing in a single resource to acquire rather than many
780 if isinstance(names, basestring):
783 names = sorted(names)
786 # First we look the locks up on __lockdict. We have no way of being sure
787 # they will still be there after, but this makes it a lot faster should
788 # just one of them be the already wrong
789 for lname in utils.UniqueSequence(names):
791 lock = self.__lockdict[lname] # raises KeyError if lock is not there
792 acquire_list.append((lname, lock))
794 if self.__lock._is_owned():
795 # We are acquiring all the set, it doesn't matter if this
796 # particular element is not there anymore.
799 raise errors.LockError('non-existing lock in set (%s)' % lname)
801 # This will hold the locknames we effectively acquired.
803 # Now acquire_list contains a sorted list of resources and locks we want.
804 # In order to get them we loop on this (private) list and acquire() them.
805 # We gave no real guarantee they will still exist till this is done but
806 # .acquire() itself is safe and will alert us if the lock gets deleted.
807 for (lname, lock) in acquire_list:
809 lock.acquire(shared=shared) # raises LockError if the lock is deleted
810 # now the lock cannot be deleted, we have it!
811 self._add_owned(name=lname)
813 except (errors.LockError):
814 if self.__lock._is_owned():
815 # We are acquiring all the set, it doesn't matter if this
816 # particular element is not there anymore.
820 for lname in self._list_owned():
821 self.__lockdict[lname].release()
822 self._del_owned(name=lname)
823 raise errors.LockError('non-existing lock in set (%s)' % name_fail)
825 # We shouldn't have problems adding the lock to the owners list, but
826 # if we did we'll try to release this lock and re-raise exception.
827 # Of course something is going to be really wrong, after this.
833 # If something went wrong and we had the set-lock let's release it...
834 if self.__lock._is_owned():
835 self.__lock.release()
840 def release(self, names=None):
841 """Release a set of resource locks, at the same level.
843 You must have acquired the locks, either in shared or in exclusive mode,
844 before releasing them.
846 @param names: the names of the locks which shall be released
847 (defaults to all the locks acquired at that level).
850 assert self._is_owned(), "release() on lock set while not owner"
852 # Support passing in a single resource to release rather than many
853 if isinstance(names, basestring):
857 names = self._list_owned()
860 assert self._list_owned().issuperset(names), (
861 "release() on unheld resources %s" %
862 names.difference(self._list_owned()))
864 # First of all let's release the "all elements" lock, if set.
865 # After this 'add' can work again
866 if self.__lock._is_owned():
867 self.__lock.release()
870 for lockname in names:
871 # If we are sure the lock doesn't leave __lockdict without being
872 # exclusively held we can do this...
873 self.__lockdict[lockname].release()
874 self._del_owned(name=lockname)
876 def add(self, names, acquired=0, shared=0):
877 """Add a new set of elements to the set
879 @param names: names of the new elements to add
880 @param acquired: pre-acquire the new resource?
881 @param shared: is the pre-acquisition shared?
884 # Check we don't already own locks at this level
885 assert not self._is_owned() or self.__lock._is_owned(shared=0), \
886 "Cannot add locks if the set is only partially owned, or shared"
888 # Support passing in a single resource to add rather than many
889 if isinstance(names, basestring):
892 # If we don't already own the set-level lock acquired in an exclusive way
893 # we'll get it and note we need to release it later.
895 if not self.__lock._is_owned():
897 self.__lock.acquire()
900 invalid_names = set(self.__names()).intersection(names)
902 # This must be an explicit raise, not an assert, because assert is
903 # turned off when using optimization, and this can happen because of
904 # concurrency even if the user doesn't want it.
905 raise errors.LockError("duplicate add() (%s)" % invalid_names)
907 for lockname in names:
911 lock.acquire(shared=shared)
912 # now the lock cannot be deleted, we have it!
914 self._add_owned(name=lockname)
916 # We shouldn't have problems adding the lock to the owners list,
917 # but if we did we'll try to release this lock and re-raise
918 # exception. Of course something is going to be really wrong,
919 # after this. On the other hand the lock hasn't been added to the
920 # __lockdict yet so no other threads should be pending on it. This
921 # release is just a safety measure.
925 self.__lockdict[lockname] = lock
928 # Only release __lock if we were not holding it previously.
930 self.__lock.release()
934 def remove(self, names):
935 """Remove elements from the lock set.
937 You can either not hold anything in the lockset or already hold a superset
938 of the elements you want to delete, exclusively.
940 @param names: names of the resource to remove.
942 @return:: a list of locks which we removed; the list is always
943 equal to the names list if we were holding all the locks
947 # Support passing in a single resource to remove rather than many
948 if isinstance(names, basestring):
951 # If we own any subset of this lock it must be a superset of what we want
952 # to delete. The ownership must also be exclusive, but that will be checked
953 # by the lock itself.
954 assert not self._is_owned() or self._list_owned().issuperset(names), (
955 "remove() on acquired lockset while not owning all elements")
960 # Calling delete() acquires the lock exclusively if we don't already own
961 # it, and causes all pending and subsequent lock acquires to fail. It's
962 # fine to call it out of order because delete() also implies release(),
963 # and the assertion above guarantees that if we either already hold
964 # everything we want to delete, or we hold none.
966 self.__lockdict[lname].delete()
967 removed.append(lname)
968 except (KeyError, errors.LockError):
969 # This cannot happen if we were already holding it, verify:
970 assert not self._is_owned(), "remove failed while holding lockset"
972 # If no LockError was raised we are the ones who deleted the lock.
973 # This means we can safely remove it from lockdict, as any further or
974 # pending delete() or acquire() will fail (and nobody can have the lock
975 # since before our call to delete()).
977 # This is done in an else clause because if the exception was thrown
978 # it's the job of the one who actually deleted it.
979 del self.__lockdict[lname]
980 # And let's remove it from our private list if we owned it.
982 self._del_owned(name=lname)
987 # Locking levels, must be acquired in increasing order.
989 # - at level LEVEL_CLUSTER resides the Big Ganeti Lock (BGL) which must be
990 # acquired before performing any operation, either in shared or in exclusive
991 # mode. acquiring the BGL in exclusive mode is discouraged and should be
993 # - at levels LEVEL_NODE and LEVEL_INSTANCE reside node and instance locks.
994 # If you need more than one node, or more than one instance, acquire them at
1000 LEVELS = [LEVEL_CLUSTER,
1004 # Lock levels which are modifiable
1005 LEVELS_MOD = [LEVEL_NODE, LEVEL_INSTANCE]
1008 LEVEL_CLUSTER: "cluster",
1009 LEVEL_INSTANCE: "instance",
1013 # Constant for the big ganeti lock
1017 class GanetiLockManager:
1018 """The Ganeti Locking Library
1020 The purpose of this small library is to manage locking for ganeti clusters
1021 in a central place, while at the same time doing dynamic checks against
1022 possible deadlocks. It will also make it easier to transition to a different
1023 lock type should we migrate away from python threads.
1028 def __init__(self, nodes=None, instances=None):
1029 """Constructs a new GanetiLockManager object.
1031 There should be only a GanetiLockManager object at any time, so this
1032 function raises an error if this is not the case.
1034 @param nodes: list of node names
1035 @param instances: list of instance names
1038 assert self.__class__._instance is None, \
1039 "double GanetiLockManager instance"
1041 self.__class__._instance = self
1043 # The keyring contains all the locks, at their level and in the correct
1046 LEVEL_CLUSTER: LockSet([BGL]),
1047 LEVEL_NODE: LockSet(nodes),
1048 LEVEL_INSTANCE: LockSet(instances),
1051 def _names(self, level):
1052 """List the lock names at the given level.
1054 This can be used for debugging/testing purposes.
1056 @param level: the level whose list of locks to get
1059 assert level in LEVELS, "Invalid locking level %s" % level
1060 return self.__keyring[level]._names()
1062 def _is_owned(self, level):
1063 """Check whether we are owning locks at the given level
1066 return self.__keyring[level]._is_owned()
1068 is_owned = _is_owned
1070 def _list_owned(self, level):
1071 """Get the set of owned locks at the given level
1074 return self.__keyring[level]._list_owned()
1076 def _upper_owned(self, level):
1077 """Check that we don't own any lock at a level greater than the given one.
1080 # This way of checking only works if LEVELS[i] = i, which we check for in
1082 return utils.any((self._is_owned(l) for l in LEVELS[level + 1:]))
1084 def _BGL_owned(self):
1085 """Check if the current thread owns the BGL.
1087 Both an exclusive or a shared acquisition work.
1090 return BGL in self.__keyring[LEVEL_CLUSTER]._list_owned()
1092 def _contains_BGL(self, level, names):
1093 """Check if the level contains the BGL.
1095 Check if acting on the given level and set of names will change
1096 the status of the Big Ganeti Lock.
1099 return level == LEVEL_CLUSTER and (names is None or BGL in names)
1101 def acquire(self, level, names, timeout=None, shared=0):
1102 """Acquire a set of resource locks, at the same level.
1104 @param level: the level at which the locks shall be acquired;
1105 it must be a member of LEVELS.
1106 @param names: the names of the locks which shall be acquired
1107 (special lock names, or instance/node names)
1108 @param shared: whether to acquire in shared mode; by default
1109 an exclusive lock will be acquired
1110 @type timeout: float
1111 @param timeout: Maximum time to acquire all locks
1114 assert level in LEVELS, "Invalid locking level %s" % level
1116 # Check that we are either acquiring the Big Ganeti Lock or we already own
1117 # it. Some "legacy" opcodes need to be sure they are run non-concurrently
1118 # so even if we've migrated we need to at least share the BGL to be
1119 # compatible with them. Of course if we own the BGL exclusively there's no
1120 # point in acquiring any other lock, unless perhaps we are half way through
1121 # the migration of the current opcode.
1122 assert (self._contains_BGL(level, names) or self._BGL_owned()), (
1123 "You must own the Big Ganeti Lock before acquiring any other")
1125 # Check we don't own locks at the same or upper levels.
1126 assert not self._upper_owned(level), ("Cannot acquire locks at a level"
1127 " while owning some at a greater one")
1129 # Acquire the locks in the set.
1130 return self.__keyring[level].acquire(names, shared=shared, timeout=timeout)
1132 def release(self, level, names=None):
1133 """Release a set of resource locks, at the same level.
1135 You must have acquired the locks, either in shared or in exclusive
1136 mode, before releasing them.
1138 @param level: the level at which the locks shall be released;
1139 it must be a member of LEVELS
1140 @param names: the names of the locks which shall be released
1141 (defaults to all the locks acquired at that level)
1144 assert level in LEVELS, "Invalid locking level %s" % level
1145 assert (not self._contains_BGL(level, names) or
1146 not self._upper_owned(LEVEL_CLUSTER)), (
1147 "Cannot release the Big Ganeti Lock while holding something"
1150 # Release will complain if we don't own the locks already
1151 return self.__keyring[level].release(names)
1153 def add(self, level, names, acquired=0, shared=0):
1154 """Add locks at the specified level.
1156 @param level: the level at which the locks shall be added;
1157 it must be a member of LEVELS_MOD.
1158 @param names: names of the locks to acquire
1159 @param acquired: whether to acquire the newly added locks
1160 @param shared: whether the acquisition will be shared
1163 assert level in LEVELS_MOD, "Invalid or immutable level %s" % level
1164 assert self._BGL_owned(), ("You must own the BGL before performing other"
1166 assert not self._upper_owned(level), ("Cannot add locks at a level"
1167 " while owning some at a greater one")
1168 return self.__keyring[level].add(names, acquired=acquired, shared=shared)
1170 def remove(self, level, names):
1171 """Remove locks from the specified level.
1173 You must either already own the locks you are trying to remove
1174 exclusively or not own any lock at an upper level.
1176 @param level: the level at which the locks shall be removed;
1177 it must be a member of LEVELS_MOD
1178 @param names: the names of the locks which shall be removed
1179 (special lock names, or instance/node names)
1182 assert level in LEVELS_MOD, "Invalid or immutable level %s" % level
1183 assert self._BGL_owned(), ("You must own the BGL before performing other"
1185 # Check we either own the level or don't own anything from here
1186 # up. LockSet.remove() will check the case in which we don't own
1187 # all the needed resources, or we have a shared ownership.
1188 assert self._is_owned(level) or not self._upper_owned(level), (
1189 "Cannot remove locks at a level while not owning it or"
1190 " owning some at a greater one")
1191 return self.__keyring[level].remove(names)