fix: reconcile native save release and resume affected CBB batches
This commit is contained in:
1 parent
443668aa4b
commit
c76b755f13
16 files changed
+655
-36
No files matched your search
@@ -1,5 +1,5 @@
|
||||
"""Cooperative FIFO admission for this application's native operations."""
|
||||
from contextlib import contextmanager
|
||||
from contextlib import contextmanager, nullcontext
|
||||
from dataclasses import dataclass, field
|
||||
import ntpath
|
||||
from threading import Condition, Event, RLock, Thread, local
|
||||
@@ -91,6 +91,8 @@ class _Call:
|
||||
shutdown_handoff: bool = False
|
||||
native_handoff: str = ''
|
||||
handoff_cancelled: bool = False
|
||||
queue_pause: object = None
|
||||
queued_for_slot: bool = False
|
||||
phase_started: float = field(default_factory=time.monotonic)
|
||||
result: object = None
|
||||
last_result: object = None
|
||||
@@ -110,6 +112,11 @@ def task_stop(fallback=None):
|
||||
return getattr(_local, 'call', None) or fallback
|
||||
|
||||
|
||||
def allow_other_libraries():
|
||||
call = getattr(_local, 'call', None)
|
||||
return call.queue_pause() if call is not None and call.queue_pause is not None else nullcontext()
|
||||
|
||||
|
||||
def check_active():
|
||||
call = getattr(_local, 'call', None)
|
||||
if call is not None and call.is_set():
|
||||
@@ -393,7 +400,8 @@ class BatchTaskContext:
|
||||
call.abandoned = True
|
||||
raise TaskHandoff(self, cancelled=cancelled, result=call.last_result)
|
||||
elapsed = time.monotonic()-call.phase_started
|
||||
if not call.native_depth and not call.commit_depth and (call.abandoned or elapsed >= timeout_seconds):
|
||||
expired = not call.queued_for_slot and elapsed >= timeout_seconds
|
||||
if not call.native_depth and not call.commit_depth and (call.abandoned or expired):
|
||||
cancelled = call.is_set()
|
||||
call.abandoned = True
|
||||
raise TaskHandoff(self, cancelled=cancelled, result=call.last_result)
|
||||
@@ -402,6 +410,13 @@ class BatchTaskContext:
|
||||
return call.result
|
||||
|
||||
|
||||
@dataclass(eq=False)
|
||||
class _Ticket:
|
||||
roots: frozenset
|
||||
ready: bool = False
|
||||
paused: bool = False
|
||||
|
||||
|
||||
class NativeQueue:
|
||||
def __init__(self):
|
||||
self.condition = Condition()
|
||||
@@ -447,9 +462,62 @@ class NativeQueue:
|
||||
raise QueueUnavailable(path, retained_session_states(path), timed_out=True)
|
||||
return False
|
||||
return True
|
||||
ticket = [object(), False]
|
||||
roots = frozenset(ntpath.dirname(_path_key(path))
|
||||
for path in (context.library, *context.aliases)) if context is not None else frozenset()
|
||||
ticket = _Ticket(roots)
|
||||
acquired, notified = False, False
|
||||
previous_state = None
|
||||
|
||||
def first_ready():
|
||||
paused = [entry for entry in self.pending if entry.paused]
|
||||
return next((entry for entry in self.pending if entry.ready and not any(
|
||||
owner is not entry and (not entry.roots or owner.roots & entry.roots)
|
||||
for owner in paused)), None)
|
||||
|
||||
@contextmanager
|
||||
def pause():
|
||||
nonlocal acquired
|
||||
call = _local.call
|
||||
with self.condition:
|
||||
can_pause = acquired and not call.native_depth and not call.commit_depth
|
||||
if can_pause:
|
||||
ticket.ready, ticket.paused = False, True
|
||||
self.active = acquired = False
|
||||
self.condition.notify_all()
|
||||
if not can_pause:
|
||||
# Nested Verify may still be part of a management transaction.
|
||||
yield
|
||||
return
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
with _tasks_guard:
|
||||
call.queued_for_slot = True
|
||||
try:
|
||||
with self.condition:
|
||||
ticket.ready = True
|
||||
while ticket in self.pending and not self.closed and not call.is_set():
|
||||
if not self.active and first_ready() is ticket:
|
||||
self.active = acquired = True
|
||||
break
|
||||
self.condition.wait(.1)
|
||||
ticket.paused = ticket.ready = False
|
||||
self.condition.notify_all()
|
||||
if not acquired:
|
||||
raise QueueCancelled('已取消等待原生通道,未提交新的调用。')
|
||||
finally:
|
||||
with _tasks_guard:
|
||||
call.queued_for_slot = False
|
||||
call.phase_started = time.monotonic()
|
||||
|
||||
def run_admitted():
|
||||
call = _local.call
|
||||
previous_pause, call.queue_pause = call.queue_pause, pause
|
||||
try:
|
||||
return action()
|
||||
finally:
|
||||
call.queue_pause = previous_pause
|
||||
|
||||
with self.condition:
|
||||
if self.closed:
|
||||
raise QueueCancelled('队列已关闭,未启动新的操作。')
|
||||
@@ -466,15 +534,14 @@ class NativeQueue:
|
||||
if (self.closed or stop is not None and stop.is_set()
|
||||
or context is not None and (context.stop.is_set() or context.stop_after_current.is_set())):
|
||||
raise QueueCancelled('已取消排队,未启动原生操作。')
|
||||
ticket[1] = bool(available)
|
||||
first = next((entry for entry in self.pending if entry[1]), None)
|
||||
if not self.active and first is ticket:
|
||||
ticket.ready = bool(available)
|
||||
if not self.active and first_ready() is ticket:
|
||||
self.active = acquired = True
|
||||
else:
|
||||
self.condition.wait(.1)
|
||||
if acquired and not available_now():
|
||||
with self.condition:
|
||||
ticket[1] = self.active = acquired = False
|
||||
ticket.ready = self.active = acquired = False
|
||||
self.condition.notify_all()
|
||||
if not acquired and waiting is not None:
|
||||
try:
|
||||
@@ -487,7 +554,7 @@ class NativeQueue:
|
||||
if (stop is not None and stop.is_set()
|
||||
or context is not None and context.stop_after_current.is_set()):
|
||||
raise QueueCancelled('已取消排队,未启动原生操作。')
|
||||
return context.run(action, stop=stop, timeout_seconds=timeout_seconds) if context else action()
|
||||
return context.run(run_admitted, stop=stop, timeout_seconds=timeout_seconds) if context else action()
|
||||
finally:
|
||||
with self.condition:
|
||||
self.pending = [entry for entry in self.pending if entry is not ticket]
|
||||
|
||||
Reference in new issue
Block a user