1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """Module implementing the Ganeti locking code."""
22
23
24
25
26
27
28 import os
29 import select
30 import threading
31 import errno
32 import weakref
33 import logging
34 import heapq
35 import itertools
36 import time
37
38 from ganeti import errors
39 from ganeti import utils
40 from ganeti import compat
41 from ganeti import query
42
43
44 _EXCLUSIVE_TEXT = "exclusive"
45 _SHARED_TEXT = "shared"
46 _DELETED_TEXT = "deleted"
47
48 _DEFAULT_PRIORITY = 0
52 """Shared Synchronization decorator.
53
54 Calls the function holding the given lock, either in exclusive or shared
55 mode. It requires the passed lock to be a SharedLock (or support its
56 semantics).
57
58 @type mylock: lockable object or string
59 @param mylock: lock to acquire or class member name of the lock to acquire
60
61 """
62 def wrap(fn):
63 def sync_function(*args, **kwargs):
64 if isinstance(mylock, basestring):
65 assert args, "cannot ssynchronize on non-class method: self not found"
66
67 lock = getattr(args[0], mylock)
68 else:
69 lock = mylock
70 lock.acquire(shared=shared)
71 try:
72 return fn(*args, **kwargs)
73 finally:
74 lock.release()
75 return sync_function
76 return wrap
77
80 """Helper class for SingleNotifyPipeCondition
81
82 """
83 __slots__ = [
84 "_fd",
85 "_poller",
86 ]
87
89 """Constructor for _SingleNotifyPipeConditionWaiter
90
91 @type poller: select.poll
92 @param poller: Poller object
93 @type fd: int
94 @param fd: File descriptor to wait for
95
96 """
97 object.__init__(self)
98 self._poller = poller
99 self._fd = fd
100
102 """Wait for something to happen on the pipe.
103
104 @type timeout: float or None
105 @param timeout: Timeout for waiting (can be None)
106
107 """
108 running_timeout = utils.RunningTimeout(timeout, True)
109
110 while True:
111 remaining_time = running_timeout.Remaining()
112
113 if remaining_time is not None:
114 if remaining_time < 0.0:
115 break
116
117
118 remaining_time *= 1000
119
120 try:
121 result = self._poller.poll(remaining_time)
122 except EnvironmentError, err:
123 if err.errno != errno.EINTR:
124 raise
125 result = None
126
127
128 if result and result[0][0] == self._fd:
129 break
130
133 """Base class containing common code for conditions.
134
135 Some of this code is taken from python's threading module.
136
137 """
138 __slots__ = [
139 "_lock",
140 "acquire",
141 "release",
142 "_is_owned",
143 "_acquire_restore",
144 "_release_save",
145 ]
146
174
176 """Check whether lock is owned by current thread.
177
178 """
179 if self._lock.acquire(0):
180 self._lock.release()
181 return False
182 return True
183
186
189
191 """Raise an exception if the current thread doesn't own the lock.
192
193 """
194 if not self._is_owned():
195 raise RuntimeError("cannot work with un-aquired lock")
196
199 """Condition which can only be notified once.
200
201 This condition class uses pipes and poll, internally, to be able to wait for
202 notification with a timeout, without resorting to polling. It is almost
203 compatible with Python's threading.Condition, with the following differences:
204 - notifyAll can only be called once, and no wait can happen after that
205 - notify is not supported, only notifyAll
206
207 """
208
209 __slots__ = [
210 "_poller",
211 "_read_fd",
212 "_write_fd",
213 "_nwaiters",
214 "_notified",
215 ]
216
217 _waiter_class = _SingleNotifyPipeConditionWaiter
218
220 """Constructor for SingleNotifyPipeCondition
221
222 """
223 _BaseCondition.__init__(self, lock)
224 self._nwaiters = 0
225 self._notified = False
226 self._read_fd = None
227 self._write_fd = None
228 self._poller = None
229
231 """Throws an exception if already notified.
232
233 """
234 if self._notified:
235 raise RuntimeError("cannot use already notified condition")
236
238 """Cleanup open file descriptors, if any.
239
240 """
241 if self._read_fd is not None:
242 os.close(self._read_fd)
243 self._read_fd = None
244
245 if self._write_fd is not None:
246 os.close(self._write_fd)
247 self._write_fd = None
248 self._poller = None
249
250 - def wait(self, timeout):
251 """Wait for a notification.
252
253 @type timeout: float or None
254 @param timeout: Waiting timeout (can be None)
255
256 """
257 self._check_owned()
258 self._check_unnotified()
259
260 self._nwaiters += 1
261 try:
262 if self._poller is None:
263 (self._read_fd, self._write_fd) = os.pipe()
264 self._poller = select.poll()
265 self._poller.register(self._read_fd, select.POLLHUP)
266
267 wait_fn = self._waiter_class(self._poller, self._read_fd)
268 state = self._release_save()
269 try:
270
271 wait_fn(timeout)
272 finally:
273
274 self._acquire_restore(state)
275 finally:
276 self._nwaiters -= 1
277 if self._nwaiters == 0:
278 self._Cleanup()
279
281 """Close the writing side of the pipe to notify all waiters.
282
283 """
284 self._check_owned()
285 self._check_unnotified()
286 self._notified = True
287 if self._write_fd is not None:
288 os.close(self._write_fd)
289 self._write_fd = None
290
293 """Group-only non-polling condition with counters.
294
295 This condition class uses pipes and poll, internally, to be able to wait for
296 notification with a timeout, without resorting to polling. It is almost
297 compatible with Python's threading.Condition, but only supports notifyAll and
298 non-recursive locks. As an additional features it's able to report whether
299 there are any waiting threads.
300
301 """
302 __slots__ = [
303 "_waiters",
304 "_single_condition",
305 ]
306
307 _single_condition_class = SingleNotifyPipeCondition
308
310 """Initializes this class.
311
312 """
313 _BaseCondition.__init__(self, lock)
314 self._waiters = set()
315 self._single_condition = self._single_condition_class(self._lock)
316
317 - def wait(self, timeout):
318 """Wait for a notification.
319
320 @type timeout: float or None
321 @param timeout: Waiting timeout (can be None)
322
323 """
324 self._check_owned()
325
326
327
328 cond = self._single_condition
329
330 self._waiters.add(threading.currentThread())
331 try:
332 cond.wait(timeout)
333 finally:
334 self._check_owned()
335 self._waiters.remove(threading.currentThread())
336
338 """Notify all currently waiting threads.
339
340 """
341 self._check_owned()
342 self._single_condition.notifyAll()
343 self._single_condition = self._single_condition_class(self._lock)
344
346 """Returns a list of all waiting threads.
347
348 """
349 self._check_owned()
350
351 return self._waiters
352
354 """Returns whether there are active waiters.
355
356 """
357 self._check_owned()
358
359 return bool(self._waiters)
360
362 return ("<%s.%s waiters=%s at %#x>" %
363 (self.__class__.__module__, self.__class__.__name__,
364 self._waiters, id(self)))
365
368 __slots__ = [
369 "shared",
370 ]
371
373 """Initializes this class.
374
375 """
376 self.shared = shared
377 PipeCondition.__init__(self, lock)
378
381 """Implements a shared lock.
382
383 Multiple threads can acquire the lock in a shared way by calling
384 C{acquire(shared=1)}. In order to acquire the lock in an exclusive way
385 threads can call C{acquire(shared=0)}.
386
387 Notes on data structures: C{__pending} contains a priority queue (heapq) of
388 all pending acquires: C{[(priority1: prioqueue1), (priority2: prioqueue2),
389 ...]}. Each per-priority queue contains a normal in-order list of conditions
390 to be notified when the lock can be acquired. Shared locks are grouped
391 together by priority and the condition for them is stored in
392 C{__pending_shared} if it already exists. C{__pending_by_prio} keeps
393 references for the per-priority queues indexed by priority for faster access.
394
395 @type name: string
396 @ivar name: the name of the lock
397
398 """
399 __slots__ = [
400 "__weakref__",
401 "__deleted",
402 "__exc",
403 "__lock",
404 "__pending",
405 "__pending_by_prio",
406 "__pending_shared",
407 "__shr",
408 "__time_fn",
409 "name",
410 ]
411
412 __condition_class = _PipeConditionWithMode
413
414 - def __init__(self, name, monitor=None, _time_fn=time.time):
415 """Construct a new SharedLock.
416
417 @param name: the name of the lock
418 @type monitor: L{LockMonitor}
419 @param monitor: Lock monitor with which to register
420
421 """
422 object.__init__(self)
423
424 self.name = name
425
426
427 self.__time_fn = _time_fn
428
429
430 self.__lock = threading.Lock()
431
432
433 self.__pending = []
434 self.__pending_by_prio = {}
435 self.__pending_shared = {}
436
437
438 self.__shr = set()
439 self.__exc = None
440
441
442 self.__deleted = False
443
444
445 if monitor:
446 logging.debug("Adding lock %s to monitor", name)
447 monitor.RegisterLock(self)
448
450 return ("<%s.%s name=%s at %#x>" %
451 (self.__class__.__module__, self.__class__.__name__,
452 self.name, id(self)))
453
455 """Retrieves information for querying locks.
456
457 @type requested: set
458 @param requested: Requested information, see C{query.LQ_*}
459
460 """
461 self.__lock.acquire()
462 try:
463
464
465
466 mode = None
467 owner_names = None
468
469 if query.LQ_MODE in requested:
470 if self.__deleted:
471 mode = _DELETED_TEXT
472 assert not (self.__exc or self.__shr)
473 elif self.__exc:
474 mode = _EXCLUSIVE_TEXT
475 elif self.__shr:
476 mode = _SHARED_TEXT
477
478
479 if query.LQ_OWNER in requested:
480 if self.__exc:
481 owner = [self.__exc]
482 else:
483 owner = self.__shr
484
485 if owner:
486 assert not self.__deleted
487 owner_names = [i.getName() for i in owner]
488
489
490 if query.LQ_PENDING in requested:
491 pending = []
492
493
494 for (_, prioqueue) in sorted(self.__pending):
495 for cond in prioqueue:
496 if cond.shared:
497 pendmode = _SHARED_TEXT
498 else:
499 pendmode = _EXCLUSIVE_TEXT
500
501
502 pending.append((pendmode, [i.getName()
503 for i in cond.get_waiting()]))
504 else:
505 pending = None
506
507 return [(self.name, mode, owner_names, pending)]
508 finally:
509 self.__lock.release()
510
512 """Raises an exception if the lock has been deleted.
513
514 """
515 if self.__deleted:
516 raise errors.LockError("Deleted lock %s" % self.name)
517
519 """Is the current thread sharing the lock at this time?
520
521 """
522 return threading.currentThread() in self.__shr
523
525 """Is the current thread holding the lock exclusively at this time?
526
527 """
528 return threading.currentThread() == self.__exc
529
531 """Is the current thread somehow owning the lock at this time?
532
533 This is a private version of the function, which presumes you're holding
534 the internal lock.
535
536 """
537 if shared < 0:
538 return self.__is_sharer() or self.__is_exclusive()
539 elif shared:
540 return self.__is_sharer()
541 else:
542 return self.__is_exclusive()
543
545 """Is the current thread somehow owning the lock at this time?
546
547 @param shared:
548 - < 0: check for any type of ownership (default)
549 - 0: check for exclusive ownership
550 - > 0: check for shared ownership
551
552 """
553 self.__lock.acquire()
554 try:
555 return self.__is_owned(shared=shared)
556 finally:
557 self.__lock.release()
558
559
560
561 _is_owned = is_owned
562
564 """Returns the number of pending acquires.
565
566 @rtype: int
567
568 """
569 self.__lock.acquire()
570 try:
571 return sum(len(prioqueue) for (_, prioqueue) in self.__pending)
572 finally:
573 self.__lock.release()
574
576 """Checks whether there are any pending acquires.
577
578 @rtype: bool
579
580 """
581 self.__lock.acquire()
582 try:
583
584 (_, prioqueue) = self.__find_first_pending_queue()
585
586 return not (prioqueue or
587 self.__pending or
588 self.__pending_by_prio or
589 self.__pending_shared)
590 finally:
591 self.__lock.release()
592
594 """Actually acquire the lock.
595
596 """
597 if shared:
598 self.__shr.add(threading.currentThread())
599 else:
600 self.__exc = threading.currentThread()
601
603 """Determine whether lock can be acquired.
604
605 """
606 if shared:
607 return self.__exc is None
608 else:
609 return len(self.__shr) == 0 and self.__exc is None
610
612 """Tries to find the topmost queued entry with pending acquires.
613
614 Removes empty entries while going through the list.
615
616 """
617 while self.__pending:
618 (priority, prioqueue) = self.__pending[0]
619
620 if prioqueue:
621 return (priority, prioqueue)
622
623
624 heapq.heappop(self.__pending)
625 del self.__pending_by_prio[priority]
626 assert priority not in self.__pending_shared
627
628 return (None, None)
629
631 """Checks whether the passed condition is on top of the queue.
632
633 The caller must make sure the queue isn't empty.
634
635 """
636 (_, prioqueue) = self.__find_first_pending_queue()
637
638 return cond == prioqueue[0]
639
641 """Acquire a shared lock.
642
643 @param shared: whether to acquire in shared mode; by default an
644 exclusive lock will be acquired
645 @param timeout: maximum waiting time before giving up
646 @type priority: integer
647 @param priority: Priority for acquiring lock
648
649 """
650 self.__check_deleted()
651
652
653 assert not self.__is_owned(), ("double acquire() on a non-recursive lock"
654 " %s" % self.name)
655
656
657 self.__find_first_pending_queue()
658
659
660 if not self.__pending and self.__can_acquire(shared):
661
662 self.__do_acquire(shared)
663 return True
664
665 prioqueue = self.__pending_by_prio.get(priority, None)
666
667 if shared:
668
669 wait_condition = self.__pending_shared.get(priority, None)
670 assert (wait_condition is None or
671 (wait_condition.shared and wait_condition in prioqueue))
672 else:
673 wait_condition = None
674
675 if wait_condition is None:
676 if prioqueue is None:
677 assert priority not in self.__pending_by_prio
678
679 prioqueue = []
680 heapq.heappush(self.__pending, (priority, prioqueue))
681 self.__pending_by_prio[priority] = prioqueue
682
683 wait_condition = self.__condition_class(self.__lock, shared)
684 prioqueue.append(wait_condition)
685
686 if shared:
687
688
689 assert priority not in self.__pending_shared
690 self.__pending_shared[priority] = wait_condition
691
692 wait_start = self.__time_fn()
693 acquired = False
694
695 try:
696
697
698 while True:
699 if self.__is_on_top(wait_condition) and self.__can_acquire(shared):
700 self.__do_acquire(shared)
701 acquired = True
702 break
703
704
705
706 if (timeout is not None and
707 utils.TimeoutExpired(wait_start, timeout, _time_fn=self.__time_fn)):
708 break
709
710
711 wait_condition.wait(timeout)
712 self.__check_deleted()
713 finally:
714
715 if not wait_condition.has_waiting():
716 prioqueue.remove(wait_condition)
717 if wait_condition.shared:
718
719
720 self.__pending_shared.pop(priority, None)
721
722 return acquired
723
724 - def acquire(self, shared=0, timeout=None, priority=None,
725 test_notify=None):
726 """Acquire a shared lock.
727
728 @type shared: integer (0/1) used as a boolean
729 @param shared: whether to acquire in shared mode; by default an
730 exclusive lock will be acquired
731 @type timeout: float
732 @param timeout: maximum waiting time before giving up
733 @type priority: integer
734 @param priority: Priority for acquiring lock
735 @type test_notify: callable or None
736 @param test_notify: Special callback function for unittesting
737
738 """
739 if priority is None:
740 priority = _DEFAULT_PRIORITY
741
742 self.__lock.acquire()
743 try:
744
745 if __debug__ and callable(test_notify):
746 test_notify()
747
748 return self.__acquire_unlocked(shared, timeout, priority)
749 finally:
750 self.__lock.release()
751
753 """Changes the lock mode from exclusive to shared.
754
755 Pending acquires in shared mode on the same priority will go ahead.
756
757 """
758 self.__lock.acquire()
759 try:
760 assert self.__is_owned(), "Lock must be owned"
761
762 if self.__is_exclusive():
763
764 self.__exc = None
765 self.__do_acquire(1)
766
767
768
769
770
771 (priority, prioqueue) = self.__find_first_pending_queue()
772 if prioqueue:
773
774 cond = self.__pending_shared.pop(priority, None)
775 if cond:
776 assert cond.shared
777 assert cond in prioqueue
778
779
780 if len(prioqueue) > 1:
781 prioqueue.remove(cond)
782 prioqueue.insert(0, cond)
783
784
785 cond.notifyAll()
786
787 assert not self.__is_exclusive()
788 assert self.__is_sharer()
789
790 return True
791 finally:
792 self.__lock.release()
793
795 """Release a Shared Lock.
796
797 You must have acquired the lock, either in shared or in exclusive mode,
798 before calling this function.
799
800 """
801 self.__lock.acquire()
802 try:
803 assert self.__is_exclusive() or self.__is_sharer(), \
804 "Cannot release non-owned lock"
805
806
807 if self.__is_exclusive():
808 self.__exc = None
809 notify = True
810 else:
811 self.__shr.remove(threading.currentThread())
812 notify = not self.__shr
813
814
815
816 if notify:
817 self.__notify_topmost()
818 finally:
819 self.__lock.release()
820
822 """Notifies topmost condition in queue of pending acquires.
823
824 """
825 (priority, prioqueue) = self.__find_first_pending_queue()
826 if prioqueue:
827 cond = prioqueue[0]
828 cond.notifyAll()
829 if cond.shared:
830
831
832 self.__pending_shared.pop(priority, None)
833
835 """Exported version of L{__notify_topmost}.
836
837 """
838 self.__lock.acquire()
839 try:
840 return self.__notify_topmost()
841 finally:
842 self.__lock.release()
843
844 - def delete(self, timeout=None, priority=None):
845 """Delete a Shared Lock.
846
847 This operation will declare the lock for removal. First the lock will be
848 acquired in exclusive mode if you don't already own it, then the lock
849 will be put in a state where any future and pending acquire() fail.
850
851 @type timeout: float
852 @param timeout: maximum waiting time before giving up
853 @type priority: integer
854 @param priority: Priority for acquiring lock
855
856 """
857 if priority is None:
858 priority = _DEFAULT_PRIORITY
859
860 self.__lock.acquire()
861 try:
862 assert not self.__is_sharer(), "Cannot delete() a lock while sharing it"
863
864 self.__check_deleted()
865
866
867 acquired = self.__is_exclusive()
868
869 if not acquired:
870 acquired = self.__acquire_unlocked(0, timeout, priority)
871
872 if acquired:
873 assert self.__is_exclusive() and not self.__is_sharer(), \
874 "Lock wasn't acquired in exclusive mode"
875
876 self.__deleted = True
877 self.__exc = None
878
879 assert not (self.__exc or self.__shr), "Found owner during deletion"
880
881
882 for (_, prioqueue) in self.__pending:
883 for cond in prioqueue:
884 cond.notifyAll()
885
886 assert self.__deleted
887
888 return acquired
889 finally:
890 self.__lock.release()
891
896
899
900
901
902
903 ALL_SET = None
907 """Internal exception to abort an acquire on a timeout.
908
909 """
910
913 """Implements a set of locks.
914
915 This abstraction implements a set of shared locks for the same resource type,
916 distinguished by name. The user can lock a subset of the resources and the
917 LockSet will take care of acquiring the locks always in the same order, thus
918 preventing deadlock.
919
920 All the locks needed in the same set must be acquired together, though.
921
922 @type name: string
923 @ivar name: the name of the lockset
924
925 """
926 - def __init__(self, members, name, monitor=None):
927 """Constructs a new LockSet.
928
929 @type members: list of strings
930 @param members: initial members of the set
931 @type monitor: L{LockMonitor}
932 @param monitor: Lock monitor with which to register member locks
933
934 """
935 assert members is not None, "members parameter is not a list"
936 self.name = name
937
938
939 self.__monitor = monitor
940
941
942 self.__lock = SharedLock(self._GetLockName("[lockset]"), monitor=monitor)
943
944
945
946 self.__lockdict = {}
947
948 for mname in members:
949 self.__lockdict[mname] = SharedLock(self._GetLockName(mname),
950 monitor=monitor)
951
952
953
954
955
956
957
958
959 self.__owners = {}
960
962 """Returns the name for a member lock.
963
964 """
965 return "%s/%s" % (self.name, mname)
966
968 """Returns the lockset-internal lock.
969
970 """
971 return self.__lock
972
974 """Returns the lockset-internal lock dictionary.
975
976 Accessing this structure is only safe in single-thread usage or when the
977 lockset-internal lock is held.
978
979 """
980 return self.__lockdict
981
983 """Is the current thread a current level owner?
984
985 @note: Use L{check_owned} to check if a specific lock is held
986
987 """
988 return threading.currentThread() in self.__owners
989
991 """Check if locks are owned in a specific mode.
992
993 @type names: sequence or string
994 @param names: Lock names (or a single lock name)
995 @param shared: See L{SharedLock.is_owned}
996 @rtype: bool
997 @note: Use L{is_owned} to check if the current thread holds I{any} lock and
998 L{list_owned} to get the names of all owned locks
999
1000 """
1001 if isinstance(names, basestring):
1002 names = [names]
1003
1004
1005 if names and self.is_owned():
1006 candidates = []
1007
1008
1009 for lname in names:
1010 try:
1011 lock = self.__lockdict[lname]
1012 except KeyError:
1013 raise errors.LockError("Non-existing lock '%s' in set '%s' (it may"
1014 " have been removed)" % (lname, self.name))
1015 else:
1016 candidates.append(lock)
1017
1018 return compat.all(lock.is_owned(shared=shared) for lock in candidates)
1019 else:
1020 return False
1021
1023 """Note the current thread owns the given lock"""
1024 if name is None:
1025 if not self.is_owned():
1026 self.__owners[threading.currentThread()] = set()
1027 else:
1028 if self.is_owned():
1029 self.__owners[threading.currentThread()].add(name)
1030 else:
1031 self.__owners[threading.currentThread()] = set([name])
1032
1034 """Note the current thread owns the given lock"""
1035
1036 assert not (name is None and self.__lock.is_owned()), \
1037 "Cannot hold internal lock when deleting owner status"
1038
1039 if name is not None:
1040 self.__owners[threading.currentThread()].remove(name)
1041
1042
1043 if (not self.__lock.is_owned() and
1044 not self.__owners[threading.currentThread()]):
1045 del self.__owners[threading.currentThread()]
1046
1048 """Get the set of resource names owned by the current thread"""
1049 if self.is_owned():
1050 return self.__owners[threading.currentThread()].copy()
1051 else:
1052 return set()
1053
1055 """Release and delete all resources owned by the current thread"""
1056 for lname in self.list_owned():
1057 lock = self.__lockdict[lname]
1058 if lock.is_owned():
1059 lock.release()
1060 self._del_owned(name=lname)
1061
1063 """Return the current set of names.
1064
1065 Only call this function while holding __lock and don't iterate on the
1066 result after releasing the lock.
1067
1068 """
1069 return self.__lockdict.keys()
1070
1072 """Return a copy of the current set of elements.
1073
1074 Used only for debugging purposes.
1075
1076 """
1077
1078
1079 release_lock = False
1080 if not self.__lock.is_owned():
1081 release_lock = True
1082 self.__lock.acquire(shared=1)
1083 try:
1084 result = self.__names()
1085 finally:
1086 if release_lock:
1087 self.__lock.release()
1088 return set(result)
1089
1090 - def acquire(self, names, timeout=None, shared=0, priority=None,
1091 test_notify=None):
1092 """Acquire a set of resource locks.
1093
1094 @type names: list of strings (or string)
1095 @param names: the names of the locks which shall be acquired
1096 (special lock names, or instance/node names)
1097 @type shared: integer (0/1) used as a boolean
1098 @param shared: whether to acquire in shared mode; by default an
1099 exclusive lock will be acquired
1100 @type timeout: float or None
1101 @param timeout: Maximum time to acquire all locks
1102 @type priority: integer
1103 @param priority: Priority for acquiring locks
1104 @type test_notify: callable or None
1105 @param test_notify: Special callback function for unittesting
1106
1107 @return: Set of all locks successfully acquired or None in case of timeout
1108
1109 @raise errors.LockError: when any lock we try to acquire has
1110 been deleted before we succeed. In this case none of the
1111 locks requested will be acquired.
1112
1113 """
1114 assert timeout is None or timeout >= 0.0
1115
1116
1117 assert not self.is_owned(), ("Cannot acquire locks in the same set twice"
1118 " (lockset %s)" % self.name)
1119
1120 if priority is None:
1121 priority = _DEFAULT_PRIORITY
1122
1123
1124
1125 running_timeout = utils.RunningTimeout(timeout, False)
1126
1127 try:
1128 if names is not None:
1129
1130 if isinstance(names, basestring):
1131 names = [names]
1132
1133 return self.__acquire_inner(names, False, shared, priority,
1134 running_timeout.Remaining, test_notify)
1135
1136 else:
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146 if not self.__lock.acquire(shared=shared, priority=priority,
1147 timeout=running_timeout.Remaining()):
1148 raise _AcquireTimeout()
1149 try:
1150
1151 self._add_owned()
1152
1153 return self.__acquire_inner(self.__names(), True, shared, priority,
1154 running_timeout.Remaining, test_notify)
1155 except:
1156
1157
1158
1159 self.__lock.release()
1160 self._del_owned()
1161 raise
1162
1163 except _AcquireTimeout:
1164 return None
1165
1166 - def __acquire_inner(self, names, want_all, shared, priority,
1167 timeout_fn, test_notify):
1168 """Inner logic for acquiring a number of locks.
1169
1170 @param names: Names of the locks to be acquired
1171 @param want_all: Whether all locks in the set should be acquired
1172 @param shared: Whether to acquire in shared mode
1173 @param timeout_fn: Function returning remaining timeout
1174 @param priority: Priority for acquiring locks
1175 @param test_notify: Special callback function for unittesting
1176
1177 """
1178 acquire_list = []
1179
1180
1181
1182
1183
1184 for lname in sorted(utils.UniqueSequence(names)):
1185 try:
1186 lock = self.__lockdict[lname]
1187 except KeyError:
1188 if want_all:
1189
1190
1191 continue
1192
1193 raise errors.LockError("Non-existing lock %s in set %s (it may have"
1194 " been removed)" % (lname, self.name))
1195
1196 acquire_list.append((lname, lock))
1197
1198
1199 acquired = set()
1200
1201 try:
1202
1203
1204
1205
1206
1207 for (lname, lock) in acquire_list:
1208 if __debug__ and callable(test_notify):
1209 test_notify_fn = lambda: test_notify(lname)
1210 else:
1211 test_notify_fn = None
1212
1213 timeout = timeout_fn()
1214
1215 try:
1216
1217 acq_success = lock.acquire(shared=shared, timeout=timeout,
1218 priority=priority,
1219 test_notify=test_notify_fn)
1220 except errors.LockError:
1221 if want_all:
1222
1223
1224 continue
1225
1226 raise errors.LockError("Non-existing lock %s in set %s (it may"
1227 " have been removed)" % (lname, self.name))
1228
1229 if not acq_success:
1230
1231 if timeout is None:
1232
1233
1234 raise errors.LockError("Failed to get lock %s (set %s)" %
1235 (lname, self.name))
1236
1237 raise _AcquireTimeout()
1238
1239 try:
1240
1241 self._add_owned(name=lname)
1242 acquired.add(lname)
1243
1244 except:
1245
1246
1247
1248 if lock.is_owned():
1249 lock.release()
1250 raise
1251
1252 except:
1253
1254 self._release_and_delete_owned()
1255 raise
1256
1257 return acquired
1258
1260 """Downgrade a set of resource locks from exclusive to shared mode.
1261
1262 The locks must have been acquired in exclusive mode.
1263
1264 """
1265 assert self.is_owned(), ("downgrade on lockset %s while not owning any"
1266 " lock" % self.name)
1267
1268
1269 if isinstance(names, basestring):
1270 names = [names]
1271
1272 owned = self.list_owned()
1273
1274 if names is None:
1275 names = owned
1276 else:
1277 names = set(names)
1278 assert owned.issuperset(names), \
1279 ("downgrade() on unheld resources %s (set %s)" %
1280 (names.difference(owned), self.name))
1281
1282 for lockname in names:
1283 self.__lockdict[lockname].downgrade()
1284
1285
1286 if self.__lock.is_owned(shared=0):
1287
1288 if not compat.any(lock.is_owned(shared=0)
1289 for lock in self.__lockdict.values()):
1290 self.__lock.downgrade()
1291 assert self.__lock.is_owned(shared=1)
1292
1293 return True
1294
1296 """Release a set of resource locks, at the same level.
1297
1298 You must have acquired the locks, either in shared or in exclusive mode,
1299 before releasing them.
1300
1301 @type names: list of strings, or None
1302 @param names: the names of the locks which shall be released
1303 (defaults to all the locks acquired at that level).
1304
1305 """
1306 assert self.is_owned(), ("release() on lock set %s while not owner" %
1307 self.name)
1308
1309
1310 if isinstance(names, basestring):
1311 names = [names]
1312
1313 if names is None:
1314 names = self.list_owned()
1315 else:
1316 names = set(names)
1317 assert self.list_owned().issuperset(names), (
1318 "release() on unheld resources %s (set %s)" %
1319 (names.difference(self.list_owned()), self.name))
1320
1321
1322
1323 if self.__lock.is_owned():
1324 self.__lock.release()
1325 self._del_owned()
1326
1327 for lockname in names:
1328
1329
1330 self.__lockdict[lockname].release()
1331 self._del_owned(name=lockname)
1332
1333 - def add(self, names, acquired=0, shared=0):
1334 """Add a new set of elements to the set
1335
1336 @type names: list of strings
1337 @param names: names of the new elements to add
1338 @type acquired: integer (0/1) used as a boolean
1339 @param acquired: pre-acquire the new resource?
1340 @type shared: integer (0/1) used as a boolean
1341 @param shared: is the pre-acquisition shared?
1342
1343 """
1344
1345 assert not self.is_owned() or self.__lock.is_owned(shared=0), \
1346 ("Cannot add locks if the set %s is only partially owned, or shared" %
1347 self.name)
1348
1349
1350 if isinstance(names, basestring):
1351 names = [names]
1352
1353
1354
1355 release_lock = False
1356 if not self.__lock.is_owned():
1357 release_lock = True
1358 self.__lock.acquire()
1359
1360 try:
1361 invalid_names = set(self.__names()).intersection(names)
1362 if invalid_names:
1363
1364
1365
1366 raise errors.LockError("duplicate add(%s) on lockset %s" %
1367 (invalid_names, self.name))
1368
1369 for lockname in names:
1370 lock = SharedLock(self._GetLockName(lockname), monitor=self.__monitor)
1371
1372 if acquired:
1373
1374
1375 lock.acquire(shared=shared)
1376
1377 try:
1378 self._add_owned(name=lockname)
1379 except:
1380
1381
1382
1383
1384
1385
1386 lock.release()
1387 raise
1388
1389 self.__lockdict[lockname] = lock
1390
1391 finally:
1392
1393 if release_lock:
1394 self.__lock.release()
1395
1396 return True
1397
1399 """Remove elements from the lock set.
1400
1401 You can either not hold anything in the lockset or already hold a superset
1402 of the elements you want to delete, exclusively.
1403
1404 @type names: list of strings
1405 @param names: names of the resource to remove.
1406
1407 @return: a list of locks which we removed; the list is always
1408 equal to the names list if we were holding all the locks
1409 exclusively
1410
1411 """
1412
1413 if isinstance(names, basestring):
1414 names = [names]
1415
1416
1417
1418
1419 assert not self.is_owned() or self.list_owned().issuperset(names), (
1420 "remove() on acquired lockset %s while not owning all elements" %
1421 self.name)
1422
1423 removed = []
1424
1425 for lname in names:
1426
1427
1428
1429
1430
1431 try:
1432 self.__lockdict[lname].delete()
1433 removed.append(lname)
1434 except (KeyError, errors.LockError):
1435
1436 assert not self.is_owned(), ("remove failed while holding lockset %s" %
1437 self.name)
1438 else:
1439
1440
1441
1442
1443
1444
1445
1446 del self.__lockdict[lname]
1447
1448 if self.is_owned():
1449 self._del_owned(name=lname)
1450
1451 return removed
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463 LEVEL_CLUSTER = 0
1464 LEVEL_INSTANCE = 1
1465 LEVEL_NODEGROUP = 2
1466 LEVEL_NODE = 3
1467 LEVEL_NODE_RES = 4
1468
1469 LEVELS = [
1470 LEVEL_CLUSTER,
1471 LEVEL_INSTANCE,
1472 LEVEL_NODEGROUP,
1473 LEVEL_NODE,
1474 LEVEL_NODE_RES,
1475 ]
1476
1477
1478 LEVELS_MOD = frozenset([
1479 LEVEL_NODE_RES,
1480 LEVEL_NODE,
1481 LEVEL_NODEGROUP,
1482 LEVEL_INSTANCE,
1483 ])
1484
1485
1486 LEVEL_NAMES = {
1487 LEVEL_CLUSTER: "cluster",
1488 LEVEL_INSTANCE: "instance",
1489 LEVEL_NODEGROUP: "nodegroup",
1490 LEVEL_NODE: "node",
1491 LEVEL_NODE_RES: "node-res",
1492 }
1493
1494
1495 BGL = "BGL"
1499 """The Ganeti Locking Library
1500
1501 The purpose of this small library is to manage locking for ganeti clusters
1502 in a central place, while at the same time doing dynamic checks against
1503 possible deadlocks. It will also make it easier to transition to a different
1504 lock type should we migrate away from python threads.
1505
1506 """
1507 _instance = None
1508
1509 - def __init__(self, nodes, nodegroups, instances):
1510 """Constructs a new GanetiLockManager object.
1511
1512 There should be only a GanetiLockManager object at any time, so this
1513 function raises an error if this is not the case.
1514
1515 @param nodes: list of node names
1516 @param nodegroups: list of nodegroup uuids
1517 @param instances: list of instance names
1518
1519 """
1520 assert self.__class__._instance is None, \
1521 "double GanetiLockManager instance"
1522
1523 self.__class__._instance = self
1524
1525 self._monitor = LockMonitor()
1526
1527
1528
1529 self.__keyring = {
1530 LEVEL_CLUSTER: LockSet([BGL], "cluster", monitor=self._monitor),
1531 LEVEL_NODE: LockSet(nodes, "node", monitor=self._monitor),
1532 LEVEL_NODE_RES: LockSet(nodes, "node-res", monitor=self._monitor),
1533 LEVEL_NODEGROUP: LockSet(nodegroups, "nodegroup", monitor=self._monitor),
1534 LEVEL_INSTANCE: LockSet(instances, "instance",
1535 monitor=self._monitor),
1536 }
1537
1538 assert compat.all(ls.name == LEVEL_NAMES[level]
1539 for (level, ls) in self.__keyring.items())
1540
1542 """Registers a new lock with the monitor.
1543
1544 See L{LockMonitor.RegisterLock}.
1545
1546 """
1547 return self._monitor.RegisterLock(provider)
1548
1550 """Queries information from all locks.
1551
1552 See L{LockMonitor.QueryLocks}.
1553
1554 """
1555 return self._monitor.QueryLocks(fields)
1556
1558 """List the lock names at the given level.
1559
1560 This can be used for debugging/testing purposes.
1561
1562 @param level: the level whose list of locks to get
1563
1564 """
1565 assert level in LEVELS, "Invalid locking level %s" % level
1566 return self.__keyring[level]._names()
1567
1569 """Check whether we are owning locks at the given level
1570
1571 """
1572 return self.__keyring[level].is_owned()
1573
1575 """Get the set of owned locks at the given level
1576
1577 """
1578 return self.__keyring[level].list_owned()
1579
1581 """Check if locks at a certain level are owned in a specific mode.
1582
1583 @see: L{LockSet.check_owned}
1584
1585 """
1586 return self.__keyring[level].check_owned(names, shared=shared)
1587
1589 """Check that we don't own any lock at a level greater than the given one.
1590
1591 """
1592
1593
1594 return compat.any((self.is_owned(l) for l in LEVELS[level + 1:]))
1595
1597 """Check if the current thread owns the BGL.
1598
1599 Both an exclusive or a shared acquisition work.
1600
1601 """
1602 return BGL in self.__keyring[LEVEL_CLUSTER].list_owned()
1603
1604 @staticmethod
1606 """Check if the level contains the BGL.
1607
1608 Check if acting on the given level and set of names will change
1609 the status of the Big Ganeti Lock.
1610
1611 """
1612 return level == LEVEL_CLUSTER and (names is None or BGL in names)
1613
1614 - def acquire(self, level, names, timeout=None, shared=0, priority=None):
1615 """Acquire a set of resource locks, at the same level.
1616
1617 @type level: member of locking.LEVELS
1618 @param level: the level at which the locks shall be acquired
1619 @type names: list of strings (or string)
1620 @param names: the names of the locks which shall be acquired
1621 (special lock names, or instance/node names)
1622 @type shared: integer (0/1) used as a boolean
1623 @param shared: whether to acquire in shared mode; by default
1624 an exclusive lock will be acquired
1625 @type timeout: float
1626 @param timeout: Maximum time to acquire all locks
1627 @type priority: integer
1628 @param priority: Priority for acquiring lock
1629
1630 """
1631 assert level in LEVELS, "Invalid locking level %s" % level
1632
1633
1634
1635
1636
1637
1638
1639 assert (self._contains_BGL(level, names) or self._BGL_owned()), (
1640 "You must own the Big Ganeti Lock before acquiring any other")
1641
1642
1643 assert not self._upper_owned(level), ("Cannot acquire locks at a level"
1644 " while owning some at a greater one")
1645
1646
1647 return self.__keyring[level].acquire(names, shared=shared, timeout=timeout,
1648 priority=priority)
1649
1651 """Downgrade a set of resource locks from exclusive to shared mode.
1652
1653 You must have acquired the locks in exclusive mode.
1654
1655 @type level: member of locking.LEVELS
1656 @param level: the level at which the locks shall be downgraded
1657 @type names: list of strings, or None
1658 @param names: the names of the locks which shall be downgraded
1659 (defaults to all the locks acquired at the level)
1660
1661 """
1662 assert level in LEVELS, "Invalid locking level %s" % level
1663
1664 return self.__keyring[level].downgrade(names=names)
1665
1666 - def release(self, level, names=None):
1667 """Release a set of resource locks, at the same level.
1668
1669 You must have acquired the locks, either in shared or in exclusive
1670 mode, before releasing them.
1671
1672 @type level: member of locking.LEVELS
1673 @param level: the level at which the locks shall be released
1674 @type names: list of strings, or None
1675 @param names: the names of the locks which shall be released
1676 (defaults to all the locks acquired at that level)
1677
1678 """
1679 assert level in LEVELS, "Invalid locking level %s" % level
1680 assert (not self._contains_BGL(level, names) or
1681 not self._upper_owned(LEVEL_CLUSTER)), (
1682 "Cannot release the Big Ganeti Lock while holding something"
1683 " at upper levels (%r)" %
1684 (utils.CommaJoin(["%s=%r" % (LEVEL_NAMES[i], self.list_owned(i))
1685 for i in self.__keyring.keys()]), ))
1686
1687
1688 return self.__keyring[level].release(names)
1689
1690 - def add(self, level, names, acquired=0, shared=0):
1691 """Add locks at the specified level.
1692
1693 @type level: member of locking.LEVELS_MOD
1694 @param level: the level at which the locks shall be added
1695 @type names: list of strings
1696 @param names: names of the locks to acquire
1697 @type acquired: integer (0/1) used as a boolean
1698 @param acquired: whether to acquire the newly added locks
1699 @type shared: integer (0/1) used as a boolean
1700 @param shared: whether the acquisition will be shared
1701
1702 """
1703 assert level in LEVELS_MOD, "Invalid or immutable level %s" % level
1704 assert self._BGL_owned(), ("You must own the BGL before performing other"
1705 " operations")
1706 assert not self._upper_owned(level), ("Cannot add locks at a level"
1707 " while owning some at a greater one")
1708 return self.__keyring[level].add(names, acquired=acquired, shared=shared)
1709
1710 - def remove(self, level, names):
1711 """Remove locks from the specified level.
1712
1713 You must either already own the locks you are trying to remove
1714 exclusively or not own any lock at an upper level.
1715
1716 @type level: member of locking.LEVELS_MOD
1717 @param level: the level at which the locks shall be removed
1718 @type names: list of strings
1719 @param names: the names of the locks which shall be removed
1720 (special lock names, or instance/node names)
1721
1722 """
1723 assert level in LEVELS_MOD, "Invalid or immutable level %s" % level
1724 assert self._BGL_owned(), ("You must own the BGL before performing other"
1725 " operations")
1726
1727
1728
1729 assert self.is_owned(level) or not self._upper_owned(level), (
1730 "Cannot remove locks at a level while not owning it or"
1731 " owning some at a greater one")
1732 return self.__keyring[level].remove(names)
1733
1736 """Sorting key function.
1737
1738 Sort by name, registration order and then order of information. This provides
1739 a stable sort order over different providers, even if they return the same
1740 name.
1741
1742 """
1743 (name, _, _, _) = item
1744
1745 return (utils.NiceSortKey(name), num, idx)
1746
1749 _LOCK_ATTR = "_lock"
1750
1752 """Initializes this class.
1753
1754 """
1755 self._lock = SharedLock("LockMonitor")
1756
1757
1758 self._counter = itertools.count(0)
1759
1760
1761
1762 self._locks = weakref.WeakKeyDictionary()
1763
1764 @ssynchronized(_LOCK_ATTR)
1766 """Registers a new lock.
1767
1768 @param provider: Object with a callable method named C{GetLockInfo}, taking
1769 a single C{set} containing the requested information items
1770 @note: It would be nicer to only receive the function generating the
1771 requested information but, as it turns out, weak references to bound
1772 methods (e.g. C{self.GetLockInfo}) are tricky; there are several
1773 workarounds, but none of the ones I found works properly in combination
1774 with a standard C{WeakKeyDictionary}
1775
1776 """
1777 assert provider not in self._locks, "Duplicate registration"
1778
1779
1780
1781
1782
1783
1784
1785 self._locks[provider] = self._counter.next()
1786
1788 """Get information from all locks.
1789
1790 """
1791
1792 self._lock.acquire(shared=1)
1793 try:
1794 items = self._locks.items()
1795 finally:
1796 self._lock.release()
1797
1798 return [(info, idx, num)
1799 for (provider, num) in items
1800 for (idx, info) in enumerate(provider.GetLockInfo(requested))]
1801
1818
1820 """Queries information from all locks.
1821
1822 @type fields: list of strings
1823 @param fields: List of fields to return
1824
1825 """
1826 (qobj, ctx) = self._Query(fields)
1827
1828
1829 return query.GetQueryResponse(qobj, ctx)
1830