diff --git a/packages/core/src/opendirector/infrastructure/creation_projects.py b/packages/core/src/opendirector/infrastructure/creation_projects.py index c64a382e7..89e33af80 100644 --- a/packages/core/src/opendirector/infrastructure/creation_projects.py +++ b/packages/core/src/opendirector/infrastructure/creation_projects.py @@ -151,6 +151,14 @@ def object_changes(before, after, command): return changed +# Every read validates the whole commit chain, and each record holds a full +# project snapshot. The cache must fit a long project's history: below that, +# each read reparses every record from disk and holds the catalog lock long +# enough to expire sessions and interrupt running jobs. +RECORD_CACHE_ENTRIES = 4096 +RECORD_CACHE_BYTES = 512 * 1024 * 1024 + + class CreationProjects: """Authoritative repository for lifecycle and shared creation commands. @@ -247,7 +255,7 @@ def signature(): if os.name != 'nt' and before[2] <= 8 * 1024 * 1024: self._records_cache[file] = (before, record) self._records_cache_bytes += before[2] - while len(self._records_cache) > 128 or self._records_cache_bytes > 8 * 1024 * 1024: + while len(self._records_cache) > RECORD_CACHE_ENTRIES or self._records_cache_bytes > RECORD_CACHE_BYTES: _, removed = self._records_cache.popitem(last=False) self._records_cache_bytes -= removed[0][2] return record diff --git a/tests/test_creation_storage.py b/tests/test_creation_storage.py index 35a6e8353..d0fa23945 100644 --- a/tests/test_creation_storage.py +++ b/tests/test_creation_storage.py @@ -332,6 +332,26 @@ def reading(path): projects.snapshot(created.projectId) +@pytest.mark.skipif(os.name == 'nt', reason='Commit signature caching requires POSIX change time') +def test_long_history_is_validated_from_disk_only_once(projects, monkeypatch): + # Each read validates the whole chain; a cache smaller than the history + # would reparse every record on every read of a long project. + from opendirector.infrastructure import creation_projects as module + created = create(projects) + for revision in range(1, 200): + assert projects.execute(rename(created, f'Renamed {revision}', revision)).status == 'applied' + projects.snapshot(created.projectId) + reads = [] + original = module.read_json + def reading(path): + if path.parent.name == 'commits': + reads.append(path) + return original(path) + monkeypatch.setattr(module, 'read_json', reading) + assert projects.snapshot(created.projectId).project.name == 'Renamed 199' + assert reads == [] + + def test_returned_snapshots_and_history_cannot_mutate_record_cache(projects): created = create(projects) session = str(uuid4())