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."""
23 # pylint: disable-msg=W0212
25 # W0212 since e.g. LockSet methods use (a lot) the internals of
34 from ganeti import errors
35 from ganeti import utils
36 from ganeti import compat
39 def ssynchronized(lock, shared=0):
40 """Shared Synchronization decorator.
42 Calls the function holding the given lock, either in exclusive or shared
43 mode. It requires the passed lock to be a SharedLock (or support its
48 def sync_function(*args, **kwargs):
49 lock.acquire(shared=shared)
51 return fn(*args, **kwargs)
58 class RunningTimeout(object):
59 """Class to calculate remaining timeout when doing several operations.
69 def __init__(self, timeout, allow_negative, _time_fn=time.time):
70 """Initializes this class.
73 @param timeout: Timeout duration
74 @type allow_negative: bool
75 @param allow_negative: Whether to return values below zero
76 @param _time_fn: Time function for unittests
81 if timeout is not None and timeout < 0.0:
82 raise ValueError("Timeout must not be negative")
84 self._timeout = timeout
85 self._allow_negative = allow_negative
86 self._time_fn = _time_fn
88 self._start_time = None
91 """Returns the remaining timeout.
94 if self._timeout is None:
97 # Get start time on first calculation
98 if self._start_time is None:
99 self._start_time = self._time_fn()
101 # Calculate remaining time
102 remaining_timeout = self._start_time + self._timeout - self._time_fn()
104 if not self._allow_negative:
105 # Ensure timeout is always >= 0
106 return max(0.0, remaining_timeout)
108 return remaining_timeout
111 class _SingleNotifyPipeConditionWaiter(object):
112 """Helper class for SingleNotifyPipeCondition
120 def __init__(self, poller, fd):
121 """Constructor for _SingleNotifyPipeConditionWaiter
123 @type poller: select.poll
124 @param poller: Poller object
126 @param fd: File descriptor to wait for
129 object.__init__(self)
130 self._poller = poller
133 def __call__(self, timeout):
134 """Wait for something to happen on the pipe.
136 @type timeout: float or None
137 @param timeout: Timeout for waiting (can be None)
140 running_timeout = RunningTimeout(timeout, True)
143 remaining_time = running_timeout.Remaining()
145 if remaining_time is not None:
146 if remaining_time < 0.0:
149 # Our calculation uses seconds, poll() wants milliseconds
150 remaining_time *= 1000
153 result = self._poller.poll(remaining_time)
154 except EnvironmentError, err:
155 if err.errno != errno.EINTR:
159 # Check whether we were notified
160 if result and result[0][0] == self._fd:
164 class _BaseCondition(object):
165 """Base class containing common code for conditions.
167 Some of this code is taken from python's threading module.
176 def __init__(self, lock):
177 """Constructor for _BaseCondition.
179 @type lock: threading.Lock
180 @param lock: condition base lock
183 object.__init__(self)
185 # Recursive locks are not supported
186 assert not hasattr(lock, "_acquire_restore")
187 assert not hasattr(lock, "_release_save")
191 # Export the lock's acquire() and release() methods
192 self.acquire = lock.acquire
193 self.release = lock.release
196 """Check whether lock is owned by current thread.
199 if self._lock.acquire(0):
205 def _check_owned(self):
206 """Raise an exception if the current thread doesn't own the lock.
209 if not self._is_owned():
210 raise RuntimeError("cannot work with un-aquired lock")
213 class SingleNotifyPipeCondition(_BaseCondition):
214 """Condition which can only be notified once.
216 This condition class uses pipes and poll, internally, to be able to wait for
217 notification with a timeout, without resorting to polling. It is almost
218 compatible with Python's threading.Condition, with the following differences:
219 - notifyAll can only be called once, and no wait can happen after that
220 - notify is not supported, only notifyAll
232 _waiter_class = _SingleNotifyPipeConditionWaiter
234 def __init__(self, lock):
235 """Constructor for SingleNotifyPipeCondition
238 _BaseCondition.__init__(self, lock)
240 self._notified = False
242 self._write_fd = None
245 def _check_unnotified(self):
246 """Throws an exception if already notified.
250 raise RuntimeError("cannot use already notified condition")
253 """Cleanup open file descriptors, if any.
256 if self._read_fd is not None:
257 os.close(self._read_fd)
260 if self._write_fd is not None:
261 os.close(self._write_fd)
262 self._write_fd = None
265 def wait(self, timeout=None):
266 """Wait for a notification.
268 @type timeout: float or None
269 @param timeout: Waiting timeout (can be None)
273 self._check_unnotified()
277 if self._poller is None:
278 (self._read_fd, self._write_fd) = os.pipe()
279 self._poller = select.poll()
280 self._poller.register(self._read_fd, select.POLLHUP)
282 wait_fn = self._waiter_class(self._poller, self._read_fd)
285 # Wait for notification
292 if self._nwaiters == 0:
295 def notifyAll(self): # pylint: disable-msg=C0103
296 """Close the writing side of the pipe to notify all waiters.
300 self._check_unnotified()
301 self._notified = True
302 if self._write_fd is not None:
303 os.close(self._write_fd)
304 self._write_fd = None
307 class PipeCondition(_BaseCondition):
308 """Group-only non-polling condition with counters.
310 This condition class uses pipes and poll, internally, to be able to wait for
311 notification with a timeout, without resorting to polling. It is almost
312 compatible with Python's threading.Condition, but only supports notifyAll and
313 non-recursive locks. As an additional features it's able to report whether
314 there are any waiting threads.
322 _single_condition_class = SingleNotifyPipeCondition
324 def __init__(self, lock):
325 """Initializes this class.
328 _BaseCondition.__init__(self, lock)
330 self._single_condition = self._single_condition_class(self._lock)
332 def wait(self, timeout=None):
333 """Wait for a notification.
335 @type timeout: float or None
336 @param timeout: Waiting timeout (can be None)
341 # Keep local reference to the pipe. It could be replaced by another thread
342 # notifying while we're waiting.
343 my_condition = self._single_condition
345 assert self._nwaiters >= 0
348 my_condition.wait(timeout)
350 assert self._nwaiters > 0
353 def notifyAll(self): # pylint: disable-msg=C0103
354 """Notify all currently waiting threads.
358 self._single_condition.notifyAll()
359 self._single_condition = self._single_condition_class(self._lock)
361 def has_waiting(self):
362 """Returns whether there are active waiters.
367 return bool(self._nwaiters)
370 class _CountingCondition(object):
371 """Wrapper for Python's built-in threading.Condition class.
373 This wrapper keeps a count of active waiters. We can't access the internal
374 "__waiters" attribute of threading.Condition because it's not thread-safe.
382 def __init__(self, lock):
383 """Initializes this class.
386 object.__init__(self)
387 self._cond = threading.Condition(lock=lock)
390 def notifyAll(self): # pylint: disable-msg=C0103
391 """Notifies the condition.
394 return self._cond.notifyAll()
396 def wait(self, timeout=None):
397 """Waits for the condition to be notified.
399 @type timeout: float or None
400 @param timeout: Waiting timeout (can be None)
403 assert self._nwaiters >= 0
407 return self._cond.wait(timeout=timeout)
411 def has_waiting(self):
412 """Returns whether there are active waiters.
415 return bool(self._nwaiters)
418 class SharedLock(object):
419 """Implements a shared lock.
421 Multiple threads can acquire the lock in a shared way, calling
422 acquire_shared(). In order to acquire the lock in an exclusive way threads
423 can call acquire_exclusive().
425 The lock prevents starvation but does not guarantee that threads will acquire
426 the shared lock in the order they queued for it, just that they will
440 __condition_class = PipeCondition
443 """Construct a new SharedLock.
446 object.__init__(self)
449 self.__lock = threading.Lock()
451 # Queue containing waiting acquires
454 # Active and inactive conditions for shared locks
455 self.__active_shr_c = self.__condition_class(self.__lock)
456 self.__inactive_shr_c = self.__condition_class(self.__lock)
458 # Current lock holders
462 # is this lock in the deleted state?
463 self.__deleted = False
465 def __check_deleted(self):
466 """Raises an exception if the lock has been deleted.
470 raise errors.LockError("Deleted lock")
472 def __is_sharer(self):
473 """Is the current thread sharing the lock at this time?
476 return threading.currentThread() in self.__shr
478 def __is_exclusive(self):
479 """Is the current thread holding the lock exclusively at this time?
482 return threading.currentThread() == self.__exc
484 def __is_owned(self, shared=-1):
485 """Is the current thread somehow owning the lock at this time?
487 This is a private version of the function, which presumes you're holding
492 return self.__is_sharer() or self.__is_exclusive()
494 return self.__is_sharer()
496 return self.__is_exclusive()
498 def _is_owned(self, shared=-1):
499 """Is the current thread somehow owning the lock at this time?
502 - < 0: check for any type of ownership (default)
503 - 0: check for exclusive ownership
504 - > 0: check for shared ownership
507 self.__lock.acquire()
509 return self.__is_owned(shared=shared)
511 self.__lock.release()
513 def _count_pending(self):
514 """Returns the number of pending acquires.
519 self.__lock.acquire()
521 return len(self.__pending)
523 self.__lock.release()
525 def __do_acquire(self, shared):
526 """Actually acquire the lock.
530 self.__shr.add(threading.currentThread())
532 self.__exc = threading.currentThread()
534 def __can_acquire(self, shared):
535 """Determine whether lock can be acquired.
539 return self.__exc is None
541 return len(self.__shr) == 0 and self.__exc is None
543 def __is_on_top(self, cond):
544 """Checks whether the passed condition is on top of the queue.
546 The caller must make sure the queue isn't empty.
549 return self.__pending[0] == cond
551 def __acquire_unlocked(self, shared, timeout):
552 """Acquire a shared lock.
554 @param shared: whether to acquire in shared mode; by default an
555 exclusive lock will be acquired
556 @param timeout: maximum waiting time before giving up
559 self.__check_deleted()
561 # We cannot acquire the lock if we already have it
562 assert not self.__is_owned(), "double acquire() on a non-recursive lock"
564 # Check whether someone else holds the lock or there are pending acquires.
565 if not self.__pending and self.__can_acquire(shared):
566 # Apparently not, can acquire lock directly.
567 self.__do_acquire(shared)
571 wait_condition = self.__active_shr_c
573 # Check if we're not yet in the queue
574 if wait_condition not in self.__pending:
575 self.__pending.append(wait_condition)
577 wait_condition = self.__condition_class(self.__lock)
578 # Always add to queue
579 self.__pending.append(wait_condition)
582 # Wait until we become the topmost acquire in the queue or the timeout
584 while not (self.__is_on_top(wait_condition) and
585 self.__can_acquire(shared)):
586 # Wait for notification
587 wait_condition.wait(timeout)
588 self.__check_deleted()
590 # A lot of code assumes blocking acquires always succeed. Loop
591 # internally for that case.
592 if timeout is not None:
595 if self.__is_on_top(wait_condition) and self.__can_acquire(shared):
596 self.__do_acquire(shared)
599 # Remove condition from queue if there are no more waiters
600 if not wait_condition.has_waiting() and not self.__deleted:
601 self.__pending.remove(wait_condition)
605 def acquire(self, shared=0, timeout=None, test_notify=None):
606 """Acquire a shared lock.
608 @type shared: integer (0/1) used as a boolean
609 @param shared: whether to acquire in shared mode; by default an
610 exclusive lock will be acquired
612 @param timeout: maximum waiting time before giving up
613 @type test_notify: callable or None
614 @param test_notify: Special callback function for unittesting
617 self.__lock.acquire()
619 # We already got the lock, notify now
620 if __debug__ and callable(test_notify):
623 return self.__acquire_unlocked(shared, timeout)
625 self.__lock.release()
628 """Release a Shared Lock.
630 You must have acquired the lock, either in shared or in exclusive mode,
631 before calling this function.
634 self.__lock.acquire()
636 assert self.__is_exclusive() or self.__is_sharer(), \
637 "Cannot release non-owned lock"
639 # Autodetect release type
640 if self.__is_exclusive():
643 self.__shr.remove(threading.currentThread())
645 # Notify topmost condition in queue
647 first_condition = self.__pending[0]
648 first_condition.notifyAll()
650 if first_condition == self.__active_shr_c:
651 self.__active_shr_c = self.__inactive_shr_c
652 self.__inactive_shr_c = first_condition
655 self.__lock.release()
657 def delete(self, timeout=None):
658 """Delete a Shared Lock.
660 This operation will declare the lock for removal. First the lock will be
661 acquired in exclusive mode if you don't already own it, then the lock
662 will be put in a state where any future and pending acquire() fail.
665 @param timeout: maximum waiting time before giving up
668 self.__lock.acquire()
670 assert not self.__is_sharer(), "Cannot delete() a lock while sharing it"
672 self.__check_deleted()
674 # The caller is allowed to hold the lock exclusively already.
675 acquired = self.__is_exclusive()
678 acquired = self.__acquire_unlocked(0, timeout)
680 assert self.__is_exclusive() and not self.__is_sharer(), \
681 "Lock wasn't acquired in exclusive mode"
684 self.__deleted = True
687 # Notify all acquires. They'll throw an error.
688 while self.__pending:
689 self.__pending.pop().notifyAll()
693 self.__lock.release()
696 # Whenever we want to acquire a full LockSet we pass None as the value
697 # to acquire. Hide this behind this nicely named constant.
701 class _AcquireTimeout(Exception):
702 """Internal exception to abort an acquire on a timeout.
708 """Implements a set of locks.
710 This abstraction implements a set of shared locks for the same resource type,
711 distinguished by name. The user can lock a subset of the resources and the
712 LockSet will take care of acquiring the locks always in the same order, thus
715 All the locks needed in the same set must be acquired together, though.
718 def __init__(self, members=None):
719 """Constructs a new LockSet.
721 @type members: list of strings
722 @param members: initial members of the set
725 # Used internally to guarantee coherency.
726 self.__lock = SharedLock()
728 # The lockdict indexes the relationship name -> lock
729 # The order-of-locking is implied by the alphabetical order of names
732 if members is not None:
734 self.__lockdict[name] = SharedLock()
736 # The owner dict contains the set of locks each thread owns. For
737 # performance each thread can access its own key without a global lock on
738 # this structure. It is paramount though that *no* other type of access is
739 # done to this structure (eg. no looping over its keys). *_owner helper
740 # function are defined to guarantee access is correct, but in general never
741 # do anything different than __owners[threading.currentThread()], or there
746 """Is the current thread a current level owner?"""
747 return threading.currentThread() in self.__owners
749 def _add_owned(self, name=None):
750 """Note the current thread owns the given lock"""
752 if not self._is_owned():
753 self.__owners[threading.currentThread()] = set()
756 self.__owners[threading.currentThread()].add(name)
758 self.__owners[threading.currentThread()] = set([name])
760 def _del_owned(self, name=None):
761 """Note the current thread owns the given lock"""
763 assert not (name is None and self.__lock._is_owned()), \
764 "Cannot hold internal lock when deleting owner status"
767 self.__owners[threading.currentThread()].remove(name)
769 # Only remove the key if we don't hold the set-lock as well
770 if (not self.__lock._is_owned() and
771 not self.__owners[threading.currentThread()]):
772 del self.__owners[threading.currentThread()]
774 def _list_owned(self):
775 """Get the set of resource names owned by the current thread"""
777 return self.__owners[threading.currentThread()].copy()
781 def _release_and_delete_owned(self):
782 """Release and delete all resources owned by the current thread"""
783 for lname in self._list_owned():
784 lock = self.__lockdict[lname]
787 self._del_owned(name=lname)
790 """Return the current set of names.
792 Only call this function while holding __lock and don't iterate on the
793 result after releasing the lock.
796 return self.__lockdict.keys()
799 """Return a copy of the current set of elements.
801 Used only for debugging purposes.
804 # If we don't already own the set-level lock acquired
805 # we'll get it and note we need to release it later.
807 if not self.__lock._is_owned():
809 self.__lock.acquire(shared=1)
811 result = self.__names()
814 self.__lock.release()
817 def acquire(self, names, timeout=None, shared=0, test_notify=None):
818 """Acquire a set of resource locks.
820 @type names: list of strings (or string)
821 @param names: the names of the locks which shall be acquired
822 (special lock names, or instance/node names)
823 @type shared: integer (0/1) used as a boolean
824 @param shared: whether to acquire in shared mode; by default an
825 exclusive lock will be acquired
826 @type timeout: float or None
827 @param timeout: Maximum time to acquire all locks
828 @type test_notify: callable or None
829 @param test_notify: Special callback function for unittesting
831 @return: Set of all locks successfully acquired or None in case of timeout
833 @raise errors.LockError: when any lock we try to acquire has
834 been deleted before we succeed. In this case none of the
835 locks requested will be acquired.
838 assert timeout is None or timeout >= 0.0
840 # Check we don't already own locks at this level
841 assert not self._is_owned(), "Cannot acquire locks in the same set twice"
843 # We need to keep track of how long we spent waiting for a lock. The
844 # timeout passed to this function is over all lock acquires.
845 running_timeout = RunningTimeout(timeout, False)
848 if names is not None:
849 # Support passing in a single resource to acquire rather than many
850 if isinstance(names, basestring):
853 return self.__acquire_inner(names, False, shared,
854 running_timeout.Remaining, test_notify)
857 # If no names are given acquire the whole set by not letting new names
858 # being added before we release, and getting the current list of names.
859 # Some of them may then be deleted later, but we'll cope with this.
861 # We'd like to acquire this lock in a shared way, as it's nice if
862 # everybody else can use the instances at the same time. If are
863 # acquiring them exclusively though they won't be able to do this
864 # anyway, though, so we'll get the list lock exclusively as well in
865 # order to be able to do add() on the set while owning it.
866 if not self.__lock.acquire(shared=shared,
867 timeout=running_timeout.Remaining()):
868 raise _AcquireTimeout()
870 # note we own the set-lock
873 return self.__acquire_inner(self.__names(), True, shared,
874 running_timeout.Remaining, test_notify)
876 # We shouldn't have problems adding the lock to the owners list, but
877 # if we did we'll try to release this lock and re-raise exception.
878 # Of course something is going to be really wrong, after this.
879 self.__lock.release()
883 except _AcquireTimeout:
886 def __acquire_inner(self, names, want_all, shared, timeout_fn, test_notify):
887 """Inner logic for acquiring a number of locks.
889 @param names: Names of the locks to be acquired
890 @param want_all: Whether all locks in the set should be acquired
891 @param shared: Whether to acquire in shared mode
892 @param timeout_fn: Function returning remaining timeout
893 @param test_notify: Special callback function for unittesting
898 # First we look the locks up on __lockdict. We have no way of being sure
899 # they will still be there after, but this makes it a lot faster should
900 # just one of them be the already wrong. Using a sorted sequence to prevent
902 for lname in sorted(utils.UniqueSequence(names)):
904 lock = self.__lockdict[lname] # raises KeyError if lock is not there
907 # We are acquiring all the set, it doesn't matter if this particular
908 # element is not there anymore.
911 raise errors.LockError("Non-existing lock in set (%s)" % lname)
913 acquire_list.append((lname, lock))
915 # This will hold the locknames we effectively acquired.
919 # Now acquire_list contains a sorted list of resources and locks we
920 # want. In order to get them we loop on this (private) list and
921 # acquire() them. We gave no real guarantee they will still exist till
922 # this is done but .acquire() itself is safe and will alert us if the
924 for (lname, lock) in acquire_list:
925 if __debug__ and callable(test_notify):
926 test_notify_fn = lambda: test_notify(lname)
928 test_notify_fn = None
930 timeout = timeout_fn()
933 # raises LockError if the lock was deleted
934 acq_success = lock.acquire(shared=shared, timeout=timeout,
935 test_notify=test_notify_fn)
936 except errors.LockError:
938 # We are acquiring all the set, it doesn't matter if this
939 # particular element is not there anymore.
942 raise errors.LockError("Non-existing lock in set (%s)" % lname)
945 # Couldn't get lock or timeout occurred
947 # This shouldn't happen as SharedLock.acquire(timeout=None) is
949 raise errors.LockError("Failed to get lock %s" % lname)
951 raise _AcquireTimeout()
954 # now the lock cannot be deleted, we have it!
955 self._add_owned(name=lname)
959 # We shouldn't have problems adding the lock to the owners list, but
960 # if we did we'll try to release this lock and re-raise exception.
961 # Of course something is going to be really wrong after this.
967 # Release all owned locks
968 self._release_and_delete_owned()
973 def release(self, names=None):
974 """Release a set of resource locks, at the same level.
976 You must have acquired the locks, either in shared or in exclusive mode,
977 before releasing them.
979 @type names: list of strings, or None
980 @param names: the names of the locks which shall be released
981 (defaults to all the locks acquired at that level).
984 assert self._is_owned(), "release() on lock set while not owner"
986 # Support passing in a single resource to release rather than many
987 if isinstance(names, basestring):
991 names = self._list_owned()
994 assert self._list_owned().issuperset(names), (
995 "release() on unheld resources %s" %
996 names.difference(self._list_owned()))
998 # First of all let's release the "all elements" lock, if set.
999 # After this 'add' can work again
1000 if self.__lock._is_owned():
1001 self.__lock.release()
1004 for lockname in names:
1005 # If we are sure the lock doesn't leave __lockdict without being
1006 # exclusively held we can do this...
1007 self.__lockdict[lockname].release()
1008 self._del_owned(name=lockname)
1010 def add(self, names, acquired=0, shared=0):
1011 """Add a new set of elements to the set
1013 @type names: list of strings
1014 @param names: names of the new elements to add
1015 @type acquired: integer (0/1) used as a boolean
1016 @param acquired: pre-acquire the new resource?
1017 @type shared: integer (0/1) used as a boolean
1018 @param shared: is the pre-acquisition shared?
1021 # Check we don't already own locks at this level
1022 assert not self._is_owned() or self.__lock._is_owned(shared=0), \
1023 "Cannot add locks if the set is only partially owned, or shared"
1025 # Support passing in a single resource to add rather than many
1026 if isinstance(names, basestring):
1029 # If we don't already own the set-level lock acquired in an exclusive way
1030 # we'll get it and note we need to release it later.
1031 release_lock = False
1032 if not self.__lock._is_owned():
1034 self.__lock.acquire()
1037 invalid_names = set(self.__names()).intersection(names)
1039 # This must be an explicit raise, not an assert, because assert is
1040 # turned off when using optimization, and this can happen because of
1041 # concurrency even if the user doesn't want it.
1042 raise errors.LockError("duplicate add() (%s)" % invalid_names)
1044 for lockname in names:
1048 lock.acquire(shared=shared)
1049 # now the lock cannot be deleted, we have it!
1051 self._add_owned(name=lockname)
1053 # We shouldn't have problems adding the lock to the owners list,
1054 # but if we did we'll try to release this lock and re-raise
1055 # exception. Of course something is going to be really wrong,
1056 # after this. On the other hand the lock hasn't been added to the
1057 # __lockdict yet so no other threads should be pending on it. This
1058 # release is just a safety measure.
1062 self.__lockdict[lockname] = lock
1065 # Only release __lock if we were not holding it previously.
1067 self.__lock.release()
1071 def remove(self, names):
1072 """Remove elements from the lock set.
1074 You can either not hold anything in the lockset or already hold a superset
1075 of the elements you want to delete, exclusively.
1077 @type names: list of strings
1078 @param names: names of the resource to remove.
1080 @return: a list of locks which we removed; the list is always
1081 equal to the names list if we were holding all the locks
1085 # Support passing in a single resource to remove rather than many
1086 if isinstance(names, basestring):
1089 # If we own any subset of this lock it must be a superset of what we want
1090 # to delete. The ownership must also be exclusive, but that will be checked
1091 # by the lock itself.
1092 assert not self._is_owned() or self._list_owned().issuperset(names), (
1093 "remove() on acquired lockset while not owning all elements")
1098 # Calling delete() acquires the lock exclusively if we don't already own
1099 # it, and causes all pending and subsequent lock acquires to fail. It's
1100 # fine to call it out of order because delete() also implies release(),
1101 # and the assertion above guarantees that if we either already hold
1102 # everything we want to delete, or we hold none.
1104 self.__lockdict[lname].delete()
1105 removed.append(lname)
1106 except (KeyError, errors.LockError):
1107 # This cannot happen if we were already holding it, verify:
1108 assert not self._is_owned(), "remove failed while holding lockset"
1110 # If no LockError was raised we are the ones who deleted the lock.
1111 # This means we can safely remove it from lockdict, as any further or
1112 # pending delete() or acquire() will fail (and nobody can have the lock
1113 # since before our call to delete()).
1115 # This is done in an else clause because if the exception was thrown
1116 # it's the job of the one who actually deleted it.
1117 del self.__lockdict[lname]
1118 # And let's remove it from our private list if we owned it.
1119 if self._is_owned():
1120 self._del_owned(name=lname)
1125 # Locking levels, must be acquired in increasing order.
1126 # Current rules are:
1127 # - at level LEVEL_CLUSTER resides the Big Ganeti Lock (BGL) which must be
1128 # acquired before performing any operation, either in shared or in exclusive
1129 # mode. acquiring the BGL in exclusive mode is discouraged and should be
1131 # - at levels LEVEL_NODE and LEVEL_INSTANCE reside node and instance locks.
1132 # If you need more than one node, or more than one instance, acquire them at
1138 LEVELS = [LEVEL_CLUSTER,
1142 # Lock levels which are modifiable
1143 LEVELS_MOD = [LEVEL_NODE, LEVEL_INSTANCE]
1146 LEVEL_CLUSTER: "cluster",
1147 LEVEL_INSTANCE: "instance",
1151 # Constant for the big ganeti lock
1155 class GanetiLockManager:
1156 """The Ganeti Locking Library
1158 The purpose of this small library is to manage locking for ganeti clusters
1159 in a central place, while at the same time doing dynamic checks against
1160 possible deadlocks. It will also make it easier to transition to a different
1161 lock type should we migrate away from python threads.
1166 def __init__(self, nodes=None, instances=None):
1167 """Constructs a new GanetiLockManager object.
1169 There should be only a GanetiLockManager object at any time, so this
1170 function raises an error if this is not the case.
1172 @param nodes: list of node names
1173 @param instances: list of instance names
1176 assert self.__class__._instance is None, \
1177 "double GanetiLockManager instance"
1179 self.__class__._instance = self
1181 # The keyring contains all the locks, at their level and in the correct
1184 LEVEL_CLUSTER: LockSet([BGL]),
1185 LEVEL_NODE: LockSet(nodes),
1186 LEVEL_INSTANCE: LockSet(instances),
1189 def _names(self, level):
1190 """List the lock names at the given level.
1192 This can be used for debugging/testing purposes.
1194 @param level: the level whose list of locks to get
1197 assert level in LEVELS, "Invalid locking level %s" % level
1198 return self.__keyring[level]._names()
1200 def _is_owned(self, level):
1201 """Check whether we are owning locks at the given level
1204 return self.__keyring[level]._is_owned()
1206 is_owned = _is_owned
1208 def _list_owned(self, level):
1209 """Get the set of owned locks at the given level
1212 return self.__keyring[level]._list_owned()
1214 def _upper_owned(self, level):
1215 """Check that we don't own any lock at a level greater than the given one.
1218 # This way of checking only works if LEVELS[i] = i, which we check for in
1220 return compat.any((self._is_owned(l) for l in LEVELS[level + 1:]))
1222 def _BGL_owned(self): # pylint: disable-msg=C0103
1223 """Check if the current thread owns the BGL.
1225 Both an exclusive or a shared acquisition work.
1228 return BGL in self.__keyring[LEVEL_CLUSTER]._list_owned()
1231 def _contains_BGL(level, names): # pylint: disable-msg=C0103
1232 """Check if the level contains the BGL.
1234 Check if acting on the given level and set of names will change
1235 the status of the Big Ganeti Lock.
1238 return level == LEVEL_CLUSTER and (names is None or BGL in names)
1240 def acquire(self, level, names, timeout=None, shared=0):
1241 """Acquire a set of resource locks, at the same level.
1243 @type level: member of locking.LEVELS
1244 @param level: the level at which the locks shall be acquired
1245 @type names: list of strings (or string)
1246 @param names: the names of the locks which shall be acquired
1247 (special lock names, or instance/node names)
1248 @type shared: integer (0/1) used as a boolean
1249 @param shared: whether to acquire in shared mode; by default
1250 an exclusive lock will be acquired
1251 @type timeout: float
1252 @param timeout: Maximum time to acquire all locks
1255 assert level in LEVELS, "Invalid locking level %s" % level
1257 # Check that we are either acquiring the Big Ganeti Lock or we already own
1258 # it. Some "legacy" opcodes need to be sure they are run non-concurrently
1259 # so even if we've migrated we need to at least share the BGL to be
1260 # compatible with them. Of course if we own the BGL exclusively there's no
1261 # point in acquiring any other lock, unless perhaps we are half way through
1262 # the migration of the current opcode.
1263 assert (self._contains_BGL(level, names) or self._BGL_owned()), (
1264 "You must own the Big Ganeti Lock before acquiring any other")
1266 # Check we don't own locks at the same or upper levels.
1267 assert not self._upper_owned(level), ("Cannot acquire locks at a level"
1268 " while owning some at a greater one")
1270 # Acquire the locks in the set.
1271 return self.__keyring[level].acquire(names, shared=shared, timeout=timeout)
1273 def release(self, level, names=None):
1274 """Release a set of resource locks, at the same level.
1276 You must have acquired the locks, either in shared or in exclusive
1277 mode, before releasing them.
1279 @type level: member of locking.LEVELS
1280 @param level: the level at which the locks shall be released
1281 @type names: list of strings, or None
1282 @param names: the names of the locks which shall be released
1283 (defaults to all the locks acquired at that level)
1286 assert level in LEVELS, "Invalid locking level %s" % level
1287 assert (not self._contains_BGL(level, names) or
1288 not self._upper_owned(LEVEL_CLUSTER)), (
1289 "Cannot release the Big Ganeti Lock while holding something"
1290 " at upper levels (%r)" %
1291 (utils.CommaJoin(["%s=%r" % (LEVEL_NAMES[i], self._list_owned(i))
1292 for i in self.__keyring.keys()]), ))
1294 # Release will complain if we don't own the locks already
1295 return self.__keyring[level].release(names)
1297 def add(self, level, names, acquired=0, shared=0):
1298 """Add locks at the specified level.
1300 @type level: member of locking.LEVELS_MOD
1301 @param level: the level at which the locks shall be added
1302 @type names: list of strings
1303 @param names: names of the locks to acquire
1304 @type acquired: integer (0/1) used as a boolean
1305 @param acquired: whether to acquire the newly added locks
1306 @type shared: integer (0/1) used as a boolean
1307 @param shared: whether the acquisition will be shared
1310 assert level in LEVELS_MOD, "Invalid or immutable level %s" % level
1311 assert self._BGL_owned(), ("You must own the BGL before performing other"
1313 assert not self._upper_owned(level), ("Cannot add locks at a level"
1314 " while owning some at a greater one")
1315 return self.__keyring[level].add(names, acquired=acquired, shared=shared)
1317 def remove(self, level, names):
1318 """Remove locks from the specified level.
1320 You must either already own the locks you are trying to remove
1321 exclusively or not own any lock at an upper level.
1323 @type level: member of locking.LEVELS_MOD
1324 @param level: the level at which the locks shall be removed
1325 @type names: list of strings
1326 @param names: the names of the locks which shall be removed
1327 (special lock names, or instance/node names)
1330 assert level in LEVELS_MOD, "Invalid or immutable level %s" % level
1331 assert self._BGL_owned(), ("You must own the BGL before performing other"
1333 # Check we either own the level or don't own anything from here
1334 # up. LockSet.remove() will check the case in which we don't own
1335 # all the needed resources, or we have a shared ownership.
1336 assert self._is_owned(level) or not self._upper_owned(level), (
1337 "Cannot remove locks at a level while not owning it or"
1338 " owning some at a greater one")
1339 return self.__keyring[level].remove(names)