]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/volumes/fs/operations/lock.py
f5c022935e8faf0b5dccf0a0f8d1bd78738ee974
[ceph.git] / ceph / src / pybind / mgr / volumes / fs / operations / lock.py
1 from contextlib import contextmanager
2 from threading import Lock
3 from typing import Dict
4
5 # singleton design pattern taken from http://www.aleax.it/5ep.html
6
7 class GlobalLock(object):
8 """
9 Global lock to serialize operations in mgr/volumes. This lock
10 is currently held when accessing (opening) a volume to perform
11 group/subvolume operations. Since this is a big lock, it's rather
12 inefficient -- but right now it's ok since mgr/volumes does not
13 expect concurrent operations via its APIs.
14
15 As and when features get added (such as clone, where mgr/volumes
16 would maintain subvolume states in the filesystem), there might
17 be a need to allow concurrent operations. In that case it would
18 be nice to implement an efficient path based locking mechanism.
19
20 See: https://people.eecs.berkeley.edu/~kubitron/courses/cs262a-F14/projects/reports/project6_report.pdf
21 """
22 _shared_state = {
23 'lock' : Lock(),
24 'init' : False
25 } # type: Dict
26
27 def __init__(self):
28 with self._shared_state['lock']:
29 if not self._shared_state['init']:
30 self._shared_state['init'] = True
31 # share this state among all instances
32 self.__dict__ = self._shared_state
33
34 @contextmanager
35 def lock_op(self):
36 with self._shared_state['lock']:
37 yield