Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/test_creation_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down