mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-11-27 17:00:37 +00:00
fix(events): stop pinning a DB write lock for every thread ever written (#8483)
DbRunEventStore kept a strong pin for each thread's asyncio.Lock next to its weak registry, and released it only in delete_by_thread. Every thread a process wrote events for, and never deleted, kept a lock and its id for the life of the process. The weak registry alone already keeps a lock generation alive while a holder or waiter references it, which is what the deletion handoff needs. Drop the pin, as the JSONL store already does.
This commit is contained in:
parent
ea1872e429
commit
0c1e2119df
@ -50,13 +50,11 @@ class DbRunEventStore(RunEventStore):
|
||||
# single-process case where two coroutines interleave between the
|
||||
# max(seq) read and the INSERT and would otherwise collide on seq.
|
||||
#
|
||||
# The weak registry preserves one lock generation while an admitted
|
||||
# holder/waiter still references it. A separate pin keeps the historical
|
||||
# one-lock-per-live-thread behavior until delete_by_thread() explicitly
|
||||
# retires that thread; after retirement, outstanding users alone keep
|
||||
# the generation alive until they drain.
|
||||
# The registry is weak, as in the JSONL store: a lock generation lives
|
||||
# exactly as long as an admitted holder/waiter references it, and an
|
||||
# idle thread keeps no entry. Never pin locks strongly; every thread a
|
||||
# process ever wrote would then hold one until it was deleted.
|
||||
self._write_locks: weakref.WeakValueDictionary[str, asyncio.Lock] = weakref.WeakValueDictionary()
|
||||
self._write_lock_pins: dict[str, asyncio.Lock] = {}
|
||||
|
||||
def _get_write_lock(self, thread_id: str) -> asyncio.Lock:
|
||||
"""Return (creating if needed) the per-thread seq-assignment lock."""
|
||||
@ -64,9 +62,6 @@ class DbRunEventStore(RunEventStore):
|
||||
if lock is None:
|
||||
lock = asyncio.Lock()
|
||||
self._write_locks[thread_id] = lock
|
||||
# A fresh caller after deletion makes the thread live again. Repin the
|
||||
# current generation so normal live-thread registry lifetime is stable.
|
||||
self._write_lock_pins[thread_id] = lock
|
||||
return lock
|
||||
|
||||
async def validate_producer_fencing(self) -> None:
|
||||
@ -707,14 +702,12 @@ class DbRunEventStore(RunEventStore):
|
||||
# owner still need the allocation floor for deleted runs.
|
||||
remaining_events = select(RunEventRow.id).where(RunEventRow.thread_id == thread_id).exists()
|
||||
await session.execute(delete(RunEventThreadSeqRow).where(RunEventThreadSeqRow.thread_id == thread_id, ~remaining_events))
|
||||
# Retire the live-thread pin, but never remove the weak registry
|
||||
# entry directly. asyncio.Lock.release() clears ``locked()`` before
|
||||
# a queued waiter resumes, so an unlocked check can observe the
|
||||
# handoff window and split one thread onto two lock generations.
|
||||
# Holders/waiters keep the old generation alive until they drain; a
|
||||
# later caller therefore resolves that same lock instead of racing
|
||||
# it with a fresh one.
|
||||
self._write_lock_pins.pop(thread_id, None)
|
||||
# The weak registry entry is left alone on purpose.
|
||||
# asyncio.Lock.release() clears ``locked()`` before a queued waiter
|
||||
# resumes, so an unlocked check can observe the handoff window and
|
||||
# split one thread onto two lock generations. Holders/waiters keep
|
||||
# the generation alive until they drain; a later caller therefore
|
||||
# resolves that same lock instead of racing it with a fresh one.
|
||||
return count
|
||||
|
||||
async def delete_by_run(
|
||||
@ -726,8 +719,7 @@ class DbRunEventStore(RunEventStore):
|
||||
):
|
||||
"""Delete one run's events inside the thread mutation fence.
|
||||
|
||||
Shares ``delete_by_thread``'s critical section; deleting a single run
|
||||
leaves the thread alive, so the write-lock pin is deliberately kept.
|
||||
Shares ``delete_by_thread``'s critical section.
|
||||
"""
|
||||
resolved_user_id = resolve_user_id(user_id, method_name="DbRunEventStore.delete_by_run")
|
||||
async with self._get_write_lock(thread_id):
|
||||
|
||||
@ -1257,7 +1257,9 @@ class TestDbRunEventStoreWriteLock:
|
||||
await close_engine()
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_delete_by_thread_evicts_orphaned_write_lock(self, tmp_path):
|
||||
async def test_idle_thread_keeps_no_write_lock(self, tmp_path):
|
||||
import gc
|
||||
|
||||
from deerflow.persistence.engine import close_engine, get_session_factory, init_engine
|
||||
from deerflow.runtime.events.store.db import DbRunEventStore
|
||||
|
||||
@ -1265,19 +1267,29 @@ class TestDbRunEventStoreWriteLock:
|
||||
await init_engine("sqlite", url=url, sqlite_dir=str(tmp_path))
|
||||
s = DbRunEventStore(get_session_factory())
|
||||
|
||||
# A write materializes the per-thread lock in the registry.
|
||||
# Once every writer drains, a thread keeps no lock, so the registry
|
||||
# does not grow with every thread the singleton store ever wrote.
|
||||
await s.put_batch([{"thread_id": "t1", "run_id": "r1", "event_type": "trace", "category": "trace"}])
|
||||
assert "t1" in s._write_locks
|
||||
|
||||
# Deleting the thread must evict the now-orphaned lock so the registry
|
||||
# does not grow unbounded across the singleton store's lifetime.
|
||||
await s.delete_by_thread("t1", user_id=None)
|
||||
await s.put_batch([{"thread_id": "t2", "run_id": "r1", "event_type": "trace", "category": "trace"}])
|
||||
gc.collect()
|
||||
assert "t1" not in s._write_locks
|
||||
assert "t2" not in s._write_locks
|
||||
|
||||
# A subsequent write recreates a fresh lock and seq restarts from 1.
|
||||
# While a caller still references a generation, every caller resolves it.
|
||||
held = s._get_write_lock("t1")
|
||||
await s.put_batch([{"thread_id": "t1", "run_id": "r1", "event_type": "trace", "category": "trace"}])
|
||||
assert s._get_write_lock("t1") is held
|
||||
|
||||
# Deleting the thread leaves no entry once its users drain, and a later
|
||||
# write starts a fresh lock with seq restarting from 1.
|
||||
del held
|
||||
await s.delete_by_thread("t1", user_id=None)
|
||||
gc.collect()
|
||||
assert "t1" not in s._write_locks
|
||||
result = await s.put_batch([{"thread_id": "t1", "run_id": "r2", "event_type": "trace", "category": "trace"}])
|
||||
assert "t1" in s._write_locks
|
||||
assert result[0]["seq"] == 1
|
||||
gc.collect()
|
||||
assert "t1" not in s._write_locks
|
||||
|
||||
await close_engine()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user