fix: separate verification and ICDB maintenance with live session leases
This commit is contained in:
1 parent
2587d33c5a
commit
477a5a46e3
15 files changed
+420
-194
No files matched your search
@@ -78,13 +78,14 @@ def _persist_blocker(library, status='OWNER_RELEASE_UNCONFIRMED', *, lease_id):
|
||||
|
||||
|
||||
class LibraryLock:
|
||||
def __init__(self, library, *, safe_to_release=None):
|
||||
def __init__(self, library, *, safe_to_release=None, checkpoint=True):
|
||||
library = Path(library).resolve()
|
||||
self.library = library
|
||||
self.path = library.with_name('.' + library.stem + '.cbb-upload.lock')
|
||||
self.key = _key(self.path)
|
||||
self.entered = False
|
||||
self.safe_to_release = safe_to_release
|
||||
self.checkpoint = checkpoint
|
||||
self.maintenance_guard = None
|
||||
self.maintenance_mutex = None
|
||||
|
||||
@@ -96,6 +97,17 @@ class LibraryLock:
|
||||
return lease
|
||||
|
||||
def __enter__(self):
|
||||
with _guard:
|
||||
owner = _owners.get(self.key)
|
||||
if owner and owner['retained'] and owner['depth'] == 0 and owner.get('release_probe'):
|
||||
try:
|
||||
released = owner['release_probe']() is True
|
||||
except Exception:
|
||||
released = False
|
||||
if released:
|
||||
owner['handle'].Close()
|
||||
del _owners[self.key]
|
||||
owner = None
|
||||
if self.maintenance_guard is not None:
|
||||
return _enter_maintenance(self)
|
||||
import win32con, win32file
|
||||
@@ -106,7 +118,7 @@ class LibraryLock:
|
||||
raise LibraryBusy(f'中心库被其他任务占用:{self.path}')
|
||||
owner['depth'] += 1
|
||||
else:
|
||||
if has_blocker(self.library):
|
||||
if self.checkpoint and has_blocker(self.library):
|
||||
raise LibraryBusy(f'中心库存在未解除的会话阻止记录,请先执行释放检查:{blocker_path(self.library)}')
|
||||
try:
|
||||
lease_id = uuid4().hex
|
||||
@@ -127,12 +139,15 @@ class LibraryLock:
|
||||
# it explicitly before complete-copy metadata is captured.
|
||||
import pywintypes
|
||||
win32file.SetFileTime(handle, None, None, pywintypes.Time(datetime.now()))
|
||||
_persist_blocker(self.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id)
|
||||
if self.checkpoint:
|
||||
_persist_blocker(self.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id)
|
||||
except Exception:
|
||||
handle.Close()
|
||||
raise
|
||||
_owners[self.key] = {'thread': threading.get_ident(), 'depth': 1,
|
||||
'handle': handle, 'retained': False, 'lease_id': lease_id}
|
||||
'handle': handle, 'retained': False, 'lease_id': lease_id,
|
||||
'checkpoint': self.checkpoint,
|
||||
'release_probe': self.safe_to_release if not self.checkpoint else None}
|
||||
self.entered = True
|
||||
return self
|
||||
|
||||
@@ -162,6 +177,20 @@ class LibraryLock:
|
||||
if not self.entered:
|
||||
return
|
||||
owner = _owners[self.key]
|
||||
if not owner.get('checkpoint', True):
|
||||
if self.safe_to_release is not None:
|
||||
try:
|
||||
released = self.safe_to_release() is True
|
||||
except Exception:
|
||||
released = False
|
||||
if not released:
|
||||
owner['retained'] = True
|
||||
owner['depth'] -= 1
|
||||
self.entered = False
|
||||
if owner['depth'] == 0 and not owner['retained']:
|
||||
owner['handle'].Close()
|
||||
del _owners[self.key]
|
||||
return
|
||||
checkpoint = checkpoint_record(blocker_path(self.library))
|
||||
if checkpoint is not None and 'export_analysis' in checkpoint:
|
||||
owner['retained'] = True
|
||||
@@ -237,7 +266,7 @@ def _enter_maintenance(lease):
|
||||
raise LibraryBusy('本进程库锁仍在使用或检查点身份不符')
|
||||
handle = existing['handle']
|
||||
else:
|
||||
if record is not None and _supervisor_alive(record['pid']):
|
||||
if record is not None and record['pid'] != os.getpid() and _supervisor_alive(record['pid']):
|
||||
raise LibraryBusy('原监督进程仍存在,不能接续其库锁')
|
||||
handle = win32file.CreateFile(str(lease.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
|
||||
Reference in new issue
Block a user