Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_append_cache_torn_tail.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 18 minutes ago History
test_append_cache_torn_tail.py · 4.6 KB · 114 lines
1"""A records-cache entry parsed while a torn tail was on disk must not
2become the place the next read starts folding from.
3
4A full parse of an append-mode file drops a torn tail (a final row with no
5newline: a writer died mid-append) from the records, but the cache entry it
6stored said it covered the whole file, torn bytes included
7(_cache_entry stored covered_bytes = signature[1]). The next writer -- in
8another process -- first repairs the tail (_repair_torn_tail truncates the
9fragment in place, same inode) and then appends its row. The file grew
10past the stale covered_bytes on the same inode, so this process's next read
11took the incremental branch (_try_incremental_cache_entry) and parsed from
12there: partway into the new row, not at its start. The rest of that row,
13read as a row, shifted every column left -- the new record was missing,
14and a record whose id was one of its VALUES appeared instead.
15
16Every "other process" here is a real one (a subprocess, a fresh
17interpreter), as in tests/test_append_cache_foreign_writer.py: the bug is
18about what one process's memory believes after another process wrote.
19"""
20
21import json
22import subprocess
23import sys
24from pathlib import Path
25
26import pytest
27
28import object_records
29
30REPO_ROOT = str(Path(__file__).resolve().parents[1])
31
32_OTHER = """\
33import sys
34sys.path.insert(0, sys.argv[1])
35import object_records
36data_dir, record_id, value, note = sys.argv[2:6]
37object_records.create_collection_record(
38 "events", {"id": record_id, "value": value, "note": note}, base_dir=data_dir, roots=[])
39"""
40
41
42@pytest.fixture
43def data_dir(tmp_path):
44 data = tmp_path / "data"
45 (data / "schemas").mkdir(parents=True)
46 (data / "schemas" / "events.json").write_text(json.dumps({
47 "name": "events", "storage": "append",
48 "fields": [{"name": "id"}, {"name": "value", "type": "text"},
49 {"name": "note", "type": "text"}]}))
50 (tmp_path / "_other.py").write_text(_OTHER)
51 return data
52
53
54def elsewhere(data_dir, record_id, value, note):
55 done = subprocess.run(
56 [sys.executable, str(data_dir.parent / "_other.py"), REPO_ROOT, str(data_dir),
57 record_id, value, note],
58 capture_output=True, text=True, timeout=60)
59 assert done.returncode == 0, done.stderr
60
61
62def records_file(data_dir):
63 return object_records.collection_records_file("events", base_dir=data_dir)
64
65
66def stored(data_dir):
67 return {row["id"]: (row["value"], row["note"])
68 for row in object_records.read_collection_records("events", base_dir=data_dir, roots=[])}
69
70
71def on_disk(data_dir):
72 """What a process with no cache at all reads: the file is the truth."""
73 object_records._RECORDS_CACHE.clear()
74 object_records._OIDX_CACHE.clear()
75 return stored(data_dir)
76
77
78@pytest.mark.parametrize("fragment", [
79 b"x", # torn one byte in
80 b"\tdead\tpart", # torn inside the second field
81 b'\tq\t"an open', # torn inside an open quote
82], ids=["one-byte", "mid-value", "open-quote"])
83def test_a_row_another_process_appends_after_repairing_a_torn_tail_reads_whole(
84 data_dir, fragment):
85 object_records.create_collection_record(
86 "events", {"id": "e1", "value": "ours", "note": ""}, base_dir=data_dir, roots=[])
87 with records_file(data_dir).open("ab") as handle: # a writer died mid-append
88 handle.write(fragment)
89 # This process reads the torn file -- e1 only -- and caches what it read.
90 assert stored(data_dir) == {"e1": ("ours", "")}
91 inode = records_file(data_dir).stat().st_ino
92
93 elsewhere(data_dir, "e2", "theirs", "some note text") # repairs, then appends
94
95 assert records_file(data_dir).stat().st_ino == inode # in place: the incremental case
96 expected = {"e1": ("ours", ""), "e2": ("theirs", "some note text")}
97 assert stored(data_dir) == expected # the reader holding the entry
98 assert object_records.get_collection_record(
99 "events", "e2", base_dir=data_dir, roots=[])["note"] == "some note text"
100 assert on_disk(data_dir) == expected
101
102
103def test_an_untorn_file_still_folds_only_the_new_rows(data_dir):
104 """The fix must not cost the path it guards: with no torn tail, another
105 process's append is still folded incrementally, not by a full parse."""
106 object_records.create_collection_record(
107 "events", {"id": "e1", "value": "ours", "note": ""}, base_dir=data_dir, roots=[])
108 assert stored(data_dir) == {"e1": ("ours", "")}
109 folds = object_records._INCREMENTAL_CACHE_FOLDS
110
111 elsewhere(data_dir, "e2", "theirs", "n")
112
113 assert stored(data_dir) == {"e1": ("ours", ""), "e2": ("theirs", "n")}
114 assert object_records._INCREMENTAL_CACHE_FOLDS == folds + 1