Statistics
| Branch: | Tag: | Revision:

root / lib / locking.py @ 84e344d4

History | View | Annotate | Download (30.1 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007 Google Inc.
5
#
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.
10
#
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.
15
#
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
19
# 02110-1301, USA.
20

    
21
"""Module implementing the Ganeti locking code."""
22

    
23
import threading
24

    
25
from ganeti import errors
26
from ganeti import utils
27

    
28

    
29
def ssynchronized(lock, shared=0):
30
  """Shared Synchronization decorator.
31

32
  Calls the function holding the given lock, either in exclusive or shared
33
  mode. It requires the passed lock to be a SharedLock (or support its
34
  semantics).
35

36
  """
37
  def wrap(fn):
38
    def sync_function(*args, **kwargs):
39
      lock.acquire(shared=shared)
40
      try:
41
        return fn(*args, **kwargs)
42
      finally:
43
        lock.release()
44
    return sync_function
45
  return wrap
46

    
47

    
48
class _CountingCondition(object):
49
  """Wrapper for Python's built-in threading.Condition class.
50

51
  This wrapper keeps a count of active waiters. We can't access the internal
52
  "__waiters" attribute of threading.Condition because it's not thread-safe.
53

54
  """
55
  __slots__ = [
56
    "_cond",
57
    "_nwaiters",
58
    ]
59

    
60
  def __init__(self, lock):
61
    """Initializes this class.
62

63
    """
64
    object.__init__(self)
65
    self._cond = threading.Condition(lock=lock)
66
    self._nwaiters = 0
67

    
68
  def notifyAll(self):
69
    """Notifies the condition.
70

71
    """
72
    return self._cond.notifyAll()
73

    
74
  def wait(self, timeout=None):
75
    """Waits for the condition to be notified.
76

77
    @type timeout: float or None
78
    @param timeout: Timeout in seconds
79

80
    """
81
    assert self._nwaiters >= 0
82

    
83
    self._nwaiters += 1
84
    try:
85
      return self._cond.wait(timeout=timeout)
86
    finally:
87
      self._nwaiters -= 1
88

    
89
  def has_waiting(self):
90
    """Returns whether there are active waiters.
91

92
    """
93
    return bool(self._nwaiters)
94

    
95

    
96
class SharedLock(object):
97
  """Implements a shared lock.
98

99
  Multiple threads can acquire the lock in a shared way, calling
100
  acquire_shared().  In order to acquire the lock in an exclusive way threads
101
  can call acquire_exclusive().
102

103
  The lock prevents starvation but does not guarantee that threads will acquire
104
  the shared lock in the order they queued for it, just that they will
105
  eventually do so.
106

107
  """
108
  __slots__ = [
109
    "__active_shr_c",
110
    "__inactive_shr_c",
111
    "__deleted",
112
    "__exc",
113
    "__lock",
114
    "__pending",
115
    "__shr",
116
    ]
117

    
118
  __condition_class = _CountingCondition
119

    
120
  def __init__(self):
121
    """Construct a new SharedLock.
122

123
    """
124
    object.__init__(self)
125

    
126
    # Internal lock
127
    self.__lock = threading.Lock()
128

    
129
    # Queue containing waiting acquires
130
    self.__pending = []
131

    
132
    # Active and inactive conditions for shared locks
133
    self.__active_shr_c = self.__condition_class(self.__lock)
134
    self.__inactive_shr_c = self.__condition_class(self.__lock)
135

    
136
    # Current lock holders
137
    self.__shr = set()
138
    self.__exc = None
139

    
140
    # is this lock in the deleted state?
141
    self.__deleted = False
142

    
143
  def __check_deleted(self):
144
    """Raises an exception if the lock has been deleted.
145

146
    """
147
    if self.__deleted:
148
      raise errors.LockError("Deleted lock")
149

    
150
  def __is_sharer(self):
151
    """Is the current thread sharing the lock at this time?
152

153
    """
154
    return threading.currentThread() in self.__shr
155

    
156
  def __is_exclusive(self):
157
    """Is the current thread holding the lock exclusively at this time?
158

159
    """
160
    return threading.currentThread() == self.__exc
161

    
162
  def __is_owned(self, shared=-1):
163
    """Is the current thread somehow owning the lock at this time?
164

165
    This is a private version of the function, which presumes you're holding
166
    the internal lock.
167

168
    """
169
    if shared < 0:
170
      return self.__is_sharer() or self.__is_exclusive()
171
    elif shared:
172
      return self.__is_sharer()
173
    else:
174
      return self.__is_exclusive()
175

    
176
  def _is_owned(self, shared=-1):
177
    """Is the current thread somehow owning the lock at this time?
178

179
    @param shared:
180
        - < 0: check for any type of ownership (default)
181
        - 0: check for exclusive ownership
182
        - > 0: check for shared ownership
183

184
    """
185
    self.__lock.acquire()
186
    try:
187
      return self.__is_owned(shared=shared)
188
    finally:
189
      self.__lock.release()
190

    
191
  def _count_pending(self):
192
    """Returns the number of pending acquires.
193

194
    @rtype: int
195

196
    """
197
    self.__lock.acquire()
198
    try:
199
      return len(self.__pending)
200
    finally:
201
      self.__lock.release()
202

    
203
  def __do_acquire(self, shared):
204
    """Actually acquire the lock.
205

206
    """
207
    if shared:
208
      self.__shr.add(threading.currentThread())
209
    else:
210
      self.__exc = threading.currentThread()
211

    
212
  def __can_acquire(self, shared):
213
    """Determine whether lock can be acquired.
214

215
    """
216
    if shared:
217
      return self.__exc is None
218
    else:
219
      return len(self.__shr) == 0 and self.__exc is None
220

    
221
  def __is_on_top(self, cond):
222
    """Checks whether the passed condition is on top of the queue.
223

224
    The caller must make sure the queue isn't empty.
225

226
    """
227
    return self.__pending[0] == cond
228

    
229
  def __acquire_unlocked(self, shared=0, timeout=None):
230
    """Acquire a shared lock.
231

232
    @param shared: whether to acquire in shared mode; by default an
233
        exclusive lock will be acquired
234
    @param timeout: maximum waiting time before giving up
235

236
    """
237
    self.__check_deleted()
238

    
239
    # We cannot acquire the lock if we already have it
240
    assert not self.__is_owned(), "double acquire() on a non-recursive lock"
241

    
242
    # Check whether someone else holds the lock or there are pending acquires.
243
    if not self.__pending and self.__can_acquire(shared):
244
      # Apparently not, can acquire lock directly.
245
      self.__do_acquire(shared)
246
      return True
247

    
248
    if shared:
249
      wait_condition = self.__active_shr_c
250

    
251
      # Check if we're not yet in the queue
252
      if wait_condition not in self.__pending:
253
        self.__pending.append(wait_condition)
254
    else:
255
      wait_condition = self.__condition_class(self.__lock)
256
      # Always add to queue
257
      self.__pending.append(wait_condition)
258

    
259
    try:
260
      # Wait until we become the topmost acquire in the queue or the timeout
261
      # expires.
262
      while not (self.__is_on_top(wait_condition) and
263
                 self.__can_acquire(shared)):
264
        # Wait for notification
265
        wait_condition.wait(timeout)
266
        self.__check_deleted()
267

    
268
        # A lot of code assumes blocking acquires always succeed. Loop
269
        # internally for that case.
270
        if timeout is not None:
271
          break
272

    
273
      if self.__is_on_top(wait_condition) and self.__can_acquire(shared):
274
        self.__do_acquire(shared)
275
        return True
276
    finally:
277
      # Remove condition from queue if there are no more waiters
278
      if not wait_condition.has_waiting() and not self.__deleted:
279
        self.__pending.remove(wait_condition)
280

    
281
    return False
282

    
283
  def acquire(self, shared=0, timeout=None):
284
    """Acquire a shared lock.
285

286
    @type shared: int
287
    @param shared: whether to acquire in shared mode; by default an
288
        exclusive lock will be acquired
289
    @type timeout: float
290
    @param timeout: maximum waiting time before giving up
291

292
    """
293
    self.__lock.acquire()
294
    try:
295
      return self.__acquire_unlocked(shared, timeout)
296
    finally:
297
      self.__lock.release()
298

    
299
  def release(self):
300
    """Release a Shared Lock.
301

302
    You must have acquired the lock, either in shared or in exclusive mode,
303
    before calling this function.
304

305
    """
306
    self.__lock.acquire()
307
    try:
308
      assert self.__is_exclusive() or self.__is_sharer(), \
309
        "Cannot release non-owned lock"
310

    
311
      # Autodetect release type
312
      if self.__is_exclusive():
313
        self.__exc = None
314
      else:
315
        self.__shr.remove(threading.currentThread())
316

    
317
      # Notify topmost condition in queue
318
      if self.__pending:
319
        first_condition = self.__pending[0]
320
        first_condition.notifyAll()
321

    
322
        if first_condition == self.__active_shr_c:
323
          self.__active_shr_c = self.__inactive_shr_c
324
          self.__inactive_shr_c = first_condition
325

    
326
    finally:
327
      self.__lock.release()
328

    
329
  def delete(self, timeout=None):
330
    """Delete a Shared Lock.
331

332
    This operation will declare the lock for removal. First the lock will be
333
    acquired in exclusive mode if you don't already own it, then the lock
334
    will be put in a state where any future and pending acquire() fail.
335

336
    @type timeout: float
337
    @param timeout: maximum waiting time before giving up
338

339
    """
340
    self.__lock.acquire()
341
    try:
342
      assert not self.__is_sharer(), "Cannot delete() a lock while sharing it"
343

    
344
      self.__check_deleted()
345

    
346
      # The caller is allowed to hold the lock exclusively already.
347
      acquired = self.__is_exclusive()
348

    
349
      if not acquired:
350
        acquired = self.__acquire_unlocked(timeout)
351

    
352
      if acquired:
353
        self.__deleted = True
354
        self.__exc = None
355

    
356
        # Notify all acquires. They'll throw an error.
357
        while self.__pending:
358
          self.__pending.pop().notifyAll()
359

    
360
      return acquired
361
    finally:
362
      self.__lock.release()
363

    
364

    
365
# Whenever we want to acquire a full LockSet we pass None as the value
366
# to acquire.  Hide this behind this nicely named constant.
367
ALL_SET = None
368

    
369

    
370
class LockSet:
371
  """Implements a set of locks.
372

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

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

380
  """
381
  def __init__(self, members=None):
382
    """Constructs a new LockSet.
383

384
    @param members: initial members of the set
385

386
    """
387
    # Used internally to guarantee coherency.
388
    self.__lock = SharedLock()
389

    
390
    # The lockdict indexes the relationship name -> lock
391
    # The order-of-locking is implied by the alphabetical order of names
392
    self.__lockdict = {}
393

    
394
    if members is not None:
395
      for name in members:
396
        self.__lockdict[name] = SharedLock()
397

    
398
    # The owner dict contains the set of locks each thread owns. For
399
    # performance each thread can access its own key without a global lock on
400
    # this structure. It is paramount though that *no* other type of access is
401
    # done to this structure (eg. no looping over its keys). *_owner helper
402
    # function are defined to guarantee access is correct, but in general never
403
    # do anything different than __owners[threading.currentThread()], or there
404
    # will be trouble.
405
    self.__owners = {}
406

    
407
  def _is_owned(self):
408
    """Is the current thread a current level owner?"""
409
    return threading.currentThread() in self.__owners
410

    
411
  def _add_owned(self, name=None):
412
    """Note the current thread owns the given lock"""
413
    if name is None:
414
      if not self._is_owned():
415
        self.__owners[threading.currentThread()] = set()
416
    else:
417
      if self._is_owned():
418
        self.__owners[threading.currentThread()].add(name)
419
      else:
420
        self.__owners[threading.currentThread()] = set([name])
421

    
422
  def _del_owned(self, name=None):
423
    """Note the current thread owns the given lock"""
424

    
425
    if name is not None:
426
      self.__owners[threading.currentThread()].remove(name)
427

    
428
    # Only remove the key if we don't hold the set-lock as well
429
    if (not self.__lock._is_owned() and
430
        not self.__owners[threading.currentThread()]):
431
      del self.__owners[threading.currentThread()]
432

    
433
  def _list_owned(self):
434
    """Get the set of resource names owned by the current thread"""
435
    if self._is_owned():
436
      return self.__owners[threading.currentThread()].copy()
437
    else:
438
      return set()
439

    
440
  def __names(self):
441
    """Return the current set of names.
442

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

446
    """
447
    return self.__lockdict.keys()
448

    
449
  def _names(self):
450
    """Return a copy of the current set of elements.
451

452
    Used only for debugging purposes.
453

454
    """
455
    # If we don't already own the set-level lock acquired
456
    # we'll get it and note we need to release it later.
457
    release_lock = False
458
    if not self.__lock._is_owned():
459
      release_lock = True
460
      self.__lock.acquire(shared=1)
461
    try:
462
      result = self.__names()
463
    finally:
464
      if release_lock:
465
        self.__lock.release()
466
    return set(result)
467

    
468
  def acquire(self, names, blocking=1, shared=0):
469
    """Acquire a set of resource locks.
470

471
    @param names: the names of the locks which shall be acquired
472
        (special lock names, or instance/node names)
473
    @param shared: whether to acquire in shared mode; by default an
474
        exclusive lock will be acquired
475
    @param blocking: whether to block while trying to acquire or to
476
        operate in try-lock mode (this locking mode is not supported yet)
477

478
    @return: True when all the locks are successfully acquired
479

480
    @raise errors.LockError: when any lock we try to acquire has
481
        been deleted before we succeed. In this case none of the
482
        locks requested will be acquired.
483

484
    """
485
    if not blocking:
486
      # We don't have non-blocking mode for now
487
      raise NotImplementedError
488

    
489
    # Check we don't already own locks at this level
490
    assert not self._is_owned(), "Cannot acquire locks in the same set twice"
491

    
492
    if names is None:
493
      # If no names are given acquire the whole set by not letting new names
494
      # being added before we release, and getting the current list of names.
495
      # Some of them may then be deleted later, but we'll cope with this.
496
      #
497
      # We'd like to acquire this lock in a shared way, as it's nice if
498
      # everybody else can use the instances at the same time. If are acquiring
499
      # them exclusively though they won't be able to do this anyway, though,
500
      # so we'll get the list lock exclusively as well in order to be able to
501
      # do add() on the set while owning it.
502
      self.__lock.acquire(shared=shared)
503
      try:
504
        # note we own the set-lock
505
        self._add_owned()
506
        names = self.__names()
507
      except:
508
        # We shouldn't have problems adding the lock to the owners list, but
509
        # if we did we'll try to release this lock and re-raise exception.
510
        # Of course something is going to be really wrong, after this.
511
        self.__lock.release()
512
        raise
513

    
514
    try:
515
      # Support passing in a single resource to acquire rather than many
516
      if isinstance(names, basestring):
517
        names = [names]
518
      else:
519
        names = sorted(names)
520

    
521
      acquire_list = []
522
      # First we look the locks up on __lockdict. We have no way of being sure
523
      # they will still be there after, but this makes it a lot faster should
524
      # just one of them be the already wrong
525
      for lname in utils.UniqueSequence(names):
526
        try:
527
          lock = self.__lockdict[lname] # raises KeyError if lock is not there
528
          acquire_list.append((lname, lock))
529
        except (KeyError):
530
          if self.__lock._is_owned():
531
            # We are acquiring all the set, it doesn't matter if this
532
            # particular element is not there anymore.
533
            continue
534
          else:
535
            raise errors.LockError('non-existing lock in set (%s)' % lname)
536

    
537
      # This will hold the locknames we effectively acquired.
538
      acquired = set()
539
      # Now acquire_list contains a sorted list of resources and locks we want.
540
      # In order to get them we loop on this (private) list and acquire() them.
541
      # We gave no real guarantee they will still exist till this is done but
542
      # .acquire() itself is safe and will alert us if the lock gets deleted.
543
      for (lname, lock) in acquire_list:
544
        try:
545
          lock.acquire(shared=shared) # raises LockError if the lock is deleted
546
          # now the lock cannot be deleted, we have it!
547
          self._add_owned(name=lname)
548
          acquired.add(lname)
549
        except (errors.LockError):
550
          if self.__lock._is_owned():
551
            # We are acquiring all the set, it doesn't matter if this
552
            # particular element is not there anymore.
553
            continue
554
          else:
555
            name_fail = lname
556
            for lname in self._list_owned():
557
              self.__lockdict[lname].release()
558
              self._del_owned(name=lname)
559
            raise errors.LockError('non-existing lock in set (%s)' % name_fail)
560
        except:
561
          # We shouldn't have problems adding the lock to the owners list, but
562
          # if we did we'll try to release this lock and re-raise exception.
563
          # Of course something is going to be really wrong, after this.
564
          if lock._is_owned():
565
            lock.release()
566
          raise
567

    
568
    except:
569
      # If something went wrong and we had the set-lock let's release it...
570
      if self.__lock._is_owned():
571
        self.__lock.release()
572
      raise
573

    
574
    return acquired
575

    
576
  def release(self, names=None):
577
    """Release a set of resource locks, at the same level.
578

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

582
    @param names: the names of the locks which shall be released
583
        (defaults to all the locks acquired at that level).
584

585
    """
586
    assert self._is_owned(), "release() on lock set while not owner"
587

    
588
    # Support passing in a single resource to release rather than many
589
    if isinstance(names, basestring):
590
      names = [names]
591

    
592
    if names is None:
593
      names = self._list_owned()
594
    else:
595
      names = set(names)
596
      assert self._list_owned().issuperset(names), (
597
               "release() on unheld resources %s" %
598
               names.difference(self._list_owned()))
599

    
600
    # First of all let's release the "all elements" lock, if set.
601
    # After this 'add' can work again
602
    if self.__lock._is_owned():
603
      self.__lock.release()
604
      self._del_owned()
605

    
606
    for lockname in names:
607
      # If we are sure the lock doesn't leave __lockdict without being
608
      # exclusively held we can do this...
609
      self.__lockdict[lockname].release()
610
      self._del_owned(name=lockname)
611

    
612
  def add(self, names, acquired=0, shared=0):
613
    """Add a new set of elements to the set
614

615
    @param names: names of the new elements to add
616
    @param acquired: pre-acquire the new resource?
617
    @param shared: is the pre-acquisition shared?
618

619
    """
620
    # Check we don't already own locks at this level
621
    assert not self._is_owned() or self.__lock._is_owned(shared=0), \
622
      "Cannot add locks if the set is only partially owned, or shared"
623

    
624
    # Support passing in a single resource to add rather than many
625
    if isinstance(names, basestring):
626
      names = [names]
627

    
628
    # If we don't already own the set-level lock acquired in an exclusive way
629
    # we'll get it and note we need to release it later.
630
    release_lock = False
631
    if not self.__lock._is_owned():
632
      release_lock = True
633
      self.__lock.acquire()
634

    
635
    try:
636
      invalid_names = set(self.__names()).intersection(names)
637
      if invalid_names:
638
        # This must be an explicit raise, not an assert, because assert is
639
        # turned off when using optimization, and this can happen because of
640
        # concurrency even if the user doesn't want it.
641
        raise errors.LockError("duplicate add() (%s)" % invalid_names)
642

    
643
      for lockname in names:
644
        lock = SharedLock()
645

    
646
        if acquired:
647
          lock.acquire(shared=shared)
648
          # now the lock cannot be deleted, we have it!
649
          try:
650
            self._add_owned(name=lockname)
651
          except:
652
            # We shouldn't have problems adding the lock to the owners list,
653
            # but if we did we'll try to release this lock and re-raise
654
            # exception.  Of course something is going to be really wrong,
655
            # after this.  On the other hand the lock hasn't been added to the
656
            # __lockdict yet so no other threads should be pending on it. This
657
            # release is just a safety measure.
658
            lock.release()
659
            raise
660

    
661
        self.__lockdict[lockname] = lock
662

    
663
    finally:
664
      # Only release __lock if we were not holding it previously.
665
      if release_lock:
666
        self.__lock.release()
667

    
668
    return True
669

    
670
  def remove(self, names, blocking=1):
671
    """Remove elements from the lock set.
672

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

676
    @param names: names of the resource to remove.
677
    @param blocking: whether to block while trying to acquire or to
678
        operate in try-lock mode (this locking mode is not supported
679
        yet unless you are already holding exclusively the locks)
680

681
    @return:: a list of locks which we removed; the list is always
682
        equal to the names list if we were holding all the locks
683
        exclusively
684

685
    """
686
    if not blocking and not self._is_owned():
687
      # We don't have non-blocking mode for now
688
      raise NotImplementedError
689

    
690
    # Support passing in a single resource to remove rather than many
691
    if isinstance(names, basestring):
692
      names = [names]
693

    
694
    # If we own any subset of this lock it must be a superset of what we want
695
    # to delete. The ownership must also be exclusive, but that will be checked
696
    # by the lock itself.
697
    assert not self._is_owned() or self._list_owned().issuperset(names), (
698
      "remove() on acquired lockset while not owning all elements")
699

    
700
    removed = []
701

    
702
    for lname in names:
703
      # Calling delete() acquires the lock exclusively if we don't already own
704
      # it, and causes all pending and subsequent lock acquires to fail. It's
705
      # fine to call it out of order because delete() also implies release(),
706
      # and the assertion above guarantees that if we either already hold
707
      # everything we want to delete, or we hold none.
708
      try:
709
        self.__lockdict[lname].delete()
710
        removed.append(lname)
711
      except (KeyError, errors.LockError):
712
        # This cannot happen if we were already holding it, verify:
713
        assert not self._is_owned(), "remove failed while holding lockset"
714
      else:
715
        # If no LockError was raised we are the ones who deleted the lock.
716
        # This means we can safely remove it from lockdict, as any further or
717
        # pending delete() or acquire() will fail (and nobody can have the lock
718
        # since before our call to delete()).
719
        #
720
        # This is done in an else clause because if the exception was thrown
721
        # it's the job of the one who actually deleted it.
722
        del self.__lockdict[lname]
723
        # And let's remove it from our private list if we owned it.
724
        if self._is_owned():
725
          self._del_owned(name=lname)
726

    
727
    return removed
728

    
729

    
730
# Locking levels, must be acquired in increasing order.
731
# Current rules are:
732
#   - at level LEVEL_CLUSTER resides the Big Ganeti Lock (BGL) which must be
733
#   acquired before performing any operation, either in shared or in exclusive
734
#   mode. acquiring the BGL in exclusive mode is discouraged and should be
735
#   avoided.
736
#   - at levels LEVEL_NODE and LEVEL_INSTANCE reside node and instance locks.
737
#   If you need more than one node, or more than one instance, acquire them at
738
#   the same time.
739
LEVEL_CLUSTER = 0
740
LEVEL_INSTANCE = 1
741
LEVEL_NODE = 2
742

    
743
LEVELS = [LEVEL_CLUSTER,
744
          LEVEL_INSTANCE,
745
          LEVEL_NODE]
746

    
747
# Lock levels which are modifiable
748
LEVELS_MOD = [LEVEL_NODE, LEVEL_INSTANCE]
749

    
750
LEVEL_NAMES = {
751
  LEVEL_CLUSTER: "cluster",
752
  LEVEL_INSTANCE: "instance",
753
  LEVEL_NODE: "node",
754
  }
755

    
756
# Constant for the big ganeti lock
757
BGL = 'BGL'
758

    
759

    
760
class GanetiLockManager:
761
  """The Ganeti Locking Library
762

763
  The purpose of this small library is to manage locking for ganeti clusters
764
  in a central place, while at the same time doing dynamic checks against
765
  possible deadlocks. It will also make it easier to transition to a different
766
  lock type should we migrate away from python threads.
767

768
  """
769
  _instance = None
770

    
771
  def __init__(self, nodes=None, instances=None):
772
    """Constructs a new GanetiLockManager object.
773

774
    There should be only a GanetiLockManager object at any time, so this
775
    function raises an error if this is not the case.
776

777
    @param nodes: list of node names
778
    @param instances: list of instance names
779

780
    """
781
    assert self.__class__._instance is None, \
782
           "double GanetiLockManager instance"
783

    
784
    self.__class__._instance = self
785

    
786
    # The keyring contains all the locks, at their level and in the correct
787
    # locking order.
788
    self.__keyring = {
789
      LEVEL_CLUSTER: LockSet([BGL]),
790
      LEVEL_NODE: LockSet(nodes),
791
      LEVEL_INSTANCE: LockSet(instances),
792
    }
793

    
794
  def _names(self, level):
795
    """List the lock names at the given level.
796

797
    This can be used for debugging/testing purposes.
798

799
    @param level: the level whose list of locks to get
800

801
    """
802
    assert level in LEVELS, "Invalid locking level %s" % level
803
    return self.__keyring[level]._names()
804

    
805
  def _is_owned(self, level):
806
    """Check whether we are owning locks at the given level
807

808
    """
809
    return self.__keyring[level]._is_owned()
810

    
811
  is_owned = _is_owned
812

    
813
  def _list_owned(self, level):
814
    """Get the set of owned locks at the given level
815

816
    """
817
    return self.__keyring[level]._list_owned()
818

    
819
  def _upper_owned(self, level):
820
    """Check that we don't own any lock at a level greater than the given one.
821

822
    """
823
    # This way of checking only works if LEVELS[i] = i, which we check for in
824
    # the test cases.
825
    return utils.any((self._is_owned(l) for l in LEVELS[level + 1:]))
826

    
827
  def _BGL_owned(self):
828
    """Check if the current thread owns the BGL.
829

830
    Both an exclusive or a shared acquisition work.
831

832
    """
833
    return BGL in self.__keyring[LEVEL_CLUSTER]._list_owned()
834

    
835
  def _contains_BGL(self, level, names):
836
    """Check if the level contains the BGL.
837

838
    Check if acting on the given level and set of names will change
839
    the status of the Big Ganeti Lock.
840

841
    """
842
    return level == LEVEL_CLUSTER and (names is None or BGL in names)
843

    
844
  def acquire(self, level, names, blocking=1, shared=0):
845
    """Acquire a set of resource locks, at the same level.
846

847
    @param level: the level at which the locks shall be acquired;
848
        it must be a member of LEVELS.
849
    @param names: the names of the locks which shall be acquired
850
        (special lock names, or instance/node names)
851
    @param shared: whether to acquire in shared mode; by default
852
        an exclusive lock will be acquired
853
    @param blocking: whether to block while trying to acquire or to
854
        operate in try-lock mode (this locking mode is not supported yet)
855

856
    """
857
    assert level in LEVELS, "Invalid locking level %s" % level
858

    
859
    # Check that we are either acquiring the Big Ganeti Lock or we already own
860
    # it. Some "legacy" opcodes need to be sure they are run non-concurrently
861
    # so even if we've migrated we need to at least share the BGL to be
862
    # compatible with them. Of course if we own the BGL exclusively there's no
863
    # point in acquiring any other lock, unless perhaps we are half way through
864
    # the migration of the current opcode.
865
    assert (self._contains_BGL(level, names) or self._BGL_owned()), (
866
            "You must own the Big Ganeti Lock before acquiring any other")
867

    
868
    # Check we don't own locks at the same or upper levels.
869
    assert not self._upper_owned(level), ("Cannot acquire locks at a level"
870
           " while owning some at a greater one")
871

    
872
    # Acquire the locks in the set.
873
    return self.__keyring[level].acquire(names, shared=shared,
874
                                         blocking=blocking)
875

    
876
  def release(self, level, names=None):
877
    """Release a set of resource locks, at the same level.
878

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

882
    @param level: the level at which the locks shall be released;
883
        it must be a member of LEVELS
884
    @param names: the names of the locks which shall be released
885
        (defaults to all the locks acquired at that level)
886

887
    """
888
    assert level in LEVELS, "Invalid locking level %s" % level
889
    assert (not self._contains_BGL(level, names) or
890
            not self._upper_owned(LEVEL_CLUSTER)), (
891
            "Cannot release the Big Ganeti Lock while holding something"
892
            " at upper levels")
893

    
894
    # Release will complain if we don't own the locks already
895
    return self.__keyring[level].release(names)
896

    
897
  def add(self, level, names, acquired=0, shared=0):
898
    """Add locks at the specified level.
899

900
    @param level: the level at which the locks shall be added;
901
        it must be a member of LEVELS_MOD.
902
    @param names: names of the locks to acquire
903
    @param acquired: whether to acquire the newly added locks
904
    @param shared: whether the acquisition will be shared
905

906
    """
907
    assert level in LEVELS_MOD, "Invalid or immutable level %s" % level
908
    assert self._BGL_owned(), ("You must own the BGL before performing other"
909
           " operations")
910
    assert not self._upper_owned(level), ("Cannot add locks at a level"
911
           " while owning some at a greater one")
912
    return self.__keyring[level].add(names, acquired=acquired, shared=shared)
913

    
914
  def remove(self, level, names, blocking=1):
915
    """Remove locks from the specified level.
916

917
    You must either already own the locks you are trying to remove
918
    exclusively or not own any lock at an upper level.
919

920
    @param level: the level at which the locks shall be removed;
921
        it must be a member of LEVELS_MOD
922
    @param names: the names of the locks which shall be removed
923
        (special lock names, or instance/node names)
924
    @param blocking: whether to block while trying to operate in
925
        try-lock mode (this locking mode is not supported yet)
926

927
    """
928
    assert level in LEVELS_MOD, "Invalid or immutable level %s" % level
929
    assert self._BGL_owned(), ("You must own the BGL before performing other"
930
           " operations")
931
    # Check we either own the level or don't own anything from here
932
    # up. LockSet.remove() will check the case in which we don't own
933
    # all the needed resources, or we have a shared ownership.
934
    assert self._is_owned(level) or not self._upper_owned(level), (
935
           "Cannot remove locks at a level while not owning it or"
936
           " owning some at a greater one")
937
    return self.__keyring[level].remove(names, blocking=blocking)